Created
June 13, 2013 12:20
-
-
Save K-Phoen/5773250 to your computer and use it in GitHub Desktop.
Little improvement to the Symfony router:match command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* This file is part of the Symfony package. | |
* | |
* (c) Fabien Potencier <[email protected]> | |
* | |
* 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; | |
use Symfony\Component\Routing\Route; | |
use Symfony\Component\Routing\RouterInterface; | |
use Symfony\Component\Routing\Matcher\TraceableUrlMatcher; | |
/** | |
* A console command to test route matching. | |
* | |
* @author Fabien Potencier <[email protected]> | |
*/ | |
class RouterMatchCommand extends ContainerAwareCommand | |
{ | |
/** | |
* {@inheritDoc} | |
*/ | |
public function isEnabled() | |
{ | |
if (!$this->getContainer()->has('router')) { | |
return false; | |
} | |
$router = $this->getContainer()->get('router'); | |
if (!$router instanceof RouterInterface) { | |
return false; | |
} | |
return parent::isEnabled(); | |
} | |
/** | |
* @see Command | |
*/ | |
protected function configure() | |
{ | |
$this | |
->setName('router:match') | |
->setDefinition(array( | |
new InputArgument('path_info', InputArgument::REQUIRED, 'A path info'), | |
)) | |
->setDescription('Helps debug routes by simulating a path info match') | |
->setHelp(<<<EOF | |
The <info>%command.name%</info> simulates a path info match: | |
<info>php %command.full_name% /foo</info> | |
EOF | |
) | |
; | |
} | |
/** | |
* @see Command | |
*/ | |
protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$router = $this->getContainer()->get('router'); | |
$routes = $router->getRouteCollection(); | |
$matcher = new TraceableUrlMatcher($routes, $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'])); | |
$matches = true; | |
$this->printRouteDetails($routes->get($trace['name']), $output); | |
} 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</>'); | |
return 1; | |
} | |
} | |
protected function printRouteDetails(Route $route, OutputInterface $output) | |
{ | |
$output->writeln('Pattern: ' . $route->getPattern()); | |
$this->printList('Defaults', $route->getDefaults(), $output); | |
$this->printList('Requirements', $route->getRequirements(), $output); | |
$this->printList('Options', $route->getOptions(), $output); | |
} | |
protected function printList($title, $list, OutputInterface $output) | |
{ | |
$output->writeln($title . ': '); | |
foreach ($list as $key => $value) { | |
$output->writeln(sprintf(' %s: %s', $key, $value)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment