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

/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Command/RouterDebugCommand.php

https://github.com/nattaphat/hgis
PHP | 192 lines | 129 code | 34 blank | 29 comment | 9 complexity | 999d16d80dcedff97f78b14388774fd0 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\Command;
  11. use Symfony\Component\Console\Input\InputArgument;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. use Symfony\Component\Routing\RouterInterface;
  15. /**
  16. * A console command for retrieving information about routes
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. * @author Tobias Schultze <http://tobion.de>
  20. */
  21. class RouterDebugCommand extends ContainerAwareCommand
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function isEnabled()
  27. {
  28. if (!$this->getContainer()->has('router')) {
  29. return false;
  30. }
  31. $router = $this->getContainer()->get('router');
  32. if (!$router instanceof RouterInterface) {
  33. return false;
  34. }
  35. return parent::isEnabled();
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. protected function configure()
  41. {
  42. $this
  43. ->setName('router:debug')
  44. ->setDefinition(array(
  45. new InputArgument('name', InputArgument::OPTIONAL, 'A route name'),
  46. ))
  47. ->setDescription('Displays current routes for an application')
  48. ->setHelp(<<<EOF
  49. The <info>%command.name%</info> displays the configured routes:
  50. <info>php %command.full_name%</info>
  51. EOF
  52. )
  53. ;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. *
  58. * @throws \InvalidArgumentException When route does not exist
  59. */
  60. protected function execute(InputInterface $input, OutputInterface $output)
  61. {
  62. $name = $input->getArgument('name');
  63. if ($name) {
  64. $this->outputRoute($output, $name);
  65. } else {
  66. $this->outputRoutes($output);
  67. }
  68. }
  69. protected function outputRoutes(OutputInterface $output, $routes = null)
  70. {
  71. if (null === $routes) {
  72. $routes = $this->getContainer()->get('router')->getRouteCollection()->all();
  73. }
  74. $output->writeln($this->getHelper('formatter')->formatSection('router', 'Current routes'));
  75. $maxName = strlen('name');
  76. $maxMethod = strlen('method');
  77. $maxScheme = strlen('scheme');
  78. $maxHost = strlen('host');
  79. foreach ($routes as $name => $route) {
  80. $method = $route->getMethods() ? implode('|', $route->getMethods()) : 'ANY';
  81. $scheme = $route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY';
  82. $host = '' !== $route->getHost() ? $route->getHost() : 'ANY';
  83. $maxName = max($maxName, strlen($name));
  84. $maxMethod = max($maxMethod, strlen($method));
  85. $maxScheme = max($maxScheme, strlen($scheme));
  86. $maxHost = max($maxHost, strlen($host));
  87. }
  88. $format = '%-'.$maxName.'s %-'.$maxMethod.'s %-'.$maxScheme.'s %-'.$maxHost.'s %s';
  89. $formatHeader = '%-'.($maxName + 19).'s %-'.($maxMethod + 19).'s %-'.($maxScheme + 19).'s %-'.($maxHost + 19).'s %s';
  90. $output->writeln(sprintf($formatHeader, '<comment>Name</comment>', '<comment>Method</comment>', '<comment>Scheme</comment>', '<comment>Host</comment>', '<comment>Path</comment>'));
  91. foreach ($routes as $name => $route) {
  92. $method = $route->getMethods() ? implode('|', $route->getMethods()) : 'ANY';
  93. $scheme = $route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY';
  94. $host = '' !== $route->getHost() ? $route->getHost() : 'ANY';
  95. $output->writeln(sprintf($format, $name, $method, $scheme, $host, $route->getPath()), OutputInterface::OUTPUT_RAW);
  96. }
  97. }
  98. /**
  99. * @throws \InvalidArgumentException When route does not exist
  100. */
  101. protected function outputRoute(OutputInterface $output, $name)
  102. {
  103. $route = $this->getContainer()->get('router')->getRouteCollection()->get($name);
  104. if (!$route) {
  105. throw new \InvalidArgumentException(sprintf('The route "%s" does not exist.', $name));
  106. }
  107. $output->writeln($this->getHelper('formatter')->formatSection('router', sprintf('Route "%s"', $name)));
  108. $method = $route->getMethods() ? implode('|', $route->getMethods()) : 'ANY';
  109. $scheme = $route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY';
  110. $host = '' !== $route->getHost() ? $route->getHost() : 'ANY';
  111. $output->write('<comment>Name</comment> ');
  112. $output->writeln($name, OutputInterface::OUTPUT_RAW);
  113. $output->write('<comment>Path</comment> ');
  114. $output->writeln($route->getPath(), OutputInterface::OUTPUT_RAW);
  115. $output->write('<comment>Host</comment> ');
  116. $output->writeln($host, OutputInterface::OUTPUT_RAW);
  117. $output->write('<comment>Scheme</comment> ');
  118. $output->writeln($scheme, OutputInterface::OUTPUT_RAW);
  119. $output->write('<comment>Method</comment> ');
  120. $output->writeln($method, OutputInterface::OUTPUT_RAW);
  121. $output->write('<comment>Class</comment> ');
  122. $output->writeln(get_class($route), OutputInterface::OUTPUT_RAW);
  123. $output->write('<comment>Defaults</comment> ');
  124. $output->writeln($this->formatConfigs($route->getDefaults()), OutputInterface::OUTPUT_RAW);
  125. $output->write('<comment>Requirements</comment> ');
  126. // we do not want to show the schemes and methods again that are also in the requirements for BC
  127. $requirements = $route->getRequirements();
  128. unset($requirements['_scheme'], $requirements['_method']);
  129. $output->writeln($this->formatConfigs($requirements) ?: 'NO CUSTOM', OutputInterface::OUTPUT_RAW);
  130. $output->write('<comment>Options</comment> ');
  131. $output->writeln($this->formatConfigs($route->getOptions()), OutputInterface::OUTPUT_RAW);
  132. $output->write('<comment>Path-Regex</comment> ');
  133. $output->writeln($route->compile()->getRegex(), OutputInterface::OUTPUT_RAW);
  134. if (null !== $route->compile()->getHostRegex()) {
  135. $output->write('<comment>Host-Regex</comment> ');
  136. $output->writeln($route->compile()->getHostRegex(), OutputInterface::OUTPUT_RAW);
  137. }
  138. }
  139. protected function formatValue($value)
  140. {
  141. if (is_object($value)) {
  142. return sprintf('object(%s)', get_class($value));
  143. }
  144. if (is_string($value)) {
  145. return $value;
  146. }
  147. return preg_replace("/\n\s*/s", '', var_export($value, true));
  148. }
  149. private function formatConfigs(array $array)
  150. {
  151. $string = '';
  152. ksort($array);
  153. foreach ($array as $name => $value) {
  154. $string .= ($string ? "\n" . str_repeat(' ', 13) : '') . $name . ': ' . $this->formatValue($value);
  155. }
  156. return $string;
  157. }
  158. }