vendor/symfony/framework-bundle/Command/RouterDebugCommand.php line 45

  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\Attribute\AsCommand;
  13. use Symfony\Component\Console\Command\Command;
  14. use Symfony\Component\Console\Completion\CompletionInput;
  15. use Symfony\Component\Console\Completion\CompletionSuggestions;
  16. use Symfony\Component\Console\Exception\InvalidArgumentException;
  17. use Symfony\Component\Console\Input\InputArgument;
  18. use Symfony\Component\Console\Input\InputInterface;
  19. use Symfony\Component\Console\Input\InputOption;
  20. use Symfony\Component\Console\Output\OutputInterface;
  21. use Symfony\Component\Console\Style\SymfonyStyle;
  22. use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
  23. use Symfony\Component\Routing\RouteCollection;
  24. use Symfony\Component\Routing\RouterInterface;
  25. /**
  26.  * A console command for retrieving information about routes.
  27.  *
  28.  * @author Fabien Potencier <fabien@symfony.com>
  29.  * @author Tobias Schultze <http://tobion.de>
  30.  *
  31.  * @final
  32.  */
  33. #[AsCommand(name'debug:router'description'Display current routes for an application')]
  34. class RouterDebugCommand extends Command
  35. {
  36.     use BuildDebugContainerTrait;
  37.     private RouterInterface $router;
  38.     private ?FileLinkFormatter $fileLinkFormatter;
  39.     public function __construct(RouterInterface $routerFileLinkFormatter $fileLinkFormatter null)
  40.     {
  41.         parent::__construct();
  42.         $this->router $router;
  43.         $this->fileLinkFormatter $fileLinkFormatter;
  44.     }
  45.     protected function configure()
  46.     {
  47.         $this
  48.             ->setDefinition([
  49.                 new InputArgument('name'InputArgument::OPTIONAL'A route name'),
  50.                 new InputOption('show-controllers'nullInputOption::VALUE_NONE'Show assigned controllers in overview'),
  51.                 new InputOption('format'nullInputOption::VALUE_REQUIRED'The output format (txt, xml, json, or md)''txt'),
  52.                 new InputOption('raw'nullInputOption::VALUE_NONE'To output raw route(s)'),
  53.             ])
  54.             ->setHelp(<<<'EOF'
  55. The <info>%command.name%</info> displays the configured routes:
  56.   <info>php %command.full_name%</info>
  57. EOF
  58.             )
  59.         ;
  60.     }
  61.     /**
  62.      * @throws InvalidArgumentException When route does not exist
  63.      */
  64.     protected function execute(InputInterface $inputOutputInterface $output): int
  65.     {
  66.         $io = new SymfonyStyle($input$output);
  67.         $name $input->getArgument('name');
  68.         $helper = new DescriptorHelper($this->fileLinkFormatter);
  69.         $routes $this->router->getRouteCollection();
  70.         $container null;
  71.         if ($this->fileLinkFormatter) {
  72.             $container = function () {
  73.                 return $this->getContainerBuilder($this->getApplication()->getKernel());
  74.             };
  75.         }
  76.         if ($name) {
  77.             $route $routes->get($name);
  78.             $matchingRoutes $this->findRouteNameContaining($name$routes);
  79.             if (!$input->isInteractive() && !$route && \count($matchingRoutes) > 1) {
  80.                 $helper->describe($io$this->findRouteContaining($name$routes), [
  81.                     'format' => $input->getOption('format'),
  82.                     'raw_text' => $input->getOption('raw'),
  83.                     'show_controllers' => $input->getOption('show-controllers'),
  84.                     'output' => $io,
  85.                 ]);
  86.                 return 0;
  87.             }
  88.             if (!$route && $matchingRoutes) {
  89.                 $default === \count($matchingRoutes) ? $matchingRoutes[0] : null;
  90.                 $name $io->choice('Select one of the matching routes'$matchingRoutes$default);
  91.                 $route $routes->get($name);
  92.             }
  93.             if (!$route) {
  94.                 throw new InvalidArgumentException(sprintf('The route "%s" does not exist.'$name));
  95.             }
  96.             $helper->describe($io$route, [
  97.                 'format' => $input->getOption('format'),
  98.                 'raw_text' => $input->getOption('raw'),
  99.                 'name' => $name,
  100.                 'output' => $io,
  101.                 'container' => $container,
  102.             ]);
  103.         } else {
  104.             $helper->describe($io$routes, [
  105.                 'format' => $input->getOption('format'),
  106.                 'raw_text' => $input->getOption('raw'),
  107.                 'show_controllers' => $input->getOption('show-controllers'),
  108.                 'output' => $io,
  109.                 'container' => $container,
  110.             ]);
  111.         }
  112.         return 0;
  113.     }
  114.     private function findRouteNameContaining(string $nameRouteCollection $routes): array
  115.     {
  116.         $foundRoutesNames = [];
  117.         foreach ($routes as $routeName => $route) {
  118.             if (false !== stripos($routeName$name)) {
  119.                 $foundRoutesNames[] = $routeName;
  120.             }
  121.         }
  122.         return $foundRoutesNames;
  123.     }
  124.     public function complete(CompletionInput $inputCompletionSuggestions $suggestions): void
  125.     {
  126.         if ($input->mustSuggestArgumentValuesFor('name')) {
  127.             $suggestions->suggestValues(array_keys($this->router->getRouteCollection()->all()));
  128.             return;
  129.         }
  130.         if ($input->mustSuggestOptionValuesFor('format')) {
  131.             $helper = new DescriptorHelper();
  132.             $suggestions->suggestValues($helper->getFormats());
  133.         }
  134.     }
  135.     private function findRouteContaining(string $nameRouteCollection $routes): RouteCollection
  136.     {
  137.         $foundRoutes = new RouteCollection();
  138.         foreach ($routes as $routeName => $route) {
  139.             if (false !== stripos($routeName$name)) {
  140.                 $foundRoutes->add($routeName$route);
  141.             }
  142.         }
  143.         return $foundRoutes;
  144.     }
  145. }