8000 [DependencyInjection] Prevent a loop in aliases within the `findDefinition` method by sroze · Pull Request #25364 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading< 8000 /span>
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prevent a loop in aliases within the findDefinition method
  • Loading branch information
sroze committed Dec 6, 2017
commit 22f35239a46f5a4cf659bd0e5f373281fa7f29f7
Original file line number Diff line number Diff line change
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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The message should be improved see the CheckCircularReferencePass

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean?

Copy link
Member
@nicolas-grekas nicolas-grekas Dec 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry was on mobile :)
This is what I mean:

if (isset($seen[$id])) {
    $seen = array_slice($seen, array_search($id, $seen));
    $seen[] = $id;

    throw new ServiceCircularReferenceException($id, $seen);
}

$seen[$id] = $id;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the idea. Tho, we can't use array_slice with string keys in an array, we'll have to use 2 of them.

}

$seen[$id] = true;
}

return $this->getDefinition($id);
Expand Down
Original file line number Diff line number Diff line change
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