Security update for Core, with self-updated composer
[yaffs-website] / vendor / symfony / dependency-injection / Dumper / GraphvizDumper.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\Dumper;
13
14 use Symfony\Component\DependencyInjection\Definition;
15 use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
16 use Symfony\Component\DependencyInjection\Reference;
17 use Symfony\Component\DependencyInjection\Parameter;
18 use Symfony\Component\DependencyInjection\ContainerBuilder;
19 use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
20
21 /**
22  * GraphvizDumper dumps a service container as a graphviz file.
23  *
24  * You can convert the generated dot file with the dot utility (http://www.graphviz.org/):
25  *
26  *   dot -Tpng container.dot > foo.png
27  *
28  * @author Fabien Potencier <fabien@symfony.com>
29  */
30 class GraphvizDumper extends Dumper
31 {
32     private $nodes;
33     private $edges;
34     private $options = array(
35             'graph' => array('ratio' => 'compress'),
36             'node' => array('fontsize' => 11, 'fontname' => 'Arial', 'shape' => 'record'),
37             'edge' => array('fontsize' => 9, 'fontname' => 'Arial', 'color' => 'grey', 'arrowhead' => 'open', 'arrowsize' => 0.5),
38             'node.instance' => array('fillcolor' => '#9999ff', 'style' => 'filled'),
39             'node.definition' => array('fillcolor' => '#eeeeee'),
40             'node.missing' => array('fillcolor' => '#ff9999', 'style' => 'filled'),
41         );
42
43     /**
44      * Dumps the service container as a graphviz graph.
45      *
46      * Available options:
47      *
48      *  * graph: The default options for the whole graph
49      *  * node: The default options for nodes
50      *  * edge: The default options for edges
51      *  * node.instance: The default options for services that are defined directly by object instances
52      *  * node.definition: The default options for services that are defined via service definition instances
53      *  * node.missing: The default options for missing services
54      *
55      * @param array $options An array of options
56      *
57      * @return string The dot representation of the service container
58      */
59     public function dump(array $options = array())
60     {
61         foreach (array('graph', 'node', 'edge', 'node.instance', 'node.definition', 'node.missing') as $key) {
62             if (isset($options[$key])) {
63                 $this->options[$key] = array_merge($this->options[$key], $options[$key]);
64             }
65         }
66
67         $this->nodes = $this->findNodes();
68
69         $this->edges = array();
70         foreach ($this->container->getDefinitions() as $id => $definition) {
71             $this->edges[$id] = array_merge(
72                 $this->findEdges($id, $definition->getArguments(), true, ''),
73                 $this->findEdges($id, $definition->getProperties(), false, '')
74             );
75
76             foreach ($definition->getMethodCalls() as $call) {
77                 $this->edges[$id] = array_merge(
78                     $this->edges[$id],
79                     $this->findEdges($id, $call[1], false, $call[0].'()')
80                 );
81             }
82         }
83
84         return $this->container->resolveEnvPlaceholders($this->startDot().$this->addNodes().$this->addEdges().$this->endDot(), '__ENV_%s__');
85     }
86
87     /**
88      * Returns all nodes.
89      *
90      * @return string A string representation of all nodes
91      */
92     private function addNodes()
93     {
94         $code = '';
95         foreach ($this->nodes as $id => $node) {
96             $aliases = $this->getAliases($id);
97
98             $code .= sprintf("  node_%s [label=\"%s\\n%s\\n\", shape=%s%s];\n", $this->dotize($id), $id.($aliases ? ' ('.implode(', ', $aliases).')' : ''), $node['class'], $this->options['node']['shape'], $this->addAttributes($node['attributes']));
99         }
100
101         return $code;
102     }
103
104     /**
105      * Returns all edges.
106      *
107      * @return string A string representation of all edges
108      */
109     private function addEdges()
110     {
111         $code = '';
112         foreach ($this->edges as $id => $edges) {
113             foreach ($edges as $edge) {
114                 $code .= sprintf("  node_%s -> node_%s [label=\"%s\" style=\"%s\"];\n", $this->dotize($id), $this->dotize($edge['to']), $edge['name'], $edge['required'] ? 'filled' : 'dashed');
115             }
116         }
117
118         return $code;
119     }
120
121     /**
122      * Finds all edges belonging to a specific service id.
123      *
124      * @param string $id        The service id used to find edges
125      * @param array  $arguments An array of arguments
126      * @param bool   $required
127      * @param string $name
128      *
129      * @return array An array of edges
130      */
131     private function findEdges($id, array $arguments, $required, $name)
132     {
133         $edges = array();
134         foreach ($arguments as $argument) {
135             if ($argument instanceof Parameter) {
136                 $argument = $this->container->hasParameter($argument) ? $this->container->getParameter($argument) : null;
137             } elseif (is_string($argument) && preg_match('/^%([^%]+)%$/', $argument, $match)) {
138                 $argument = $this->container->hasParameter($match[1]) ? $this->container->getParameter($match[1]) : null;
139             }
140
141             if ($argument instanceof Reference) {
142                 if (!$this->container->has((string) $argument)) {
143                     $this->nodes[(string) $argument] = array('name' => $name, 'required' => $required, 'class' => '', 'attributes' => $this->options['node.missing']);
144                 }
145
146                 $edges[] = array('name' => $name, 'required' => $required, 'to' => $argument);
147             } elseif (is_array($argument)) {
148                 $edges = array_merge($edges, $this->findEdges($id, $argument, $required, $name));
149             }
150         }
151
152         return $edges;
153     }
154
155     /**
156      * Finds all nodes.
157      *
158      * @return array An array of all nodes
159      */
160     private function findNodes()
161     {
162         $nodes = array();
163
164         $container = $this->cloneContainer();
165
166         foreach ($container->getDefinitions() as $id => $definition) {
167             $class = $definition->getClass();
168
169             if ('\\' === substr($class, 0, 1)) {
170                 $class = substr($class, 1);
171             }
172
173             try {
174                 $class = $this->container->getParameterBag()->resolveValue($class);
175             } catch (ParameterNotFoundException $e) {
176             }
177
178             $nodes[$id] = array('class' => str_replace('\\', '\\\\', $class), 'attributes' => array_merge($this->options['node.definition'], array('style' => $definition->isShared() ? 'filled' : 'dotted')));
179             $container->setDefinition($id, new Definition('stdClass'));
180         }
181
182         foreach ($container->getServiceIds() as $id) {
183             if (array_key_exists($id, $container->getAliases())) {
184                 continue;
185             }
186
187             if (!$container->hasDefinition($id)) {
188                 $class = get_class('service_container' === $id ? $this->container : $container->get($id));
189                 $nodes[$id] = array('class' => str_replace('\\', '\\\\', $class), 'attributes' => $this->options['node.instance']);
190             }
191         }
192
193         return $nodes;
194     }
195
196     private function cloneContainer()
197     {
198         $parameterBag = new ParameterBag($this->container->getParameterBag()->all());
199
200         $container = new ContainerBuilder($parameterBag);
201         $container->setDefinitions($this->container->getDefinitions());
202         $container->setAliases($this->container->getAliases());
203         $container->setResources($this->container->getResources());
204         foreach ($this->container->getExtensions() as $extension) {
205             $container->registerExtension($extension);
206         }
207
208         return $container;
209     }
210
211     /**
212      * Returns the start dot.
213      *
214      * @return string The string representation of a start dot
215      */
216     private function startDot()
217     {
218         return sprintf("digraph sc {\n  %s\n  node [%s];\n  edge [%s];\n\n",
219             $this->addOptions($this->options['graph']),
220             $this->addOptions($this->options['node']),
221             $this->addOptions($this->options['edge'])
222         );
223     }
224
225     /**
226      * Returns the end dot.
227      *
228      * @return string
229      */
230     private function endDot()
231     {
232         return "}\n";
233     }
234
235     /**
236      * Adds attributes.
237      *
238      * @param array $attributes An array of attributes
239      *
240      * @return string A comma separated list of attributes
241      */
242     private function addAttributes(array $attributes)
243     {
244         $code = array();
245         foreach ($attributes as $k => $v) {
246             $code[] = sprintf('%s="%s"', $k, $v);
247         }
248
249         return $code ? ', '.implode(', ', $code) : '';
250     }
251
252     /**
253      * Adds options.
254      *
255      * @param array $options An array of options
256      *
257      * @return string A space separated list of options
258      */
259     private function addOptions(array $options)
260     {
261         $code = array();
262         foreach ($options as $k => $v) {
263             $code[] = sprintf('%s="%s"', $k, $v);
264         }
265
266         return implode(' ', $code);
267     }
268
269     /**
270      * Dotizes an identifier.
271      *
272      * @param string $id The identifier to dotize
273      *
274      * @return string A dotized string
275      */
276     private function dotize($id)
277     {
278         return strtolower(preg_replace('/\W/i', '_', $id));
279     }
280
281     /**
282      * Compiles an array of aliases for a specified service id.
283      *
284      * @param string $id A service id
285      *
286      * @return array An array of aliases
287      */
288     private function getAliases($id)
289     {
290         $aliases = array();
291         foreach ($this->container->getAliases() as $alias => $origin) {
292             if ($id == $origin) {
293                 $aliases[] = $alias;
294             }
295         }
296
297         return $aliases;
298     }
299 }