RouterMatchCommand.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\Component\Console\Command\Command;
  12. use Symfony\Component\Console\Input\ArrayInput;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Input\InputOption;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. use Symfony\Component\Console\Style\SymfonyStyle;
  18. use Symfony\Component\Routing\Matcher\TraceableUrlMatcher;
  19. use Symfony\Component\Routing\RouterInterface;
  20. /**
  21. * A console command to test route matching.
  22. *
  23. * @author Fabien Potencier <fabien@symfony.com>
  24. *
  25. * @final
  26. */
  27. class RouterMatchCommand extends Command
  28. {
  29. protected static $defaultName = 'router:match';
  30. private $router;
  31. private $expressionLanguageProviders;
  32. public function __construct(RouterInterface $router, iterable $expressionLanguageProviders = [])
  33. {
  34. parent::__construct();
  35. $this->router = $router;
  36. $this->expressionLanguageProviders = $expressionLanguageProviders;
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. protected function configure()
  42. {
  43. $this
  44. ->setDefinition([
  45. new InputArgument('path_info', InputArgument::REQUIRED, 'A path info'),
  46. new InputOption('method', null, InputOption::VALUE_REQUIRED, 'Set the HTTP method'),
  47. new InputOption('scheme', null, InputOption::VALUE_REQUIRED, 'Set the URI scheme (usually http or https)'),
  48. new InputOption('host', null, InputOption::VALUE_REQUIRED, 'Set the URI host'),
  49. ])
  50. ->setDescription('Help debug routes by simulating a path info match')
  51. ->setHelp(<<<'EOF'
  52. The <info>%command.name%</info> shows which routes match a given request and which don't and for what reason:
  53. <info>php %command.full_name% /foo</info>
  54. or
  55. <info>php %command.full_name% /foo --method POST --scheme https --host symfony.com --verbose</info>
  56. EOF
  57. )
  58. ;
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. protected function execute(InputInterface $input, OutputInterface $output): int
  64. {
  65. $io = new SymfonyStyle($input, $output);
  66. $context = $this->router->getContext();
  67. if (null !== $method = $input->getOption('method')) {
  68. $context->setMethod($method);
  69. }
  70. if (null !== $scheme = $input->getOption('scheme')) {
  71. $context->setScheme($scheme);
  72. }
  73. if (null !== $host = $input->getOption('host')) {
  74. $context->setHost($host);
  75. }
  76. $matcher = new TraceableUrlMatcher($this->router->getRouteCollection(), $context);
  77. foreach ($this->expressionLanguageProviders as $provider) {
  78. $matcher->addExpressionLanguageProvider($provider);
  79. }
  80. $traces = $matcher->getTraces($input->getArgument('path_info'));
  81. $io->newLine();
  82. $matches = false;
  83. foreach ($traces as $trace) {
  84. if (TraceableUrlMatcher::ROUTE_ALMOST_MATCHES == $trace['level']) {
  85. $io->text(sprintf('Route <info>"%s"</> almost matches but %s', $trace['name'], lcfirst($trace['log'])));
  86. } elseif (TraceableUrlMatcher::ROUTE_MATCHES == $trace['level']) {
  87. $io->success(sprintf('Route "%s" matches', $trace['name']));
  88. $routerDebugCommand = $this->getApplication()->find('debug:router');
  89. $routerDebugCommand->run(new ArrayInput(['name' => $trace['name']]), $output);
  90. $matches = true;
  91. } elseif ($input->getOption('verbose')) {
  92. $io->text(sprintf('Route "%s" does not match: %s', $trace['name'], $trace['log']));
  93. }
  94. }
  95. if (!$matches) {
  96. $io->error(sprintf('None of the routes match the path "%s"', $input->getArgument('path_info')));
  97. return 1;
  98. }
  99. return 0;
  100. }
  101. }