Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\DependencyInjection\Compiler;
|
Chris@0
|
13
|
Chris@17
|
14 use Symfony\Component\DependencyInjection\ContainerBuilder;
|
Chris@0
|
15 use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * Checks your services for circular references.
|
Chris@0
|
19 *
|
Chris@0
|
20 * References from method calls are ignored since we might be able to resolve
|
Chris@0
|
21 * these references depending on the order in which services are called.
|
Chris@0
|
22 *
|
Chris@0
|
23 * Circular reference from method calls will only be detected at run-time.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
Chris@0
|
26 */
|
Chris@0
|
27 class CheckCircularReferencesPass implements CompilerPassInterface
|
Chris@0
|
28 {
|
Chris@0
|
29 private $currentPath;
|
Chris@0
|
30 private $checkedNodes;
|
Chris@0
|
31
|
Chris@0
|
32 /**
|
Chris@0
|
33 * Checks the ContainerBuilder object for circular references.
|
Chris@0
|
34 */
|
Chris@0
|
35 public function process(ContainerBuilder $container)
|
Chris@0
|
36 {
|
Chris@0
|
37 $graph = $container->getCompiler()->getServiceReferenceGraph();
|
Chris@0
|
38
|
Chris@17
|
39 $this->checkedNodes = [];
|
Chris@0
|
40 foreach ($graph->getNodes() as $id => $node) {
|
Chris@17
|
41 $this->currentPath = [$id];
|
Chris@0
|
42
|
Chris@0
|
43 $this->checkOutEdges($node->getOutEdges());
|
Chris@0
|
44 }
|
Chris@0
|
45 }
|
Chris@0
|
46
|
Chris@0
|
47 /**
|
Chris@0
|
48 * Checks for circular references.
|
Chris@0
|
49 *
|
Chris@0
|
50 * @param ServiceReferenceGraphEdge[] $edges An array of Edges
|
Chris@0
|
51 *
|
Chris@14
|
52 * @throws ServiceCircularReferenceException when a circular reference is found
|
Chris@0
|
53 */
|
Chris@0
|
54 private function checkOutEdges(array $edges)
|
Chris@0
|
55 {
|
Chris@0
|
56 foreach ($edges as $edge) {
|
Chris@0
|
57 $node = $edge->getDestNode();
|
Chris@0
|
58 $id = $node->getId();
|
Chris@0
|
59
|
Chris@0
|
60 if (empty($this->checkedNodes[$id])) {
|
Chris@14
|
61 // Don't check circular references for lazy edges
|
Chris@14
|
62 if (!$node->getValue() || (!$edge->isLazy() && !$edge->isWeak())) {
|
Chris@0
|
63 $searchKey = array_search($id, $this->currentPath);
|
Chris@0
|
64 $this->currentPath[] = $id;
|
Chris@0
|
65
|
Chris@0
|
66 if (false !== $searchKey) {
|
Chris@17
|
67 throw new ServiceCircularReferenceException($id, \array_slice($this->currentPath, $searchKey));
|
Chris@0
|
68 }
|
Chris@0
|
69
|
Chris@0
|
70 $this->checkOutEdges($node->getOutEdges());
|
Chris@0
|
71 }
|
Chris@0
|
72
|
Chris@0
|
73 $this->checkedNodes[$id] = true;
|
Chris@0
|
74 array_pop($this->currentPath);
|
Chris@0
|
75 }
|
Chris@0
|
76 }
|
Chris@0
|
77 }
|
Chris@0
|
78 }
|