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;
|
|
|
|
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'),
|
|
|
|
))
|
|
|
|
->setDescription('Helps debug routes by simulating a path info match')
|
|
|
|
->setHelp(<<<EOF
|
2012-02-12 15:37:55 +00:00
|
|
|
The <info>%command.name%</info> simulates a path info match:
|
2011-10-22 09:01:37 +01:00
|
|
|
|
2012-02-12 15:37:55 +00:00
|
|
|
<info>php %command.full_name% /foo</info>
|
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');
|
|
|
|
$matcher = new TraceableUrlMatcher($router->getRouteCollection(), $router->getContext());
|
|
|
|
|
|
|
|
$traces = $matcher->getTraces($input->getArgument('path_info'));
|
|
|
|
|
|
|
|
$matches = false;
|
|
|
|
foreach ($traces as $i => $trace) {
|
|
|
|
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
|
|
|
|
|
|
|
$routerDebugcommand = $this->getApplication()->find('router:debug');
|
|
|
|
$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) {
|
|
|
|
$output->writeln('<fg=red>None of the routes matches</>');
|
2012-09-24 09:52:21 +01:00
|
|
|
|
|
|
|
return 1;
|
2011-10-22 09:01:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|