Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 15bf033

Browse files
committed
[FrameworkBundle] fix router debug command
- use dedicated Route:getMethods, getSchemes - pattern -> path - show missing scheme requirement - show missing host regex - refactoring
1 parent d16d193 commit 15bf033

File tree

1 file changed

+60
-46
lines changed

1 file changed

+60
-46
lines changed

src/Symfony/Bundle/FrameworkBundle/Command/RouterDebugCommand.php

Lines changed: 60 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* A console command for retrieving information about routes
2121
*
2222
* @author Fabien Potencier <[email protected]>
23+
* @author Tobias Schultze <http://tobion.de>
2324
*/
2425
class RouterDebugCommand extends ContainerAwareCommand
2526
{
@@ -85,34 +86,28 @@ protected function outputRoutes(OutputInterface $output, $routes = null)
8586

8687
$maxName = strlen('name');
8788
$maxMethod = strlen('method');
89+
$maxScheme = strlen('scheme');
8890
$maxHost = strlen('host');
8991

9092
foreach ($routes as $name => $route) {
91-
$requirements = $route->getRequirements();
92-
$method = isset($requirements['_method'])
93-
? strtoupper(is_array($requirements['_method'])
94-
? implode(', ', $requirements['_method']) : $requirements['_method']
95-
)
96-
: 'ANY';
93+
$method = $route->getMethods() ? implode('|', $route->getMethods()) : 'ANY';
94+
$scheme = $route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY';
9795
$host = '' !== $route->getHost() ? $route->getHost() : 'ANY';
9896
$maxName = max($maxName, strlen($name));
9997
$maxMethod = max($maxMethod, strlen($method));
98+
$maxScheme = max($maxScheme, strlen($scheme));
10099
$maxHost = max($maxHost, strlen($host));
101100
}
102-
$format = '%-'.$maxName.'s %-'.$maxMethod.'s %-'.$maxHost.'s %s';
103101

104-
// displays the generated routes
105-
$format1 = '%-'.($maxName + 19).'s %-'.($maxMethod + 19).'s %-'.($maxHost + 19).'s %s';
106-
$output->writeln(sprintf($format1, '<comment>Name</comment>', '<comment>Method</comment>', '<comment>Host</comment>', '<comment>Pattern</comment>'));
102+
$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>'));
105+
107106
foreach ($routes as $name => $route) {
108-
$requirements = $route->getRequirements();
109-
$method = isset($requirements['_method'])
110-
? strtoupper(is_array($requirements['_method'])
111-
? implode(', ', $requirements['_method']) : $requirements['_method']
112-
)
113-
: 'ANY';
107+
$method = $route->getMethods() ? implode('|', $route->getMethods()) : 'ANY';
108+
$scheme = $route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY';
114109
$host = '' !== $route->getHost() ? $route->getHost() : 'ANY';
115-
$output->writeln(sprintf($format, $name, $method, $host, $route->getPath()));
110+
$output->writeln(sprintf($format, $name, $method, $scheme, $host, $route->getPath()), OutputInterface::OUTPUT_RAW);
116111
}
117112
}
118113

@@ -126,41 +121,49 @@ protected function outputRoute(OutputInterface $output, $name)
126121
throw new \InvalidArgumentException(sprintf('The route "%s" does not exist.', $name));
127122
}
128123

124+
$output->writeln($this->getHelper('formatter')->formatSection('router', sprintf('Route "%s"', $name)));
125+
126+
$method = $route->getMethods() ? implode('|', $route->getMethods()) : 'ANY';
127+
$scheme = $route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY';
129128
$host = '' !== $route->getHost() ? $route->getHost() : 'ANY';
130129

131-
$output->writeln($this->getHelper('formatter')->formatSection('router', sprintf('Route "%s"', $name)));
130+
$output->write('<comment>Name</comment> ');
131+
$output->writeln($name, OutputInterface::OUTPUT_RAW);
132132

