Added option to show controllers optionally in the router:debug command

This commit is contained in:
Peter Rehm 2013-08-03 15:13:38 +02:00 committed by Fabien Potencier
parent 5ea5921918
commit 6fd32f3da4

View File

@ -13,6 +13,7 @@ namespace Symfony\Bundle\FrameworkBundle\Command;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Routing\RouterInterface; use Symfony\Component\Routing\RouterInterface;
@ -49,6 +50,7 @@ class RouterDebugCommand extends ContainerAwareCommand
->setName('router:debug') ->setName('router:debug')
->setDefinition(array( ->setDefinition(array(
new InputArgument('name', InputArgument::OPTIONAL, 'A route name'), new InputArgument('name', InputArgument::OPTIONAL, 'A route name'),
new InputOption('show-controllers', null, InputOption::VALUE_NONE, 'Show assigned controllers in overview')
)) ))
->setDescription('Displays current routes for an application') ->setDescription('Displays current routes for an application')
->setHelp(<<<EOF ->setHelp(<<<EOF
@ -72,11 +74,11 @@ EOF
if ($name) { if ($name) {
$this->outputRoute($output, $name); $this->outputRoute($output, $name);
} else { } else {
$this->outputRoutes($output); $this->outputRoutes($output, null, $input->getOption('show-controllers'));
} }
} }
protected function outputRoutes(OutputInterface $output, $routes = null) protected function outputRoutes(OutputInterface $output, $routes = null, $showControllers = false)
{ {
if (null === $routes) { if (null === $routes) {
$routes = $this->getContainer()->get('router')->getRouteCollection()->all(); $routes = $this->getContainer()->get('router')->getRouteCollection()->all();
@ -88,26 +90,52 @@ EOF
$maxMethod = strlen('method'); $maxMethod = strlen('method');
$maxScheme = strlen('scheme'); $maxScheme = strlen('scheme');
$maxHost = strlen('host'); $maxHost = strlen('host');
$maxPath = strlen('path');
foreach ($routes as $name => $route) { foreach ($routes as $name => $route) {
$method = $route->getMethods() ? implode('|', $route->getMethods()) : 'ANY'; $method = $route->getMethods() ? implode('|', $route->getMethods()) : 'ANY';
$scheme = $route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY'; $scheme = $route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY';
$host = '' !== $route->getHost() ? $route->getHost() : 'ANY'; $host = '' !== $route->getHost() ? $route->getHost() : 'ANY';
$path = $route->getPath();
$maxName = max($maxName, strlen($name)); $maxName = max($maxName, strlen($name));
$maxMethod = max($maxMethod, strlen($method)); $maxMethod = max($maxMethod, strlen($method));
$maxScheme = max($maxScheme, strlen($scheme)); $maxScheme = max($maxScheme, strlen($scheme));
$maxHost = max($maxHost, strlen($host)); $maxHost = max($maxHost, strlen($host));
$maxPath = max($maxPath, strlen($path));
} }
$format = '%-'.$maxName.'s %-'.$maxMethod.'s %-'.$maxScheme.'s %-'.$maxHost.'s %s'; $format = '%-'.$maxName.'s %-'.$maxMethod.'s %-'.$maxScheme.'s %-'.$maxHost.'s %s';
$formatHeader = '%-'.($maxName + 19).'s %-'.($maxMethod + 19).'s %-'.($maxScheme + 19).'s %-'.($maxHost + 19).'s %s'; $formatHeader = '%-'.($maxName + 19).'s %-'.($maxMethod + 19).'s %-'.($maxScheme + 19).'s %-'.($maxHost + 19).'s %-'.($maxPath + 19).'s';
$output->writeln(sprintf($formatHeader, '<comment>Name</comment>', '<comment>Method</comment>', '<comment>Scheme</comment>', '<comment>Host</comment>', '<comment>Path</comment>'));
if ($showControllers) {
$format = str_replace('s %s', 's %-'.$maxPath.'s %s', $format);
$formatHeader = $formatHeader . ' %s';
}
if ($showControllers) {
$output->writeln(sprintf($formatHeader, '<comment>Name</comment>', '<comment>Method</comment>', '<comment>Scheme</comment>', '<comment>Host</comment>', '<comment>Path</comment>', '<comment>Controller</comment>'));
} else {
$output->writeln(sprintf($formatHeader, '<comment>Name</comment>', '<comment>Method</comment>', '<comment>Scheme</comment>', '<comment>Host</comment>', '<comment>Path</comment>'));
}
foreach ($routes as $name => $route) { foreach ($routes as $name => $route) {
$method = $route->getMethods() ? implode('|', $route->getMethods()) : 'ANY'; $method = $route->getMethods() ? implode('|', $route->getMethods()) : 'ANY';
$scheme = $route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY'; $scheme = $route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY';
$host = '' !== $route->getHost() ? $route->getHost() : 'ANY'; $host = '' !== $route->getHost() ? $route->getHost() : 'ANY';
$output->writeln(sprintf($format, $name, $method, $scheme, $host, $route->getPath()), OutputInterface::OUTPUT_RAW); if ($showControllers) {
$defaultData = $route->getDefaults();
$controller = $defaultData['_controller'] ? $defaultData['_controller'] : '';
if ($controller instanceof \Closure) {
$controller = 'Closure';
} else {
if (is_object($controller)) {
$controller = get_class($controller);
}
}
$output->writeln(sprintf($format, $name, $method, $scheme, $host, $route->getPath(), $controller), OutputInterface::OUTPUT_RAW);
} else {
$output->writeln(sprintf($format, $name, $method, $scheme, $host, $route->getPath()), OutputInterface::OUTPUT_RAW);
}
} }
} }