-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace annotations to attributes for custom index metadata
- Loading branch information
1 parent
00d4b65
commit a536fe8
Showing
4 changed files
with
145 additions
and
124 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Intaro\CustomIndexBundle\Metadata\Attribute; | ||
|
||
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)] | ||
class CustomIndex | ||
{ | ||
/** @param array<string> $columns */ | ||
public function __construct( | ||
public ?string $name = null, | ||
public array $columns = [], | ||
public ?string $where = null, | ||
public ?string $using = null, | ||
public bool $unique = false, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
|
||
namespace Intaro\CustomIndexBundle\Metadata; | ||
|
||
use Doctrine\ORM\EntityManagerInterface; | ||
use Doctrine\ORM\Mapping\ClassMetadata; | ||
use Doctrine\ORM\Mapping\ClassMetadataInfo; | ||
use Intaro\CustomIndexBundle\DTO\CustomIndex; | ||
|
||
final class Reader implements ReaderInterface | ||
{ | ||
public function __construct(private readonly EntityManagerInterface $em) | ||
{ | ||
} | ||
|
||
public function getIndexes(string $currentSchema, bool $searchInAllSchemas): array | ||
{ | ||
$metadata = $this->em->getMetadataFactory()->getAllMetadata(); | ||
$indexNamesToCustomIndexes = []; | ||
$abstractClassesInfo = $this->getAbstractClassesInfo($metadata); | ||
foreach ($metadata as $meta) { | ||
if ($this->isAbstract($meta)) { | ||
continue; | ||
} | ||
|
||
$this->collect($indexNamesToCustomIndexes, $meta, $meta->getTableName(), $currentSchema, $searchInAllSchemas); | ||
$parentsMeta = $this->searchParentsWithIndex($meta, $abstractClassesInfo); | ||
foreach ($parentsMeta as $parentMeta) { | ||
$tableName = $this->getTableNameFromMetadata($meta, $parentMeta); | ||
$this->collect($indexNamesToCustomIndexes, $parentMeta, $tableName, $currentSchema, $searchInAllSchemas, true); | ||
} | ||
} | ||
|
||
return $indexNamesToCustomIndexes; | ||
} | ||
|
||
private function collect( | ||
array &$indexNamesToCustomIndexes, | ||
ClassMetadata $metadata, | ||
string $tableName, | ||
string $currentSchema, | ||
bool $searchInAllSchemas, | ||
bool $tablePostfix = false | ||
): void { | ||
$reflectionAttributes = $this->getCustomIndexesAttributes($metadata); | ||
if (empty($reflectionAttributes)) { | ||
return; | ||
} | ||
|
||
foreach ($reflectionAttributes as $attribute) { | ||
$schema = $metadata->getSchemaName() ?: $currentSchema; | ||
// skip index from side schema in single schema mode | ||
if (!$searchInAllSchemas && $schema !== $currentSchema) { | ||
continue; | ||
} | ||
|
||
$attributeArguments = $attribute->getArguments(); | ||
$name = $attributeArguments['name'] ?? ''; | ||
$index = new CustomIndex( | ||
$tableName, | ||
$schema, | ||
$attributeArguments['columns'] ?? [], | ||
$name . ($name && $tablePostfix ? '_' . $tableName : ''), | ||
$attributeArguments['unique'] ?? false, | ||
$attributeArguments['using'] ?? null, | ||
$attributeArguments['where'] ?? null, | ||
); | ||
|
||
$key = $schema . '.' . $index->getName(); | ||
$indexNamesToCustomIndexes[$key] = $index; | ||
} | ||
} | ||
|
||
private function getTableNameFromMetadata(ClassMetadata $metadata, ClassMetadata $parentMetadata): string | ||
{ | ||
if ($metadata->inheritanceType === ClassMetadataInfo::INHERITANCE_TYPE_JOINED) { | ||
return $parentMetadata->getTableName(); | ||
} | ||
|
||
return $metadata->getTableName(); | ||
} | ||
|
||
/** @return array<\ReflectionAttribute> */ | ||
private function getCustomIndexesAttributes(ClassMetadata $meta): array | ||
{ | ||
return $meta->getReflectionClass()->getAttributes(\Intaro\CustomIndexBundle\Metadata\Attribute\CustomIndex::class); | ||
} | ||
|
||
private function isAbstract(ClassMetadata $meta): bool | ||
{ | ||
return $meta->getReflectionClass()->isAbstract(); | ||
} | ||
|
||
|
||
/** | ||
* @param array $metadata | ||
* @return array<string, mixed> | ||
*/ | ||
private function getAbstractClassesInfo(array $metadata): array | ||
{ | ||
$abstractClasses = []; | ||
foreach ($metadata as $meta) { | ||
if ($this->isAbstract($meta)) { | ||
$abstractClasses[$meta->getName()] = $meta; | ||
} | ||
} | ||
|
||
return $abstractClasses; | ||
} | ||
|
||
/** | ||
* @param array<string, mixed> $abstractClasses | ||
* @return array<string, mixed> | ||
*/ | ||
private function searchParentsWithIndex(ClassMetadata $meta, array $abstractClasses): array | ||
{ | ||
$reflectionClass = $meta->getReflectionClass(); | ||
$parentMeta = []; | ||
foreach ($abstractClasses as $entityName => $entityMeta) { | ||
if ($reflectionClass->isSubclassOf($entityName)) { | ||
$parentMeta[$entityName] = $entityMeta; | ||
} | ||
} | ||
|
||
return $parentMeta; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters