comparison vendor/symfony/dependency-injection/Compiler/ServiceReferenceGraph.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\DependencyInjection\Compiler;
13
14 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
15
16 /**
17 * This is a directed graph of your services.
18 *
19 * This information can be used by your compiler passes instead of collecting
20 * it themselves which improves performance quite a lot.
21 *
22 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
23 */
24 class ServiceReferenceGraph
25 {
26 /**
27 * @var ServiceReferenceGraphNode[]
28 */
29 private $nodes = array();
30
31 /**
32 * Checks if the graph has a specific node.
33 *
34 * @param string $id Id to check
35 *
36 * @return bool
37 */
38 public function hasNode($id)
39 {
40 return isset($this->nodes[$id]);
41 }
42
43 /**
44 * Gets a node by identifier.
45 *
46 * @param string $id The id to retrieve
47 *
48 * @return ServiceReferenceGraphNode
49 *
50 * @throws InvalidArgumentException if no node matches the supplied identifier
51 */
52 public function getNode($id)
53 {
54 if (!isset($this->nodes[$id])) {
55 throw new InvalidArgumentException(sprintf('There is no node with id "%s".', $id));
56 }
57
58 return $this->nodes[$id];
59 }
60
61 /**
62 * Returns all nodes.
63 *
64 * @return ServiceReferenceGraphNode[]
65 */
66 public function getNodes()
67 {
68 return $this->nodes;
69 }
70
71 /**
72 * Clears all nodes.
73 */
74 public function clear()
75 {
76 $this->nodes = array();
77 }
78
79 /**
80 * Connects 2 nodes together in the Graph.
81 *
82 * @param string $sourceId
83 * @param string $sourceValue
84 * @param string $destId
85 * @param string $destValue
86 * @param string $reference
87 */
88 public function connect($sourceId, $sourceValue, $destId, $destValue = null, $reference = null)
89 {
90 $sourceNode = $this->createNode($sourceId, $sourceValue);
91 $destNode = $this->createNode($destId, $destValue);
92 $edge = new ServiceReferenceGraphEdge($sourceNode, $destNode, $reference);
93
94 $sourceNode->addOutEdge($edge);
95 $destNode->addInEdge($edge);
96 }
97
98 /**
99 * Creates a graph node.
100 *
101 * @param string $id
102 * @param string $value
103 *
104 * @return ServiceReferenceGraphNode
105 */
106 private function createNode($id, $value)
107 {
108 if (isset($this->nodes[$id]) && $this->nodes[$id]->getValue() === $value) {
109 return $this->nodes[$id];
110 }
111
112 return $this->nodes[$id] = new ServiceReferenceGraphNode($id, $value);
113 }
114 }