merged branch bamarni/http-kernel-lazy-classmap (PR #7322)

This PR was merged into the master branch.

Discussion
----------

[HttpKernel] made the classmap lazy-loaded

| Q             | A
| ------------- | ---
| Bug fix?      | [no]
| New feature?  | [yes]
| BC breaks?    | [no]
| Deprecations? | [no]
| Tests pass?   | [yes]
| License       | MIT

see https://github.com/symfony/symfony-standard/pull/437#issuecomment-14667140

Instead of managing this from HttpCache, I suggest to handle it at Kernel's level, what do you think?

Commits
-------

2aefe21 [HttpKernel] made the classmap lazy-loaded
This commit is contained in:
Fabien Potencier 2013-04-21 10:36:53 +02:00
commit 5ad5a5ca48
2 changed files with 59 additions and 3 deletions

View File

@ -57,6 +57,7 @@ abstract class Kernel implements KernelInterface, TerminableInterface
protected $name;
protected $startTime;
protected $classes;
protected $loadClassCache;
const VERSION = '2.3.0-DEV';
const VERSION_ID = '20300';
@ -118,6 +119,10 @@ abstract class Kernel implements KernelInterface, TerminableInterface
return;
}
if ($this->loadClassCache) {
call_user_func_array(array($this, 'doLoadClassCache', $this->loadClassCache);
}
// init bundles
$this->initializeBundles();
@ -389,9 +394,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);
}
/**
@ -442,6 +445,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.
*

View File

@ -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')