diff --git a/README.md b/README.md index 985de23..ad9b2b3 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ A flat database manager. [![Build Status](https://github.com/micoli/elql/workflows/Tests/badge.svg)](https://github.com/micoli/elql/actions) -[![Coverage Status](https://coveralls.io/repos/github/micoli/Elql/badge.svg?branch=main)](https://coveralls.io/github/micoli/elql?branch=main) +[![Coverage Status](https://coveralls.io/repos/github/micoli/Elql/badge.svg?branch=main)](https://coveralls.io/github/micoli/Elql?branch=main) [![Latest Stable Version](http://poser.pugx.org/micoli/elql/v)](https://packagist.org/packages/micoli/elql) [![Total Downloads](http://poser.pugx.org/micoli/elql/downloads)](https://packagist.org/packages/micoli/elql) [![Latest Unstable Version](http://poser.pugx.org/micoli/elql/v/unstable)](https://packagist.org/packages/micoli/elql) [![License](http://poser.pugx.org/micoli/elql/license)](https://packagist.org/packages/micoli/elql) diff --git a/src/Metadata/MetadataManager.php b/src/Metadata/MetadataManager.php index f750ed1..0c474de 100644 --- a/src/Metadata/MetadataManager.php +++ b/src/Metadata/MetadataManager.php @@ -4,15 +4,51 @@ namespace Micoli\Elql\Metadata; +use ReflectionClass; + class MetadataManager implements MetadataManagerInterface { + public function __construct( + /** + * @var array + */ + private array $tableNames = [], + ) { + } + /** * @param class-string $model */ public function tableNameExtractor(string $model): string { + if (isset($this->tableNames[$model])) { + return $this->tableNames[$model]; + } + $reflectedClass = new ReflectionClass($model); + $attributes = $reflectedClass->getAttributes(); + + foreach ($attributes as $reflectedAttribute) { + $attribute = $reflectedAttribute->newInstance(); + // @codeCoverageIgnoreStart + if (!$attribute instanceof Table) { + continue; + } + // @codeCoverageIgnoreStart + + // @codeCoverageIgnoreEnd + if ($attribute->name === null) { + continue; + } + // @codeCoverageIgnoreEnd + $this->tableNames[$model] = $attribute->name; + + return $this->tableNames[$model]; + } + $parts = explode('\\', $model); - return $parts[count($parts) - 1]; + $this->tableNames[$model] = $parts[count($parts) - 1]; + + return $this->tableNames[$model]; } } diff --git a/src/Metadata/Table.php b/src/Metadata/Table.php new file mode 100644 index 0000000..0a00103 --- /dev/null +++ b/src/Metadata/Table.php @@ -0,0 +1,15 @@ +databaseDir . '/Baz.yaml')), + trim(file_get_contents($this->databaseDir . '/b_a_z.yaml')), ); self::assertcount(2, $this->database->persister->getDatabase()); } @@ -81,7 +81,7 @@ public function testACompleteScenarioPass(): void public function testItShouldReadFromFiles(): void { file_put_contents( - $this->databaseDir . '/Baz.yaml', + $this->databaseDir . '/b_a_z.yaml', << 'aaa']); + self::assertSame('Bar', $metadataManager->tableNameExtractor(Bar::class)); + self::assertSame('aaa', $metadataManager->tableNameExtractor(Foo::class)); + } +}