Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / symfony / dependency-injection / Compiler / ServiceReferenceGraph.php
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  * @final since version 3.4
25  */
26 class ServiceReferenceGraph
27 {
28     /**
29      * @var ServiceReferenceGraphNode[]
30      */
31     private $nodes = array();
32
33     /**
34      * Checks if the graph has a specific node.
35      *
36      * @param string $id Id to check
37      *
38      * @return bool
39      */
40     public function hasNode($id)
41     {
42         return isset($this->nodes[$id]);
43     }
44
45     /**
46      * Gets a node by identifier.
47      *
48      * @param string $id The id to retrieve
49      *
50      * @return ServiceReferenceGraphNode
51      *
52      * @throws InvalidArgumentException if no node matches the supplied identifier
53      */
54     public function getNode($id)
55     {
56         if (!isset($this->nodes[$id])) {
57             throw new InvalidArgumentException(sprintf('There is no node with id "%s".', $id));
58         }
59
60         return $this->nodes[$id];
61     }
62
63     /**
64      * Returns all nodes.
65      *
66      * @return ServiceReferenceGraphNode[]
67      */
68     public function getNodes()
69     {
70         return $this->nodes;
71     }
72
73     /**
74      * Clears all nodes.
75      */
76     public function clear()
77     {
78         foreach ($this->nodes as $node) {
79             $node->clear();
80         }
81         $this->nodes = array();
82     }
83
84     /**
85      * Connects 2 nodes together in the Graph.
86      *
87      * @param string $sourceId
88      * @param mixed  $sourceValue
89      * @param string $destId
90      * @param mixed  $destValue
91      * @param string $reference
92      * @param bool   $lazy
93      * @param bool   $weak
94      */
95     public function connect($sourceId, $sourceValue, $destId, $destValue = null, $reference = null/*, bool $lazy = false, bool $weak = false*/)
96     {
97         $lazy = func_num_args() >= 6 ? func_get_arg(5) : false;
98         $weak = func_num_args() >= 7 ? func_get_arg(6) : false;
99
100         if (null === $sourceId || null === $destId) {
101             return;
102         }
103
104         $sourceNode = $this->createNode($sourceId, $sourceValue);
105         $destNode = $this->createNode($destId, $destValue);
106         $edge = new ServiceReferenceGraphEdge($sourceNode, $destNode, $reference, $lazy, $weak);
107
108         $sourceNode->addOutEdge($edge);
109         $destNode->addInEdge($edge);
110     }
111
112     /**
113      * Creates a graph node.
114      *
115      * @param string $id
116      * @param mixed  $value
117      *
118      * @return ServiceReferenceGraphNode
119      */
120     private function createNode($id, $value)
121     {
122         if (isset($this->nodes[$id]) && $this->nodes[$id]->getValue() === $value) {
123             return $this->nodes[$id];
124         }
125
126         return $this->nodes[$id] = new ServiceReferenceGraphNode($id, $value);
127     }
128 }