Skip to content

Commit

Permalink
Allow Kernel::$name to be overridden by subclasses
Browse files Browse the repository at this point in the history
Because the name of the kernel is calculated in the constructor,
any child class that had overriden the kernel name, will be
ignored.

By setting the kernel name in the child class, we can avoid having
to execute the regex to calculate the name upon every construction
of a Kernel.

A test (and a kernel fixture) is added to prove that the override
works correctly.

Note: the Kernel API has not been touched, so there should be no
issues with BC.
  • Loading branch information
jonathaningram committed Jul 11, 2012
1 parent 064ad62 commit 02e0a8f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Symfony/Component/HttpKernel/Kernel.php
Expand Up @@ -79,7 +79,7 @@ public function __construct($environment, $debug)
$this->debug = (Boolean) $debug;
$this->booted = false;
$this->rootDir = $this->getRootDir();
$this->name = preg_replace('/[^a-zA-Z0-9_]+/', '', basename($this->rootDir));
$this->name = $this->getName();
$this->classes = array();

if ($this->debug) {
Expand Down Expand Up @@ -354,6 +354,10 @@ public function locateResource($name, $dir = null, $first = true)
*/
public function getName()
{
if (null === $this->name) {
$this->name = preg_replace('/[^a-zA-Z0-9_]+/', '', basename($this->rootDir));
}

return $this->name;
}

Expand Down
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpKernel\Tests\Fixtures;

use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;

class KernelForOverrideName extends Kernel
{
protected $name = 'overridden';

public function registerBundles()
{

}

public function registerContainerConfiguration(LoaderInterface $loader)
{

}
}
8 changes: 8 additions & 0 deletions src/Symfony/Component/HttpKernel/Tests/KernelTest.php
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest;
use Symfony\Component\HttpKernel\Tests\Fixtures\KernelForOverrideName;
use Symfony\Component\HttpKernel\Tests\Fixtures\FooBarBundle;

class KernelTest extends \PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -310,6 +311,13 @@ public function testGetName()
$this->assertEquals('Fixtures', $kernel->getName());
}

public function testOverrideGetName()
{
$kernel = new KernelForOverrideName('test', true);

$this->assertEquals('overridden', $kernel->getName());
}

public function testSerialize()
{
$env = 'test_env';
Expand Down

0 comments on commit 02e0a8f

Please sign in to comment.