From 90a2f5594d0cc9805992639ff8b065f7d9bce870 Mon Sep 17 00:00:00 2001 From: eater Date: Tue, 23 Jan 2018 13:53:06 +0100 Subject: [PATCH] Several fixes --- composer.json | 7 ++- config/default.php | 84 +++++++++++++++++++------------ src/ContextAwareEntityManager.php | 34 +++++++++---- src/DefaultEntityManager.php | 7 ++- src/Entity/Lorem.php | 26 ++++++++++ src/EntityManager.php | 25 +++++++++ src/EntityManagerLike.php | 11 ---- src/functions.php | 35 +++++++++++++ 8 files changed, 168 insertions(+), 61 deletions(-) create mode 100644 src/Entity/Lorem.php create mode 100644 src/EntityManager.php delete mode 100644 src/EntityManagerLike.php create mode 100644 src/functions.php diff --git a/composer.json b/composer.json index d34de34..bd281b9 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "bitcommunism/doctrine", - "version": "1.0.0", + "version": "1.0.3", "license": "MIT", "require": { "doctrine/orm": "^2.6" @@ -8,6 +8,9 @@ "autoload": { "psr-4": { "BitCommunism\\Doctrine\\": "src/" - } + }, + "files": [ + "src/functions.php" + ] } } diff --git a/config/default.php b/config/default.php index 6dc7cce..bee2eae 100644 --- a/config/default.php +++ b/config/default.php @@ -9,46 +9,64 @@ use function DI\factory; use function DI\get; use function DI\string; -return [ +return array_merge( + [ + 'marx.calls' => add([ + 'doctrine' => function (EntityManager $em, $arguments) { + $connection = $em->getResponsibleEntityManager($arguments[0] ?? 'default'); - 'marx.calls' => add([ - 'doctrine' => function (EntityManagerLike $em, $arguments) { - $connection = $em->getEntityManager($arguments[0] ?? 'default'); + return ConsoleRunner::createHelperSet($connection); + }, + ]), - return ConsoleRunner::createHelperSet($connection); - }, - ]), + 'doctrine.debug' => get('debug'), + 'doctrine.single-connection' => false, - 'doctrine.debug' => get('debug'), - 'doctrine.single-connection' => false, + 'doctrine.connections' => add([ + 'default', + 'example', + ]), - 'doctrine.connections' => add([ - 'default', - ]), + 'doctrine.connection.default' => get('doctrine.connection'), + 'doctrine.entity-paths.default' => get('doctrine.entity-paths'), - 'doctrine.connection.default' => get('doctrine.connection'), - 'doctrine.entity-paths.default' => get('doctrine.entity-paths'), + 'doctrine.entity-paths' => add([ + string('{basedir}/src/Entity') + ]), - 'doctrine.entity-paths' => add([ - string('{basedir}/src/Entity') - ]), + 'doctrine.connection' => add([ + 'driver' => 'pdo_mysql', + 'hostname' => 'localhost', + 'password' => '', + 'user' => 'root', + ]), - 'doctrine.connection' => add([ - 'driver' => 'pdo_mysql', - 'hostname' => 'localhost', - 'password' => '', - 'user' => 'root', - ]), + 'em' => get(EntityManager::class), - 'em' => get(EntityManagerLike::class), + EntityManager::class => factory(function (Container $c) { + $contextAware = $c->get(ContextAwareEntityManager::class); - EntityManagerLike::class => factory(function (Container $c) { - $contextAware = $c->get(ContextAwareEntityManager::class); + if ($c->get('doctrine.single-connection')) { + return $contextAware->getResponsibleEntityManager('default'); + } - if ($c->get('doctrine.single-connection')) { - return $contextAware->getEntityManager('default'); - } - - return $contextAware; - }), -]; \ No newline at end of file + return $contextAware; + }), + ], + connections([ + 'example' => [ + 'connection' => [ + 'driver' => 'pdo_mysql', + 'hostname' => 'localhost', + 'password' => '', + 'user' => 'root', + 'dbname' => 'ipsum', + ], + 'entity-paths' => realpath(__DIR__ . '/../src/Entity') + ], + 'default' => [ + 'connection' => get('doctrine.connection'), + 'entity-paths' => get('doctrine.entity-paths'), + ] + ]) +); \ No newline at end of file diff --git a/src/ContextAwareEntityManager.php b/src/ContextAwareEntityManager.php index b29e13a..ceddead 100644 --- a/src/ContextAwareEntityManager.php +++ b/src/ContextAwareEntityManager.php @@ -7,7 +7,7 @@ namespace BitCommunism\Doctrine; use DI\Container; use Doctrine\ORM\Tools\Setup; -class ContextAwareEntityManager implements EntityManagerLike +class ContextAwareEntityManager implements EntityManager { /** * @var Container @@ -50,6 +50,8 @@ class ContextAwareEntityManager implements EntityManagerLike $this->container = $c; $connectionNames = $c->get('doctrine.connections'); + + foreach ($connectionNames as $connectionName) { $this->indexConnection($connectionName); } @@ -100,11 +102,11 @@ class ContextAwareEntityManager implements EntityManagerLike */ public function getEntityManagerForClass(string $className): DefaultEntityManager { - if (!isset($this->entityMangerKeyCache)) { + if (!isset($this->entityMangerKeyCache[$className])) { $this->entityMangerKeyCache[$className] = $this->resolveEntityManagerKeyForClass($className); } - return $this->getEntityManager($this->entityMangerKeyCache[$className]); + return $this->getResponsibleEntityManager($this->entityMangerKeyCache[$className]); } /** @@ -114,6 +116,7 @@ class ContextAwareEntityManager implements EntityManagerLike */ private function resolveEntityManagerKeyForClass(string $className): string { + if (isset($this->entityManagerKeyMap[$className])) { return $this->entityManagerKeyMap[$className]; } @@ -149,7 +152,7 @@ class ContextAwareEntityManager implements EntityManagerLike * @param string $name * @return DefaultEntityManager */ - public function getEntityManager(string $name): DefaultEntityManager + public function getResponsibleEntityManager(string $name): DefaultEntityManager { if (!isset($this->entityManagers[$name])) { $this->entityManagers[$name] = $this->createEntityManager($name); @@ -158,10 +161,6 @@ class ContextAwareEntityManager implements EntityManagerLike return $this->entityManagers[$name]; } - /** - * @param string $name - * @return DefaultEntityManager - */ protected function createEntityManager(string $name): DefaultEntityManager { if (!$this->container->has('doctrine.connection.' . $name)) { @@ -176,7 +175,14 @@ class ContextAwareEntityManager implements EntityManagerLike $paths = $this->container->get('doctrine.entity-paths.' . $name); - $config = Setup::createAnnotationMetadataConfiguration($paths, $this->container->get('doctrine.debug')); + $config = Setup::createAnnotationMetadataConfiguration( + $paths, + $this->container->get('doctrine.debug'), + null, + null, + false + ); + return DefaultEntityManager::create($connection, $config, null, $name); } @@ -210,7 +216,13 @@ class ContextAwareEntityManager implements EntityManagerLike public function clear($objectName = null) { - // TODO: Implement clear() method. + if ($objectName !== null) { + return $this->getEntityManagerForClass($objectName)->clear($objectName); + } + + foreach ($this->entityManagers as $entityManager) { + $entityManager->clear(); + } } public function detach($object) @@ -225,7 +237,7 @@ class ContextAwareEntityManager implements EntityManagerLike public function flush($entity = null) { - if ($entity === null) { + if ($entity !== null) { return $this->getEntityManagerForObject($entity)->flush($entity); } diff --git a/src/DefaultEntityManager.php b/src/DefaultEntityManager.php index 2ee8db8..fff258f 100644 --- a/src/DefaultEntityManager.php +++ b/src/DefaultEntityManager.php @@ -3,14 +3,13 @@ namespace BitCommunism\Doctrine; - use Doctrine\Common\EventManager; use Doctrine\DBAL\Connection; use Doctrine\ORM\Configuration; -use Doctrine\ORM\EntityManager; +use Doctrine\ORM\EntityManager as DoctrineEntityManager; use Doctrine\ORM\ORMException; -class DefaultEntityManager extends EntityManager implements EntityManagerLike +class DefaultEntityManager extends DoctrineEntityManager implements EntityManager { protected $name; @@ -46,7 +45,7 @@ class DefaultEntityManager extends EntityManager implements EntityManagerLike return new static($connection, $config, $connection->getEventManager(), $name); } - public function getEntityManager(string $name): DefaultEntityManager + public function getResponsibleEntityManager(string $name): DefaultEntityManager { if ($name !== $this->name) { throw new \RuntimeException("This DefaultEntityManager instance only has em with the name '{$name}'"); diff --git a/src/Entity/Lorem.php b/src/Entity/Lorem.php new file mode 100644 index 0000000..fe1a242 --- /dev/null +++ b/src/Entity/Lorem.php @@ -0,0 +1,26 @@ +id; + } +} \ No newline at end of file diff --git a/src/EntityManager.php b/src/EntityManager.php new file mode 100644 index 0000000..61060a5 --- /dev/null +++ b/src/EntityManager.php @@ -0,0 +1,25 @@ + $connection) { + $connectionNames[] = $name; + + $entityPaths['doctrine.entity-paths.' . $name] = $connection['entity-paths'] instanceof EntryReference ? $connection['entity-paths'] : add($connection['entity-paths'] ?? []); + $entityPaths['doctrine.connection.' . $name] = $connection['connection'] instanceof EntryReference ? $connection['connection'] : add($connection['connection'] ?? []); + } + + + return array_merge( + [ + 'doctrine.connections' => add($connectionNames), + ], + $entityPaths, + $connectionInfos + ); + } +} \ No newline at end of file