Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / symfony / dependency-injection / Compiler / CheckCircularReferencesPass.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
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 $currentPath;
30     private $checkedNodes;
31
32     /**
33      * Checks the ContainerBuilder object for circular references.
34      */
35     public function process(ContainerBuilder $container)
36     {
37         $graph = $container->getCompiler()->getServiceReferenceGraph();
38
39         $this->checkedNodes = array();
40         foreach ($graph->getNodes() as $id => $node) {
41             $this->currentPath = array($id);
42
43             $this->checkOutEdges($node->getOutEdges());
44         }
45     }
46
47     /**
48      * Checks for circular references.
49      *
50      * @param ServiceReferenceGraphEdge[] $edges An array of Edges
51      *
52      * @throws ServiceCircularReferenceException when a circular reference is found
53      */
54     private function checkOutEdges(array $edges)
55     {
56         foreach ($edges as $edge) {
57             $node = $edge->getDestNode();
58             $id = $node->getId();
59
60             if (empty($this->checkedNodes[$id])) {
61                 // Don't check circular references for lazy edges
62                 if (!$node->getValue() || (!$edge->isLazy() && !$edge->isWeak())) {
63                     $searchKey = array_search($id, $this->currentPath);
64                     $this->currentPath[] = $id;
65
66                     if (false !== $searchKey) {
67                         throw new ServiceCircularReferenceException($id, array_slice($this->currentPath, $searchKey));
68                     }
69
70                     $this->checkOutEdges($node->getOutEdges());
71                 }
72
73                 $this->checkedNodes[$id] = true;
74                 array_pop($this->currentPath);
75             }
76         }
77     }
78 }