* * 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\Console\Input\ArrayInput; use Symfony\Component\Routing\RouterInterface; use Symfony\Component\Routing\Matcher\TraceableUrlMatcher; /** * A console command to test route matching. * * @author Fabien Potencier */ 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(); } /** * {@inheritdoc} */ 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(<<%command.name% simulates a path info match: php %command.full_name% /foo EOF ) ; } /** * {@inheritdoc} */ 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('Route "%s" almost matches but %s', $trace['name'], lcfirst($trace['log']))); } elseif (TraceableUrlMatcher::ROUTE_MATCHES == $trace['level']) { $output->writeln(sprintf('Route "%s" matches', $trace['name'])); $routerDebugcommand = $this->getApplication()->find('router:debug'); $output->writeln(''); $routerDebugcommand->run(new ArrayInput(array('name' => $trace['name'])), $output); $matches = true; } elseif ($input->getOption('verbose')) { $output->writeln(sprintf('Route "%s" does not match: %s', $trace['name'], $trace['log'])); } } if (!$matches) { $output->writeln('None of the routes matches'); return 1; } } }