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

Skip to content

Commit 8a434ed

Browse files
committed
fix a DI circular reference recognition bug
1 parent 580b249 commit 8a434ed

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

src/Symfony/Component/DependencyInjection/Compiler/CheckCircularReferencesPass.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,13 @@ public function process(ContainerBuilder $container)
5656
private function checkOutEdges(array $edges)
5757
{
5858
foreach ($edges as $edge) {
59-
$node = $edge->getDestNode();
60-
$this->currentPath[] = $id = $node->getId();
59+
$node = $edge->getDestNode();
60+
$id = $node->getId();
61+
$searchKey = array_search($id, $this->currentPath);
62+
$this->currentPath[] = $id;
6163

62-
if ($this->currentId === $id) {
63-
throw new ServiceCircularReferenceException($this->currentId, $this->currentPath);
64+
if (false !== $searchKey) {
65+
throw new ServiceCircularReferenceException($id, array_slice($this->currentPath, $searchKey));
6466
}
6567

6668
$this->checkOutEdges($node->getOutEdges());

src/Symfony/Component/DependencyInjection/Tests/Compiler/CheckCircularReferencesPassTest.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
class CheckCircularReferencesPassTest extends \PHPUnit_Framework_TestCase
2525
{
2626
/**
27-
* @expectedException \RuntimeException
27+
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
2828
*/
2929
public function testProcess()
3030
{
@@ -36,7 +36,7 @@ public function testProcess()
3636
}
3737

3838
/**
39-
* @expectedException \RuntimeException
39+
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
4040
*/
4141
public function testProcessWithAliases()
4242
{
@@ -49,7 +49,7 @@ public function testProcessWithAliases()
4949
}
5050

5151
/**
52-
* @expectedException \RuntimeException
52+
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
5353
*/
5454
public function testProcessDetectsIndirectCircularReference()
5555
{
@@ -61,6 +61,19 @@ public function testProcessDetectsIndirectCircularReference()
6161
$this->process($container);
6262
}
6363

64+
/**
65+
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
66+
*/
67+
public function testDeepCircularReference()
68+
{
69+
$container = new ContainerBuilder();
70+
$container->register('a')->addArgument(new Reference('b'));
71+
$container->register('b')->addArgument(new Reference('c'));
72+
$container->register('c')->addArgument(new Reference('b'));
73+
74+
$this->process($container);
75+
}
76+
6477
public function testProcessIgnoresMethodCalls()
6578
{
6679
$container = new ContainerBuilder();

0 commit comments

Comments
 (0)