From 669fe31c8422c7f91e607b28dccf8880e1c18f50 Mon Sep 17 00:00:00 2001 From: Olivier Michaud Date: Fri, 14 Jul 2023 12:19:05 +0200 Subject: [PATCH] Add a Table attribute to specify filename --- README.md | 2 +- src/Metadata/MetadataManager.php | 31 ++++++++++++++++++++++++++++++- src/Metadata/Table.php | 15 +++++++++++++++ tests/ElqlTest.php | 4 ++-- tests/Fixtures/Bar.php | 17 +++++++++++++++++ tests/Fixtures/Baz.php | 3 +++ 6 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 src/Metadata/Table.php create mode 100644 tests/Fixtures/Bar.php 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..405bb9e 100644 --- a/src/Metadata/MetadataManager.php +++ b/src/Metadata/MetadataManager.php @@ -4,15 +4,44 @@ namespace Micoli\Elql\Metadata; +use ReflectionClass; + class MetadataManager implements MetadataManagerInterface { + /** + * @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(); + if (!$attribute instanceof Table) { + /** @codeCoverageIgnore */ + continue; + } + if ($attribute->name === null) { + continue; + } + $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', <<