PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/jeisoncg/personal_collection_mv
PHP | 366 lines | 261 code | 54 blank | 51 comment | 41 complexity | f6fa07ef40b8ec5ab89e6303547c7a0f 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. if (!(isset($options['omit_tags']) && $options['omit_tags'])) {
  190. foreach ($definition->getTags() as $tagName => $tagData) {
  191. foreach ($tagData as $parameters) {
  192. $output .= "\n".'- Tag: `'.$tagName.'`';
  193. foreach ($parameters as $name => $value) {
  194. $output .= "\n".' - '.ucfirst($name).': '.$value;
  195. }
  196. }
  197. }
  198. }
  199. $this->write(isset($options['id']) ? sprintf("%s\n%s\n\n%s\n", $options['id'], str_repeat('~', strlen($options['id'])), $output) : $output);
  200. }
  201. /**
  202. * {@inheritdoc}
  203. */
  204. protected function describeContainerAlias(Alias $alias, array $options = array())
  205. {
  206. $output = '- Service: `'.$alias.'`'
  207. ."\n".'- Public: '.($alias->isPublic() ? 'yes' : 'no');
  208. $this->write(isset($options['id']) ? sprintf("%s\n%s\n\n%s\n", $options['id'], str_repeat('~', strlen($options['id'])), $output) : $output);
  209. }
  210. /**
  211. * {@inheritdoc}
  212. */
  213. protected function describeContainerParameter($parameter, array $options = array())
  214. {
  215. $this->write(isset($options['parameter']) ? sprintf("%s\n%s\n\n%s", $options['parameter'], str_repeat('=', strlen($options['parameter'])), $this->formatParameter($parameter)) : $parameter);
  216. }
  217. /**
  218. * {@inheritdoc}
  219. */
  220. protected function describeEventDispatcherListeners(EventDispatcherInterface $eventDispatcher, array $options = array())
  221. {
  222. $event = array_key_exists('event', $options) ? $options['event'] : null;
  223. $title = 'Registered listeners';
  224. if (null !== $event) {
  225. $title .= sprintf(' for event `%s` ordered by descending priority', $event);
  226. }
  227. $this->write(sprintf('# %s', $title)."\n");
  228. $registeredListeners = $eventDispatcher->getListeners($event);
  229. if (null !== $event) {
  230. foreach ($registeredListeners as $order => $listener) {
  231. $this->write("\n".sprintf('## Listener %d', $order + 1)."\n");
  232. $this->describeCallable($listener);
  233. $this->write(sprintf('- Priority: `%d`', $eventDispatcher->getListenerPriority($event, $listener))."\n");
  234. }
  235. } else {
  236. ksort($registeredListeners);
  237. foreach ($registeredListeners as $eventListened => $eventListeners) {
  238. $this->write("\n".sprintf('## %s', $eventListened)."\n");
  239. foreach ($eventListeners as $order => $eventListener) {
  240. $this->write("\n".sprintf('### Listener %d', $order + 1)."\n");
  241. $this->describeCallable($eventListener);
  242. $this->write(sprintf('- Priority: `%d`', $eventDispatcher->getListenerPriority($eventListened, $eventListener))."\n");
  243. }
  244. }
  245. }
  246. }
  247. /**
  248. * {@inheritdoc}
  249. */
  250. protected function describeCallable($callable, array $options = array())
  251. {
  252. $string = '';
  253. if (is_array($callable)) {
  254. $string .= "\n- Type: `function`";
  255. if (is_object($callable[0])) {
  256. $string .= "\n".sprintf('- Name: `%s`', $callable[1]);
  257. $string .= "\n".sprintf('- Class: `%s`', get_class($callable[0]));
  258. } else {
  259. if (0 !== strpos($callable[1], 'parent::')) {
  260. $string .= "\n".sprintf('- Name: `%s`', $callable[1]);
  261. $string .= "\n".sprintf('- Class: `%s`', $callable[0]);
  262. $string .= "\n- Static: yes";
  263. } else {
  264. $string .= "\n".sprintf('- Name: `%s`', substr($callable[1], 8));
  265. $string .= "\n".sprintf('- Class: `%s`', $callable[0]);
  266. $string .= "\n- Static: yes";
  267. $string .= "\n- Parent: yes";
  268. }
  269. }
  270. return $this->write($string."\n");
  271. }
  272. if (is_string($callable)) {
  273. $string .= "\n- Type: `function`";
  274. if (false === strpos($callable, '::')) {
  275. $string .= "\n".sprintf('- Name: `%s`', $callable);
  276. } else {
  277. $callableParts = explode('::', $callable);
  278. $string .= "\n".sprintf('- Name: `%s`', $callableParts[1]);
  279. $string .= "\n".sprintf('- Class: `%s`', $callableParts[0]);
  280. $string .= "\n- Static: yes";
  281. }
  282. return $this->write($string."\n");
  283. }
  284. if ($callable instanceof \Closure) {
  285. $string .= "\n- Type: `closure`";
  286. return $this->write($string."\n");
  287. }
  288. if (method_exists($callable, '__invoke')) {
  289. $string .= "\n- Type: `object`";
  290. $string .= "\n".sprintf('- Name: `%s`', get_class($callable));
  291. return $this->write($string."\n");
  292. }
  293. throw new \InvalidArgumentException('Callable is not describable.');
  294. }
  295. /**
  296. * @param array $array
  297. *
  298. * @return string
  299. */
  300. private function formatRouterConfig(array $array)
  301. {
  302. if (!count($array)) {
  303. return 'NONE';
  304. }
  305. $string = '';
  306. ksort($array);
  307. foreach ($array as $name => $value) {
  308. $string .= "\n".' - `'.$name.'`: '.$this->formatValue($value);
  309. }
  310. return $string;
  311. }
  312. }