PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

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

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