PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Dumper/GraphvizDumper.php

https://gitlab.com/mohamedchiheb.bida/workshopFOS
PHP | 302 lines | 167 code | 39 blank | 96 comment | 9 complexity | aec5ba204797f5305569c26cb4d8c17a MD5 | raw file
  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, $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. $className = $definition->getClass();
  149. try {
  150. $className = $this->container->getParameterBag()->resolveValue($className);
  151. } catch (ParameterNotFoundException $e) {
  152. }
  153. $nodes[$id] = array('class' => str_replace('\\', '\\\\', $className), 'attributes' => array_merge($this->options['node.definition'], array('style' => ContainerInterface::SCOPE_PROTOTYPE !== $definition->getScope() ? 'filled' : 'dotted')));
  154. $container->setDefinition($id, new Definition('stdClass'));
  155. }
  156. foreach ($container->getServiceIds() as $id) {
  157. $service = $container->get($id);
  158. if (array_key_exists($id, $container->getAliases())) {
  159. continue;
  160. }
  161. if (!$container->hasDefinition($id)) {
  162. $class = ('service_container' === $id) ? get_class($this->container) : get_class($service);
  163. $nodes[$id] = array('class' => str_replace('\\', '\\\\', $class), 'attributes' => $this->options['node.instance']);
  164. }
  165. }
  166. return $nodes;
  167. }
  168. private function cloneContainer()
  169. {
  170. $parameterBag = new ParameterBag($this->container->getParameterBag()->all());
  171. $container = new ContainerBuilder($parameterBag);
  172. $container->setDefinitions($this->container->getDefinitions());
  173. $container->setAliases($this->container->getAliases());
  174. $container->setResources($this->container->getResources());
  175. foreach ($this->container->getScopes() as $scope => $parentScope) {
  176. $container->addScope(new Scope($scope, $parentScope));
  177. }
  178. foreach ($this->container->getExtensions() as $extension) {
  179. $container->registerExtension($extension);
  180. }
  181. return $container;
  182. }
  183. /**
  184. * Returns the start dot.
  185. *
  186. * @return string The string representation of a start dot
  187. */
  188. private function startDot()
  189. {
  190. return sprintf("digraph sc {\n %s\n node [%s];\n edge [%s];\n\n",
  191. $this->addOptions($this->options['graph']),
  192. $this->addOptions($this->options['node']),
  193. $this->addOptions($this->options['edge'])
  194. );
  195. }
  196. /**
  197. * Returns the end dot.
  198. *
  199. * @return string
  200. */
  201. private function endDot()
  202. {
  203. return "}\n";
  204. }
  205. /**
  206. * Adds attributes.
  207. *
  208. * @param array $attributes An array of attributes
  209. *
  210. * @return string A comma separated list of attributes
  211. */
  212. private function addAttributes($attributes)
  213. {
  214. $code = array();
  215. foreach ($attributes as $k => $v) {
  216. $code[] = sprintf('%s="%s"', $k, $v);
  217. }
  218. return $code ? ', '.implode(', ', $code) : '';
  219. }
  220. /**
  221. * Adds options.
  222. *
  223. * @param array $options An array of options
  224. *
  225. * @return string A space separated list of options
  226. */
  227. private function addOptions($options)
  228. {
  229. $code = array();
  230. foreach ($options as $k => $v) {
  231. $code[] = sprintf('%s="%s"', $k, $v);
  232. }
  233. return implode(' ', $code);
  234. }
  235. /**
  236. * Dotizes an identifier.
  237. *
  238. * @param string $id The identifier to dotize
  239. *
  240. * @return string A dotized string
  241. */
  242. private function dotize($id)
  243. {
  244. return strtolower(preg_replace('/\W/i', '_', $id));
  245. }
  246. /**
  247. * Compiles an array of aliases for a specified service id.
  248. *
  249. * @param string $id A service id
  250. *
  251. * @return array An array of aliases
  252. */
  253. private function getAliases($id)
  254. {
  255. $aliases = array();
  256. foreach ($this->container->getAliases() as $alias => $origin) {
  257. if ($id == $origin) {
  258. $aliases[] = $alias;
  259. }
  260. }
  261. return $aliases;
  262. }
  263. }