From 96bb731b28f473279eb3b3d642bd9e376729c530 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=B3th=20G=C3=A1bor?= Date: Fri, 30 Aug 2013 12:51:09 +0200 Subject: [PATCH] optimized circular reference checker --- .../Compiler/CheckCircularReferencesPass.php | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Compiler/CheckCircularReferencesPass.php b/src/Symfony/Component/DependencyInjection/Compiler/CheckCircularReferencesPass.php index 03528fefa9..e81110dc01 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/CheckCircularReferencesPass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/CheckCircularReferencesPass.php @@ -28,6 +28,7 @@ class CheckCircularReferencesPass implements CompilerPassInterface { private $currentId; private $currentPath; + private $checkedNodes; /** * Checks the ContainerBuilder object for circular references. @@ -38,6 +39,7 @@ class CheckCircularReferencesPass implements CompilerPassInterface { $graph = $container->getCompiler()->getServiceReferenceGraph(); + $this->checkedNodes = array(); foreach ($graph->getNodes() as $id => $node) { $this->currentId = $id; $this->currentPath = array($id); @@ -58,15 +60,20 @@ class CheckCircularReferencesPass implements CompilerPassInterface foreach ($edges as $edge) { $node = $edge->getDestNode(); $id = $node->getId(); - $searchKey = array_search($id, $this->currentPath); - $this->currentPath[] = $id; - if (false !== $searchKey) { - throw new ServiceCircularReferenceException($id, array_slice($this->currentPath, $searchKey)); + if (empty($this->checkedNodes[$id])) { + $searchKey = array_search($id, $this->currentPath); + $this->currentPath[] = $id; + + if (false !== $searchKey) { + throw new ServiceCircularReferenceException($id, array_slice($this->currentPath, $searchKey)); + } + + $this->checkOutEdges($node->getOutEdges()); + + $this->checkedNodes[$id] = true; + array_pop($this->currentPath); } - - $this->checkOutEdges($node->getOutEdges()); - array_pop($this->currentPath); } } }