Drupal investigation

GraphvizDumper.php 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DependencyInjection\Dumper;
  11. use Symfony\Component\DependencyInjection\Definition;
  12. use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
  13. use Symfony\Component\DependencyInjection\Reference;
  14. use Symfony\Component\DependencyInjection\Parameter;
  15. use Symfony\Component\DependencyInjection\ContainerInterface;
  16. use Symfony\Component\DependencyInjection\ContainerBuilder;
  17. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
  18. use Symfony\Component\DependencyInjection\Scope;
  19. /**
  20. * GraphvizDumper dumps a service container as a graphviz file.
  21. *
  22. * You can convert the generated dot file with the dot utility (http://www.graphviz.org/):
  23. *
  24. * dot -Tpng container.dot > foo.png
  25. *
  26. * @author Fabien Potencier <fabien@symfony.com>
  27. */
  28. class GraphvizDumper extends Dumper
  29. {
  30. private $nodes;
  31. private $edges;
  32. private $options = array(
  33. 'graph' => array('ratio' => 'compress'),
  34. 'node' => array('fontsize' => 11, 'fontname' => 'Arial', 'shape' => 'record'),
  35. 'edge' => array('fontsize' => 9, 'fontname' => 'Arial', 'color' => 'grey', 'arrowhead' => 'open', 'arrowsize' => 0.5),
  36. 'node.instance' => array('fillcolor' => '#9999ff', 'style' => 'filled'),
  37. 'node.definition' => array('fillcolor' => '#eeeeee'),
  38. 'node.missing' => array('fillcolor' => '#ff9999', 'style' => 'filled'),
  39. );
  40. /**
  41. * Dumps the service container as a graphviz graph.
  42. *
  43. * Available options:
  44. *
  45. * * graph: The default options for the whole graph
  46. * * node: The default options for nodes
  47. * * edge: The default options for edges
  48. * * node.instance: The default options for services that are defined directly by object instances
  49. * * node.definition: The default options for services that are defined via service definition instances
  50. * * node.missing: The default options for missing services
  51. *
  52. * @param array $options An array of options
  53. *
  54. * @return string The dot representation of the service container
  55. */
  56. public function dump(array $options = array())
  57. {
  58. foreach (array('graph', 'node', 'edge', 'node.instance', 'node.definition', 'node.missing') as $key) {
  59. if (isset($options[$key])) {
  60. $this->options[$key] = array_merge($this->options[$key], $options[$key]);
  61. }
  62. }
  63. $this->nodes = $this->findNodes();
  64. $this->edges = array();
  65. foreach ($this->container->getDefinitions() as $id => $definition) {
  66. $this->edges[$id] = array_merge(
  67. $this->findEdges($id, $definition->getArguments(), true, ''),
  68. $this->findEdges($id, $definition->getProperties(), false, '')
  69. );
  70. foreach ($definition->getMethodCalls() as $call) {
  71. $this->edges[$id] = array_merge(
  72. $this->edges[$id],
  73. $this->findEdges($id, $call[1], false, $call[0].'()')
  74. );
  75. }
  76. }
  77. return $this->startDot().$this->addNodes().$this->addEdges().$this->endDot();
  78. }
  79. /**
  80. * Returns all nodes.
  81. *
  82. * @return string A string representation of all nodes
  83. */
  84. private function addNodes()
  85. {
  86. $code = '';
  87. foreach ($this->nodes as $id => $node) {
  88. $aliases = $this->getAliases($id);
  89. $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']));
  90. }
  91. return $code;
  92. }
  93. /**
  94. * Returns all edges.
  95. *
  96. * @return string A string representation of all edges
  97. */
  98. private function addEdges()
  99. {
  100. $code = '';
  101. foreach ($this->edges as $id => $edges) {
  102. foreach ($edges as $edge) {
  103. $code .= sprintf(" node_%s -> node_%s [label=\"%s\" style=\"%s\"];\n", $this->dotize($id), $this->dotize($edge['to']), $edge['name'], $edge['required'] ? 'filled' : 'dashed');
  104. }
  105. }
  106. return $code;
  107. }
  108. /**
  109. * Finds all edges belonging to a specific service id.
  110. *
  111. * @param string $id The service id used to find edges
  112. * @param array $arguments An array of arguments
  113. * @param bool $required
  114. * @param string $name
  115. *
  116. * @return array An array of edges
  117. */
  118. private function findEdges($id, array $arguments, $required, $name)
  119. {
  120. $edges = array();
  121. foreach ($arguments as $argument) {
  122. if ($argument instanceof Parameter) {
  123. $argument = $this->container->hasParameter($argument) ? $this->container->getParameter($argument) : null;
  124. } elseif (is_string($argument) && preg_match('/^%([^%]+)%$/', $argument, $match)) {
  125. $argument = $this->container->hasParameter($match[1]) ? $this->container->getParameter($match[1]) : null;
  126. }
  127. if ($argument instanceof Reference) {
  128. if (!$this->container->has((string) $argument)) {
  129. $this->nodes[(string) $argument] = array('name' => $name, 'required' => $required, 'class' => '', 'attributes' => $this->options['node.missing']);
  130. }
  131. $edges[] = array('name' => $name, 'required' => $required, 'to' => $argument);
  132. } elseif (is_array($argument)) {
  133. $edges = array_merge($edges, $this->findEdges($id, $argument, $required, $name));
  134. }
  135. }
  136. return $edges;
  137. }
  138. /**
  139. * Finds all nodes.
  140. *
  141. * @return array An array of all nodes
  142. */
  143. private function findNodes()
  144. {
  145. $nodes = array();
  146. $container = $this->cloneContainer();
  147. foreach ($container->getDefinitions() as $id => $definition) {
  148. $class = $definition->getClass();
  149. if ('\\' === substr($class, 0, 1)) {
  150. $class = substr($class, 1);
  151. }
  152. try {
  153. $class = $this->container->getParameterBag()->resolveValue($class);
  154. } catch (ParameterNotFoundException $e) {
  155. }
  156. $nodes[$id] = array('class' => str_replace('\\', '\\\\', $class), 'attributes' => array_merge($this->options['node.definition'], array('style' => $definition->isShared() && ContainerInterface::SCOPE_PROTOTYPE !== $definition->getScope(false) ? 'filled' : 'dotted')));
  157. $container->setDefinition($id, new Definition('stdClass'));
  158. }
  159. foreach ($container->getServiceIds() as $id) {
  160. $service = $container->get($id);
  161. if (array_key_exists($id, $container->getAliases())) {
  162. continue;
  163. }
  164. if (!$container->hasDefinition($id)) {
  165. $class = ('service_container' === $id) ? get_class($this->container) : get_class($service);
  166. $nodes[$id] = array('class' => str_replace('\\', '\\\\', $class), 'attributes' => $this->options['node.instance']);
  167. }
  168. }
  169. return $nodes;
  170. }
  171. private function cloneContainer()
  172. {
  173. $parameterBag = new ParameterBag($this->container->getParameterBag()->all());
  174. $container = new ContainerBuilder($parameterBag);
  175. $container->setDefinitions($this->container->getDefinitions());
  176. $container->setAliases($this->container->getAliases());
  177. $container->setResources($this->container->getResources());
  178. foreach ($this->container->getScopes(false) as $scope => $parentScope) {
  179. $container->addScope(new Scope($scope, $parentScope));
  180. }
  181. foreach ($this->container->getExtensions() as $extension) {
  182. $container->registerExtension($extension);
  183. }
  184. return $container;
  185. }
  186. /**
  187. * Returns the start dot.
  188. *
  189. * @return string The string representation of a start dot
  190. */
  191. private function startDot()
  192. {
  193. return sprintf("digraph sc {\n %s\n node [%s];\n edge [%s];\n\n",
  194. $this->addOptions($this->options['graph']),
  195. $this->addOptions($this->options['node']),
  196. $this->addOptions($this->options['edge'])
  197. );
  198. }
  199. /**
  200. * Returns the end dot.
  201. *
  202. * @return string
  203. */
  204. private function endDot()
  205. {
  206. return "}\n";
  207. }
  208. /**
  209. * Adds attributes.
  210. *
  211. * @param array $attributes An array of attributes
  212. *
  213. * @return string A comma separated list of attributes
  214. */
  215. private function addAttributes(array $attributes)
  216. {
  217. $code = array();
  218. foreach ($attributes as $k => $v) {
  219. $code[] = sprintf('%s="%s"', $k, $v);
  220. }
  221. return $code ? ', '.implode(', ', $code) : '';
  222. }
  223. /**
  224. * Adds options.
  225. *
  226. * @param array $options An array of options
  227. *
  228. * @return string A space separated list of options
  229. */
  230. private function addOptions(array $options)
  231. {
  232. $code = array();
  233. foreach ($options as $k => $v) {
  234. $code[] = sprintf('%s="%s"', $k, $v);
  235. }
  236. return implode(' ', $code);
  237. }
  238. /**
  239. * Dotizes an identifier.
  240. *
  241. * @param string $id The identifier to dotize
  242. *
  243. * @return string A dotized string
  244. */
  245. private function dotize($id)
  246. {
  247. return strtolower(preg_replace('/\W/i', '_', $id));
  248. }
  249. /**
  250. * Compiles an array of aliases for a specified service id.
  251. *
  252. * @param string $id A service id
  253. *
  254. * @return array An array of aliases
  255. */
  256. private function getAliases($id)
  257. {
  258. $aliases = array();
  259. foreach ($this->container->getAliases() as $alias => $origin) {
  260. if ($id == $origin) {
  261. $aliases[] = $alias;
  262. }
  263. }
  264. return $aliases;
  265. }
  266. }