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;
|
|
|
|
|
|
|
|
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;
|
2013-02-02 15:51:31 +00:00
|
|
|
use Symfony\Component\Console\Input\ArrayInput;
|
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>
|
|
|
|
*/
|
|
|
|
class RouterMatchCommand extends ContainerAwareCommand
|
|
|
|
{
|
|
|
|
/**
|
2013-03-07 13:36:36 +00:00
|
|
|
* {@inheritdoc}
|
2011-10-22 09:01:37 +01:00
|
|
|
*/
|
|
|
|
public function isEnabled()
|
|
|
|
{
|
|
|
|
if (!$this->getContainer()->has('router')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$router = $this->getContainer()->get('router');
|
|
|
|
if (!$router instanceof RouterInterface) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-10-29 11:05:45 +01:00
|
|
|
|
2011-10-22 09:01:37 +01:00
|
|
|
return parent::isEnabled();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-03-07 13:36:36 +00:00
|
|
|
* {@inheritdoc}
|
2011-10-22 09:01:37 +01:00
|
|
|
*/
|
|
|
|
protected function configure()
|
|
|
|
{
|
|
|
|
$this
|
2012-02-12 15:37:55 +00:00
|
|
|
->setName('router:match')
|
2011-10-22 09:01:37 +01:00
|
|
|
->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')
|
|
|
|
->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-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)
|
|
|
|
{
|
|
|
|
$router = $this->getContainer()->get('router');
|
2013-10-18 19:18:27 +01:00
|
|
|
$context = $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
|
|
|
|
|
|
|
$matcher = new TraceableUrlMatcher($router->getRouteCollection(), $context);
|
2011-10-22 09:01:37 +01:00
|
|
|
|
|
|
|
$traces = $matcher->getTraces($input->getArgument('path_info'));
|
|
|
|
|
|
|
|
$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']) {
|
|
|
|
$output->writeln(sprintf('<fg=yellow>Route "%s" almost matches but %s</>', $trace['name'], lcfirst($trace['log'])));
|
|
|
|
} elseif (TraceableUrlMatcher::ROUTE_MATCHES == $trace['level']) {
|
|
|
|
$output->writeln(sprintf('<fg=green>Route "%s" matches</>', $trace['name']));
|
2013-02-02 15:51:31 +00:00
|
|
|
|
2014-12-01 14:24:26 +00:00
|
|
|
$routerDebugcommand = $this->getApplication()->find('debug:router');
|
2013-02-02 15:51:31 +00:00
|
|
|
$output->writeln('');
|
|
|
|
$routerDebugcommand->run(new ArrayInput(array('name' => $trace['name'])), $output);
|
|
|
|
|
2011-10-22 09:01:37 +01:00
|
|
|
$matches = true;
|
|
|
|
} elseif ($input->getOption('verbose')) {
|
|
|
|
$output->writeln(sprintf('Route "%s" does not match: %s', $trace['name'], $trace['log']));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$matches) {
|
2014-09-24 23:57:27 +01:00
|
|
|
$output->writeln(sprintf('<fg=red>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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|