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\Reference
;
16 use Symfony\Component\DependencyInjection\Parameter
;
17 use Symfony\Component\DependencyInjection\ContainerInterface
;
18 use Symfony\Component\DependencyInjection\ContainerBuilder
;
19 use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag
;
20 use Symfony\Component\DependencyInjection\Scope
;
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 * @param array $options An array of options
58 * @return string The dot representation of the service container
60 public function dump(array $options = array())
62 foreach (array('graph', 'node', 'edge', 'node.instance', 'node.definition', 'node.missing') as $key) {
63 if (isset($options[$key])) {
64 $this->options
[$key] = array_merge($this->options
[$key], $options[$key]);
68 $this->nodes
= $this->findNodes();
70 $this->edges
= array();
71 foreach ($this->container
->getDefinitions() as $id => $definition) {
72 $this->edges
[$id] = array_merge(
73 $this->findEdges($id, $definition->getArguments(), true, ''),
74 $this->findEdges($id, $definition->getProperties(), false, '')
77 foreach ($definition->getMethodCalls() as $call) {
78 $this->edges
[$id] = array_merge(
80 $this->findEdges($id, $call[1], false, $call[0].'()')
85 return $this->startDot().$this->addNodes().$this->addEdges().$this->endDot();
91 * @return string A string representation of all nodes
93 private function addNodes()
96 foreach ($this->nodes
as $id => $node) {
97 $aliases = $this->getAliases($id);
99 $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']));
108 * @return string A string representation of all edges
110 private function addEdges()
113 foreach ($this->edges
as $id => $edges) {
114 foreach ($edges as $edge) {
115 $code .= sprintf(" node_%s -> node_%s [label=\"%s\" style=\"%s\"];\n", $this->dotize($id), $this->dotize($edge['to']), $edge['name'], $edge['required'] ?
'filled' : 'dashed');
123 * Finds all edges belonging to a specific service id.
125 * @param string $id The service id used to find edges
126 * @param array $arguments An array of arguments
127 * @param bool $required
128 * @param string $name
130 * @return array An array of edges
132 private function findEdges($id, $arguments, $required, $name)
135 foreach ($arguments as $argument) {
136 if ($argument instanceof Parameter
) {
137 $argument = $this->container
->hasParameter($argument) ?
$this->container
->getParameter($argument) : null;
138 } elseif (is_string($argument) && preg_match('/^%([^%]+)%$/', $argument, $match)) {
139 $argument = $this->container
->hasParameter($match[1]) ?
$this->container
->getParameter($match[1]) : null;
142 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']);
147 $edges[] = array('name' => $name, 'required' => $required, 'to' => $argument);
148 } elseif (is_array($argument)) {
149 $edges = array_merge($edges, $this->findEdges($id, $argument, $required, $name));
159 * @return array An array of all nodes
161 private function findNodes()
165 $container = $this->cloneContainer();
167 foreach ($container->getDefinitions() as $id => $definition) {
168 $class = $definition->getClass();
170 if ('\\' === substr($class, 0, 1)) {
171 $class = substr($class, 1);
174 $nodes[$id] = array('class' => str_replace('\\', '\\\\', $this->container
->getParameterBag()->resolveValue($class)), 'attributes' => array_merge($this->options
['node.definition'], array('style' => ContainerInterface
::SCOPE_PROTOTYPE
!== $definition->getScope() ?
'filled' : 'dotted')));
176 $container->setDefinition($id, new Definition('stdClass'));
179 foreach ($container->getServiceIds() as $id) {
180 $service = $container->get($id);
182 if (array_key_exists($id, $container->getAliases())) {
186 if (!$container->hasDefinition($id)) {
187 $class = ('service_container' === $id) ?
get_class($this->container
) : get_class($service);
188 $nodes[$id] = array('class' => str_replace('\\', '\\\\', $class), 'attributes' => $this->options
['node.instance']);
195 private function cloneContainer()
197 $parameterBag = new ParameterBag($this->container
->getParameterBag()->all());
199 $container = new ContainerBuilder($parameterBag);
200 $container->setDefinitions($this->container
->getDefinitions());
201 $container->setAliases($this->container
->getAliases());
202 $container->setResources($this->container
->getResources());
203 foreach ($this->container
->getScopes() as $scope => $parentScope) {
204 $container->addScope(new Scope($scope, $parentScope));
206 foreach ($this->container
->getExtensions() as $extension) {
207 $container->registerExtension($extension);
214 * Returns the start dot.
216 * @return string The string representation of a start dot
218 private function startDot()
220 return sprintf("digraph sc {\n %s\n node [%s];\n edge [%s];\n\n",
221 $this->addOptions($this->options
['graph']),
222 $this->addOptions($this->options
['node']),
223 $this->addOptions($this->options
['edge'])
228 * Returns the end dot.
232 private function endDot()
240 * @param array $attributes An array of attributes
242 * @return string A comma separated list of attributes
244 private function addAttributes($attributes)
247 foreach ($attributes as $k => $v) {
248 $code[] = sprintf('%s="%s"', $k, $v);
251 return $code ?
', '.implode(', ', $code) : '';
257 * @param array $options An array of options
259 * @return string A space separated list of options
261 private function addOptions($options)
264 foreach ($options as $k => $v) {
265 $code[] = sprintf('%s="%s"', $k, $v);
268 return implode(' ', $code);
272 * Dotizes an identifier.
274 * @param string $id The identifier to dotize
276 * @return string A dotized string
278 private function dotize($id)
280 return strtolower(preg_replace('/\W/i', '_', $id));
284 * Compiles an array of aliases for a specified service id.
286 * @param string $id A service id
288 * @return array An array of aliases
290 private function getAliases($id)
293 foreach ($this->container
->getAliases() as $alias => $origin) {
294 if ($id == $origin) {