PageRenderTime 63ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/Marwamimo/Crowdrise_Web
PHP | 325 lines | 226 code | 43 blank | 56 comment | 31 complexity | c8194c2a61279cc329589c119b398cbd 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\Console\Helper\TableHelper;
  12. use Symfony\Component\DependencyInjection\Alias;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\Definition;
  15. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
  16. use Symfony\Component\Routing\Route;
  17. use Symfony\Component\Routing\RouteCollection;
  18. /**
  19. * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
  20. */
  21. class TextDescriptor extends Descriptor
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. protected function describeRouteCollection(RouteCollection $routes, array $options = array())
  27. {
  28. $showControllers = isset($options['show_controllers']) && $options['show_controllers'];
  29. $headers = array('Name', 'Method', 'Scheme', 'Host', 'Path');
  30. $table = new TableHelper();
  31. $table->setLayout(TableHelper::LAYOUT_COMPACT);
  32. $table->setHeaders($showControllers ? array_merge($headers, array('Controller')) : $headers);
  33. foreach ($routes->all() as $name => $route) {
  34. $row = array(
  35. $name,
  36. $route->getMethods() ? implode('|', $route->getMethods()) : 'ANY',
  37. $route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY',
  38. '' !== $route->getHost() ? $route->getHost() : 'ANY',
  39. $route->getPath(),
  40. );
  41. if ($showControllers) {
  42. $controller = $route->getDefault('_controller');
  43. if ($controller instanceof \Closure) {
  44. $controller = 'Closure';
  45. } elseif (is_object($controller)) {
  46. $controller = get_class($controller);
  47. }
  48. $row[] = $controller;
  49. }
  50. $table->addRow($row);
  51. }
  52. $this->writeText($this->formatSection('router', 'Current routes')."\n", $options);
  53. $this->renderTable($table, !(isset($options['raw_output']) && $options['raw_output']));
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. protected function describeRoute(Route $route, array $options = array())
  59. {
  60. $requirements = $route->getRequirements();
  61. unset($requirements['_scheme'], $requirements['_method']);
  62. // fixme: values were originally written as raw
  63. $description = array(
  64. '<comment>Path</comment> '.$route->getPath(),
  65. '<comment>Host</comment> '.('' !== $route->getHost() ? $route->getHost() : 'ANY'),
  66. '<comment>Scheme</comment> '.($route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY'),
  67. '<comment>Method</comment> '.($route->getMethods() ? implode('|', $route->getMethods()) : 'ANY'),
  68. '<comment>Class</comment> '.get_class($route),
  69. '<comment>Defaults</comment> '.$this->formatRouterConfig($route->getDefaults()),
  70. '<comment>Requirements</comment> '.$this->formatRouterConfig($requirements) ?: 'NO CUSTOM',
  71. '<comment>Options</comment> '.$this->formatRouterConfig($route->getOptions()),
  72. '<comment>Path-Regex</comment> '.$route->compile()->getRegex(),
  73. );
  74. if (isset($options['name'])) {
  75. array_unshift($description, '<comment>Name</comment> '.$options['name']);
  76. array_unshift($description, $this->formatSection('router', sprintf('Route "%s"', $options['name'])));
  77. }
  78. if (null !== $route->compile()->getHostRegex()) {
  79. $description[] = '<comment>Host-Regex</comment> '.$route->compile()->getHostRegex();
  80. }
  81. $this->writeText(implode("\n", $description)."\n", $options);
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. protected function describeContainerParameters(ParameterBag $parameters, array $options = array())
  87. {
  88. $table = new TableHelper();
  89. $table->setLayout(TableHelper::LAYOUT_COMPACT);
  90. $table->setHeaders(array('Parameter', 'Value'));
  91. foreach ($this->sortParameters($parameters) as $parameter => $value) {
  92. $table->addRow(array($parameter, $this->formatParameter($value)));
  93. }
  94. $this->writeText($this->formatSection('container', 'List of parameters')."\n", $options);
  95. $this->renderTable($table, !(isset($options['raw_output']) && $options['raw_output']));
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. protected function describeContainerTags(ContainerBuilder $builder, array $options = array())
  101. {
  102. $showPrivate = isset($options['show_private']) && $options['show_private'];
  103. $description = array($this->formatSection('container', 'Tagged services'));
  104. foreach ($this->findDefinitionsByTag($builder, $showPrivate) as $tag => $definitions) {
  105. $description[] = $this->formatSection('tag', $tag);
  106. $description = array_merge($description, array_keys($definitions));
  107. $description[] = '';
  108. }
  109. $this->writeText(implode("\n", $description), $options);
  110. }
  111. /**
  112. * {@inheritdoc}
  113. */
  114. protected function describeContainerService($service, array $options = array())
  115. {
  116. if (!isset($options['id'])) {
  117. throw new \InvalidArgumentException('An "id" option must be provided.');
  118. }
  119. if ($service instanceof Alias) {
  120. $this->describeContainerAlias($service, $options);
  121. } elseif ($service instanceof Definition) {
  122. $this->describeContainerDefinition($service, $options);
  123. } else {
  124. $description = $this->formatSection('container', sprintf('Information for service <info>%s</info>', $options['id']))
  125. ."\n".sprintf('<comment>Service Id</comment> %s', isset($options['id']) ? $options['id'] : '-')
  126. ."\n".sprintf('<comment>Class</comment> %s', get_class($service));
  127. $this->writeText($description, $options);
  128. }
  129. }
  130. /**
  131. * {@inheritdoc}
  132. */
  133. protected function describeContainerServices(ContainerBuilder $builder, array $options = array())
  134. {
  135. $showPrivate = isset($options['show_private']) && $options['show_private'];
  136. $showTag = isset($options['tag']) ? $options['tag'] : null;
  137. if ($showPrivate) {
  138. $label = '<comment>Public</comment> and <comment>private</comment> services';
  139. } else {
  140. $label = '<comment>Public</comment> services';
  141. }
  142. if ($showTag) {
  143. $label .= ' with tag <info>'.$options['tag'].'</info>';
  144. }
  145. $this->writeText($this->formatSection('container', $label)."\n", $options);
  146. $serviceIds = isset($options['tag']) && $options['tag'] ? array_keys($builder->findTaggedServiceIds($options['tag'])) : $builder->getServiceIds();
  147. $maxTags = array();
  148. foreach ($serviceIds as $key => $serviceId) {
  149. $definition = $this->resolveServiceDefinition($builder, $serviceId);
  150. if ($definition instanceof Definition) {
  151. // filter out private services unless shown explicitly
  152. if (!$showPrivate && !$definition->isPublic()) {
  153. unset($serviceIds[$key]);
  154. continue;
  155. }
  156. if ($showTag) {
  157. $tags = $definition->getTag($showTag);
  158. foreach ($tags as $tag) {
  159. foreach ($tag as $key => $value) {
  160. if (!isset($maxTags[$key])) {
  161. $maxTags[$key] = strlen($key);
  162. }
  163. if (strlen($value) > $maxTags[$key]) {
  164. $maxTags[$key] = strlen($value);
  165. }
  166. }
  167. }
  168. }
  169. }
  170. }
  171. $tagsCount = count($maxTags);
  172. $tagsNames = array_keys($maxTags);
  173. $table = new TableHelper();
  174. $table->setLayout(TableHelper::LAYOUT_COMPACT);
  175. $table->setHeaders(array_merge(array('Service ID'), $tagsNames, array('Scope', 'Class name')));
  176. foreach ($this->sortServiceIds($serviceIds) as $serviceId) {
  177. $definition = $this->resolveServiceDefinition($builder, $serviceId);
  178. if ($definition instanceof Definition) {
  179. if ($showTag) {
  180. foreach ($definition->getTag($showTag) as $key => $tag) {
  181. $tagValues = array();
  182. foreach ($tagsNames as $tagName) {
  183. $tagValues[] = isset($tag[$tagName]) ? $tag[$tagName] : "";
  184. }
  185. if (0 === $key) {
  186. $table->addRow(array_merge(array($serviceId), $tagValues, array($definition->getScope(), $definition->getClass())));
  187. } else {
  188. $table->addRow(array_merge(array(' "'), $tagValues, array('', '')));
  189. }
  190. }
  191. } else {
  192. $table->addRow(array($serviceId, $definition->getScope(), $definition->getClass()));
  193. }
  194. } elseif ($definition instanceof Alias) {
  195. $alias = $definition;
  196. $table->addRow(array_merge(array($serviceId, 'n/a', sprintf('alias for "%s"', $alias)), $tagsCount ? array_fill(0, $tagsCount, "") : array()));
  197. } else {
  198. // we have no information (happens with "service_container")
  199. $table->addRow(array_merge(array($serviceId, '', get_class($definition)), $tagsCount ? array_fill(0, $tagsCount, "") : array()));
  200. }
  201. }
  202. $this->renderTable($table);
  203. }
  204. /**
  205. * {@inheritdoc}
  206. */
  207. protected function describeContainerDefinition(Definition $definition, array $options = array())
  208. {
  209. $description = isset($options['id'])
  210. ? array($this->formatSection('container', sprintf('Information for service <info>%s</info>', $options['id'])))
  211. : array();
  212. $description[] = sprintf('<comment>Service Id</comment> %s', isset($options['id']) ? $options['id'] : '-');
  213. $description[] = sprintf('<comment>Class</comment> %s', $definition->getClass() ?: "-");
  214. $tags = $definition->getTags();
  215. if (count($tags)) {
  216. $description[] = '<comment>Tags</comment>';
  217. foreach ($tags as $tagName => $tagData) {
  218. foreach ($tagData as $parameters) {
  219. $description[] = sprintf(' - %-30s (%s)', $tagName, implode(', ', array_map(function ($key, $value) {
  220. return sprintf('<info>%s</info>: %s', $key, $value);
  221. }, array_keys($parameters), array_values($parameters))));
  222. }
  223. }
  224. } else {
  225. $description[] = '<comment>Tags</comment> -';
  226. }
  227. $description[] = sprintf('<comment>Scope</comment> %s', $definition->getScope());
  228. $description[] = sprintf('<comment>Public</comment> %s', $definition->isPublic() ? 'yes' : 'no');
  229. $description[] = sprintf('<comment>Synthetic</comment> %s', $definition->isSynthetic() ? 'yes' : 'no');
  230. $description[] = sprintf('<comment>Required File</comment> %s', $definition->getFile() ? $definition->getFile() : '-');
  231. $this->writeText(implode("\n", $description)."\n", $options);
  232. }
  233. /**
  234. * {@inheritdoc}
  235. */
  236. protected function describeContainerAlias(Alias $alias, array $options = array())
  237. {
  238. $this->writeText(sprintf('This service is an alias for the service <info>%s</info>', (string) $alias), $options);
  239. }
  240. /**
  241. * {@inheritdoc}
  242. */
  243. protected function describeContainerParameter($parameter, array $options = array())
  244. {
  245. $this->writeText($this->formatParameter($parameter), $options);
  246. }
  247. /**
  248. * @param array $array
  249. *
  250. * @return string
  251. */
  252. private function formatRouterConfig(array $array)
  253. {
  254. $string = '';
  255. ksort($array);
  256. foreach ($array as $name => $value) {
  257. $string .= ($string ? "\n".str_repeat(' ', 13) : '').$name.': '.$this->formatValue($value);
  258. }
  259. return $string;
  260. }
  261. /**
  262. * @param string $section
  263. * @param string $message
  264. *
  265. * @return string
  266. */
  267. private function formatSection($section, $message)
  268. {
  269. return sprintf('<info>[%s]</info> %s', $section, $message);
  270. }
  271. /**
  272. * @param string $content
  273. * @param array $options
  274. */
  275. private function writeText($content, array $options = array())
  276. {
  277. $this->write(
  278. isset($options['raw_text']) && $options['raw_text'] ? strip_tags($content) : $content,
  279. isset($options['raw_output']) ? !$options['raw_output'] : true
  280. );
  281. }
  282. }