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