Skip to content

Commit

Permalink
feature #30906 [symfony/HttpKernel] Throws an error when the generate…
Browse files Browse the repository at this point in the history
…d 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.
  • Loading branch information
fabpot committed Apr 6, 2019
2 parents ea5ff18 + c976866 commit de3c742
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Symfony/Component/HttpKernel/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -442,14 +442,21 @@ protected function build(ContainerBuilder $container)
/**
* 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';

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 $this->name.str_replace('\\', '_', $class).ucfirst($this->environment).($this->debug ? 'Debug' : '').'Container';
return $class;
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/HttpKernel/Tests/KernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ public function testClone()
$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();
Expand Down

0 comments on commit de3c742

Please sign in to comment.