2010-02-17 13:55:05 +00:00
|
|
|
<?php
|
|
|
|
|
2010-07-09 09:05:26 +01:00
|
|
|
namespace Symfony\Bundle\FrameworkBundle\Command;
|
2010-02-17 13:55:05 +00:00
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
use Symfony\Component\Console\Output\Output;
|
|
|
|
use Symfony\Component\Routing\Matcher\Dumper\ApacheMatcherDumper;
|
2010-02-17 13:55:05 +00:00
|
|
|
|
|
|
|
/*
|
2010-04-07 01:51:29 +01:00
|
|
|
* This file is part of the Symfony framework.
|
2010-02-17 13:55:05 +00:00
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
|
|
|
* This source file is subject to the MIT license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-07 01:51:29 +01:00
|
|
|
* RouterDebugCommand.
|
2010-02-17 13:55:05 +00:00
|
|
|
*
|
2010-10-17 12:45:15 +01:00
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
2010-02-17 13:55:05 +00:00
|
|
|
*/
|
|
|
|
class RouterDebugCommand extends Command
|
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* @see Command
|
|
|
|
*/
|
|
|
|
protected function configure()
|
2010-02-17 13:55:05 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$this
|
|
|
|
->setDefinition(array(
|
|
|
|
new InputArgument('name', InputArgument::OPTIONAL, 'A route name'),
|
|
|
|
))
|
|
|
|
->setName('router:debug')
|
|
|
|
->setDescription('Displays current routes for an application')
|
|
|
|
->setHelp(<<<EOF
|
|
|
|
The <info>router:debug</info> displays the configured routes:
|
|
|
|
|
|
|
|
<info>router:debug</info>
|
|
|
|
EOF
|
|
|
|
)
|
|
|
|
;
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* @see Command
|
|
|
|
*/
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
2010-02-17 13:55:05 +00:00
|
|
|
{
|
2010-06-27 17:28:29 +01:00
|
|
|
$router = $this->container->get('router');
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
$routes = array();
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($router->getRouteCollection()->getRoutes() as $name => $route) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$routes[$name] = $route->compile();
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($input->getArgument('name')) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->outputRoute($output, $routes, $input->getArgument('name'));
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->outputRoutes($output, $routes);
|
|
|
|
}
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function outputRoutes(OutputInterface $output, $routes)
|
2010-02-17 13:55:05 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$output->writeln($this->getHelper('formatter')->formatSection('router', 'Current routes'));
|
|
|
|
|
|
|
|
$maxName = 4;
|
|
|
|
$maxMethod = 6;
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($routes as $name => $route) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$requirements = $route->getRequirements();
|
|
|
|
$method = isset($requirements['_method']) ? strtoupper(is_array($requirements['_method']) ? implode(', ', $requirements['_method']) : $requirements['_method']) : 'ANY';
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (strlen($name) > $maxName) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$maxName = strlen($name);
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (strlen($method) > $maxMethod) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$maxMethod = strlen($method);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$format = '%-'.$maxName.'s %-'.$maxMethod.'s %s';
|
|
|
|
|
|
|
|
// displays the generated routes
|
|
|
|
$format1 = '%-'.($maxName + 9).'s %-'.($maxMethod + 9).'s %s';
|
|
|
|
$output->writeln(sprintf($format1, '<comment>Name</comment>', '<comment>Method</comment>', '<comment>Pattern</comment>'));
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($routes as $name => $route) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$requirements = $route->getRequirements();
|
|
|
|
$method = isset($requirements['_method']) ? strtoupper(is_array($requirements['_method']) ? implode(', ', $requirements['_method']) : $requirements['_method']) : 'ANY';
|
|
|
|
$output->writeln(sprintf($format, $name, $method, $route->getPattern()));
|
|
|
|
}
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* @throws \InvalidArgumentException When route does not exist
|
|
|
|
*/
|
|
|
|
protected function outputRoute(OutputInterface $output, $routes, $name)
|
2010-02-17 13:55:05 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$output->writeln($this->getHelper('formatter')->formatSection('router', sprintf('Route "%s"', $name)));
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!isset($routes[$name])) {
|
2010-05-06 12:25:53 +01:00
|
|
|
throw new \InvalidArgumentException(sprintf('The route "%s" does not exist.', $name));
|
|
|
|
}
|
|
|
|
|
|
|
|
$route = $routes[$name];
|
|
|
|
$output->writeln(sprintf('<comment>Name</comment> %s', $name));
|
|
|
|
$output->writeln(sprintf('<comment>Pattern</comment> %s', $route->getPattern()));
|
|
|
|
$output->writeln(sprintf('<comment>Class</comment> %s', get_class($route)));
|
|
|
|
|
|
|
|
$defaults = '';
|
|
|
|
$d = $route->getDefaults();
|
|
|
|
ksort($d);
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($d as $name => $value) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$defaults .= ($defaults ? "\n".str_repeat(' ', 13) : '').$name.': '.$this->formatValue($value);
|
|
|
|
}
|
|
|
|
$output->writeln(sprintf('<comment>Defaults</comment> %s', $defaults));
|
|
|
|
|
|
|
|
$requirements = '';
|
|
|
|
$r = $route->getRequirements();
|
|
|
|
ksort($r);
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($r as $name => $value) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$requirements .= ($requirements ? "\n".str_repeat(' ', 13) : '').$name.': '.$this->formatValue($value);
|
|
|
|
}
|
|
|
|
$output->writeln(sprintf('<comment>Requirements</comment> %s', $requirements));
|
|
|
|
|
|
|
|
$options = '';
|
|
|
|
$o = $route->getOptions();
|
|
|
|
ksort($o);
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($o as $name => $value) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$options .= ($options ? "\n".str_repeat(' ', 13) : '').$name.': '.$this->formatValue($value);
|
|
|
|
}
|
|
|
|
$output->writeln(sprintf('<comment>Options</comment> %s', $options));
|
|
|
|
$output->write('<comment>Regex</comment> ');
|
|
|
|
$output->writeln(preg_replace('/^ /', '', preg_replace('/^/m', ' ', $route->getRegex())), Output::OUTPUT_RAW);
|
|
|
|
|
|
|
|
$tokens = '';
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($route->getTokens() as $token) {
|
2010-05-08 14:32:30 +01:00
|
|
|
if (!$tokens) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$tokens = $this->displayToken($token);
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2010-05-06 12:25:53 +01:00
|
|
|
$tokens .= "\n".str_repeat(' ', 13).$this->displayToken($token);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$output->writeln(sprintf('<comment>Tokens</comment> %s', $tokens));
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function displayToken($token)
|
2010-02-17 13:55:05 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$type = array_shift($token);
|
|
|
|
array_shift($token);
|
2010-02-17 13:55:05 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return sprintf('%-10s %s', $type, $this->formatValue($token));
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function formatValue($value)
|
2010-02-17 13:55:05 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (is_object($value)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return sprintf('object(%s)', get_class($value));
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2010-05-06 12:25:53 +01:00
|
|
|
return preg_replace("/\n\s*/s", '', var_export($value, true));
|
|
|
|
}
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|
|
|
|
}
|