4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Compiler
;
14 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
;
17 * This is a directed graph of your services.
19 * This information can be used by your compiler passes instead of collecting
20 * it themselves which improves performance quite a lot.
22 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
24 class ServiceReferenceGraph
27 * @var ServiceReferenceGraphNode[]
29 private $nodes = array();
32 * Checks if the graph has a specific node.
34 * @param string $id Id to check
38 public function hasNode($id)
40 return isset($this->nodes
[$id]);
44 * Gets a node by identifier.
46 * @param string $id The id to retrieve
48 * @return ServiceReferenceGraphNode
50 * @throws InvalidArgumentException if no node matches the supplied identifier
52 public function getNode($id)
54 if (!isset($this->nodes
[$id])) {
55 throw new InvalidArgumentException(sprintf('There is no node with id "%s".', $id));
58 return $this->nodes
[$id];
64 * @return ServiceReferenceGraphNode[]
66 public function getNodes()
74 public function clear()
76 $this->nodes
= array();
80 * Connects 2 nodes together in the Graph.
82 * @param string $sourceId
83 * @param mixed $sourceValue
84 * @param string $destId
85 * @param mixed $destValue
86 * @param string $reference
88 public function connect($sourceId, $sourceValue, $destId, $destValue = null, $reference = null)
90 if (null === $sourceId ||
null === $destId) {
93 $sourceNode = $this->createNode($sourceId, $sourceValue);
94 $destNode = $this->createNode($destId, $destValue);
95 $edge = new ServiceReferenceGraphEdge($sourceNode, $destNode, $reference);
97 $sourceNode->addOutEdge($edge);
98 $destNode->addInEdge($edge);
102 * Creates a graph node.
105 * @param mixed $value
107 * @return ServiceReferenceGraphNode
109 private function createNode($id, $value)
111 if (isset($this->nodes
[$id]) && $this->nodes
[$id]->getValue() === $value) {
112 return $this->nodes
[$id];
115 return $this->nodes
[$id] = new ServiceReferenceGraphNode($id, $value);