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

Skip to content

Commit 96bb731

Browse files
tgabi333fabpot
authored andcommitted
optimized circular reference checker
1 parent 0009deb commit 96bb731

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class CheckCircularReferencesPass implements CompilerPassInterface
2828
{
2929
private $currentId;
3030
private $currentPath;
31+
private $checkedNodes;
3132

3233
/**
3334
* Checks the ContainerBuilder object for circular references.
@@ -38,6 +39,7 @@ public function process(ContainerBuilder $container)
3839
{
3940
$graph = $container->getCompiler()->getServiceReferenceGraph();
4041

42+
$this->checkedNodes = array();
4143
foreach ($graph->getNodes() as $id => $node) {
4244
$this->currentId = $id;
4345
$this->currentPath = array($id);
@@ -58,15 +60,20 @@ private function checkOutEdges(array $edges)
5860
foreach ($edges as $edge) {
5961
$node = $edge->getDestNode();
6062
$id = $node->getId();
61-
$searchKey = array_search($id, $this->currentPath);
62-
$this->currentPath[] = $id;
6363

64-
if (false !== $searchKey) {
65-
throw new ServiceCircularReferenceException($id, array_slice($this->currentPath, $searchKey));
66-
}
64+
if (empty($this->checkedNodes[$id])) {
65+
$searchKey = array_search($id, $this->currentPath);
66+
$this->currentPath[] = $id;
6767

68-
$this->checkOutEdges($node->getOutEdges());
69-
array_pop($this->currentPath);
68+
if (false !== $searchKey) {
69+
throw new ServiceCircularReferenceException($id, array_slice($this->currentPath, $searchKey));
70+
}
71+
72+
$this->checkOutEdges($node->getOutEdges());
73+
74+
$this->checkedNodes[$id] = true;
75+
array_pop($this->currentPath);
76+
}
7077
}
7178
}
7279
}

0 commit comments

Comments
 (0)