8000 feature #30906 [symfony/HttpKernel] Throws an error when the generate… · alquerci/symfony@6f954a1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6f954a1

Browse files
committed
feature symfony#30906 [symfony/HttpKernel] Throws an error when the generated class name is invalid. (drupol)
1 parent 6f50403 commit 6f954a1

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/Symfony/Component/Kernel/Kernel.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,14 +400,21 @@ protected function build(ContainerBuilder $container)
400400
/**
401401
* Gets the container class.
402402
*
403+
* @throws \InvalidArgumentException If the generated classname is invalid
404+
*
403405
* @return string The container class
404406
*/
405407
protected function getContainerClass()
406408
{
407409
$class = \get_class($this);
408410
$class = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).str_replace('.', '_', ContainerBuilder::hash($class)) : $class;
411+
$class = $this->name.str_replace('\\', '_', $class).ucfirst($this->environment).($this->debug ? 'Debug' : '').'Container';
412+
413+
if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $class)) {
414+
throw new \InvalidArgumentException(sprintf('The environment "%s" contains invalid characters, it can only contain characters allowed in PHP class names.', $this->environment));
415+
}
409416

410-
return $this->name.str_replace('\\', '_', $class).ucfirst($this->environment).($this->debug ? 'Debug' : '').'Container';
417+
return $class;
411418
}
412419

413420
/**

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,20 @@ public function testClone()
6161
$this->assertNull($clone->getContainer());
6262
}
6363

64+
/**
65+
* @expectedException \InvalidArgumentException
66+
* @expectedExceptionMessage The environment "test.env" contains invalid characters, it can only contain characters allowed in PHP class names.
67+
*/
68+
public function testClassNameValidityGetter()
69+
{
70+
// We check the classname that will be generated by using a $env that
71+
// contains invalid characters.
72+
$env = 'test.env';
73+
$kernel = new KernelForTest($env, false);
74+
75+
$kernel->boot();
76+
}
77+
6478
public function testInitializeContainerClearsOldContainers()
6579
{
6680
$fs = new Filesystem();

0 commit comments

Comments
 (0)
0