[symfony/HttpKernel] Throws an error when the generated class name is invalid.

This commit is contained in:
Pol Dellaiera 2019-04-06 16:33:16 +02:00 committed by Fabien Potencier
parent fe7363fff1
commit c976866566
2 changed files with 22 additions and 1 deletions

View File

@ -442,14 +442,21 @@ abstract class Kernel implements KernelInterface, RebootableInterface, Terminabl
/**
* Gets the container class.
*
* @throws \InvalidArgumentException If the generated classname is invalid
*
* @return string The container class
*/
protected function getContainerClass()
{
$class = \get_class($this);
$class = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).str_replace('.', '_', ContainerBuilder::hash($class)) : $class;
$class = $this->name.str_replace('\\', '_', $class).ucfirst($this->environment).($this->debug ? 'Debug' : '').'Container';
return $this->name.str_replace('\\', '_', $class).ucfirst($this->environment).($this->debug ? 'Debug' : '').'Container';
if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $class)) {
throw new \InvalidArgumentException(sprintf('The environment "%s" contains invalid characters, it can only contain characters allowed in PHP class names.', $this->environment));
}
return $class;
}
/**

View File

@ -64,6 +64,20 @@ class KernelTest extends TestCase
$this->assertNull($clone->getContainer());
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The environment "test.env" contains invalid characters, it can only contain characters allowed in PHP class names.
*/
public function testClassNameValidityGetter()
{
// We check the classname that will be generated by using a $env that
// contains invalid characters.
$env = 'test.env';
$kernel = new KernelForTest($env, false);
$kernel->boot();
}
public function testInitializeContainerClearsOldContainers()
{
$fs = new Filesystem();