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\InvalidArgumentException;
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * This is a directed graph of your services.
|
Chris@0
|
18 *
|
Chris@0
|
19 * This information can be used by your compiler passes instead of collecting
|
Chris@0
|
20 * it themselves which improves performance quite a lot.
|
Chris@0
|
21 *
|
Chris@0
|
22 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
Chris@14
|
23 *
|
Chris@14
|
24 * @final since version 3.4
|
Chris@0
|
25 */
|
Chris@0
|
26 class ServiceReferenceGraph
|
Chris@0
|
27 {
|
Chris@0
|
28 /**
|
Chris@0
|
29 * @var ServiceReferenceGraphNode[]
|
Chris@0
|
30 */
|
Chris@0
|
31 private $nodes = array();
|
Chris@0
|
32
|
Chris@0
|
33 /**
|
Chris@0
|
34 * Checks if the graph has a specific node.
|
Chris@0
|
35 *
|
Chris@0
|
36 * @param string $id Id to check
|
Chris@0
|
37 *
|
Chris@0
|
38 * @return bool
|
Chris@0
|
39 */
|
Chris@0
|
40 public function hasNode($id)
|
Chris@0
|
41 {
|
Chris@0
|
42 return isset($this->nodes[$id]);
|
Chris@0
|
43 }
|
Chris@0
|
44
|
Chris@0
|
45 /**
|
Chris@0
|
46 * Gets a node by identifier.
|
Chris@0
|
47 *
|
Chris@0
|
48 * @param string $id The id to retrieve
|
Chris@0
|
49 *
|
Chris@0
|
50 * @return ServiceReferenceGraphNode
|
Chris@0
|
51 *
|
Chris@0
|
52 * @throws InvalidArgumentException if no node matches the supplied identifier
|
Chris@0
|
53 */
|
Chris@0
|
54 public function getNode($id)
|
Chris@0
|
55 {
|
Chris@0
|
56 if (!isset($this->nodes[$id])) {
|
Chris@0
|
57 throw new InvalidArgumentException(sprintf('There is no node with id "%s".', $id));
|
Chris@0
|
58 }
|
Chris@0
|
59
|
Chris@0
|
60 return $this->nodes[$id];
|
Chris@0
|
61 }
|
Chris@0
|
62
|
Chris@0
|
63 /**
|
Chris@0
|
64 * Returns all nodes.
|
Chris@0
|
65 *
|
Chris@0
|
66 * @return ServiceReferenceGraphNode[]
|
Chris@0
|
67 */
|
Chris@0
|
68 public function getNodes()
|
Chris@0
|
69 {
|
Chris@0
|
70 return $this->nodes;
|
Chris@0
|
71 }
|
Chris@0
|
72
|
Chris@0
|
73 /**
|
Chris@0
|
74 * Clears all nodes.
|
Chris@0
|
75 */
|
Chris@0
|
76 public function clear()
|
Chris@0
|
77 {
|
Chris@14
|
78 foreach ($this->nodes as $node) {
|
Chris@14
|
79 $node->clear();
|
Chris@14
|
80 }
|
Chris@0
|
81 $this->nodes = array();
|
Chris@0
|
82 }
|
Chris@0
|
83
|
Chris@0
|
84 /**
|
Chris@0
|
85 * Connects 2 nodes together in the Graph.
|
Chris@0
|
86 *
|
Chris@0
|
87 * @param string $sourceId
|
Chris@14
|
88 * @param mixed $sourceValue
|
Chris@0
|
89 * @param string $destId
|
Chris@14
|
90 * @param mixed $destValue
|
Chris@0
|
91 * @param string $reference
|
Chris@14
|
92 * @param bool $lazy
|
Chris@14
|
93 * @param bool $weak
|
Chris@0
|
94 */
|
Chris@14
|
95 public function connect($sourceId, $sourceValue, $destId, $destValue = null, $reference = null/*, bool $lazy = false, bool $weak = false*/)
|
Chris@0
|
96 {
|
Chris@14
|
97 $lazy = func_num_args() >= 6 ? func_get_arg(5) : false;
|
Chris@14
|
98 $weak = func_num_args() >= 7 ? func_get_arg(6) : false;
|
Chris@14
|
99
|
Chris@14
|
100 if (null === $sourceId || null === $destId) {
|
Chris@14
|
101 return;
|
Chris@14
|
102 }
|
Chris@14
|
103
|
Chris@0
|
104 $sourceNode = $this->createNode($sourceId, $sourceValue);
|
Chris@0
|
105 $destNode = $this->createNode($destId, $destValue);
|
Chris@14
|
106 $edge = new ServiceReferenceGraphEdge($sourceNode, $destNode, $reference, $lazy, $weak);
|
Chris@0
|
107
|
Chris@0
|
108 $sourceNode->addOutEdge($edge);
|
Chris@0
|
109 $destNode->addInEdge($edge);
|
Chris@0
|
110 }
|
Chris@0
|
111
|
Chris@0
|
112 /**
|
Chris@0
|
113 * Creates a graph node.
|
Chris@0
|
114 *
|
Chris@0
|
115 * @param string $id
|
Chris@14
|
116 * @param mixed $value
|
Chris@0
|
117 *
|
Chris@0
|
118 * @return ServiceReferenceGraphNode
|
Chris@0
|
119 */
|
Chris@0
|
120 private function createNode($id, $value)
|
Chris@0
|
121 {
|
Chris@0
|
122 if (isset($this->nodes[$id]) && $this->nodes[$id]->getValue() === $value) {
|
Chris@0
|
123 return $this->nodes[$id];
|
Chris@0
|
124 }
|
Chris@0
|
125
|
Chris@0
|
126 return $this->nodes[$id] = new ServiceReferenceGraphNode($id, $value);
|
Chris@0
|
127 }
|
Chris@0
|
128 }
|