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@0
|
14 use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
|
Chris@0
|
15 use Symfony\Component\DependencyInjection\ContainerBuilder;
|
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 * @param ContainerBuilder $container The ContainerBuilder instances
|
Chris@0
|
36 */
|
Chris@0
|
37 public function process(ContainerBuilder $container)
|
Chris@0
|
38 {
|
Chris@0
|
39 $graph = $container->getCompiler()->getServiceReferenceGraph();
|
Chris@0
|
40
|
Chris@0
|
41 $this->checkedNodes = array();
|
Chris@0
|
42 foreach ($graph->getNodes() as $id => $node) {
|
Chris@0
|
43 $this->currentPath = array($id);
|
Chris@0
|
44
|
Chris@0
|
45 $this->checkOutEdges($node->getOutEdges());
|
Chris@0
|
46 }
|
Chris@0
|
47 }
|
Chris@0
|
48
|
Chris@0
|
49 /**
|
Chris@0
|
50 * Checks for circular references.
|
Chris@0
|
51 *
|
Chris@0
|
52 * @param ServiceReferenceGraphEdge[] $edges An array of Edges
|
Chris@0
|
53 *
|
Chris@0
|
54 * @throws ServiceCircularReferenceException When a circular reference is found.
|
Chris@0
|
55 */
|
Chris@0
|
56 private function checkOutEdges(array $edges)
|
Chris@0
|
57 {
|
Chris@0
|
58 foreach ($edges as $edge) {
|
Chris@0
|
59 $node = $edge->getDestNode();
|
Chris@0
|
60 $id = $node->getId();
|
Chris@0
|
61
|
Chris@0
|
62 if (empty($this->checkedNodes[$id])) {
|
Chris@0
|
63 // don't check circular dependencies for lazy services
|
Chris@0
|
64 if (!$node->getValue() || !$node->getValue()->isLazy()) {
|
Chris@0
|
65 $searchKey = array_search($id, $this->currentPath);
|
Chris@0
|
66 $this->currentPath[] = $id;
|
Chris@0
|
67
|
Chris@0
|
68 if (false !== $searchKey) {
|
Chris@0
|
69 throw new ServiceCircularReferenceException($id, array_slice($this->currentPath, $searchKey));
|
Chris@0
|
70 }
|
Chris@0
|
71
|
Chris@0
|
72 $this->checkOutEdges($node->getOutEdges());
|
Chris@0
|
73 }
|
Chris@0
|
74
|
Chris@0
|
75 $this->checkedNodes[$id] = true;
|
Chris@0
|
76 array_pop($this->currentPath);
|
Chris@0
|
77 }
|
Chris@0
|
78 }
|
Chris@0
|
79 }
|
Chris@0
|
80 }
|