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:
+24
-6
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user