From 15db2fec8e32769504af9521f38cef316ffb5cd1 Mon Sep 17 00:00:00 2001 From: Christian Schmidt Date: Tue, 8 Sep 2015 08:39:21 +0200 Subject: [PATCH] Separate entity manager factory --- .../DoctrineOrmServiceProvider.php | 17 +++++++++-- .../DoctrineOrmServiceProviderTest.php | 30 +++++++++++++++++-- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/Dflydev/Pimple/Provider/DoctrineOrm/DoctrineOrmServiceProvider.php b/src/Dflydev/Pimple/Provider/DoctrineOrm/DoctrineOrmServiceProvider.php index 8b5e53e..23042fe 100755 --- a/src/Dflydev/Pimple/Provider/DoctrineOrm/DoctrineOrmServiceProvider.php +++ b/src/Dflydev/Pimple/Provider/DoctrineOrm/DoctrineOrmServiceProvider.php @@ -94,6 +94,19 @@ public function register(\Pimple $app) $app['orm.ems.options.initializer'](); $ems = new \Pimple(); + foreach ($app['orm.ems.options'] as $name => $options) { + $ems[$name] = $app->share(function() use ($name, $app) { + return $app['orm.ems.factory'][$name](); + }); + } + + return $ems; + }); + + $app['orm.ems.factory'] = $app->share(function($app) { + $app['orm.ems.options.initializer'](); + + $factory = new \Pimple(); foreach ($app['orm.ems.options'] as $name => $options) { if ($app['orm.ems.default'] === $name) { // we use shortcuts here in case the default has been overridden @@ -102,7 +115,7 @@ public function register(\Pimple $app) $config = $app['orm.ems.config'][$name]; } - $ems[$name] = $app->share(function ($ems) use ($app, $options, $config) { + $factory[$name] = $app->protect(function () use ($app, $options, $config) { return EntityManager::create( $app['dbs'][$options['connection']], $config, @@ -111,7 +124,7 @@ public function register(\Pimple $app) }); } - return $ems; + return $factory; }); $app['orm.ems.config'] = $app->share(function($app) { diff --git a/tests/Dflydev/Pimple/Provider/DoctrineOrm/DoctrineOrmServiceProviderTest.php b/tests/Dflydev/Pimple/Provider/DoctrineOrm/DoctrineOrmServiceProviderTest.php index aa751b8..0484e2b 100644 --- a/tests/Dflydev/Pimple/Provider/DoctrineOrm/DoctrineOrmServiceProviderTest.php +++ b/tests/Dflydev/Pimple/Provider/DoctrineOrm/DoctrineOrmServiceProviderTest.php @@ -51,6 +51,32 @@ protected function createMockDefaultApp() return $app; } + /** + * Test entity manager factory. + */ + public function testEntityManagerFactory() + { + $app = $this->createMockDefaultApp(); + + $doctrineOrmServiceProvider = new DoctrineOrmServiceProvider; + $doctrineOrmServiceProvider->register($app); + + // Only one instance is created. + $em1 = $app['orm.em']; + $this->assertSame($em1, $app['orm.em']); + $this->assertSame($em1, $app['orm.ems']['default']); + + // Reset the entity manager. + $app['orm.em'] = $app['orm.ems']['default'] = $app['orm.ems.factory'][$app['orm.ems.default']](); + + // One more instance is created. + $em2 = $app['orm.em']; + $this->assertInstanceOf('Doctrine\ORM\EntityManager', $em2); + $this->assertNotSame($em1, $em2); + $this->assertSame($em2, $app['orm.em']); + $this->assertSame($em2, $app['orm.ems']['default']); + } + /** * Test registration (test expected class for default implementations) */ @@ -61,7 +87,7 @@ public function testRegisterDefaultImplementations() $doctrineOrmServiceProvider = new DoctrineOrmServiceProvider; $doctrineOrmServiceProvider->register($app); - $this->assertEquals($app['orm.em'], $app['orm.ems']['default']); + $this->assertSame($app['orm.em'], $app['orm.ems']['default']); $this->assertInstanceOf('Doctrine\Common\Cache\ArrayCache', $app['orm.em.config']->getQueryCacheImpl()); $this->assertInstanceOf('Doctrine\Common\Cache\ArrayCache', $app['orm.em.config']->getResultCacheImpl()); $this->assertInstanceOf('Doctrine\Common\Cache\ArrayCache', $app['orm.em.config']->getMetadataCacheImpl()); @@ -91,7 +117,7 @@ public function testRegisterDefinedImplementations() $doctrineOrmServiceProvider = new DoctrineOrmServiceProvider; $doctrineOrmServiceProvider->register($app); - $this->assertEquals($app['orm.em'], $app['orm.ems']['default']); + $this->assertSame($app['orm.em'], $app['orm.ems']['default']); $this->assertEquals($queryCache, $app['orm.em.config']->getQueryCacheImpl()); $this->assertEquals($resultCache, $app['orm.em.config']->getResultCacheImpl()); $this->assertEquals($metadataCache, $app['orm.em.config']->getMetadataCacheImpl());