RouterDebugCommand.php 4.3 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\Command\Command;
  13. use Symfony\Component\Console\Exception\InvalidArgumentException;
  14. use Symfony\Component\Console\Input\InputArgument;
  15. use Symfony\Component\Console\Input\InputInterface;
  16. use Symfony\Component\Console\Input\InputOption;
  17. use Symfony\Component\Console\Output\OutputInterface;
  18. use Symfony\Component\Console\Style\SymfonyStyle;
  19. use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
  20. use Symfony\Component\Routing\RouteCollection;
  21. use Symfony\Component\Routing\RouterInterface;
  22. /**
  23. * A console command for retrieving information about routes.
  24. *
  25. * @author Fabien Potencier <fabien@symfony.com>
  26. * @author Tobias Schultze <http://tobion.de>
  27. *
  28. * @final
  29. */
  30. class RouterDebugCommand extends Command
  31. {
  32. use BuildDebugContainerTrait;
  33. protected static $defaultName = 'debug:router';
  34. private $router;
  35. private $fileLinkFormatter;
  36. public function __construct(RouterInterface $router, FileLinkFormatter $fileLinkFormatter = null)
  37. {
  38. parent::__construct();
  39. $this->router = $router;
  40. $this->fileLinkFormatter = $fileLinkFormatter;
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. protected function configure()
  46. {
  47. $this
  48. ->setDefinition([
  49. new InputArgument('name', InputArgument::OPTIONAL, 'A route name'),
  50. new InputOption('show-controllers', null, InputOption::VALUE_NONE, 'Show assigned controllers in overview'),
  51. new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt'),
  52. new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw route(s)'),
  53. ])
  54. ->setDescription('Display current routes for an application')
  55. ->setHelp(<<<'EOF'
  56. The <info>%command.name%</info> displays the configured routes:
  57. <info>php %command.full_name%</info>
  58. EOF
  59. )
  60. ;
  61. }
  62. /**
  63. * {@inheritdoc}
  64. *
  65. * @throws InvalidArgumentException When route does not exist
  66. */
  67. protected function execute(InputInterface $input, OutputInterface $output): int
  68. {
  69. $io = new SymfonyStyle($input, $output);
  70. $name = $input->getArgument('name');
  71. $helper = new DescriptorHelper($this->fileLinkFormatter);
  72. $routes = $this->router->getRouteCollection();
  73. $container = $this->fileLinkFormatter ? \Closure::fromCallable([$this, 'getContainerBuilder']) : null;
  74. if ($name) {
  75. if (!($route = $routes->get($name)) && $matchingRoutes = $this->findRouteNameContaining($name, $routes)) {
  76. $default = 1 === \count($matchingRoutes) ? $matchingRoutes[0] : null;
  77. $name = $io->choice('Select one of the matching routes', $matchingRoutes, $default);
  78. $route = $routes->get($name);
  79. }
  80. if (!$route) {
  81. throw new InvalidArgumentException(sprintf('The route "%s" does not exist.', $name));
  82. }
  83. $helper->describe($io, $route, [
  84. 'format' => $input->getOption('format'),
  85. 'raw_text' => $input->getOption('raw'),
  86. 'name' => $name,
  87. 'output' => $io,
  88. 'container' => $container,
  89. ]);
  90. } else {
  91. $helper->describe($io, $routes, [
  92. 'format' => $input->getOption('format'),
  93. 'raw_text' => $input->getOption('raw'),
  94. 'show_controllers' => $input->getOption('show-controllers'),
  95. 'output' => $io,
  96. 'container' => $container,
  97. ]);
  98. }
  99. return 0;
  100. }
  101. private function findRouteNameContaining(string $name, RouteCollection $routes): array
  102. {
  103. $foundRoutesNames = [];
  104. foreach ($routes as $routeName => $route) {
  105. if (false !== stripos($routeName, $name)) {
  106. $foundRoutesNames[] = $routeName;
  107. }
  108. }
  109. return $foundRoutesNames;
  110. }
  111. }