From 2aefe211df6b42dbde2c4f4ba4e1534995fde456 Mon Sep 17 00:00:00 2001 From: Bilal Amarni Date: Sun, 10 Mar 2013 10:40:42 +0100 Subject: [PATCH] [HttpKernel] made the classmap lazy-loaded --- src/Symfony/Component/HttpKernel/Kernel.php | 16 +++++-- .../Component/HttpKernel/Tests/KernelTest.php | 46 +++++++++++++++++++ 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/HttpKernel/Kernel.php b/src/Symfony/Component/HttpKernel/Kernel.php index 6c6035b694..3eec09dd0f 100644 --- a/src/Symfony/Component/HttpKernel/Kernel.php +++ b/src/Symfony/Component/HttpKernel/Kernel.php @@ -60,6 +60,7 @@ abstract class Kernel implements KernelInterface, TerminableInterface protected $name; protected $startTime; protected $classes; + protected $loadClassCache; protected $errorReportingLevel; const VERSION = '2.3.0-DEV'; @@ -132,6 +133,10 @@ abstract class Kernel implements KernelInterface, TerminableInterface return; } + if (!empty($this->loadClassCache)) { + call_user_func_array(array($this, 'doLoadClassCache'), $this->loadClassCache); + } + // init bundles $this->initializeBundles(); @@ -403,9 +408,7 @@ abstract class Kernel implements KernelInterface, TerminableInterface */ public function loadClassCache($name = 'classes', $extension = '.php') { - if (!$this->booted && is_file($this->getCacheDir().'/classes.map')) { - ClassCollectionLoader::load(include($this->getCacheDir().'/classes.map'), $this->getCacheDir(), $name, $this->debug, false, $extension); - } + $this->loadClassCache = array($name, $extension); } /** @@ -456,6 +459,13 @@ abstract class Kernel implements KernelInterface, TerminableInterface return 'UTF-8'; } + protected function doLoadClassCache($name, $extension) + { + if (!$this->booted && is_file($this->getCacheDir().'/classes.map')) { + ClassCollectionLoader::load(include($this->getCacheDir().'/classes.map'), $this->getCacheDir(), $name, $this->debug, false, $extension); + } + } + /** * Initializes the data structures related to the bundle management. * diff --git a/src/Symfony/Component/HttpKernel/Tests/KernelTest.php b/src/Symfony/Component/HttpKernel/Tests/KernelTest.php index d4295f5ab8..4a25089ca1 100644 --- a/src/Symfony/Component/HttpKernel/Tests/KernelTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/KernelTest.php @@ -108,6 +108,52 @@ class KernelTest extends \PHPUnit_Framework_TestCase $this->assertTrue($kernel->isBooted()); } + public function testClassCacheIsLoaded() + { + $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest') + ->disableOriginalConstructor() + ->setMethods(array('initializeBundles', 'initializeContainer', 'getBundles', 'doLoadClassCache')) + ->getMock(); + $kernel->loadClassCache('name', '.extension'); + $kernel->expects($this->any()) + ->method('getBundles') + ->will($this->returnValue(array())); + $kernel->expects($this->once()) + ->method('doLoadClassCache') + ->with('name', '.extension'); + + $kernel->boot(); + } + + public function testClassCacheIsNotLoadedByDefault() + { + $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest') + ->disableOriginalConstructor() + ->setMethods(array('initializeBundles', 'initializeContainer', 'getBundles', 'doLoadClassCache')) + ->getMock(); + $kernel->expects($this->any()) + ->method('getBundles') + ->will($this->returnValue(array())); + $kernel->expects($this->never()) + ->method('doLoadClassCache'); + + $kernel->boot(); + } + + public function testClassCacheIsNotLoadedWhenKernelIsNotBooted() + { + $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest') + ->disableOriginalConstructor() + ->setMethods(array('initializeBundles', 'initializeContainer', 'getBundles', 'doLoadClassCache')) + ->getMock(); + $kernel->loadClassCache(); + $kernel->expects($this->any()) + ->method('getBundles') + ->will($this->returnValue(array())); + $kernel->expects($this->never()) + ->method('doLoadClassCache'); + } + public function testBootKernelSeveralTimesOnlyInitializesBundlesOnce() { $kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest')