Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\DependencyInjection\Dumper; Chris@0: Chris@0: use Symfony\Component\DependencyInjection\Definition; Chris@0: use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException; Chris@0: use Symfony\Component\DependencyInjection\Reference; Chris@0: use Symfony\Component\DependencyInjection\Parameter; Chris@0: use Symfony\Component\DependencyInjection\ContainerBuilder; Chris@0: use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; Chris@0: Chris@0: /** Chris@0: * GraphvizDumper dumps a service container as a graphviz file. Chris@0: * Chris@0: * You can convert the generated dot file with the dot utility (http://www.graphviz.org/): Chris@0: * Chris@0: * dot -Tpng container.dot > foo.png Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: class GraphvizDumper extends Dumper Chris@0: { Chris@0: private $nodes; Chris@0: private $edges; Chris@0: private $options = array( Chris@0: 'graph' => array('ratio' => 'compress'), Chris@0: 'node' => array('fontsize' => 11, 'fontname' => 'Arial', 'shape' => 'record'), Chris@0: 'edge' => array('fontsize' => 9, 'fontname' => 'Arial', 'color' => 'grey', 'arrowhead' => 'open', 'arrowsize' => 0.5), Chris@0: 'node.instance' => array('fillcolor' => '#9999ff', 'style' => 'filled'), Chris@0: 'node.definition' => array('fillcolor' => '#eeeeee'), Chris@0: 'node.missing' => array('fillcolor' => '#ff9999', 'style' => 'filled'), Chris@0: ); Chris@0: Chris@0: /** Chris@0: * Dumps the service container as a graphviz graph. Chris@0: * Chris@0: * Available options: Chris@0: * Chris@0: * * graph: The default options for the whole graph Chris@0: * * node: The default options for nodes Chris@0: * * edge: The default options for edges Chris@0: * * node.instance: The default options for services that are defined directly by object instances Chris@0: * * node.definition: The default options for services that are defined via service definition instances Chris@0: * * node.missing: The default options for missing services Chris@0: * Chris@0: * @param array $options An array of options Chris@0: * Chris@0: * @return string The dot representation of the service container Chris@0: */ Chris@0: public function dump(array $options = array()) Chris@0: { Chris@0: foreach (array('graph', 'node', 'edge', 'node.instance', 'node.definition', 'node.missing') as $key) { Chris@0: if (isset($options[$key])) { Chris@0: $this->options[$key] = array_merge($this->options[$key], $options[$key]); Chris@0: } Chris@0: } Chris@0: Chris@0: $this->nodes = $this->findNodes(); Chris@0: Chris@0: $this->edges = array(); Chris@0: foreach ($this->container->getDefinitions() as $id => $definition) { Chris@0: $this->edges[$id] = array_merge( Chris@0: $this->findEdges($id, $definition->getArguments(), true, ''), Chris@0: $this->findEdges($id, $definition->getProperties(), false, '') Chris@0: ); Chris@0: Chris@0: foreach ($definition->getMethodCalls() as $call) { Chris@0: $this->edges[$id] = array_merge( Chris@0: $this->edges[$id], Chris@0: $this->findEdges($id, $call[1], false, $call[0].'()') Chris@0: ); Chris@0: } Chris@0: } Chris@0: Chris@0: return $this->container->resolveEnvPlaceholders($this->startDot().$this->addNodes().$this->addEdges().$this->endDot(), '__ENV_%s__'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns all nodes. Chris@0: * Chris@0: * @return string A string representation of all nodes Chris@0: */ Chris@0: private function addNodes() Chris@0: { Chris@0: $code = ''; Chris@0: foreach ($this->nodes as $id => $node) { Chris@0: $aliases = $this->getAliases($id); Chris@0: Chris@0: $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'])); Chris@0: } Chris@0: Chris@0: return $code; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns all edges. Chris@0: * Chris@0: * @return string A string representation of all edges Chris@0: */ Chris@0: private function addEdges() Chris@0: { Chris@0: $code = ''; Chris@0: foreach ($this->edges as $id => $edges) { Chris@0: foreach ($edges as $edge) { Chris@0: $code .= sprintf(" node_%s -> node_%s [label=\"%s\" style=\"%s\"];\n", $this->dotize($id), $this->dotize($edge['to']), $edge['name'], $edge['required'] ? 'filled' : 'dashed'); Chris@0: } Chris@0: } Chris@0: Chris@0: return $code; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Finds all edges belonging to a specific service id. Chris@0: * Chris@0: * @param string $id The service id used to find edges Chris@0: * @param array $arguments An array of arguments Chris@0: * @param bool $required Chris@0: * @param string $name Chris@0: * Chris@0: * @return array An array of edges Chris@0: */ Chris@0: private function findEdges($id, array $arguments, $required, $name) Chris@0: { Chris@0: $edges = array(); Chris@0: foreach ($arguments as $argument) { Chris@0: if ($argument instanceof Parameter) { Chris@0: $argument = $this->container->hasParameter($argument) ? $this->container->getParameter($argument) : null; Chris@0: } elseif (is_string($argument) && preg_match('/^%([^%]+)%$/', $argument, $match)) { Chris@0: $argument = $this->container->hasParameter($match[1]) ? $this->container->getParameter($match[1]) : null; Chris@0: } Chris@0: Chris@0: if ($argument instanceof Reference) { Chris@0: if (!$this->container->has((string) $argument)) { Chris@0: $this->nodes[(string) $argument] = array('name' => $name, 'required' => $required, 'class' => '', 'attributes' => $this->options['node.missing']); Chris@0: } Chris@0: Chris@0: $edges[] = array('name' => $name, 'required' => $required, 'to' => $argument); Chris@0: } elseif (is_array($argument)) { Chris@0: $edges = array_merge($edges, $this->findEdges($id, $argument, $required, $name)); Chris@0: } Chris@0: } Chris@0: Chris@0: return $edges; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Finds all nodes. Chris@0: * Chris@0: * @return array An array of all nodes Chris@0: */ Chris@0: private function findNodes() Chris@0: { Chris@0: $nodes = array(); Chris@0: Chris@0: $container = $this->cloneContainer(); Chris@0: Chris@0: foreach ($container->getDefinitions() as $id => $definition) { Chris@0: $class = $definition->getClass(); Chris@0: Chris@0: if ('\\' === substr($class, 0, 1)) { Chris@0: $class = substr($class, 1); Chris@0: } Chris@0: Chris@0: try { Chris@0: $class = $this->container->getParameterBag()->resolveValue($class); Chris@0: } catch (ParameterNotFoundException $e) { Chris@0: } Chris@0: Chris@0: $nodes[$id] = array('class' => str_replace('\\', '\\\\', $class), 'attributes' => array_merge($this->options['node.definition'], array('style' => $definition->isShared() ? 'filled' : 'dotted'))); Chris@0: $container->setDefinition($id, new Definition('stdClass')); Chris@0: } Chris@0: Chris@0: foreach ($container->getServiceIds() as $id) { Chris@0: if (array_key_exists($id, $container->getAliases())) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: if (!$container->hasDefinition($id)) { Chris@0: $class = get_class('service_container' === $id ? $this->container : $container->get($id)); Chris@0: $nodes[$id] = array('class' => str_replace('\\', '\\\\', $class), 'attributes' => $this->options['node.instance']); Chris@0: } Chris@0: } Chris@0: Chris@0: return $nodes; Chris@0: } Chris@0: Chris@0: private function cloneContainer() Chris@0: { Chris@0: $parameterBag = new ParameterBag($this->container->getParameterBag()->all()); Chris@0: Chris@0: $container = new ContainerBuilder($parameterBag); Chris@0: $container->setDefinitions($this->container->getDefinitions()); Chris@0: $container->setAliases($this->container->getAliases()); Chris@0: $container->setResources($this->container->getResources()); Chris@0: foreach ($this->container->getExtensions() as $extension) { Chris@0: $container->registerExtension($extension); Chris@0: } Chris@0: Chris@0: return $container; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the start dot. Chris@0: * Chris@0: * @return string The string representation of a start dot Chris@0: */ Chris@0: private function startDot() Chris@0: { Chris@0: return sprintf("digraph sc {\n %s\n node [%s];\n edge [%s];\n\n", Chris@0: $this->addOptions($this->options['graph']), Chris@0: $this->addOptions($this->options['node']), Chris@0: $this->addOptions($this->options['edge']) Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the end dot. Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: private function endDot() Chris@0: { Chris@0: return "}\n"; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Adds attributes. Chris@0: * Chris@0: * @param array $attributes An array of attributes Chris@0: * Chris@0: * @return string A comma separated list of attributes Chris@0: */ Chris@0: private function addAttributes(array $attributes) Chris@0: { Chris@0: $code = array(); Chris@0: foreach ($attributes as $k => $v) { Chris@0: $code[] = sprintf('%s="%s"', $k, $v); Chris@0: } Chris@0: Chris@0: return $code ? ', '.implode(', ', $code) : ''; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Adds options. Chris@0: * Chris@0: * @param array $options An array of options Chris@0: * Chris@0: * @return string A space separated list of options Chris@0: */ Chris@0: private function addOptions(array $options) Chris@0: { Chris@0: $code = array(); Chris@0: foreach ($options as $k => $v) { Chris@0: $code[] = sprintf('%s="%s"', $k, $v); Chris@0: } Chris@0: Chris@0: return implode(' ', $code); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Dotizes an identifier. Chris@0: * Chris@0: * @param string $id The identifier to dotize Chris@0: * Chris@0: * @return string A dotized string Chris@0: */ Chris@0: private function dotize($id) Chris@0: { Chris@0: return strtolower(preg_replace('/\W/i', '_', $id)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Compiles an array of aliases for a specified service id. Chris@0: * Chris@0: * @param string $id A service id Chris@0: * Chris@0: * @return array An array of aliases Chris@0: */ Chris@0: private function getAliases($id) Chris@0: { Chris@0: $aliases = array(); Chris@0: foreach ($this->container->getAliases() as $alias => $origin) { Chris@0: if ($id == $origin) { Chris@0: $aliases[] = $alias; Chris@0: } Chris@0: } Chris@0: Chris@0: return $aliases; Chris@0: } Chris@0: }