From 02e0a8f232f7dacda005befe057762e8be48f826 Mon Sep 17 00:00:00 2001 From: Jonathan Ingram Date: Wed, 11 Jul 2012 16:13:02 +1000 Subject: [PATCH] Allow Kernel::$name to be overridden by subclasses 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. --- src/Symfony/Component/HttpKernel/Kernel.php | 6 +++- .../Tests/Fixtures/KernelForOverrideName.php | 30 +++++++++++++++++++ .../Component/HttpKernel/Tests/KernelTest.php | 8 +++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/Symfony/Component/HttpKernel/Tests/Fixtures/KernelForOverrideName.php diff --git a/src/Symfony/Component/HttpKernel/Kernel.php b/src/Symfony/Component/HttpKernel/Kernel.php index 40dc4b0833..ced3fa1504 100644 --- a/src/Symfony/Component/HttpKernel/Kernel.php +++ b/src/Symfony/Component/HttpKernel/Kernel.php @@ -79,7 +79,7 @@ abstract class Kernel implements KernelInterface, TerminableInterface $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) { @@ -354,6 +354,10 @@ abstract class Kernel implements KernelInterface, TerminableInterface */ public function getName() { + if (null === $this->name) { + $this->name = preg_replace('/[^a-zA-Z0-9_]+/', '', basename($this->rootDir)); + } + return $this->name; } diff --git a/src/Symfony/Component/HttpKernel/Tests/Fixtures/KernelForOverrideName.php b/src/Symfony/Component/HttpKernel/Tests/Fixtures/KernelForOverrideName.php new file mode 100644 index 0000000000..32c05f4ba6 --- /dev/null +++ b/src/Symfony/Component/HttpKernel/Tests/Fixtures/KernelForOverrideName.php @@ -0,0 +1,30 @@ + + * + * 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) + { + + } +} diff --git a/src/Symfony/Component/HttpKernel/Tests/KernelTest.php b/src/Symfony/Component/HttpKernel/Tests/KernelTest.php index 86cec9fabc..0def224354 100644 --- a/src/Symfony/Component/HttpKernel/Tests/KernelTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/KernelTest.php @@ -17,6 +17,7 @@ use Symfony\Component\HttpKernel\HttpKernelInterface; 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 @@ -310,6 +311,13 @@ EOF; $this->assertEquals('Fixtures', $kernel->getName()); } + public function testOverrideGetName() + { + $kernel = new KernelForOverrideName('test', true); + + $this->assertEquals('overridden', $kernel->getName()); + } + public function testSerialize() { $env = 'test_env';