Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit de3c742

Browse files
committed
feature #30906 [symfony/HttpKernel] Throws an error when the generated class name is invalid. (drupol)
This PR was squashed before being merged into the 4.3-dev branch (closes #30906). Discussion ---------- [symfony/HttpKernel] Throws an error when the generated class name is invalid. | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #30845 | License | MIT Commits ------- c976866 [symfony/HttpKernel] Throws an error when the generated class name is invalid.
2 parents ea5ff18 + c976866 commit de3c742

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,14 +442,21 @@ protected function build(ContainerBuilder $container)
442442
/**
443443
* Gets the container class.
444444
*
445+
* @throws \InvalidArgumentException If the generated classname is invalid
446+
*
445447
* @return string The container class
446448
*/
447449
protected function getContainerClass()
448450
{
449451
$class = \get_class($this);
450452
$class = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).str_replace('.', '_', ContainerBuilder::hash($class)) : $class;
453+
$class = $this->name.str_replace('\\', '_', $class).ucfirst($this->environment).($this->debug ? 'Debug' : '').'Container';
454+
455+
if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $class)) {
456+
throw new \InvalidArgumentException(sprintf('The environment "%s" contains invalid characters, it can only contain characters allowed in PHP class names.', $this->environment));
457+
}
451458

452-
return $this->name.str_replace('\\', '_', $class).ucfirst($this->environment).($this->debug ? 'Debug' : '').'Container';
459+
return $class;
453460
}
454461

455462
/**

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

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

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

0 commit comments

Comments
 (0)