bug #25364 [DependencyInjection] Prevent a loop in aliases within the `findDefinition` method (sroze)

This PR was merged into the 3.3 branch.

Discussion
----------

[DependencyInjection] Prevent a loop in aliases within the `findDefinition` method

| Q             | A
| ------------- | ---
| Branch?       | 3.3
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #25338
| License       | MIT
| Doc PR        | ø

This prevents an infinite loop going when aliases reference themselves. This is based on 3.3 as the "normalized ID" changed to allow non-lowercase names. Fixing this in 2.7 would mean a merge conflict that IMO is not worth it.

Commits
-------

22f35239a4 Prevent a loop in aliases within the `findDefinition` method
This commit is contained in:
Fabien Potencier 2017-12-07 08:06:29 -08:00
commit 6e7e6847d9
2 changed files with 23 additions and 0 deletions

View File

@ -993,8 +993,15 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
{
$id = $this->normalizeId($id);
$seen = array();
while (isset($this->aliasDefinitions[$id])) {
$id = (string) $this->aliasDefinitions[$id];
if (isset($seen[$id])) {
throw new ServiceCircularReferenceException($id, array_keys($seen));
}
$seen[$id] = true;
}
return $this->getDefinition($id);

View File

@ -1044,6 +1044,22 @@ class ContainerBuilderTest extends TestCase
$this->assertNotSame($bar->foo, $barUser->foo);
}
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
* @expectedExceptionMessage Circular reference detected for service "app.test_class", path: "app.test_class -> App\TestClass".
*/
public function testThrowsCircularExceptionForCircularAliases()
{
$builder = new ContainerBuilder();
$builder->setAliases(array(
'app.test_class' => new Alias('App\\TestClass'),
'App\\TestClass' => new Alias('app.test_class'),
));
$builder->findDefinition('App\\TestClass');
}
public function testInitializePropertiesBeforeMethodCalls()
{
$container = new ContainerBuilder();