From 755dea5eadc68b384af3fc51b32b797febb2874a Mon Sep 17 00:00:00 2001 From: Alex Farcas Date: Wed, 20 Apr 2016 15:49:46 +0300 Subject: [PATCH] Fixed entity manager resetting --- src/Doctrine/ManagerRegistry.php | 32 ++++++++------- .../DoctrineOrmManagerRegistryProvider.php | 27 +++++++++++++ tests/Doctrine/ManagerRegistryTest.php | 25 +++++++++++- ...DoctrineOrmManagerRegistryProviderTest.php | 39 +++++++++++++++++++ 4 files changed, 108 insertions(+), 15 deletions(-) diff --git a/src/Doctrine/ManagerRegistry.php b/src/Doctrine/ManagerRegistry.php index 3291a0f..70ef549 100644 --- a/src/Doctrine/ManagerRegistry.php +++ b/src/Doctrine/ManagerRegistry.php @@ -29,7 +29,12 @@ class ManagerRegistry implements ManagerRegistryInterface /** * @var EntityManager[] */ - protected $managers; + protected $originalManagers; + + /** + * @var EntityManager[] + */ + protected $resetManagers = []; /** * @var string @@ -138,7 +143,7 @@ public function getManager($name = null) $this->loadManagers(); $name = $this->validateManagerName($name); - return $this->managers[$name]; + return isset($this->resetManagers[$name]) ? $this->resetManagers[$name] : $this->originalManagers[$name]; } /** @@ -148,7 +153,7 @@ public function getManager($name = null) protected function validateManagerName($name) { return $this->validateName( - $this->managers, + $this->originalManagers, $name, $this->getDefaultManagerName()) ; @@ -181,15 +186,15 @@ public function getManagers() { $this->loadManagers(); - if ($this->managers instanceof Container) { + if ($this->originalManagers instanceof Container) { $managers = array(); foreach ($this->getManagerNames() as $name) { - $managers[$name] = $this->managers[$name]; + $managers[$name] = $this->originalManagers[$name]; } - $this->managers = $managers; + $this->originalManagers = $managers; } - return $this->managers; + return array_replace($this->originalManagers, $this->resetManagers); } /** @@ -199,30 +204,29 @@ public function getManagerNames() { $this->loadManagers(); - if ($this->managers instanceof Container) { - return $this->managers->keys(); + if ($this->originalManagers instanceof Container) { + return $this->originalManagers->keys(); } else { - return array_keys($this->managers); + return array_keys($this->originalManagers); } } /** * @param string|null $name * @return void - * @throws \InvalidArgumentException */ public function resetManager($name = null) { $this->loadManagers(); $name = $this->validateManagerName($name); - $this->managers[$name] = null; + $this->resetManagers[$name] = $this->container['orm.ems.factory'][$name](); } protected function loadManagers() { - if (is_null($this->managers)) { - $this->managers = $this->container['orm.ems']; + if (is_null($this->originalManagers)) { + $this->originalManagers = $this->container['orm.ems']; $this->defaultManagerName = $this->container['orm.ems.default']; } } diff --git a/src/Provider/DoctrineOrmManagerRegistryProvider.php b/src/Provider/DoctrineOrmManagerRegistryProvider.php index 0360039..921780d 100644 --- a/src/Provider/DoctrineOrmManagerRegistryProvider.php +++ b/src/Provider/DoctrineOrmManagerRegistryProvider.php @@ -2,6 +2,7 @@ namespace Saxulum\DoctrineOrmManagerRegistry\Provider; +use Doctrine\ORM\EntityManager; use Pimple\Container; use Pimple\ServiceProviderInterface; use Saxulum\DoctrineOrmManagerRegistry\Doctrine\ManagerRegistry; @@ -33,6 +34,32 @@ public function register(Container $container) return new ManagerRegistry($container); }; + if (!isset($container['orm.ems.factory'])) { + $container['orm.ems.factory'] = function (Container $container) { + $container['orm.ems.options.initializer'](); + $factory = new Container(); + foreach ($container['orm.ems.options'] as $name => $options) { + if ($container['orm.ems.default'] === $name) { + // we use shortcuts here in case the default has been overridden + $config = $container['orm.em.config']; + } else { + $config = $container['orm.ems.config'][$name]; + } + $factory[$name] = $factory->protect( + function () use ($container, $options, $config) { + return EntityManager::create( + $container['dbs'][$options['connection']], + $config, + $container['dbs.event_manager'][$options['connection']] + ); + } + ); + } + return $factory; + }; + } + + if (isset($container['form.extensions']) && class_exists('Symfony\\Bridge\\Doctrine\\Form\\DoctrineOrmExtension')) { $container['form.extensions'] = $container->extend('form.extensions', function ($extensions, $container) { $extensions[] = new DoctrineOrmExtension($container['doctrine']); diff --git a/tests/Doctrine/ManagerRegistryTest.php b/tests/Doctrine/ManagerRegistryTest.php index 115abf9..b7a8a38 100644 --- a/tests/Doctrine/ManagerRegistryTest.php +++ b/tests/Doctrine/ManagerRegistryTest.php @@ -9,7 +9,7 @@ class ManagerRegistryTest extends \PHPUnit_Framework_TestCase { /** - * @return array + * @return Container */ protected function createMockDefaultAppAndDeps() { @@ -67,6 +67,17 @@ protected function createMockDefaultAppAndDeps() 'default' => $entityManager, )); + $container['orm.ems.factory'] = new Container(); + $container['orm.ems.factory']['default'] = $container['orm.ems.factory']->protect( + function () { + return $this + ->getMockBuilder('Doctrine\ORM\EntityManager') + ->disableOriginalConstructor() + ->getMock() + ; + } + ); + $container['orm.ems.default'] = 'default'; return $container; @@ -90,5 +101,17 @@ public function testRegisterDefaultImplementations() $this->assertEquals($container['doctrine']->getAliasNamespace('Test'), 'Saxulum\DoctrineOrmManagerRegistry\Doctrine\ManagerRegistry'); $this->assertInstanceOf('Doctrine\Common\Persistence\ObjectRepository', $container['doctrine']->getRepository('Saxulum\DoctrineOrmManagerRegistry\Doctrine\ManagerRegistry')); $this->assertInstanceOf('Doctrine\ORM\EntityManager', $container['doctrine']->getManagerForClass('Saxulum\DoctrineOrmManagerRegistry\Doctrine\ManagerRegistry')); + + $initialManager = $container['doctrine']->getManager(); + $container['doctrine']->resetManager(); + $resetManager = $container['doctrine']->getManager(); + $this->assertNotSame($resetManager, $initialManager); + $this->assertSame($resetManager, $container['doctrine']->getManagers()['default']); + + $container['doctrine']->resetManager(); + $reResetManager = $container['doctrine']->getManager(); + $this->assertNotSame($reResetManager, $resetManager); + $this->assertNotSame($reResetManager, $initialManager); + $this->assertSame($reResetManager, $container['doctrine']->getManagers()['default']); } } diff --git a/tests/Provider/DoctrineOrmManagerRegistryProviderTest.php b/tests/Provider/DoctrineOrmManagerRegistryProviderTest.php index 34bcb88..cde0707 100644 --- a/tests/Provider/DoctrineOrmManagerRegistryProviderTest.php +++ b/tests/Provider/DoctrineOrmManagerRegistryProviderTest.php @@ -28,6 +28,45 @@ public function testSchema() $this->dropSchema($schemaTool, $metadatas); } + public function testManagerFactory() + { + $app = $this->createApplication(); + + /** @var EntityManager $initialEm */ + $initialEm = $app['doctrine']->getManager(); + + $schemaTool = $this->getSchemaTool($initialEm); + $metadatas = $this->getMetadatas($initialEm); + + $this->createSchema($schemaTool, $metadatas); + $this->dropSchema($schemaTool, $metadatas); + + $app['doctrine']->resetManager(); + /** @var EntityManager $resetEm */ + $resetEm = $app['doctrine']->getManager(); + + $schemaTool = $this->getSchemaTool($resetEm); + $metadatas = $this->getMetadatas($resetEm); + + $this->createSchema($schemaTool, $metadatas); + $this->dropSchema($schemaTool, $metadatas); + + $this->assertNotSame($resetEm, $initialEm); + + $app['doctrine']->resetManager(); + /** @var EntityManager $reResetEm */ + $reResetEm = $app['doctrine']->getManager(); + + $schemaTool = $this->getSchemaTool($reResetEm); + $metadatas = $this->getMetadatas($reResetEm); + + $this->createSchema($schemaTool, $metadatas); + $this->dropSchema($schemaTool, $metadatas); + + $this->assertNotSame($reResetEm, $resetEm); + $this->assertNotSame($reResetEm, $initialEm); + } + public function testValidator() { $app = $this->createApplication();