2011-10-22 09:01:37 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Bundle\FrameworkBundle\Command;
|
|
|
|
|
2017-08-07 11:35:57 +01:00
|
|
|
use Symfony\Component\Console\Command\Command;
|
2015-03-31 13:42:44 +01:00
|
|
|
use Symfony\Component\Console\Input\ArrayInput;
|
2011-10-22 09:01:37 +01:00
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
2013-10-18 19:18:27 +01:00
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
2011-10-22 09:01:37 +01:00
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
2015-03-31 13:42:44 +01:00
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
2011-10-22 09:01:37 +01:00
|
|
|
use Symfony\Component\Routing\RouterInterface;
|
|
|
|
use Symfony\Component\Routing\Matcher\TraceableUrlMatcher;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A console command to test route matching.
|
|
|
|
*
|
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2017-07-22 10:58:19 +01:00
|
|
|
*
|
2018-02-19 12:18:43 +00:00
|
|
|
* @final
|
2011-10-22 09:01:37 +01:00
|
|
|
*/
|
2017-08-07 11:35:57 +01:00
|
|
|
class RouterMatchCommand extends Command
|
2011-10-22 09:01:37 +01:00
|
|
|
{
|
2017-08-21 09:40:46 +01:00
|
|
|
protected static $defaultName = 'router:match';
|
|
|
|
|
2017-07-22 10:58:19 +01:00
|
|
|
private $router;
|
|
|
|
|
2017-08-07 11:35:57 +01:00
|
|
|
public function __construct(RouterInterface $router)
|
2017-07-22 10:58:19 +01:00
|
|
|
{
|
2017-08-06 11:59:30 +01:00
|
|
|
parent::__construct();
|
|
|
|
|
2017-07-22 10:58:19 +01:00
|
|
|
$this->router = $router;
|
|
|
|
}
|
|
|
|
|
2011-10-22 09:01:37 +01:00
|
|
|
/**
|
2013-03-07 13:36:36 +00:00
|
|
|
* {@inheritdoc}
|
2011-10-22 09:01:37 +01:00
|
|
|
*/
|
|
|
|
protected function configure()
|
|
|
|
{
|
|
|
|
$this
|
|
|
|
->setDefinition(array(
|
|
|
|
new InputArgument('path_info', InputArgument::REQUIRED, 'A path info'),
|
2014-03-13 15:16:35 +00:00
|
|
|
new InputOption('method', null, InputOption::VALUE_REQUIRED, 'Sets the HTTP method'),
|
2014-03-21 16:46:08 +00:00
|
|
|
new InputOption('scheme', null, InputOption::VALUE_REQUIRED, 'Sets the URI scheme (usually http or https)'),
|
|
|
|
new InputOption('host', null, InputOption::VALUE_REQUIRED, 'Sets the URI host'),
|
2011-10-22 09:01:37 +01:00
|
|
|
))
|
|
|
|
->setDescription('Helps debug routes by simulating a path info match')
|
2015-12-21 11:01:57 +00:00
|
|
|
->setHelp(<<<'EOF'
|
2014-03-21 16:46:08 +00:00
|
|
|
The <info>%command.name%</info> shows which routes match a given request and which don't and for what reason:
|
2011-10-22 09:01:37 +01:00
|
|
|
|
2012-02-12 15:37:55 +00:00
|
|
|
<info>php %command.full_name% /foo</info>
|
2015-03-31 13:42:44 +01:00
|
|
|
|
2015-01-04 09:52:37 +00:00
|
|
|
or
|
|
|
|
|
2014-03-21 16:46:08 +00:00
|
|
|
<info>php %command.full_name% /foo --method POST --scheme https --host symfony.com --verbose</info>
|
2014-03-13 04:05:55 +00:00
|
|
|
|
2011-10-22 09:01:37 +01:00
|
|
|
EOF
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-03-07 13:36:36 +00:00
|
|
|
* {@inheritdoc}
|
2011-10-22 09:01:37 +01:00
|
|
|
*/
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
|
{
|
2015-12-07 12:11:35 +00:00
|
|
|
$io = new SymfonyStyle($input, $output);
|
2015-03-31 13:42:44 +01:00
|
|
|
|
2017-07-22 10:58:19 +01:00
|
|
|
$context = $this->router->getContext();
|
2014-03-13 15:16:35 +00:00
|
|
|
if (null !== $method = $input->getOption('method')) {
|
|
|
|
$context->setMethod($method);
|
|
|
|
}
|
2014-03-21 16:46:08 +00:00
|
|
|
if (null !== $scheme = $input->getOption('scheme')) {
|
|
|
|
$context->setScheme($scheme);
|
|
|
|
}
|
2014-03-13 15:16:35 +00:00
|
|
|
if (null !== $host = $input->getOption('host')) {
|
|
|
|
$context->setHost($host);
|
|
|
|
}
|
2013-10-18 19:18:27 +01:00
|
|
|
|
2017-07-22 10:58:19 +01:00
|
|
|
$matcher = new TraceableUrlMatcher($this->router->getRouteCollection(), $context);
|
2011-10-22 09:01:37 +01:00
|
|
|
|
|
|
|
$traces = $matcher->getTraces($input->getArgument('path_info'));
|
|
|
|
|
2015-12-07 12:11:35 +00:00
|
|
|
$io->newLine();
|
2015-03-31 13:42:44 +01:00
|
|
|
|
2011-10-22 09:01:37 +01:00
|
|
|
$matches = false;
|
2014-03-13 04:05:55 +00:00
|
|
|
foreach ($traces as $trace) {
|
2011-10-22 09:01:37 +01:00
|
|
|
if (TraceableUrlMatcher::ROUTE_ALMOST_MATCHES == $trace['level']) {
|
2015-12-07 12:11:35 +00:00
|
|
|
$io->text(sprintf('Route <info>"%s"</> almost matches but %s', $trace['name'], lcfirst($trace['log'])));
|
2011-10-22 09:01:37 +01:00
|
|
|
} elseif (TraceableUrlMatcher::ROUTE_MATCHES == $trace['level']) {
|
2015-12-07 12:11:35 +00:00
|
|
|
$io->success(sprintf('Route "%s" matches', $trace['name']));
|
2013-02-02 15:51:31 +00:00
|
|
|
|
2015-03-31 13:42:44 +01:00
|
|
|
$routerDebugCommand = $this->getApplication()->find('debug:router');
|
|
|
|
$routerDebugCommand->run(new ArrayInput(array('name' => $trace['name'])), $output);
|
2013-02-02 15:51:31 +00:00
|
|
|
|
2011-10-22 09:01:37 +01:00
|
|
|
$matches = true;
|
|
|
|
} elseif ($input->getOption('verbose')) {
|
2015-12-07 12:11:35 +00:00
|
|
|
$io->text(sprintf('Route "%s" does not match: %s', $trace['name'], $trace['log']));
|
2011-10-22 09:01:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$matches) {
|
2015-12-07 12:11:35 +00:00
|
|
|
$io->error(sprintf('None of the routes match the path "%s"', $input->getArgument('path_info')));
|
2012-09-24 09:52:21 +01:00
|
|
|
|
|
|
|
return 1;
|
2011-10-22 09:01:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|