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\Command\Command;
  13. use Symfony\Component\Console\Completion\CompletionInput;
  14. use Symfony\Component\Console\Completion\CompletionSuggestions;
  15. use Symfony\Component\Console\Exception\InvalidArgumentException;
  16. use Symfony\Component\Console\Input\InputArgument;
  17. use Symfony\Component\Console\Input\InputInterface;
  18. use Symfony\Component\Console\Input\InputOption;
  19. use Symfony\Component\Console\Output\OutputInterface;
  20. use Symfony\Component\Console\Style\SymfonyStyle;
  21. use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
  22. use Symfony\Component\Routing\RouteCollection;
  23. use Symfony\Component\Routing\RouterInterface;
  24. /**
  25.  * A console command for retrieving information about routes.
  26.  *
  27.  * @author Fabien Potencier <fabien@symfony.com>
  28.  * @author Tobias Schultze <http://tobion.de>
  29.  *
  30.  * @final
  31.  */
  32. class RouterDebugCommand extends Command
  33. {
  34.     use BuildDebugContainerTrait;
  35.     protected static $defaultName 'debug:router';
  36.     protected static $defaultDescription 'Display current routes for an application';
  37.     private $router;
  38.     private $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.             ->setDescription(self::$defaultDescription)
  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 $inputOutputInterface $output): int
  71.     {
  72.         $io = new SymfonyStyle($input$output);
  73.         $name $input->getArgument('name');
  74.         $helper = new DescriptorHelper($this->fileLinkFormatter);
  75.         $routes $this->router->getRouteCollection();
  76.         $container null;
  77.         if ($this->fileLinkFormatter) {
  78.             $container = function () {
  79.                 return $this->getContainerBuilder($this->getApplication()->getKernel());
  80.             };
  81.         }
  82.         if ($name) {
  83.             $route $routes->get($name);
  84.             $matchingRoutes $this->findRouteNameContaining($name$routes);
  85.             if (!$input->isInteractive() && !$route && \count($matchingRoutes) > 1) {
  86.                 $helper->describe($io$this->findRouteContaining($name$routes), [
  87.                     'format' => $input->getOption('format'),
  88.                     'raw_text' => $input->getOption('raw'),
  89.                     'show_controllers' => $input->getOption('show-controllers'),
  90.                     'output' => $io,
  91.                 ]);
  92.                 return 0;
  93.             }
  94.             if (!$route && $matchingRoutes) {
  95.                 $default === \count($matchingRoutes) ? $matchingRoutes[0] : null;
  96.                 $name $io->choice('Select one of the matching routes'$matchingRoutes$default);
  97.                 $route $routes->get($name);
  98.             }
  99.             if (!$route) {
  100.                 throw new InvalidArgumentException(sprintf('The route "%s" does not exist.'$name));
  101.             }
  102.             $helper->describe($io$route, [
  103.                 'format' => $input->getOption('format'),
  104.                 'raw_text' => $input->getOption('raw'),
  105.                 'name' => $name,
  106.                 'output' => $io,
  107.                 'container' => $container,
  108.             ]);
  109.         } else {
  110.             $helper->describe($io$routes, [
  111.                 'format' => $input->getOption('format'),
  112.                 'raw_text' => $input->getOption('raw'),
  113.                 'show_controllers' => $input->getOption('show-controllers'),
  114.                 'output' => $io,
  115.                 'container' => $container,
  116.             ]);
  117.         }
  118.         return 0;
  119.     }
  120.     private function findRouteNameContaining(string $nameRouteCollection $routes): array
  121.     {
  122.         $foundRoutesNames = [];
  123.         foreach ($routes as $routeName => $route) {
  124.             if (false !== stripos($routeName$name)) {
  125.                 $foundRoutesNames[] = $routeName;
  126.             }
  127.         }
  128.         return $foundRoutesNames;
  129.     }
  130.     public function complete(CompletionInput $inputCompletionSuggestions $suggestions): void
  131.     {
  132.         if ($input->mustSuggestArgumentValuesFor('name')) {
  133.             $suggestions->suggestValues(array_keys($this->router->getRouteCollection()->all()));
  134.             return;
  135.         }
  136.         if ($input->mustSuggestOptionValuesFor('format')) {
  137.             $helper = new DescriptorHelper();
  138.             $suggestions->suggestValues($helper->getFormats());
  139.         }
  140.     }
  141.     private function findRouteContaining(string $nameRouteCollection $routes): RouteCollection
  142.     {
  143.         $foundRoutes = new RouteCollection();
  144.         foreach ($routes as $routeName => $route) {
  145.             if (false !== stripos($routeName$name)) {
  146.                 $foundRoutes->add($routeName$route);
  147.             }
  148.         }
  149.         return $foundRoutes;
  150.     }
  151. }