vendor/symfony/src/Symfony/Component/DependencyInjection/Compiler/CheckCircularReferencesPass.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony framework.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.com>
       
     7  *
       
     8  * This source file is subject to the MIT license that is bundled
       
     9  * with this source code in the file LICENSE.
       
    10  */
       
    11 
       
    12 namespace Symfony\Component\DependencyInjection\Compiler;
       
    13 
       
    14 use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
       
    15 use Symfony\Component\DependencyInjection\ContainerBuilder;
       
    16 
       
    17 /**
       
    18  * Checks your services for circular references
       
    19  *
       
    20  * References from method calls are ignored since we might be able to resolve
       
    21  * these references depending on the order in which services are called.
       
    22  *
       
    23  * Circular reference from method calls will only be detected at run-time.
       
    24  *
       
    25  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
       
    26  */
       
    27 class CheckCircularReferencesPass implements CompilerPassInterface
       
    28 {
       
    29     private $currentId;
       
    30     private $currentPath;
       
    31 
       
    32     /**
       
    33      * Checks the ContainerBuilder object for circular references.
       
    34      *
       
    35      * @param ContainerBuilder $container The ContainerBuilder instances
       
    36      */
       
    37     public function process(ContainerBuilder $container)
       
    38     {
       
    39         $graph = $container->getCompiler()->getServiceReferenceGraph();
       
    40 
       
    41         foreach ($graph->getNodes() as $id => $node) {
       
    42             $this->currentId = $id;
       
    43             $this->currentPath = array($id);
       
    44 
       
    45             $this->checkOutEdges($node->getOutEdges());
       
    46         }
       
    47     }
       
    48 
       
    49     /**
       
    50      * Checks for circular references.
       
    51      *
       
    52      * @param array $edges An array of Nodes
       
    53      * @throws \RuntimeException When a circular reference is found.
       
    54      */
       
    55     private function checkOutEdges(array $edges)
       
    56     {
       
    57         foreach ($edges as $edge) {
       
    58             $node = $edge->getDestNode();
       
    59             $this->currentPath[] = $id = $node->getId();
       
    60 
       
    61             if ($this->currentId === $id) {
       
    62                 throw new ServiceCircularReferenceException($this->currentId, $this->currentPath);
       
    63             }
       
    64 
       
    65             $this->checkOutEdges($node->getOutEdges());
       
    66             array_pop($this->currentPath);
       
    67         }
       
    68     }
       
    69 }