RouterDebugCommand.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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\InputOption;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Symfony\Component\Console\Output\Output;
  16. use Symfony\Component\Routing\Matcher\Dumper\ApacheMatcherDumper;
  17. /**
  18. * A console command for retrieving information about routes
  19. *
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. */
  22. class RouterDebugCommand extends ContainerAwareCommand
  23. {
  24. /**
  25. * @see Command
  26. */
  27. protected function configure()
  28. {
  29. $this
  30. ->setDefinition(array(
  31. new InputArgument('name', InputArgument::OPTIONAL, 'A route name'),
  32. ))
  33. ->setName('router:debug')
  34. ->setDescription('Displays current routes for an application')
  35. ->setHelp(<<<EOF
  36. The <info>router:debug</info> displays the configured routes:
  37. <info>router:debug</info>
  38. EOF
  39. )
  40. ;
  41. }
  42. /**
  43. * @see Command
  44. */
  45. protected function execute(InputInterface $input, OutputInterface $output)
  46. {
  47. $router = $this->getContainer()->get('router');
  48. $routes = array();
  49. foreach ($router->getRouteCollection()->all() as $name => $route) {
  50. $routes[$name] = $route->compile();
  51. }
  52. if ($input->getArgument('name')) {
  53. $this->outputRoute($output, $routes, $input->getArgument('name'));
  54. } else {
  55. $this->outputRoutes($output, $routes);
  56. }
  57. }
  58. protected function outputRoutes(OutputInterface $output, $routes)
  59. {
  60. $output->writeln($this->getHelper('formatter')->formatSection('router', 'Current routes'));
  61. $maxName = 4;
  62. $maxMethod = 6;
  63. foreach ($routes as $name => $route) {
  64. $requirements = $route->getRequirements();
  65. $method = isset($requirements['_method']) ? strtoupper(is_array($requirements['_method']) ? implode(', ', $requirements['_method']) : $requirements['_method']) : 'ANY';
  66. if (strlen($name) > $maxName) {
  67. $maxName = strlen($name);
  68. }
  69. if (strlen($method) > $maxMethod) {
  70. $maxMethod = strlen($method);
  71. }
  72. }
  73. $format = '%-'.$maxName.'s %-'.$maxMethod.'s %s';
  74. // displays the generated routes
  75. $format1 = '%-'.($maxName + 19).'s %-'.($maxMethod + 19).'s %s';
  76. $output->writeln(sprintf($format1, '<comment>Name</comment>', '<comment>Method</comment>', '<comment>Pattern</comment>'));
  77. foreach ($routes as $name => $route) {
  78. $requirements = $route->getRequirements();
  79. $method = isset($requirements['_method']) ? strtoupper(is_array($requirements['_method']) ? implode(', ', $requirements['_method']) : $requirements['_method']) : 'ANY';
  80. $output->writeln(sprintf($format, $name, $method, $route->getPattern()));
  81. }
  82. }
  83. /**
  84. * @throws \InvalidArgumentException When route does not exist
  85. */
  86. protected function outputRoute(OutputInterface $output, $routes, $name)
  87. {
  88. $output->writeln($this->getHelper('formatter')->formatSection('router', sprintf('Route "%s"', $name)));
  89. if (!isset($routes[$name])) {
  90. throw new \InvalidArgumentException(sprintf('The route "%s" does not exist.', $name));
  91. }
  92. $route = $routes[$name];
  93. $output->writeln(sprintf('<comment>Name</comment> %s', $name));
  94. $output->writeln(sprintf('<comment>Pattern</comment> %s', $route->getPattern()));
  95. $output->writeln(sprintf('<comment>Class</comment> %s', get_class($route)));
  96. $defaults = '';
  97. $d = $route->getDefaults();
  98. ksort($d);
  99. foreach ($d as $name => $value) {
  100. $defaults .= ($defaults ? "\n".str_repeat(' ', 13) : '').$name.': '.$this->formatValue($value);
  101. }
  102. $output->writeln(sprintf('<comment>Defaults</comment> %s', $defaults));
  103. $requirements = '';
  104. $r = $route->getRequirements();
  105. ksort($r);
  106. foreach ($r as $name => $value) {
  107. $requirements .= ($requirements ? "\n".str_repeat(' ', 13) : '').$name.': '.$this->formatValue($value);
  108. }
  109. $output->writeln(sprintf('<comment>Requirements</comment> %s', $requirements));
  110. $options = '';
  111. $o = $route->getOptions();
  112. ksort($o);
  113. foreach ($o as $name => $value) {
  114. $options .= ($options ? "\n".str_repeat(' ', 13) : '').$name.': '.$this->formatValue($value);
  115. }
  116. $output->writeln(sprintf('<comment>Options</comment> %s', $options));
  117. $output->write('<comment>Regex</comment> ');
  118. $output->writeln(preg_replace('/^ /', '', preg_replace('/^/m', ' ', $route->getRegex())), OutputInterface::OUTPUT_RAW);
  119. }
  120. protected function formatValue($value)
  121. {
  122. if (is_object($value)) {
  123. return sprintf('object(%s)', get_class($value));
  124. }
  125. if (is_string($value)) {
  126. return $value;
  127. }
  128. return preg_replace("/\n\s*/s", '', var_export($value, true));
  129. }
  130. }