Skip to content

Commit

Permalink
bug #25364 [DependencyInjection] Prevent a loop in aliases within the…
Browse files Browse the repository at this point in the history
… `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
-------

22f3523 Prevent a loop in aliases within the `findDefinition` method
  • Loading branch information
fabpot committed Dec 7, 2017
2 parents b783602 + 22f3523 commit 6e7e684
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
Expand Up @@ -993,8 +993,15 @@ public function findDefinition($id)
{
$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);
Expand Down
Expand Up @@ -1044,6 +1044,22 @@ public function testInlinedDefinitions()
$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();
Expand Down

0 comments on commit 6e7e684

Please sign in to comment.