8000 merged branch jonathaningram/override_kernel_name (PR #4846) · jacobmaster/symfony@a94d41d · GitHub
[go: up one dir, main page]

Skip to content

Commit a94d41d

Browse files
committed
merged branch jonathaningram/override_kernel_name (PR symfony#4846)
Commits ------- 02e0a8f Allow Kernel::$name to be overridden by subclasses Discussion ---------- 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. What do you think?
2 parents bfda8a3 + 02e0a8f commit a94d41d

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function __construct($environment, $debug)
7979
$this->debug = (Boolean) $debug;
8080
$this->booted = false;
8181
$this->rootDir = $this->getRootDir();
82-
$this->name = preg_replace('/[^a-zA-Z0-9_]+/', '', basename($this->rootDir));
82+
$this->name = $this->getName();
8383
$this->classes = array();
8484

8585
if ($this->debug) {
@@ -354,6 +354,10 @@ public function locateResource($name, $dir = null, $first = true)
354354
*/
355355
public function getName()
356356
{
357+
if (null === $this->name) {
358+
$this->name = preg_replace('/[^a-zA-Z0-9_]+/', '', basename($this->rootDir));
359+
}
360+
357361
return $this->name;
358362
}
359363

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\Tests\Fixtures;
13+
14+
use Symfony\Component\HttpKernel\Kernel;
15+
use Symfony\Component\Config\Loader\LoaderInterface;
16+
17+
class KernelForOverrideName extends Kernel
18+
{
19+
protected $name = 'overridden';
20+
21+
public function registerBundles()
22+
{
23+
24+
}
25+
26+
public function registerContainerConfiguration(LoaderInterface $loader)
27+
{
28+
29+
}
30+
}

src/Symfony/Component/HttpKernel/Tests/KernelTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\HttpFoundation\Request;
1818
use Symfony\Component\HttpFoundation\Response;
1919
use Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest;
20+
use Symfony\Component\HttpKernel\Tests\Fixtures\KernelForOverrideName;
2021
use Symfony\Component\HttpKernel\Tests\Fixtures\FooBarBundle;
2122

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

314+
public function testOverrideGetName()
315+
{
316+
$kernel = new KernelForOverrideName('test', true);
317+
318+
$this->assertEquals('overridden', $kernel->getName());
319+
}
320+
313321
public function testSerialize()
314322
{
315323
$env = 'test_env';

0 commit comments

Comments
 (0)
0