2010-02-17 13:55:05 +00:00
|
|
|
<?php
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-15 13:29:43 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
2011-03-10 15:09:40 +00:00
|
|
|
* A console command for retrieving information about routes
|
2010-02-17 13:55:05 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2010-02-17 13:55:05 +00:00
|
|
|
*/
|
2011-06-20 20:04:55 +01:00
|
|
|
class RouterDebugCommand extends ContainerAwareCommand
|
2010-02-17 13:55:05 +00:00
|
|
|
{
|
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
|
|
|
{
|
2011-06-20 20:04:55 +01:00
|
|
|
$router = $this->getContainer()->get('router');
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
$routes = array();
|
2010-11-25 19:38:04 +00:00
|
|
|
foreach ($router->getRouteCollection()->all() 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
|
2011-03-12 13:08:29 +00:00
|
|
|
$format1 = '%-'.($maxName + 19).'s %-'.($maxMethod + 19).'s %s';
|
2010-05-06 12:25:53 +01:00
|
|
|
$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> ');
|
2011-03-29 20:53:28 +01:00
|
|
|
$output->writeln(preg_replace('/^ /', '', preg_replace('/^/m', ' ', $route->getRegex())), OutputInterface::OUTPUT_RAW);
|
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));
|
|
|
|
}
|
2011-02-27 16:24:32 +00:00
|
|
|
|
2011-04-09 23:53:33 +01:00
|
|
|
if (is_string($value)) {
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2011-02-27 16:24:32 +00:00
|
|
|
return preg_replace("/\n\s*/s", '', var_export($value, true));
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|
|
|
|
}
|