8000 [HttpKernel] Fixed Kernel::getContainerClass, to prevent invalid PHP … · symfony/symfony@a9c1bcc · GitHub
[go: up one dir, main page]

Skip to content

Commit a9c1bcc

Browse files
committed
[HttpKernel] Fixed Kernel::getContainerClass, to prevent invalid PHP class name
1 parent 819d104 commit a9c1bcc

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ public function locateResource($name, $dir = null, $first = true)
308308
public function getName()
309309
{
310310
if (null === $this->name) {
311-
$this->name = preg_replace('/(^[0-9]*|[^a-zA-Z0-9_]+)/', '', basename($this->rootDir)) ?: 'app';
311+
$this->name = preg_replace('/[^a-zA-Z0-9_]+/', '', basename($this->rootDir));
312312
}
313313

314314
return $this->name;
@@ -502,7 +502,7 @@ protected function initializeBundles()
502502
*/
503503
protected function getContainerClass()
504504
{
505-
return $this->name.ucfirst($this->environment).($this->debug ? 'Debug' : '').'ProjectContainer';
505+
return preg_replace('/(^[0-9]*|[^a-zA-Z0-9_]+)/', '', $this->name.ucfirst($this->environment)).($this->debug ? 'Debug' : '').'ProjectContainer';
506506
}
507507

508508
/**

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -389,26 +389,30 @@ public function testGetName()
389389
public function getGetNameWithWeirdDirectoryTests()
390390
{
391391
return array(
392-
array('app', 12345),
393-
array('Application', '12345Application'),
394-
array('Application12345', 'Application12345'),
395-
array('Application12345', '12345Application12345'),
392+
array('appDevProjectContainer', 'dev', 'app'),
393+
array('appFoobarProjectContainer', 'foo bar', 'app'),
394+
array('appProjectContainer', '..**..', 'app'),
395+
array('ProjectContainer', '..**..', '12345'),
396+
array('DevProjectContainer', 'dev', 12345),
397+
array('ApplicationDevProjectContainer', 'dev', '12345Application'),
398+
array('Application12345DevProjectContainer', 'dev', 'Application12345'),
399+
array('Application12345DevProjectContainer', 'dev', '12345Application12345'),
396400
);
397401
}
398402

399403
/** @dataProvider getGetNameWithWeirdDirectoryTests */
400-
public function testGetNameWithWeirdDirectory($expected, $rootDir)
404+
public function testGetContainerClass($expected, $environment, $rootDir)
401405
{
402-
$kernel = new KernelForTest('test', true);
403-
$p = new \ReflectionProperty($kernel, 'rootDir');
404-
$p->setAccessible(true);
405-
$p->setValue($kernel, $rootDir);
406+
$kernel = new KernelForTest($environment, false);
406407

407408
$p = new \ReflectionProperty($kernel, 'name');
408409
$p->setAccessible(true);
409-
$p->setValue($kernel, null);
410+
$p->setValue($kernel, $rootDir);
411+
412+
$m = new \ReflectionMethod($kernel, 'getContainerClass');
413+
$m->setAccessible(true);
410414

411-
$this->assertEquals($expected, $kernel->getName());
415+
$this->assertEquals($expected, $m->invoke($kernel));
412416
}
413417

414418
public function testOverrideGetName()

0 commit comments

Comments
 (0)
0