133-
$output->writeln(sprintf('<comment>Name</comment> %s', $name));
134-
$output->writeln(sprintf('<comment>Pattern</comment> %s', $route->getPath()));
135-
$output->writeln(sprintf('<comment>Host</comment> %s', $host));
136-
$output->writeln(sprintf('<comment>Class</comment> %s', get_class($route)));
133+
$output->write('<comment>Path</comment> ');
134+
$output->writeln($route->getPath(), OutputInterface::OUTPUT_RAW);
137135

138-
$defaults = '';
139-
$d = $route->getDefaults();
140-
ksort($d);
141-
foreach ($d as $name => $value) {
142-
$defaults .= ($defaults ? "\n".str_repeat(' ', 13) : '').$name.': '.$this->formatValue($value);
143-
}
144-
$output->writeln(sprintf('<comment>Defaults</comment> %s', $defaults));
136+
$output->write('<comment>Host</comment> ');
137+
$output->writeln($host, OutputInterface::OUTPUT_RAW);
145138

146-
$requirements = '';
147-
$r = $route->getRequirements();
148-
ksort($r);
149-
foreach ($r as $name => $value) {
150-
$requirements .= ($requirements ? "\n".str_repeat(' ', 13) : '').$name.': '.$this->formatValue($value);
151-
}
152-
$requirements = '' !== $requirements ? $requirements : 'NONE';
153-
$output->writeln(sprintf('<comment>Requirements</comment> %s', $requirements));
154-
155-
$options = '';
156-
$o = $route->getOptions();
157-
ksort($o);
158-
foreach ($o as $name => $value) {
159-
$options .= ($options ? "\n".str_repeat(' ', 13) : '').$name.': '.$this->formatValue($value);
139+
$output->write('<comment>Scheme</comment> ');
140+
$output->writeln($scheme, OutputInterface::OUTPUT_RAW);
141+
142+
$output->write('<comment>Method</comment> ');
143+
$output->writeln($method, OutputInterface::OUTPUT_RAW);
144+
145+
$output->write('<comment>Class</comment> ');
146+
$output->writeln(get_class($route), OutputInterface::OUTPUT_RAW);
147+
148+
$output->write('<comment>Defaults</comment> ');
149+
$output->writeln($this->formatConfigs($route->getDefaults()), OutputInterface::OUTPUT_RAW);
150+
151+
$output->write('<comment>Requirements</comment> ');
152+
// we do not want to show the schemes and methods again that are also in the requirements for BC
153+
$requirements = $route->getRequirements();
154+
unset($requirements['_scheme'], $requirements['_method']);
155+
$output->writeln($this->formatConfigs($requirements) ?: 'NO CUSTOM', OutputInterface::OUTPUT_RAW);
156+
157+
$output->write('<comment>Options</comment> ');
158+
$output->writeln($this->formatConfigs($route->getOptions()), OutputInterface::OUTPUT_RAW);
159+
160+
$output->write('<comment>Path-Regex</comment> ');
161+
$output->writeln($route->compile()->getRegex(), OutputInterface::OUTPUT_RAW);
162+
163+
if (null !== $route->compile()->getHostRegex()) {
164+
$output->write('<comment>Host-Regex</comment> ');
165+
$output->writeln($route->compile()->getHostRegex(), OutputInterface::OUTPUT_RAW);
160166
}
161-
$output->writeln(sprintf('<comment>Options</comment> %s', $options));
162-
$output->write('<comment>Regex</comment> ');
163-
$output->writeln(preg_replace('/^ /', '', preg_replace('/^/m', ' ', $route->compile()->getRegex())), OutputInterface::OUTPUT_RAW);
164167
}
165168

166169
protected function formatValue($value)
@@ -175,4 +178,15 @@ protected function formatValue($value)
175178

176179
return preg_replace("/\n\s*/s", '', var_export($value, true));
177180
}
181+
182+
private function formatConfigs(array $array)
183+
{
184+
$string = '';
185+
ksort($array);
186+
foreach ($array as $name => $value) {
187+
$string .= ($string ? "\n" . str_repeat(' ', 13) : '') . $name . ': ' . $this->formatValue($value);
188+
}
189+
190+
return $string;
191+
}
178192
}

0 commit comments

Comments
 (0)