From 819d10484c1f3f5451fd7259f4918b4aedc7b158 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Wed, 14 Jan 2015 20:04:43 +0100 Subject: [PATCH 1/2] [HttpKernel] Fixed name resolution when the parent directory contain fordidden char for a class name --- src/Symfony/Component/HttpKernel/Kernel.php | 2 +- .../Component/HttpKernel/Tests/KernelTest.php | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/HttpKernel/Kernel.php b/src/Symfony/Component/HttpKernel/Kernel.php index dcdb5d4335d4e..e4d804c33aa6b 100644 --- a/src/Symfony/Component/HttpKernel/Kernel.php +++ b/src/Symfony/Component/HttpKernel/Kernel.php @@ -308,7 +308,7 @@ 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)); + $this->name = preg_replace('/(^[0-9]*|[^a-zA-Z0-9_]+)/', '', basename($this->rootDir)) ?: 'app'; } return $this->name; diff --git a/src/Symfony/Component/HttpKernel/Tests/KernelTest.php b/src/Symfony/Component/HttpKernel/Tests/KernelTest.php index 5606efc77415c..0bdf570908ab2 100644 --- a/src/Symfony/Component/HttpKernel/Tests/KernelTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/KernelTest.php @@ -386,6 +386,31 @@ public function testGetName() $this->assertEquals('Fixtures', $kernel->getName()); } + public function getGetNameWithWeirdDirectoryTests() + { + return array( + array('app', 12345), + array('Application', '12345Application'), + array('Application12345', 'Application12345'), + array('Application12345', '12345Application12345'), + ); + } + + /** @dataProvider getGetNameWithWeirdDirectoryTests */ + public function testGetNameWithWeirdDirectory($expected, $rootDir) + { + $kernel = new KernelForTest('test', true); + $p = new \ReflectionProperty($kernel, 'rootDir'); + $p->setAccessible(true); + $p->setValue($kernel, $rootDir); + + $p = new \ReflectionProperty($kernel, 'name'); + $p->setAccessible(true); + $p->setValue($kernel, null); + + $this->assertEquals($expected, $kernel->getName()); + } + public function testOverrideGetName() { $kernel = new KernelForOverrideName('test', true); From d86c40429467cc80ebc2678354f2c90e55c4669f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Wed, 14 Jan 2015 20:17:01 +0100 Subject: [PATCH 2/2] [HttpKernel] Fixed Kernel::getContainerClass, to prevent invalid PHP class name --- src/Symfony/Component/HttpKernel/Kernel.php | 6 ++--- .../Component/HttpKernel/Tests/KernelTest.php | 26 +++++++++++-------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/Symfony/Component/HttpKernel/Kernel.php b/src/Symfony/Component/HttpKernel/Kernel.php index e4d804c33aa6b..2a1c3771c9222 100644 --- a/src/Symfony/Component/HttpKernel/Kernel.php +++ b/src/Symfony/Component/HttpKernel/Kernel.php @@ -242,7 +242,7 @@ public function getBundle($name, $first = true) } /** - * {@inheritDoc} + * {@inheritdoc} * * @throws \RuntimeException if a custom resource is hidden by a resource in a derived bundle */ @@ -308,7 +308,7 @@ public function locateResource($name, $dir = null, $first = true) public function getName() { if (null === $this->name) { - $this->name = preg_replace('/(^[0-9]*|[^a-zA-Z0-9_]+)/', '', basename($this->rootDir)) ?: 'app'; + $this->name = preg_replace('/[^a-zA-Z0-9_]+/', '', basename($this->rootDir)); } return $this->name; @@ -502,7 +502,7 @@ protected function initializeBundles() */ protected function getContainerClass() { - return $this->name.ucfirst($this->environment).($this->debug ? 'Debug' : '').'ProjectContainer'; + return preg_replace('/(^[0-9]*|[^a-zA-Z0-9_]+)/', '', $this->name.ucfirst($this->environment)).($this->debug ? 'Debug' : '').'ProjectContainer'; } /** diff --git a/src/Symfony/Component/HttpKernel/Tests/KernelTest.php b/src/Symfony/Component/HttpKernel/Tests/KernelTest.php index 0bdf570908ab2..11324a3467ca9 100644 --- a/src/Symfony/Component/HttpKernel/Tests/KernelTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/KernelTest.php @@ -389,26 +389,30 @@ public function testGetName() public function getGetNameWithWeirdDirectoryTests() { return array( - array('app', 12345), - array('Application', '12345Application'), - array('Application12345', 'Application12345'), - array('Application12345', '12345Application12345'), + array('appDevProjectContainer', 'dev', 'app'), + array('appFoobarProjectContainer', 'foo bar', 'app'), + array('appProjectContainer', '..**..', 'app'), + array('ProjectContainer', '..**..', '12345'), + array('DevProjectContainer', 'dev', 12345), + array('ApplicationDevProjectContainer', 'dev', '12345Application'), + array('Application12345DevProjectContainer', 'dev', 'Application12345'), + array('Application12345DevProjectContainer', 'dev', '12345Application12345'), ); } /** @dataProvider getGetNameWithWeirdDirectoryTests */ - public function testGetNameWithWeirdDirectory($expected, $rootDir) + public function testGetContainerClass($expected, $environment, $rootDir) { - $kernel = new KernelForTest('test', true); - $p = new \ReflectionProperty($kernel, 'rootDir'); - $p->setAccessible(true); - $p->setValue($kernel, $rootDir); + $kernel = new KernelForTest($environment, false); $p = new \ReflectionProperty($kernel, 'name'); $p->setAccessible(true); - $p->setValue($kernel, null); + $p->setValue($kernel, $rootDir); + + $m = new \ReflectionMethod($kernel, 'getContainerClass'); + $m->setAccessible(true); - $this->assertEquals($expected, $kernel->getName()); + $this->assertEquals($expected, $m->invoke($kernel)); } public function testOverrideGetName()