From 22428b8b144ee1b8a7580ee738a86d5601b954e3 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 7 Jun 2011 08:58:10 +0200 Subject: [PATCH] [DoctrineBundle] refactored Doctrine proxy cache warmer * removed the dependency on the Container * the proxy cache is now get from each entity manager configuration --- .../CacheWarmer/ProxyCacheWarmer.php | 41 ++++---- .../DoctrineBundle/Resources/config/orm.xml | 2 +- .../CacheWarmer/ProxyCacheWarmerTest.php | 96 ------------------- 3 files changed, 21 insertions(+), 118 deletions(-) delete mode 100644 src/Symfony/Bundle/DoctrineBundle/Tests/CacheWarmer/ProxyCacheWarmerTest.php diff --git a/src/Symfony/Bundle/DoctrineBundle/CacheWarmer/ProxyCacheWarmer.php b/src/Symfony/Bundle/DoctrineBundle/CacheWarmer/ProxyCacheWarmer.php index 6c4932a44936..09fdf01869b9 100644 --- a/src/Symfony/Bundle/DoctrineBundle/CacheWarmer/ProxyCacheWarmer.php +++ b/src/Symfony/Bundle/DoctrineBundle/CacheWarmer/ProxyCacheWarmer.php @@ -11,7 +11,7 @@ namespace Symfony\Bundle\DoctrineBundle\CacheWarmer; -use Symfony\Component\DependencyInjection\Container; +use Symfony\Bundle\DoctrineBundle\Registry; use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; /** @@ -24,17 +24,16 @@ */ class ProxyCacheWarmer implements CacheWarmerInterface { - /** - * @var Container - */ - private $container; + private $registry; /** - * @param Container $container + * Constructor. + * + * @param Registry $registry The Doctrine registry */ - public function __construct(Container $container) + public function __construct(Registry $registry) { - $this->container = $container; + $this->registry = $registry; } /** @@ -49,23 +48,23 @@ public function isOptional() public function warmUp($cacheDir) { - // we need the directory no matter the proxy cache generation strategy. - $proxyCacheDir = $this->container->getParameter('doctrine.orm.proxy_dir'); - if (!file_exists($proxyCacheDir)) { - if (false === @mkdir($proxyCacheDir, 0777, true)) { - throw new \RuntimeException(sprintf('Unable to create the Doctrine Proxy directory (%s)', dirname($proxyCacheDir))); + foreach ($this->registry->getEntityManagers() as $em) { + // we need the directory no matter the proxy cache generation strategy + if (!file_exists($proxyCacheDir = $em->getConfiguration()->getProxyDir())) { + if (false === @mkdir($proxyCacheDir, 0777, true)) { + throw new \RuntimeException(sprintf('Unable to create the Doctrine Proxy directory "%s".', dirname($proxyCacheDir))); + } + } elseif (!is_writable($proxyCacheDir)) { + throw new \RuntimeException(sprintf('The Doctrine Proxy directory "%s" is not writeable for the current system user.', $proxyCacheDir)); } - } else if (!is_writable($proxyCacheDir)) { - throw new \RuntimeException(sprintf('Doctrine Proxy directory (%s) is not writeable for the current system user.', $proxyCacheDir)); - } - // if proxies are autogenerated we don't need to generate them in the cache warmer. - if ($this->container->getParameter('doctrine.orm.auto_generate_proxy_classes') === true) { - return; - } + // if proxies are autogenerated we don't need to generate them in the cache warmer + if ($em->getConfiguration()->getAutoGenerateProxyClasses()) { + continue; + } - foreach ($this->container->get('doctrine')->getEntityManagers() as $em) { $classes = $em->getMetadataFactory()->getAllMetadata(); + $em->getProxyFactory()->generateProxyClasses($classes); } } diff --git a/src/Symfony/Bundle/DoctrineBundle/Resources/config/orm.xml b/src/Symfony/Bundle/DoctrineBundle/Resources/config/orm.xml index 9e8d40de9e16..df95d0294192 100644 --- a/src/Symfony/Bundle/DoctrineBundle/Resources/config/orm.xml +++ b/src/Symfony/Bundle/DoctrineBundle/Resources/config/orm.xml @@ -44,7 +44,7 @@ - + diff --git a/src/Symfony/Bundle/DoctrineBundle/Tests/CacheWarmer/ProxyCacheWarmerTest.php b/src/Symfony/Bundle/DoctrineBundle/Tests/CacheWarmer/ProxyCacheWarmerTest.php deleted file mode 100644 index 7f98ae1800c1..000000000000 --- a/src/Symfony/Bundle/DoctrineBundle/Tests/CacheWarmer/ProxyCacheWarmerTest.php +++ /dev/null @@ -1,96 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Bundle\DoctrineBundle\Tests\CacheWarmer; - -use Symfony\Bundle\DoctrineBundle\CacheWarmer\ProxyCacheWarmer; - -class ProxyCacheWarmerTest extends \Symfony\Bundle\DoctrineBundle\Tests\TestCase -{ - /** - * This is not necessarily a good test, it doesn't generate any proxies - * because there are none in the AnnotationsBundle. However that is - * rather a task of doctrine to test. We touch the lines here and - * verify that the container is called correctly for the relevant information. - * - * @group DoctrineORMProxy - */ - public function testWarmCache() - { - $testManager = $this->createTestEntityManager(array( - __DIR__ . "/../DependencyInjection/Fixtures/Bundles/AnnotationsBundle/Entity") - ); - - $registry = $this->getMockBuilder('Symfony\Bundle\DoctrineBundle\Registry')->disableOriginalConstructor()->getMock(); - $registry->expects($this->at(0)) - ->method('getEntityManagerNames') - ->will($this->returnValue(array('default' => 'doctrine.orm.default_entity_manager', 'foo' => 'doctrine.orm.foo_entity_manager'))); - - $container = $this->getMock('Symfony\Component\DependencyInjection\Container'); - $container->expects($this->at(0)) - ->method('getParameter') - ->with($this->equalTo('doctrine.orm.proxy_dir')) - ->will($this->returnValue(sys_get_temp_dir())); - $container->expects($this->at(1)) - ->method('getParameter') - ->with($this->equalTo('doctrine.orm.auto_generate_proxy_classes')) - ->will($this->returnValue(false)); - $container->expects($this->at(2)) - ->method('get') - ->with($this->equalTo('doctrine')) - ->will($this->returnValue($registry)); - $container->expects($this->at(3)) - ->method('get') - ->with($this->equalTo('doctrine.orm.default_entity_manager')) - ->will($this->returnValue($testManager)); - $container->expects($this->at(4)) - ->method('get') - ->with($this->equalTo('doctrine.orm.foo_entity_manager')) - ->will($this->returnValue($testManager)); - - $cacheWarmer = new ProxyCacheWarmer($container); - $cacheWarmer->warmUp(sys_get_temp_dir()); - } - - public function testSkipWhenProxiesAreAutoGenerated() - { - $testManager = $this->createTestEntityManager(array( - __DIR__ . "/../DependencyInjection/Fixtures/Bundles/AnnotationsBundle/Entity") - ); - - $container = $this->getMock('Symfony\Component\DependencyInjection\Container'); - $container->expects($this->at(0)) - ->method('getParameter') - ->with($this->equalTo('doctrine.orm.proxy_dir')) - ->will($this->returnValue(sys_get_temp_dir())); - $container->expects($this->at(1)) - ->method('getParameter') - ->with($this->equalTo('doctrine.orm.auto_generate_proxy_classes')) - ->will($this->returnValue(true)); - $container->expects($this->at(2)) - ->method('getParameter') - ->with($this->equalTo('assertion')) - ->will($this->returnValue(true)); - - $cacheWarmer = new ProxyCacheWarmer($container); - $cacheWarmer->warmUp(sys_get_temp_dir()); - - $container->getParameter('assertion'); // check that the assertion is really the third call. - } - - public function testProxyCacheWarmingIsNotOptional() - { - $container = $this->getMock('Symfony\Component\DependencyInjection\Container'); - $cacheWarmer = new ProxyCacheWarmer($container); - - $this->assertFalse($cacheWarmer->isOptional()); - } -} \ No newline at end of file