From 1132a731f53a8031ded209d1df1c6e487e385ba0 Mon Sep 17 00:00:00 2001 From: Basilius Sauter Date: Mon, 13 Jun 2016 09:11:15 +0200 Subject: [PATCH] Add API to extend annotation metadata directory This commit adds the possibility for externals to add additional directories in order to extend the directories doctrine uses to read metadata from. Closes #39 --- src/Bootstrap.php | 30 +++++++++++++++++----- tests/AdditionalEntities/UserEntity.php | 34 +++++++++++++++++++++++++ tests/BootstrapTest.php | 24 +++++++++++++++++ 3 files changed, 82 insertions(+), 6 deletions(-) create mode 100644 tests/AdditionalEntities/UserEntity.php diff --git a/src/Bootstrap.php b/src/Bootstrap.php index 95b8563..ba8c83a 100644 --- a/src/Bootstrap.php +++ b/src/Bootstrap.php @@ -3,16 +3,28 @@ declare(strict_types=1); namespace LotGD\Core; -use LotGD\Core\Exceptions\InvalidConfigurationException; - use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Mapping\AnsiQuoteStrategy; use Doctrine\ORM\Tools\Setup; use Doctrine\ORM\Tools\SchemaTool; +use LotGD\Core\Exceptions\ArgumentException; +use LotGD\Core\Exceptions\InvalidConfigurationException; + class Bootstrap { + private static $annotationMetaDataDirectories = []; + + public static function registerAnnotationMetaDataDirectory(string $directory) + { + if (is_dir($directory) === false) { + throw new ArgumentException("{$directory} needs to be a valdid directory"); + } + + self::$annotationMetaDataDirectories[] = $directory; + } + /** * Create a new Game object, with all the necessary configuration. * @throws InvalidConfigurationException @@ -25,19 +37,25 @@ class Bootstrap $passwd = getenv('DB_PASSWORD'); if ($dsn === false || strlen($dsn) == 0) { - throw new InvalidConfigurationException("Invalid or missing data source name: '{$dsn}'"); + throw new InvalidConfigurationException("Invalid or missing data source name: '{$dsn}'"); } if ($user === false || strlen($user) == 0) { - throw new InvalidConfigurationException("Invalid or missing database user: '{$user}'"); + throw new InvalidConfigurationException("Invalid or missing database user: '{$user}'"); } if ($passwd === false) { - throw new InvalidConfigurationException("Invalid or missing database password: '{$passwd}'"); + throw new InvalidConfigurationException("Invalid or missing database password: '{$passwd}'"); } $pdo = new \PDO($dsn, $user, $passwd); // Read db annotations from model files - $configuration = Setup::createAnnotationMetadataConfiguration([__DIR__ . '/Models'], true); + $annotationMetaDataDirectories = array_merge( + [__DIR__ . '/Models'], + self::$annotationMetaDataDirectories + ); + $configuration = Setup::createAnnotationMetadataConfiguration($annotationMetaDataDirectories, true); + + // Set a quote $configuration->setQuoteStrategy(new AnsiQuoteStrategy()); $entityManager = EntityManager::create(["pdo" => $pdo], $configuration); diff --git a/tests/AdditionalEntities/UserEntity.php b/tests/AdditionalEntities/UserEntity.php new file mode 100644 index 0000000..272cc9b --- /dev/null +++ b/tests/AdditionalEntities/UserEntity.php @@ -0,0 +1,34 @@ +id; + } + + public function getName(): string + { + return $this->name; + } + + public function setName(string $name) + { + $this->name = $name; + } +} diff --git a/tests/BootstrapTest.php b/tests/BootstrapTest.php index 4f54109..7afcc62 100644 --- a/tests/BootstrapTest.php +++ b/tests/BootstrapTest.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace LotGD\Core\Tests; use LotGD\Core\Bootstrap; +use LotGD\Core\Tests\AdditionalEntities\UserEntity; class BootstrapTest extends \PHPUnit_Framework_TestCase { @@ -13,4 +14,27 @@ class BootstrapTest extends \PHPUnit_Framework_TestCase $this->assertNotNull($g->getEntityManager()); $this->assertNotNull($g->getEventManager()); } + + public function testDoctrineReadsAnnotationsFromAdditionalMetaDataDirectory() + { + Bootstrap::registerAnnotationMetaDataDirectory(__DIR__ . "/AdditionalEntities"); + + $g = Bootstrap::createGame(); + + $user = new UserEntity(); + $user->setName("Monthy"); + + $g->getEntityManager()->persist($user); + $g->getEntityManager()->flush(); + + $id = $user->getId(); + $this->assertInternalType("int", $id); + + $g->getEntityManager()->clear(); + $user = $g->getEntityManager()->getRepository(UserEntity::class)->find($id); + + $this->assertInternalType("int", $user->getId()); + $this->assertInternalType("string", $user->getName()); + $this->assertSame("Monthy", $user->getName()); + } }