RouterDebugCommand.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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\Bundle\FrameworkBundle\Console\Helper\DescriptorHelper;
  12. use Symfony\Component\Console\Input\InputArgument;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Input\InputOption;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. use Symfony\Component\Console\Style\SymfonyStyle;
  17. use Symfony\Component\Routing\Route;
  18. use Symfony\Component\Routing\RouterInterface;
  19. /**
  20. * A console command for retrieving information about routes.
  21. *
  22. * @author Fabien Potencier <fabien@symfony.com>
  23. * @author Tobias Schultze <http://tobion.de>
  24. */
  25. class RouterDebugCommand extends ContainerAwareCommand
  26. {
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function isEnabled()
  31. {
  32. if (!$this->getContainer()->has('router')) {
  33. return false;
  34. }
  35. $router = $this->getContainer()->get('router');
  36. if (!$router instanceof RouterInterface) {
  37. return false;
  38. }
  39. return parent::isEnabled();
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. protected function configure()
  45. {
  46. $this
  47. ->setName('debug:router')
  48. ->setAliases(array(
  49. 'router:debug',
  50. ))
  51. ->setDefinition(array(
  52. new InputArgument('name', InputArgument::OPTIONAL, 'A route name'),
  53. new InputOption('show-controllers', null, InputOption::VALUE_NONE, 'Show assigned controllers in overview'),
  54. new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt'),
  55. new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw route(s)'),
  56. ))
  57. ->setDescription('Displays current routes for an application')
  58. ->setHelp(<<<'EOF'
  59. The <info>%command.name%</info> displays the configured routes:
  60. <info>php %command.full_name%</info>
  61. EOF
  62. )
  63. ;
  64. }
  65. /**
  66. * {@inheritdoc}
  67. *
  68. * @throws \InvalidArgumentException When route does not exist
  69. */
  70. protected function execute(InputInterface $input, OutputInterface $output)
  71. {
  72. $io = new SymfonyStyle($input, $output);
  73. if (false !== strpos($input->getFirstArgument(), ':d')) {
  74. $io->caution('The use of "router:debug" command is deprecated since version 2.7 and will be removed in 3.0. Use the "debug:router" instead.');
  75. }
  76. $name = $input->getArgument('name');
  77. $helper = new DescriptorHelper();
  78. $routes = $this->getContainer()->get('router')->getRouteCollection();
  79. if ($name) {
  80. if (!$route = $routes->get($name)) {
  81. throw new \InvalidArgumentException(sprintf('The route "%s" does not exist.', $name));
  82. }
  83. $this->convertController($route);
  84. $helper->describe($io, $route, array(
  85. 'format' => $input->getOption('format'),
  86. 'raw_text' => $input->getOption('raw'),
  87. 'name' => $name,
  88. 'output' => $io,
  89. ));
  90. } else {
  91. foreach ($routes as $route) {
  92. $this->convertController($route);
  93. }
  94. $helper->describe($io, $routes, array(
  95. 'format' => $input->getOption('format'),
  96. 'raw_text' => $input->getOption('raw'),
  97. 'show_controllers' => $input->getOption('show-controllers'),
  98. 'output' => $io,
  99. ));
  100. }
  101. }
  102. private function convertController(Route $route)
  103. {
  104. $nameParser = $this->getContainer()->get('controller_name_converter');
  105. if ($route->hasDefault('_controller')) {
  106. try {
  107. $route->setDefault('_controller', $nameParser->build($route->getDefault('_controller')));
  108. } catch (\InvalidArgumentException $e) {
  109. }
  110. }
  111. }
  112. }