From 6fd32f3da49485fefab925f6eed4ed25367a2a45 Mon Sep 17 00:00:00 2001 From: Peter Rehm Date: Sat, 3 Aug 2013 15:13:38 +0200 Subject: [PATCH] Added option to show controllers optionally in the router:debug command --- .../Command/RouterDebugCommand.php | 38 ++++++++++++++++--- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/RouterDebugCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/RouterDebugCommand.php index d581af54f92c..eb5286c4f01c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/RouterDebugCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/RouterDebugCommand.php @@ -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; @@ -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(<<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(); @@ -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, 'Name', 'Method', 'Scheme', 'Host', 'Path')); + $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, 'Name', 'Method', 'Scheme', 'Host', 'Path', 'Controller')); + } else { + $output->writeln(sprintf($formatHeader, 'Name', 'Method', 'Scheme', 'Host', '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'; - $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); + } } }