RouterMatchCommand.php 3.9 KB

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