Skip to content

Commit c49687f

Browse files
committed
merged branch peterrehm/router-command (PR #8657)
This PR was squashed before being merged into the master branch (closes #8657). Discussion ---------- Added option to show controllers optionally in the router:debug command | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Added option to show controllers in the router debug command as a convenience function. app/console router:debug --show-controllers Commits ------- 6fd32f3 Added option to show controllers optionally in the router:debug command
2 parents 43fe39b + 1b3bf17 commit c49687f

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

Command/RouterDebugCommand.php

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Console\Input\InputArgument;
1515
use Symfony\Component\Console\Input\InputInterface;
16+
use Symfony\Component\Console\Input\InputOption;
1617
use Symfony\Component\Console\Output\OutputInterface;
1718
use Symfony\Component\Routing\RouterInterface;
1819

@@ -49,6 +50,7 @@ protected function configure()
4950
->setName('router:debug')
5051
->setDefinition(array(
5152
new InputArgument('name', InputArgument::OPTIONAL, 'A route name'),
53+
new InputOption('show-controllers', null, InputOption::VALUE_NONE, 'Show assigned controllers in overview')
5254
))
5355
->setDescription('Displays current routes for an application')
5456
->setHelp(<<<EOF
@@ -72,11 +74,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
7274
if ($name) {
7375
$this->outputRoute($output, $name);
7476
} else {
75-
$this->outputRoutes($output);
77+
$this->outputRoutes($output, null, $input->getOption('show-controllers'));
7678
}
7779
}
7880

79-
protected function outputRoutes(OutputInterface $output, $routes = null)
81+
protected function outputRoutes(OutputInterface $output, $routes = null, $showControllers = false)
8082
{
8183
if (null === $routes) {
8284
$routes = $this->getContainer()->get('router')->getRouteCollection()->all();
@@ -88,26 +90,52 @@ protected function outputRoutes(OutputInterface $output, $routes = null)
8890
$maxMethod = strlen('method');
8991
$maxScheme = strlen('scheme');
9092
$maxHost = strlen('host');
93+
$maxPath = strlen('path');
9194

9295
foreach ($routes as $name => $route) {
9396
$method = $route->getMethods() ? implode('|', $route->getMethods()) : 'ANY';
9497
$scheme = $route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY';
9598
$host = '' !== $route->getHost() ? $route->getHost() : 'ANY';
99+
$path = $route->getPath();
96100
$maxName = max($maxName, strlen($name));
97101
$maxMethod = max($maxMethod, strlen($method));
98102
$maxScheme = max($maxScheme, strlen($scheme));
99103
$maxHost = max($maxHost, strlen($host));
104+
$maxPath = max($maxPath, strlen($path));
100105
}
101106

102107
$format = '%-'.$maxName.'s %-'.$maxMethod.'s %-'.$maxScheme.'s %-'.$maxHost.'s %s';
103-
$formatHeader = '%-'.($maxName + 19).'s %-'.($maxMethod + 19).'s %-'.($maxScheme + 19).'s %-'.($maxHost + 19).'s %s';
104-
$output->writeln(sprintf($formatHeader, '<comment>Name</comment>', '<comment>Method</comment>', '<comment>Scheme</comment>', '<comment>Host</comment>', '<comment>Path</comment>'));
108+
$formatHeader = '%-'.($maxName + 19).'s %-'.($maxMethod + 19).'s %-'.($maxScheme + 19).'s %-'.($maxHost + 19).'s %-'.($maxPath + 19).'s';
109+
110+
if ($showControllers) {
111+
$format = str_replace('s %s', 's %-'.$maxPath.'s %s', $format);
112+
$formatHeader = $formatHeader . ' %s';
113+
}
114+
115+
if ($showControllers) {
116+
$output->writeln(sprintf($formatHeader, '<comment>Name</comment>', '<comment>Method</comment>', '<comment>Scheme</comment>', '<comment>Host</comment>', '<comment>Path</comment>', '<comment>Controller</comment>'));
117+
} else {
118+
$output->writeln(sprintf($formatHeader, '<comment>Name</comment>', '<comment>Method</comment>', '<comment>Scheme</comment>', '<comment>Host</comment>', '<comment>Path</comment>'));
119+
}
105120

106121
foreach ($routes as $name => $route) {
107122
$method = $route->getMethods() ? implode('|', $route->getMethods()) : 'ANY';
108123
$scheme = $route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY';
109124
$host = '' !== $route->getHost() ? $route->getHost() : 'ANY';
110-
$output->writeln(sprintf($format, $name, $method, $scheme, $host, $route->getPath()), OutputInterface::OUTPUT_RAW);
125+
if ($showControllers) {
126+
$defaultData = $route->getDefaults();
127+
$controller = $defaultData['_controller'] ? $defaultData['_controller'] : '';
128+
if ($controller instanceof \Closure) {
129+
$controller = 'Closure';
130+
} else {
131+
if (is_object($controller)) {
132+
$controller = get_class($controller);
133+
}
134+
}
135+
$output->writeln(sprintf($format, $name, $method, $scheme, $host, $route->getPath(), $controller), OutputInterface::OUTPUT_RAW);
136+
} else {
137+
$output->writeln(sprintf($format, $name, $method, $scheme, $host, $route->getPath()), OutputInterface::OUTPUT_RAW);
138+
}
111139
}
112140
}
113141

0 commit comments

Comments
 (0)