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
This commit is contained in:
Basilius Sauter
2016-06-13 09:11:15 +02:00
parent d4136909f8
commit 1132a731f5
3 changed files with 82 additions and 6 deletions
+24 -6
View File
@@ -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);
+34
View File
@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace LotGD\Core\Tests\AdditionalEntities;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Table;
/**
* @Entity
* @Table(name="Users")
*/
class UserEntity
{
/** @Id @Column(type="integer") @GeneratedValue */
private $id;
/** @Column(type="string", length=50); */
private $name;
public function getId(): int
{
return $this->id;
}
public function getName(): string
{
return $this->name;
}
public function setName(string $name)
{
$this->name = $name;
}
}
+24
View File
@@ -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());
}
}