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@0
|
23 */
|
Chris@0
|
24 class ServiceReferenceGraph
|
Chris@0
|
25 {
|
Chris@0
|
26 /**
|
Chris@0
|
27 * @var ServiceReferenceGraphNode[]
|
Chris@0
|
28 */
|
Chris@0
|
29 private $nodes = array();
|
Chris@0
|
30
|
Chris@0
|
31 /**
|
Chris@0
|
32 * Checks if the graph has a specific node.
|
Chris@0
|
33 *
|
Chris@0
|
34 * @param string $id Id to check
|
Chris@0
|
35 *
|
Chris@0
|
36 * @return bool
|
Chris@0
|
37 */
|
Chris@0
|
38 public function hasNode($id)
|
Chris@0
|
39 {
|
Chris@0
|
40 return isset($this->nodes[$id]);
|
Chris@0
|
41 }
|
Chris@0
|
42
|
Chris@0
|
43 /**
|
Chris@0
|
44 * Gets a node by identifier.
|
Chris@0
|
45 *
|
Chris@0
|
46 * @param string $id The id to retrieve
|
Chris@0
|
47 *
|
Chris@0
|
48 * @return ServiceReferenceGraphNode
|
Chris@0
|
49 *
|
Chris@0
|
50 * @throws InvalidArgumentException if no node matches the supplied identifier
|
Chris@0
|
51 */
|
Chris@0
|
52 public function getNode($id)
|
Chris@0
|
53 {
|
Chris@0
|
54 if (!isset($this->nodes[$id])) {
|
Chris@0
|
55 throw new InvalidArgumentException(sprintf('There is no node with id "%s".', $id));
|
Chris@0
|
56 }
|
Chris@0
|
57
|
Chris@0
|
58 return $this->nodes[$id];
|
Chris@0
|
59 }
|
Chris@0
|
60
|
Chris@0
|
61 /**
|
Chris@0
|
62 * Returns all nodes.
|
Chris@0
|
63 *
|
Chris@0
|
64 * @return ServiceReferenceGraphNode[]
|
Chris@0
|
65 */
|
Chris@0
|
66 public function getNodes()
|
Chris@0
|
67 {
|
Chris@0
|
68 return $this->nodes;
|
Chris@0
|
69 }
|
Chris@0
|
70
|
Chris@0
|
71 /**
|
Chris@0
|
72 * Clears all nodes.
|
Chris@0
|
73 */
|
Chris@0
|
74 public function clear()
|
Chris@0
|
75 {
|
Chris@0
|
76 $this->nodes = array();
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 /**
|
Chris@0
|
80 * Connects 2 nodes together in the Graph.
|
Chris@0
|
81 *
|
Chris@0
|
82 * @param string $sourceId
|
Chris@0
|
83 * @param string $sourceValue
|
Chris@0
|
84 * @param string $destId
|
Chris@0
|
85 * @param string $destValue
|
Chris@0
|
86 * @param string $reference
|
Chris@0
|
87 */
|
Chris@0
|
88 public function connect($sourceId, $sourceValue, $destId, $destValue = null, $reference = null)
|
Chris@0
|
89 {
|
Chris@0
|
90 $sourceNode = $this->createNode($sourceId, $sourceValue);
|
Chris@0
|
91 $destNode = $this->createNode($destId, $destValue);
|
Chris@0
|
92 $edge = new ServiceReferenceGraphEdge($sourceNode, $destNode, $reference);
|
Chris@0
|
93
|
Chris@0
|
94 $sourceNode->addOutEdge($edge);
|
Chris@0
|
95 $destNode->addInEdge($edge);
|
Chris@0
|
96 }
|
Chris@0
|
97
|
Chris@0
|
98 /**
|
Chris@0
|
99 * Creates a graph node.
|
Chris@0
|
100 *
|
Chris@0
|
101 * @param string $id
|
Chris@0
|
102 * @param string $value
|
Chris@0
|
103 *
|
Chris@0
|
104 * @return ServiceReferenceGraphNode
|
Chris@0
|
105 */
|
Chris@0
|
106 private function createNode($id, $value)
|
Chris@0
|
107 {
|
Chris@0
|
108 if (isset($this->nodes[$id]) && $this->nodes[$id]->getValue() === $value) {
|
Chris@0
|
109 return $this->nodes[$id];
|
Chris@0
|
110 }
|
Chris@0
|
111
|
Chris@0
|
112 return $this->nodes[$id] = new ServiceReferenceGraphNode($id, $value);
|
Chris@0
|
113 }
|
Chris@0
|
114 }
|