comparison vendor/symfony/dependency-injection/Dumper/GraphvizDumper.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 1fec387a4317
children af1871eacc83
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
10 */ 10 */
11 11
12 namespace Symfony\Component\DependencyInjection\Dumper; 12 namespace Symfony\Component\DependencyInjection\Dumper;
13 13
14 use Symfony\Component\DependencyInjection\Argument\ArgumentInterface; 14 use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
15 use Symfony\Component\DependencyInjection\ContainerBuilder;
15 use Symfony\Component\DependencyInjection\Definition; 16 use Symfony\Component\DependencyInjection\Definition;
16 use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException; 17 use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
18 use Symfony\Component\DependencyInjection\Parameter;
19 use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
17 use Symfony\Component\DependencyInjection\Reference; 20 use Symfony\Component\DependencyInjection\Reference;
18 use Symfony\Component\DependencyInjection\Parameter;
19 use Symfony\Component\DependencyInjection\ContainerBuilder;
20 use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
21 21
22 /** 22 /**
23 * GraphvizDumper dumps a service container as a graphviz file. 23 * GraphvizDumper dumps a service container as a graphviz file.
24 * 24 *
25 * You can convert the generated dot file with the dot utility (http://www.graphviz.org/): 25 * You can convert the generated dot file with the dot utility (http://www.graphviz.org/):
30 */ 30 */
31 class GraphvizDumper extends Dumper 31 class GraphvizDumper extends Dumper
32 { 32 {
33 private $nodes; 33 private $nodes;
34 private $edges; 34 private $edges;
35 private $options = array( 35 private $options = [
36 'graph' => array('ratio' => 'compress'), 36 'graph' => ['ratio' => 'compress'],
37 'node' => array('fontsize' => 11, 'fontname' => 'Arial', 'shape' => 'record'), 37 'node' => ['fontsize' => 11, 'fontname' => 'Arial', 'shape' => 'record'],
38 'edge' => array('fontsize' => 9, 'fontname' => 'Arial', 'color' => 'grey', 'arrowhead' => 'open', 'arrowsize' => 0.5), 38 'edge' => ['fontsize' => 9, 'fontname' => 'Arial', 'color' => 'grey', 'arrowhead' => 'open', 'arrowsize' => 0.5],
39 'node.instance' => array('fillcolor' => '#9999ff', 'style' => 'filled'), 39 'node.instance' => ['fillcolor' => '#9999ff', 'style' => 'filled'],
40 'node.definition' => array('fillcolor' => '#eeeeee'), 40 'node.definition' => ['fillcolor' => '#eeeeee'],
41 'node.missing' => array('fillcolor' => '#ff9999', 'style' => 'filled'), 41 'node.missing' => ['fillcolor' => '#ff9999', 'style' => 'filled'],
42 ); 42 ];
43 43
44 /** 44 /**
45 * Dumps the service container as a graphviz graph. 45 * Dumps the service container as a graphviz graph.
46 * 46 *
47 * Available options: 47 * Available options:
53 * * node.definition: The default options for services that are defined via service definition instances 53 * * node.definition: The default options for services that are defined via service definition instances
54 * * node.missing: The default options for missing services 54 * * node.missing: The default options for missing services
55 * 55 *
56 * @return string The dot representation of the service container 56 * @return string The dot representation of the service container
57 */ 57 */
58 public function dump(array $options = array()) 58 public function dump(array $options = [])
59 { 59 {
60 foreach (array('graph', 'node', 'edge', 'node.instance', 'node.definition', 'node.missing') as $key) { 60 foreach (['graph', 'node', 'edge', 'node.instance', 'node.definition', 'node.missing'] as $key) {
61 if (isset($options[$key])) { 61 if (isset($options[$key])) {
62 $this->options[$key] = array_merge($this->options[$key], $options[$key]); 62 $this->options[$key] = array_merge($this->options[$key], $options[$key]);
63 } 63 }
64 } 64 }
65 65
66 $this->nodes = $this->findNodes(); 66 $this->nodes = $this->findNodes();
67 67
68 $this->edges = array(); 68 $this->edges = [];
69 foreach ($this->container->getDefinitions() as $id => $definition) { 69 foreach ($this->container->getDefinitions() as $id => $definition) {
70 $this->edges[$id] = array_merge( 70 $this->edges[$id] = array_merge(
71 $this->findEdges($id, $definition->getArguments(), true, ''), 71 $this->findEdges($id, $definition->getArguments(), true, ''),
72 $this->findEdges($id, $definition->getProperties(), false, '') 72 $this->findEdges($id, $definition->getProperties(), false, '')
73 ); 73 );
127 * 127 *
128 * @return array An array of edges 128 * @return array An array of edges
129 */ 129 */
130 private function findEdges($id, array $arguments, $required, $name, $lazy = false) 130 private function findEdges($id, array $arguments, $required, $name, $lazy = false)
131 { 131 {
132 $edges = array(); 132 $edges = [];
133 foreach ($arguments as $argument) { 133 foreach ($arguments as $argument) {
134 if ($argument instanceof Parameter) { 134 if ($argument instanceof Parameter) {
135 $argument = $this->container->hasParameter($argument) ? $this->container->getParameter($argument) : null; 135 $argument = $this->container->hasParameter($argument) ? $this->container->getParameter($argument) : null;
136 } elseif (is_string($argument) && preg_match('/^%([^%]+)%$/', $argument, $match)) { 136 } elseif (\is_string($argument) && preg_match('/^%([^%]+)%$/', $argument, $match)) {
137 $argument = $this->container->hasParameter($match[1]) ? $this->container->getParameter($match[1]) : null; 137 $argument = $this->container->hasParameter($match[1]) ? $this->container->getParameter($match[1]) : null;
138 } 138 }
139 139
140 if ($argument instanceof Reference) { 140 if ($argument instanceof Reference) {
141 $lazyEdge = $lazy; 141 $lazyEdge = $lazy;
142 142
143 if (!$this->container->has((string) $argument)) { 143 if (!$this->container->has((string) $argument)) {
144 $this->nodes[(string) $argument] = array('name' => $name, 'required' => $required, 'class' => '', 'attributes' => $this->options['node.missing']); 144 $this->nodes[(string) $argument] = ['name' => $name, 'required' => $required, 'class' => '', 'attributes' => $this->options['node.missing']];
145 } elseif ('service_container' !== (string) $argument) { 145 } elseif ('service_container' !== (string) $argument) {
146 $lazyEdge = $lazy || $this->container->getDefinition((string) $argument)->isLazy(); 146 $lazyEdge = $lazy || $this->container->getDefinition((string) $argument)->isLazy();
147 } 147 }
148 148
149 $edges[] = array('name' => $name, 'required' => $required, 'to' => $argument, 'lazy' => $lazyEdge); 149 $edges[] = ['name' => $name, 'required' => $required, 'to' => $argument, 'lazy' => $lazyEdge];
150 } elseif ($argument instanceof ArgumentInterface) { 150 } elseif ($argument instanceof ArgumentInterface) {
151 $edges = array_merge($edges, $this->findEdges($id, $argument->getValues(), $required, $name, true)); 151 $edges = array_merge($edges, $this->findEdges($id, $argument->getValues(), $required, $name, true));
152 } elseif (is_array($argument)) { 152 } elseif ($argument instanceof Definition) {
153 $edges = array_merge($edges,
154 $this->findEdges($id, $argument->getArguments(), $required, ''),
155 $this->findEdges($id, $argument->getProperties(), false, '')
156 );
157 foreach ($argument->getMethodCalls() as $call) {
158 $edges = array_merge($edges, $this->findEdges($id, $call[1], false, $call[0].'()'));
159 }
160 } elseif (\is_array($argument)) {
153 $edges = array_merge($edges, $this->findEdges($id, $argument, $required, $name, $lazy)); 161 $edges = array_merge($edges, $this->findEdges($id, $argument, $required, $name, $lazy));
154 } 162 }
155 } 163 }
156 164
157 return $edges; 165 return $edges;
162 * 170 *
163 * @return array An array of all nodes 171 * @return array An array of all nodes
164 */ 172 */
165 private function findNodes() 173 private function findNodes()
166 { 174 {
167 $nodes = array(); 175 $nodes = [];
168 176
169 $container = $this->cloneContainer(); 177 $container = $this->cloneContainer();
170 178
171 foreach ($container->getDefinitions() as $id => $definition) { 179 foreach ($container->getDefinitions() as $id => $definition) {
172 $class = $definition->getClass(); 180 $class = $definition->getClass();
178 try { 186 try {
179 $class = $this->container->getParameterBag()->resolveValue($class); 187 $class = $this->container->getParameterBag()->resolveValue($class);
180 } catch (ParameterNotFoundException $e) { 188 } catch (ParameterNotFoundException $e) {
181 } 189 }
182 190
183 $nodes[$id] = array('class' => str_replace('\\', '\\\\', $class), 'attributes' => array_merge($this->options['node.definition'], array('style' => $definition->isShared() ? 'filled' : 'dotted'))); 191 $nodes[$id] = ['class' => str_replace('\\', '\\\\', $class), 'attributes' => array_merge($this->options['node.definition'], ['style' => $definition->isShared() ? 'filled' : 'dotted'])];
184 $container->setDefinition($id, new Definition('stdClass')); 192 $container->setDefinition($id, new Definition('stdClass'));
185 } 193 }
186 194
187 foreach ($container->getServiceIds() as $id) { 195 foreach ($container->getServiceIds() as $id) {
188 if (array_key_exists($id, $container->getAliases())) { 196 if (array_key_exists($id, $container->getAliases())) {
189 continue; 197 continue;
190 } 198 }
191 199
192 if (!$container->hasDefinition($id)) { 200 if (!$container->hasDefinition($id)) {
193 $nodes[$id] = array('class' => str_replace('\\', '\\\\', get_class($container->get($id))), 'attributes' => $this->options['node.instance']); 201 $nodes[$id] = ['class' => str_replace('\\', '\\\\', \get_class($container->get($id))), 'attributes' => $this->options['node.instance']];
194 } 202 }
195 } 203 }
196 204
197 return $nodes; 205 return $nodes;
198 } 206 }
243 * 251 *
244 * @return string A comma separated list of attributes 252 * @return string A comma separated list of attributes
245 */ 253 */
246 private function addAttributes(array $attributes) 254 private function addAttributes(array $attributes)
247 { 255 {
248 $code = array(); 256 $code = [];
249 foreach ($attributes as $k => $v) { 257 foreach ($attributes as $k => $v) {
250 $code[] = sprintf('%s="%s"', $k, $v); 258 $code[] = sprintf('%s="%s"', $k, $v);
251 } 259 }
252 260
253 return $code ? ', '.implode(', ', $code) : ''; 261 return $code ? ', '.implode(', ', $code) : '';
260 * 268 *
261 * @return string A space separated list of options 269 * @return string A space separated list of options
262 */ 270 */
263 private function addOptions(array $options) 271 private function addOptions(array $options)
264 { 272 {
265 $code = array(); 273 $code = [];
266 foreach ($options as $k => $v) { 274 foreach ($options as $k => $v) {
267 $code[] = sprintf('%s="%s"', $k, $v); 275 $code[] = sprintf('%s="%s"', $k, $v);
268 } 276 }
269 277
270 return implode(' ', $code); 278 return implode(' ', $code);
289 * 297 *
290 * @return array An array of aliases 298 * @return array An array of aliases
291 */ 299 */
292 private function getAliases($id) 300 private function getAliases($id)
293 { 301 {
294 $aliases = array(); 302 $aliases = [];
295 foreach ($this->container->getAliases() as $alias => $origin) { 303 foreach ($this->container->getAliases() as $alias => $origin) {
296 if ($id == $origin) { 304 if ($id == $origin) {
297 $aliases[] = $alias; 305 $aliases[] = $alias;
298 } 306 }
299 } 307 }