PageRenderTime 48ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/MarkdownDescriptor.php

https://gitlab.com/pr0055/symfonypizza
PHP | 371 lines | 265 code | 55 blank | 51 comment | 41 complexity | 5d8a145fe1ad0136e003c4af831e80ba 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\Bundle\FrameworkBundle\Console\Descriptor;
  11. use Symfony\Component\DependencyInjection\Alias;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\Definition;
  14. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
  15. use Symfony\Component\DependencyInjection\Reference;
  16. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  17. use Symfony\Component\Routing\Route;
  18. use Symfony\Component\Routing\RouteCollection;
  19. /**
  20. * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
  21. *
  22. * @internal
  23. */
  24. class MarkdownDescriptor extends Descriptor
  25. {
  26. /**
  27. * {@inheritdoc}
  28. */
  29. protected function describeRouteCollection(RouteCollection $routes, array $options = array())
  30. {
  31. $first = true;
  32. foreach ($routes->all() as $name => $route) {
  33. if ($first) {
  34. $first = false;
  35. } else {
  36. $this->write("\n\n");
  37. }
  38. $this->describeRoute($route, array('name' => $name));
  39. }
  40. $this->write("\n");
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. protected function describeRoute(Route $route, array $options = array())
  46. {
  47. $output = '- Path: '.$route->getPath()
  48. ."\n".'- Path Regex: '.$route->compile()->getRegex()
  49. ."\n".'- Host: '.('' !== $route->getHost() ? $route->getHost() : 'ANY')
  50. ."\n".'- Host Regex: '.('' !== $route->getHost() ? $route->compile()->getHostRegex() : '')
  51. ."\n".'- Scheme: '.($route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY')
  52. ."\n".'- Method: '.($route->getMethods() ? implode('|', $route->getMethods()) : 'ANY')
  53. ."\n".'- Class: '.get_class($route)
  54. ."\n".'- Defaults: '.$this->formatRouterConfig($route->getDefaults())
  55. ."\n".'- Requirements: '.($route->getRequirements() ? $this->formatRouterConfig($route->getRequirements()) : 'NO CUSTOM')
  56. ."\n".'- Options: '.$this->formatRouterConfig($route->getOptions());
  57. $this->write(isset($options['name'])
  58. ? $options['name']."\n".str_repeat('-', strlen($options['name']))."\n\n".$output
  59. : $output);
  60. $this->write("\n");
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. protected function describeContainerParameters(ParameterBag $parameters, array $options = array())
  66. {
  67. $this->write("Container parameters\n====================\n");
  68. foreach ($this->sortParameters($parameters) as $key => $value) {
  69. $this->write(sprintf("\n- `%s`: `%s`", $key, $this->formatParameter($value)));
  70. }
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. protected function describeContainerTags(ContainerBuilder $builder, array $options = array())
  76. {
  77. $showPrivate = isset($options['show_private']) && $options['show_private'];
  78. $this->write("Container tags\n==============");
  79. foreach ($this->findDefinitionsByTag($builder, $showPrivate) as $tag => $definitions) {
  80. $this->write("\n\n".$tag."\n".str_repeat('-', strlen($tag)));
  81. foreach ($definitions as $serviceId => $definition) {
  82. $this->write("\n\n");
  83. $this->describeContainerDefinition($definition, array('omit_tags' => true, 'id' => $serviceId));
  84. }
  85. }
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. protected function describeContainerService($service, array $options = array())
  91. {
  92. if (!isset($options['id'])) {
  93. throw new \InvalidArgumentException('An "id" option must be provided.');
  94. }
  95. $childOptions = array('id' => $options['id'], 'as_array' => true);
  96. if ($service instanceof Alias) {
  97. $this->describeContainerAlias($service, $childOptions);
  98. } elseif ($service instanceof Definition) {
  99. $this->describeContainerDefinition($service, $childOptions);
  100. } else {
  101. $this->write(sprintf('**`%s`:** `%s`', $options['id'], get_class($service)));
  102. }
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. protected function describeContainerServices(ContainerBuilder $builder, array $options = array())
  108. {
  109. $showPrivate = isset($options['show_private']) && $options['show_private'];
  110. $title = $showPrivate ? 'Public and private services' : 'Public services';
  111. if (isset($options['tag'])) {
  112. $title .= ' with tag `'.$options['tag'].'`';
  113. }
  114. $this->write($title."\n".str_repeat('=', strlen($title)));
  115. $serviceIds = isset($options['tag']) && $options['tag'] ? array_keys($builder->findTaggedServiceIds($options['tag'])) : $builder->getServiceIds();
  116. $showPrivate = isset($options['show_private']) && $options['show_private'];
  117. $services = array('definitions' => array(), 'aliases' => array(), 'services' => array());
  118. foreach ($this->sortServiceIds($serviceIds) as $serviceId) {
  119. $service = $this->resolveServiceDefinition($builder, $serviceId);
  120. if ($service instanceof Alias) {
  121. $services['aliases'][$serviceId] = $service;
  122. } elseif ($service instanceof Definition) {
  123. if (($showPrivate || $service->isPublic())) {
  124. $services['definitions'][$serviceId] = $service;
  125. }
  126. } else {
  127. $services['services'][$serviceId] = $service;
  128. }
  129. }
  130. if (!empty($services['definitions'])) {
  131. $this->write("\n\nDefinitions\n-----------\n");
  132. foreach ($services['definitions'] as $id => $service) {
  133. $this->write("\n");
  134. $this->describeContainerDefinition($service, array('id' => $id));
  135. }
  136. }
  137. if (!empty($services['aliases'])) {
  138. $this->write("\n\nAliases\n-------\n");
  139. foreach ($services['aliases'] as $id => $service) {
  140. $this->write("\n");
  141. $this->describeContainerAlias($service, array('id' => $id));
  142. }
  143. }
  144. if (!empty($services['services'])) {
  145. $this->write("\n\nServices\n--------\n");
  146. foreach ($services['services'] as $id => $service) {
  147. $this->write("\n");
  148. $this->write(sprintf('- `%s`: `%s`', $id, get_class($service)));
  149. }
  150. }
  151. }
  152. /**
  153. * {@inheritdoc}
  154. */
  155. protected function describeContainerDefinition(Definition $definition, array $options = array())
  156. {
  157. $output = '- Class: `'.$definition->getClass().'`'
  158. ."\n".'- Public: '.($definition->isPublic() ? 'yes' : 'no')
  159. ."\n".'- Synthetic: '.($definition->isSynthetic() ? 'yes' : 'no')
  160. ."\n".'- Lazy: '.($definition->isLazy() ? 'yes' : 'no')
  161. ;
  162. if (method_exists($definition, 'isShared')) {
  163. $output .= "\n".'- Shared: '.($definition->isShared() ? 'yes' : 'no');
  164. }
  165. $output .= "\n".'- Abstract: '.($definition->isAbstract() ? 'yes' : 'no');
  166. if (method_exists($definition, 'isAutowired')) {
  167. $output .= "\n".'- Autowired: '.($definition->isAutowired() ? 'yes' : 'no');
  168. foreach ($definition->getAutowiringTypes() as $autowiringType) {
  169. $output .= "\n".'- Autowiring Type: `'.$autowiringType.'`';
  170. }
  171. }
  172. if ($definition->getFile()) {
  173. $output .= "\n".'- File: `'.$definition->getFile().'`';
  174. }
  175. if ($factory = $definition->getFactory()) {
  176. if (is_array($factory)) {
  177. if ($factory[0] instanceof Reference) {
  178. $output .= "\n".'- Factory Service: `'.$factory[0].'`';
  179. } elseif ($factory[0] instanceof Definition) {
  180. throw new \InvalidArgumentException('Factory is not describable.');
  181. } else {
  182. $output .= "\n".'- Factory Class: `'.$factory[0].'`';
  183. }
  184. $output .= "\n".'- Factory Method: `'.$factory[1].'`';
  185. } else {
  186. $output .= "\n".'- Factory Function: `'.$factory.'`';
  187. }
  188. }
  189. $calls = $definition->getMethodCalls();
  190. foreach ($calls as $callData) {
  191. $output .= "\n".'- Call: `'.$callData[0].'`';
  192. }
  193. if (!(isset($options['omit_tags']) && $options['omit_tags'])) {
  194. foreach ($definition->getTags() as $tagName => $tagData) {
  195. foreach ($tagData as $parameters) {
  196. $output .= "\n".'- Tag: `'.$tagName.'`';
  197. foreach ($parameters as $name => $value) {
  198. $output .= "\n".' - '.ucfirst($name).': '.$value;
  199. }
  200. }
  201. }
  202. }
  203. $this->write(isset($options['id']) ? sprintf("%s\n%s\n\n%s\n", $options['id'], str_repeat('~', strlen($options['id'])), $output) : $output);
  204. }
  205. /**
  206. * {@inheritdoc}
  207. */
  208. protected function describeContainerAlias(Alias $alias, array $options = array())
  209. {
  210. $output = '- Service: `'.$alias.'`'
  211. ."\n".'- Public: '.($alias->isPublic() ? 'yes' : 'no');
  212. $this->write(isset($options['id']) ? sprintf("%s\n%s\n\n%s\n", $options['id'], str_repeat('~', strlen($options['id'])), $output) : $output);
  213. }
  214. /**
  215. * {@inheritdoc}
  216. */
  217. protected function describeContainerParameter($parameter, array $options = array())
  218. {
  219. $this->write(isset($options['parameter']) ? sprintf("%s\n%s\n\n%s", $options['parameter'], str_repeat('=', strlen($options['parameter'])), $this->formatParameter($parameter)) : $parameter);
  220. }
  221. /**
  222. * {@inheritdoc}
  223. */
  224. protected function describeEventDispatcherListeners(EventDispatcherInterface $eventDispatcher, array $options = array())
  225. {
  226. $event = array_key_exists('event', $options) ? $options['event'] : null;
  227. $title = 'Registered listeners';
  228. if (null !== $event) {
  229. $title .= sprintf(' for event `%s` ordered by descending priority', $event);
  230. }
  231. $this->write(sprintf('# %s', $title)."\n");
  232. $registeredListeners = $eventDispatcher->getListeners($event);
  233. if (null !== $event) {
  234. foreach ($registeredListeners as $order => $listener) {
  235. $this->write("\n".sprintf('## Listener %d', $order + 1)."\n");
  236. $this->describeCallable($listener);
  237. $this->write(sprintf('- Priority: `%d`', $eventDispatcher->getListenerPriority($event, $listener))."\n");
  238. }
  239. } else {
  240. ksort($registeredListeners);
  241. foreach ($registeredListeners as $eventListened => $eventListeners) {
  242. $this->write("\n".sprintf('## %s', $eventListened)."\n");
  243. foreach ($eventListeners as $order => $eventListener) {
  244. $this->write("\n".sprintf('### Listener %d', $order + 1)."\n");
  245. $this->describeCallable($eventListener);
  246. $this->write(sprintf('- Priority: `%d`', $eventDispatcher->getListenerPriority($eventListened, $eventListener))."\n");
  247. }
  248. }
  249. }
  250. }
  251. /**
  252. * {@inheritdoc}
  253. */
  254. protected function describeCallable($callable, array $options = array())
  255. {
  256. $string = '';
  257. if (is_array($callable)) {
  258. $string .= "\n- Type: `function`";
  259. if (is_object($callable[0])) {
  260. $string .= "\n".sprintf('- Name: `%s`', $callable[1]);
  261. $string .= "\n".sprintf('- Class: `%s`', get_class($callable[0]));
  262. } else {
  263. if (0 !== strpos($callable[1], 'parent::')) {
  264. $string .= "\n".sprintf('- Name: `%s`', $callable[1]);
  265. $string .= "\n".sprintf('- Class: `%s`', $callable[0]);
  266. $string .= "\n- Static: yes";
  267. } else {
  268. $string .= "\n".sprintf('- Name: `%s`', substr($callable[1], 8));
  269. $string .= "\n".sprintf('- Class: `%s`', $callable[0]);
  270. $string .= "\n- Static: yes";
  271. $string .= "\n- Parent: yes";
  272. }
  273. }
  274. return $this->write($string."\n");
  275. }
  276. if (is_string($callable)) {
  277. $string .= "\n- Type: `function`";
  278. if (false === strpos($callable, '::')) {
  279. $string .= "\n".sprintf('- Name: `%s`', $callable);
  280. } else {
  281. $callableParts = explode('::', $callable);
  282. $string .= "\n".sprintf('- Name: `%s`', $callableParts[1]);
  283. $string .= "\n".sprintf('- Class: `%s`', $callableParts[0]);
  284. $string .= "\n- Static: yes";
  285. }
  286. return $this->write($string."\n");
  287. }
  288. if ($callable instanceof \Closure) {
  289. $string .= "\n- Type: `closure`";
  290. return $this->write($string."\n");
  291. }
  292. if (method_exists($callable, '__invoke')) {
  293. $string .= "\n- Type: `object`";
  294. $string .= "\n".sprintf('- Name: `%s`', get_class($callable));
  295. return $this->write($string."\n");
  296. }
  297. throw new \InvalidArgumentException('Callable is not describable.');
  298. }
  299. /**
  300. * @param array $array
  301. *
  302. * @return string
  303. */
  304. private function formatRouterConfig(array $array)
  305. {
  306. if (!count($array)) {
  307. return 'NONE';
  308. }
  309. $string = '';
  310. ksort($array);
  311. foreach ($array as $name => $value) {
  312. $string .= "\n".' - `'.$name.'`: '.$this->formatValue($value);
  313. }
  314. return $string;
  315. }
  316. }