Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpKernel/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ protected function initializeBundles()
*/
protected function getContainerClass()
{
return $this->name.ucfirst($this->environment).($this->debug ? 'Debug' : '').'ProjectContainer';
return $this->name.ucfirst(preg_replace('/[^a-zA-Z0-9_]/', '', $this->environment)).($this->debug ? 'Debug' : '').'ProjectContainer';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the regex about valid chars in class names is broader than that

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The official regexp for class names is ^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$ (see http://php.net/manual/en/language.oop5.basic.php) so this part should be [^a-zA-Z0-9_\x7f-\xff] instead of [^a-zA-Z0-9_] ... but I don't think we should do this change. The probability of causing a clash because two weird env names have been stripped from wrong characters is negligible.

}

/**
Expand Down
67 changes: 67 additions & 0 deletions src/Symfony/Component/HttpKernel/Tests/KernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\HttpKernel\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
Expand Down Expand Up @@ -777,6 +778,32 @@ public function testKernelRootDirNameStartingWithANumber()
$this->assertEquals('_123', $kernel->getName());
}

/**
* @dataProvider envAndContainerClassDataProviderForCustomProjectDirKernel
*/
public function testContainerClassNormalization($env, $expected)
{
$kernel = new CustomProjectDirKernel(null, null, $env);

$containerClassMethod = (new \ReflectionObject($kernel))->getMethod('getContainerClass');
$containerClassMethod->setAccessible(true);
$class = $containerClassMethod->invoke($kernel);

$this->assertSame($expected, $class);
}

/**
* @return array
*/
public function envAndContainerClassDataProviderForCustomProjectDirKernel()
{
return array(
array('dev-server', 'FixturesDevserverDebugProjectContainer'),
array('local', 'FixturesLocalDebugProjectContainer'),
array('env_#8---*1', 'FixturesEnv_81DebugProjectContainer'),
);
}

/**
* Returns a mock for the BundleInterface.
*
Expand Down Expand Up @@ -873,3 +900,43 @@ public function handle(Request $request, $type = self::MASTER_REQUEST, $catch =
{
}
}

class CustomProjectDirKernel extends Kernel
{
private $buildContainer;
private $httpKernel;

public function __construct(\Closure $buildContainer = null, HttpKernelInterface $httpKernel = null, $name = 'custom')
{
parent::__construct($name, true);

$this->buildContainer = $buildContainer;
$this->httpKernel = $httpKernel;
}

public function registerBundles()
{
return array();
}

public function registerContainerConfiguration(LoaderInterface $loader)
{
}

public function getRootDir()
{
return __DIR__.'/Fixtures';
}

protected function build(ContainerBuilder $container)
{
if ($build = $this->buildContainer) {
$build($container);
}
}

protected function getHttpKernel()
{
return $this->httpKernel;
}
}