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\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;
22 * GraphvizDumper dumps a service container as a graphviz file.
24 * You can convert the generated dot file with the dot utility (http://www.graphviz.org/):
26 * dot -Tpng container.dot > foo.png
28 * @author Fabien Potencier <fabien@symfony.com>
30 class GraphvizDumper extends Dumper
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'),
44 * Dumps the service container as a graphviz graph.
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
55 * @param array $options An array of options
57 * @return string The dot representation of the service container
59 public function dump(array $options = array())
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]);
67 $this->nodes = $this->findNodes();
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, '')
76 foreach ($definition->getMethodCalls() as $call) {
77 $this->edges[$id] = array_merge(
79 $this->findEdges($id, $call[1], false, $call[0].'()')
84 return $this->container->resolveEnvPlaceholders($this->startDot().$this->addNodes().$this->addEdges().$this->endDot(), '__ENV_%s__');
90 * @return string A string representation of all nodes
92 private function addNodes()
95 foreach ($this->nodes as $id => $node) {
96 $aliases = $this->getAliases($id);
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']));
107 * @return string A string representation of all edges
109 private function addEdges()
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');
122 * Finds all edges belonging to a specific service id.
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
129 * @return array An array of edges
131 private function findEdges($id, array $arguments, $required, $name)
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;
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']);
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));
158 * @return array An array of all nodes
160 private function findNodes()
164 $container = $this->cloneContainer();
166 foreach ($container->getDefinitions() as $id => $definition) {
167 $class = $definition->getClass();
169 if ('\\' === substr($class, 0, 1)) {
170 $class = substr($class, 1);
174 $class = $this->container->getParameterBag()->resolveValue($class);
175 } catch (ParameterNotFoundException $e) {
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'));
182 foreach ($container->getServiceIds() as $id) {
183 if (array_key_exists($id, $container->getAliases())) {
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']);
196 private function cloneContainer()
198 $parameterBag = new ParameterBag($this->container->getParameterBag()->all());
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);
212 * Returns the start dot.
214 * @return string The string representation of a start dot
216 private function startDot()
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'])
226 * Returns the end dot.
230 private function endDot()
238 * @param array $attributes An array of attributes
240 * @return string A comma separated list of attributes
242 private function addAttributes(array $attributes)
245 foreach ($attributes as $k => $v) {
246 $code[] = sprintf('%s="%s"', $k, $v);
249 return $code ? ', '.implode(', ', $code) : '';
255 * @param array $options An array of options
257 * @return string A space separated list of options
259 private function addOptions(array $options)
262 foreach ($options as $k => $v) {
263 $code[] = sprintf('%s="%s"', $k, $v);
266 return implode(' ', $code);
270 * Dotizes an identifier.
272 * @param string $id The identifier to dotize
274 * @return string A dotized string
276 private function dotize($id)
278 return strtolower(preg_replace('/\W/i', '_', $id));
282 * Compiles an array of aliases for a specified service id.
284 * @param string $id A service id
286 * @return array An array of aliases
288 private function getAliases($id)
291 foreach ($this->container->getAliases() as $alias => $origin) {
292 if ($id == $origin) {