This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/DependencyInjection/Dumper/GraphvizDumper.php

274 lines
8.6 KiB
PHP
Raw Normal View History

2010-01-04 14:26:20 +00:00
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\DependencyInjection\Dumper;
2010-01-04 14:26:20 +00:00
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Parameter;
2011-01-17 22:28:59 +00:00
use Symfony\Component\DependencyInjection\ContainerInterface;
2010-01-04 14:26:20 +00:00
/**
* GraphvizDumper dumps a service container as a graphviz file.
*
* You can convert the generated dot file with the dot utility (http://www.graphviz.org/):
*
* dot -Tpng container.dot > foo.png
*
* @author Fabien Potencier <fabien@symfony.com>
2010-01-04 14:26:20 +00:00
*/
class GraphvizDumper extends Dumper
{
private $nodes;
private $edges;
private $options = array(
'graph' => array('ratio' => 'compress'),
'node' => array('fontsize' => 11, 'fontname' => 'Arial', 'shape' => 'record'),
'edge' => array('fontsize' => 9, 'fontname' => 'Arial', 'color' => 'grey', 'arrowhead' => 'open', 'arrowsize' => 0.5),
'node.instance' => array('fillcolor' => '#9999ff', 'style' => 'filled'),
'node.definition' => array('fillcolor' => '#eeeeee'),
'node.missing' => array('fillcolor' => '#ff9999', 'style' => 'filled'),
);
/**
* Dumps the service container as a graphviz graph.
*
* Available options:
*
* * graph: The default options for the whole graph
* * node: The default options for nodes
* * edge: The default options for edges
* * node.instance: The default options for services that are defined directly by object instances
* * node.definition: The default options for services that are defined via service definition instances
* * node.missing: The default options for missing services
*
* @param array $options An array of options
*
* @return string The dot representation of the service container
*/
public function dump(array $options = array())
2010-01-04 14:26:20 +00:00
{
foreach (array('graph', 'node', 'edge', 'node.instance', 'node.definition', 'node.missing') as $key) {
if (isset($options[$key])) {
$this->options[$key] = array_merge($this->options[$key], $options[$key]);
}
}
2010-01-04 14:26:20 +00:00
$this->nodes = $this->findNodes();
2010-01-04 14:26:20 +00:00
$this->edges = array();
foreach ($this->container->getDefinitions() as $id => $definition) {
2011-03-10 14:31:00 +00:00
$this->edges[$id] = array_merge(
$this->findEdges($id, $definition->getArguments(), true, ''),
$this->findEdges($id, $definition->getProperties(), false, '')
);
foreach ($definition->getMethodCalls() as $call) {
$this->edges[$id] = array_merge(
$this->edges[$id],
$this->findEdges($id, $call[1], false, $call[0].'()')
);
}
}
2010-01-04 14:26:20 +00:00
return $this->startDot().$this->addNodes().$this->addEdges().$this->endDot();
}
2010-01-04 14:26:20 +00:00
2011-02-13 18:06:41 +00:00
/**
* Returns all nodes.
*
* @return string A string representation of all nodes
*/
private function addNodes()
2010-01-04 14:26:20 +00:00
{
$code = '';
foreach ($this->nodes as $id => $node) {
$aliases = $this->getAliases($id);
2010-01-04 14:26:20 +00:00
$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']));
}
2010-01-04 14:26:20 +00:00
return $code;
}
2010-01-04 14:26:20 +00:00
2011-02-13 18:06:41 +00:00
/**
* Returns all edges.
*
* @return string A string representation of all edges
*/
private function addEdges()
2010-01-04 14:26:20 +00:00
{
$code = '';
foreach ($this->edges as $id => $edges) {
foreach ($edges as $edge) {
$code .= sprintf(" node_%s -> node_%s [label=\"%s\" style=\"%s\"];\n", $this->dotize($id), $this->dotize($edge['to']), $edge['name'], $edge['required'] ? 'filled' : 'dashed');
}
}
2010-01-04 14:26:20 +00:00
return $code;
}
2010-01-04 14:26:20 +00:00
2011-02-13 18:06:41 +00:00
/**
* Finds all edges belonging to a specific service id.
*
* @param string $id The service id used to find edges
* @param array $arguments An array of arguments
2011-04-27 06:25:26 +01:00
* @param Boolean $required
2011-03-10 14:31:00 +00:00
* @param string $name
2011-12-13 07:50:54 +00:00
*
2011-02-13 18:06:41 +00:00
* @return array An array of edges
*/
private function findEdges($id, $arguments, $required, $name)
2010-01-04 14:26:20 +00:00
{
$edges = array();
foreach ($arguments as $argument) {
if (is_object($argument) && $argument instanceof Parameter) {
2010-09-03 15:18:04 +01:00
$argument = $this->container->hasParameter($argument) ? $this->container->getParameter($argument) : null;
} elseif (is_string($argument) && preg_match('/^%([^%]+)%$/', $argument, $match)) {
2010-09-03 15:18:04 +01:00
$argument = $this->container->hasParameter($match[1]) ? $this->container->getParameter($match[1]) : null;
}
if ($argument instanceof Reference) {
2010-06-27 17:28:29 +01:00
if (!$this->container->has((string) $argument)) {
$this->nodes[(string) $argument] = array('name' => $name, 'required' => $required, 'class' => '', 'attributes' => $this->options['node.missing']);
}
$edges[] = array('name' => $name, 'required' => $required, 'to' => $argument);
} elseif (is_array($argument)) {
$edges = array_merge($edges, $this->findEdges($id, $argument, $required, $name));
}
2010-01-04 14:26:20 +00:00
}
return $edges;
2010-01-04 14:26:20 +00:00
}
2011-02-13 18:06:41 +00:00
/**
* Finds all nodes.
*
* @return array An array of all nodes
*/
private function findNodes()
{
$nodes = array();
2010-01-04 14:26:20 +00:00
$container = clone $this->container;
2010-01-04 14:26:20 +00:00
foreach ($container->getDefinitions() as $id => $definition) {
2011-01-17 22:28:59 +00:00
$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')));
2010-01-04 14:26:20 +00:00
$container->setDefinition($id, new Definition('stdClass'));
}
2010-01-04 14:26:20 +00:00
foreach ($container->getServiceIds() as $id) {
2010-06-27 17:28:29 +01:00
$service = $container->get($id);
if (in_array($id, array_keys($container->getAliases()))) {
continue;
}
if (!$container->hasDefinition($id)) {
$nodes[$id] = array('class' => str_replace('\\', '\\\\', get_class($service)), 'attributes' => $this->options['node.instance']);
}
}
return $nodes;
2010-01-04 14:26:20 +00:00
}
2011-02-13 18:06:41 +00:00
/**
* Returns the start dot.
*
* @return string The string representation of a start dot
*/
private function startDot()
{
return sprintf("digraph sc {\n %s\n node [%s];\n edge [%s];\n\n",
$this->addOptions($this->options['graph']),
$this->addOptions($this->options['node']),
$this->addOptions($this->options['edge'])
);
2010-01-04 14:26:20 +00:00
}
2011-02-13 18:06:41 +00:00
/**
* Returns the end dot.
*
2011-06-04 16:30:56 +01:00
* @return string
2011-02-13 18:06:41 +00:00
*/
private function endDot()
2010-01-04 14:26:20 +00:00
{
return "}\n";
2010-01-04 14:26:20 +00:00
}
2011-02-13 18:06:41 +00:00
/**
* Adds attributes
*
* @param array $attributes An array of attributes
2011-12-13 07:50:54 +00:00
*
2011-02-13 18:06:41 +00:00
* @return string A comma separated list of attributes
*/
private function addAttributes($attributes)
2010-01-04 14:26:20 +00:00
{
$code = array();
foreach ($attributes as $k => $v) {
$code[] = sprintf('%s="%s"', $k, $v);
}
return $code ? ', '.implode(', ', $code) : '';
2010-01-04 14:26:20 +00:00
}
2011-02-13 18:06:41 +00:00
/**
* Adds options
*
* @param array $options An array of options
2011-02-26 17:43:28 +00:00
*
* @return string A space separated list of options
2011-02-13 18:06:41 +00:00
*/
private function addOptions($options)
{
$code = array();
foreach ($options as $k => $v) {
$code[] = sprintf('%s="%s"', $k, $v);
}
2010-01-04 14:26:20 +00:00
return implode(' ', $code);
}
2010-01-04 14:26:20 +00:00
2011-02-13 18:06:41 +00:00
/**
* Dotizes an identifier.
*
* @param string $id The identifier to dotize
2011-12-13 07:50:54 +00:00
*
2011-02-13 18:06:41 +00:00
* @return string A dotized string
*/
private function dotize($id)
2010-01-04 14:26:20 +00:00
{
return strtolower(preg_replace('/[^\w]/i', '_', $id));
2010-01-04 14:26:20 +00:00
}
2011-02-13 18:06:41 +00:00
/**
* Compiles an array of aliases for a specified service id.
*
* @param string $id A service id
2011-12-13 07:50:54 +00:00
*
2011-02-13 18:06:41 +00:00
* @return array An array of aliases
*/
private function getAliases($id)
{
$aliases = array();
foreach ($this->container->getAliases() as $alias => $origin) {
if ($id == $origin) {
$aliases[] = $alias;
}
}
return $aliases;
}
2010-01-04 14:26:20 +00:00
}