4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Dumper;
14 use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
15 use Symfony\Component\DependencyInjection\ContainerBuilder;
16 use Symfony\Component\DependencyInjection\Definition;
17 use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
18 use Symfony\Component\DependencyInjection\Parameter;
19 use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
20 use Symfony\Component\DependencyInjection\Reference;
23 * GraphvizDumper dumps a service container as a graphviz file.
25 * You can convert the generated dot file with the dot utility (http://www.graphviz.org/):
27 * dot -Tpng container.dot > foo.png
29 * @author Fabien Potencier <fabien@symfony.com>
31 class GraphvizDumper extends Dumper
35 private $options = array(
36 'graph' => array('ratio' => 'compress'),
37 'node' => array('fontsize' => 11, 'fontname' => 'Arial', 'shape' => 'record'),
38 'edge' => array('fontsize' => 9, 'fontname' => 'Arial', 'color' => 'grey', 'arrowhead' => 'open', 'arrowsize' => 0.5),
39 'node.instance' => array('fillcolor' => '#9999ff', 'style' => 'filled'),
40 'node.definition' => array('fillcolor' => '#eeeeee'),
41 'node.missing' => array('fillcolor' => '#ff9999', 'style' => 'filled'),
45 * Dumps the service container as a graphviz graph.
49 * * graph: The default options for the whole graph
50 * * node: The default options for nodes
51 * * edge: The default options for edges
52 * * node.instance: The default options for services that are defined directly by object 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
56 * @return string The dot representation of the service container
58 public function dump(array $options = array())
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]);
66 $this->nodes = $this->findNodes();
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, '')
75 foreach ($definition->getMethodCalls() as $call) {
76 $this->edges[$id] = array_merge(
78 $this->findEdges($id, $call[1], false, $call[0].'()')
83 return $this->container->resolveEnvPlaceholders($this->startDot().$this->addNodes().$this->addEdges().$this->endDot(), '__ENV_%s__');
89 * @return string A string representation of all nodes
91 private function addNodes()
94 foreach ($this->nodes as $id => $node) {
95 $aliases = $this->getAliases($id);
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']));
106 * @return string A string representation of all edges
108 private function addEdges()
111 foreach ($this->edges as $id => $edges) {
112 foreach ($edges as $edge) {
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"' : '');
121 * Finds all edges belonging to a specific service id.
123 * @param string $id The service id used to find edges
124 * @param array $arguments An array of arguments
125 * @param bool $required
126 * @param string $name
128 * @return array An array of edges
130 private function findEdges($id, array $arguments, $required, $name, $lazy = false)
133 foreach ($arguments as $argument) {
134 if ($argument instanceof Parameter) {
135 $argument = $this->container->hasParameter($argument) ? $this->container->getParameter($argument) : null;
136 } elseif (\is_string($argument) && preg_match('/^%([^%]+)%$/', $argument, $match)) {
137 $argument = $this->container->hasParameter($match[1]) ? $this->container->getParameter($match[1]) : null;
140 if ($argument instanceof Reference) {
143 if (!$this->container->has((string) $argument)) {
144 $this->nodes[(string) $argument] = array('name' => $name, 'required' => $required, 'class' => '', 'attributes' => $this->options['node.missing']);
145 } elseif ('service_container' !== (string) $argument) {
146 $lazyEdge = $lazy || $this->container->getDefinition((string) $argument)->isLazy();
149 $edges[] = array('name' => $name, 'required' => $required, 'to' => $argument, 'lazy' => $lazyEdge);
150 } elseif ($argument instanceof ArgumentInterface) {
151 $edges = array_merge($edges, $this->findEdges($id, $argument->getValues(), $required, $name, true));
152 } elseif (\is_array($argument)) {
153 $edges = array_merge($edges, $this->findEdges($id, $argument, $required, $name, $lazy));
163 * @return array An array of all nodes
165 private function findNodes()
169 $container = $this->cloneContainer();
171 foreach ($container->getDefinitions() as $id => $definition) {
172 $class = $definition->getClass();
174 if ('\\' === substr($class, 0, 1)) {
175 $class = substr($class, 1);
179 $class = $this->container->getParameterBag()->resolveValue($class);
180 } catch (ParameterNotFoundException $e) {
183 $nodes[$id] = array('class' => str_replace('\\', '\\\\', $class), 'attributes' => array_merge($this->options['node.definition'], array('style' => $definition->isShared() ? 'filled' : 'dotted')));
184 $container->setDefinition($id, new Definition('stdClass'));
187 foreach ($container->getServiceIds() as $id) {
188 if (array_key_exists($id, $container->getAliases())) {
192 if (!$container->hasDefinition($id)) {
193 $nodes[$id] = array('class' => str_replace('\\', '\\\\', \get_class($container->get($id))), 'attributes' => $this->options['node.instance']);
200 private function cloneContainer()
202 $parameterBag = new ParameterBag($this->container->getParameterBag()->all());
204 $container = new ContainerBuilder($parameterBag);
205 $container->setDefinitions($this->container->getDefinitions());
206 $container->setAliases($this->container->getAliases());
207 $container->setResources($this->container->getResources());
208 foreach ($this->container->getExtensions() as $extension) {
209 $container->registerExtension($extension);
216 * Returns the start dot.
218 * @return string The string representation of a start dot
220 private function startDot()
222 return sprintf("digraph sc {\n %s\n node [%s];\n edge [%s];\n\n",
223 $this->addOptions($this->options['graph']),
224 $this->addOptions($this->options['node']),
225 $this->addOptions($this->options['edge'])
230 * Returns the end dot.
234 private function endDot()
242 * @param array $attributes An array of attributes
244 * @return string A comma separated list of attributes
246 private function addAttributes(array $attributes)
249 foreach ($attributes as $k => $v) {
250 $code[] = sprintf('%s="%s"', $k, $v);
253 return $code ? ', '.implode(', ', $code) : '';
259 * @param array $options An array of options
261 * @return string A space separated list of options
263 private function addOptions(array $options)
266 foreach ($options as $k => $v) {
267 $code[] = sprintf('%s="%s"', $k, $v);
270 return implode(' ', $code);
274 * Dotizes an identifier.
276 * @param string $id The identifier to dotize
278 * @return string A dotized string
280 private function dotize($id)
282 return strtolower(preg_replace('/\W/i', '_', $id));
286 * Compiles an array of aliases for a specified service id.
288 * @param string $id A service id
290 * @return array An array of aliases
292 private function getAliases($id)
295 foreach ($this->container->getAliases() as $alias => $origin) {
296 if ($id == $origin) {