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

Open in your IDE?
  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.     /**
  46.      * {@inheritdoc}
  47.      */
  48.     protected function configure()
  49.     {
  50.         $this
  51.             ->setDefinition([
  52.                 new InputArgument('name'InputArgument::OPTIONAL'A route name'),
  53.                 new InputOption('show-controllers'nullInputOption::VALUE_NONE'Show assigned controllers in overview'),
  54.                 new InputOption('format'nullInputOption::VALUE_REQUIRED'The output format (txt, xml, json, or md)''txt'),
  55.                 new InputOption('raw'nullInputOption::VALUE_NONE'To output raw route(s)'),
  56.             ])
  57.             ->setHelp(<<<'EOF'
  58. The <info>%command.name%</info> displays the configured routes:
  59.   <info>php %command.full_name%</info>
  60. EOF
  61.             )
  62.         ;
  63.     }
  64.     /**
  65.      * {@inheritdoc}
  66.      *
  67.      * @throws InvalidArgumentException When route does not exist
  68.      */
  69.     protected function execute(InputInterface $inputOutputInterface $output): int
  70.     {
  71.         $io = new SymfonyStyle($input$output);
  72.         $name $input->getArgument('name');
  73.         $helper = new DescriptorHelper($this->fileLinkFormatter);
  74.         $routes $this->router->getRouteCollection();
  75.         $container null;
  76.         if ($this->fileLinkFormatter) {
  77.             $container = function () {
  78.                 return $this->getContainerBuilder($this->getApplication()->getKernel());
  79.             };
  80.         }
  81.         if ($name) {
  82.             $route $routes->get($name);
  83.             $matchingRoutes $this->findRouteNameContaining($name$routes);
  84.             if (!$input->isInteractive() && !$route && \count($matchingRoutes) > 1) {
  85.                 $helper->describe($io$this->findRouteContaining($name$routes), [
  86.                     'format' => $input->getOption('format'),
  87.                     'raw_text' => $input->getOption('raw'),
  88.                     'show_controllers' => $input->getOption('show-controllers'),
  89.                     'output' => $io,
  90.                 ]);
  91.                 return 0;
  92.             }
  93.             if (!$route && $matchingRoutes) {
  94.                 $default === \count($matchingRoutes) ? $matchingRoutes[0] : null;
  95.                 $name $io->choice('Select one of the matching routes'$matchingRoutes$default);
  96.                 $route $routes->get($name);
  97.             }
  98.             if (!$route) {
  99.                 throw new InvalidArgumentException(sprintf('The route "%s" does not exist.'$name));
  100.             }
  101.             $helper->describe($io$route, [
  102.                 'format' => $input->getOption('format'),
  103.                 'raw_text' => $input->getOption('raw'),
  104.                 'name' => $name,
  105.                 'output' => $io,
  106.                 'container' => $container,
  107.             ]);
  108.         } else {
  109.             $helper->describe($io$routes, [
  110.                 'format' => $input->getOption('format'),
  111.                 'raw_text' => $input->getOption('raw'),
  112.                 'show_controllers' => $input->getOption('show-controllers'),
  113.                 'output' => $io,
  114.                 'container' => $container,
  115.             ]);
  116.         }
  117.         return 0;
  118.     }
  119.     private function findRouteNameContaining(string $nameRouteCollection $routes): array
  120.     {
  121.         $foundRoutesNames = [];
  122.         foreach ($routes as $routeName => $route) {
  123.             if (false !== stripos($routeName$name)) {
  124.                 $foundRoutesNames[] = $routeName;
  125.             }
  126.         }
  127.         return $foundRoutesNames;
  128.     }
  129.     public function complete(CompletionInput $inputCompletionSuggestions $suggestions): void
  130.     {
  131.         if ($input->mustSuggestArgumentValuesFor('name')) {
  132.             $suggestions->suggestValues(array_keys($this->router->getRouteCollection()->all()));
  133.             return;
  134.         }
  135.         if ($input->mustSuggestOptionValuesFor('format')) {
  136.             $helper = new DescriptorHelper();
  137.             $suggestions->suggestValues($helper->getFormats());
  138.         }
  139.     }
  140.     private function findRouteContaining(string $nameRouteCollection $routes): RouteCollection
  141.     {
  142.         $foundRoutes = new RouteCollection();
  143.         foreach ($routes as $routeName => $route) {
  144.             if (false !== stripos($routeName$name)) {
  145.                 $foundRoutes->add($routeName$route);
  146.             }
  147.         }
  148.         return $foundRoutes;
  149.     }
  150. }