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 string $sourceValue
84 * @param string $destId
85 * @param string $destValue
86 * @param string $reference
88 public function connect($sourceId, $sourceValue, $destId, $destValue = null, $reference = null)
90 $sourceNode = $this->createNode($sourceId, $sourceValue);
91 $destNode = $this->createNode($destId, $destValue);
92 $edge = new ServiceReferenceGraphEdge($sourceNode, $destNode, $reference);
94 $sourceNode->addOutEdge($edge);
95 $destNode->addInEdge($edge);
99 * Creates a graph node.
102 * @param string $value
104 * @return ServiceReferenceGraphNode
106 private function createNode($id, $value)
108 if (isset($this->nodes[$id]) && $this->nodes[$id]->getValue() === $value) {
109 return $this->nodes[$id];
112 return $this->nodes[$id] = new ServiceReferenceGraphNode($id, $value);