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