Skip to content

Commit

Permalink
Added option to show controllers optionally in the router:debug command
Browse files Browse the repository at this point in the history
  • Loading branch information
peterrehm authored and fabpot committed Sep 9, 2013
1 parent 5ea5921 commit 6fd32f3
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions src/Symfony/Bundle/FrameworkBundle/Command/RouterDebugCommand.php
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Routing\RouterInterface;

Expand Down Expand Up @@ -49,6 +50,7 @@ protected function configure()
->setName('router:debug')
->setDefinition(array(
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')
->setHelp(<<<EOF
Expand All @@ -72,11 +74,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
if ($name) {
$this->outputRoute($output, $name);
} 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) {
$routes = $this->getContainer()->get('router')->getRouteCollection()->all();
Expand All @@ -88,26 +90,52 @@ protected function outputRoutes(OutputInterface $output, $routes = null)
$maxMethod = strlen('method');
$maxScheme = strlen('scheme');
$maxHost = strlen('host');
$maxPath = strlen('path');

foreach ($routes as $name => $route) {
$method = $route->getMethods() ? implode('|', $route->getMethods()) : 'ANY';
$scheme = $route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY';
$host = '' !== $route->getHost() ? $route->getHost() : 'ANY';
$path = $route->getPath();
$maxName = max($maxName, strlen($name));
$maxMethod = max($maxMethod, strlen($method));
$maxScheme = max($maxScheme, strlen($scheme));
$maxHost = max($maxHost, strlen($host));
$maxPath = max($maxPath, strlen($path));
}

$format = '%-'.$maxName.'s %-'.$maxMethod.'s %-'.$maxScheme.'s %-'.$maxHost.'s %s';
$formatHeader = '%-'.($maxName + 19).'s %-'.($maxMethod + 19).'s %-'.($maxScheme + 19).'s %-'.($maxHost + 19).'s %s';
$output->writeln(sprintf($formatHeader, '<comment>Name</comment>', '<comment>Method</comment>', '<comment>Scheme</comment>', '<comment>Host</comment>', '<comment>Path</comment>'));
$formatHeader = '%-'.($maxName + 19).'s %-'.($maxMethod + 19).'s %-'.($maxScheme + 19).'s %-'.($maxHost + 19).'s %-'.($maxPath + 19).'s';

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) {
$method = $route->getMethods() ? implode('|', $route->getMethods()) : 'ANY';
$scheme = $route->getSchemes() ? implode('|', $route->getSchemes()) : '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);
}
}
}

Expand Down

0 comments on commit 6fd32f3

Please sign in to comment.