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