20
20
* A console command for retrieving information about routes
21
21
*
22
22
* @author Fabien Potencier <[email protected] >
23
+ * @author Tobias Schultze <http://tobion.de>
23
24
*/
24
25
class RouterDebugCommand extends ContainerAwareCommand
25
26
{
@@ -85,34 +86,28 @@ protected function outputRoutes(OutputInterface $output, $routes = null)
85
86
86
87
$ maxName = strlen ('name ' );
87
88
$ maxMethod = strlen ('method ' );
89
+ $ maxScheme = strlen ('scheme ' );
88
90
$ maxHost = strlen ('host ' );
89
91
90
92
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 ' ;
97
95
$ host = '' !== $ route ->getHost () ? $ route ->getHost () : 'ANY ' ;
98
96
$ maxName = max ($ maxName , strlen ($ name ));
99
97
$ maxMethod = max ($ maxMethod , strlen ($ method ));
98
+ $ maxScheme = max ($ maxScheme , strlen ($ scheme ));
100
99
$ maxHost = max ($ maxHost , strlen ($ host ));
101
100
}
102
- $ format = '%- ' .$ maxName .'s %- ' .$ maxMethod .'s %- ' .$ maxHost .'s %s ' ;
103
101
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
+
107
106
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 ' ;
114
109
$ 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 );
116
111
}
117
112
}
118
113
@@ -126,41 +121,49 @@ protected function outputRoute(OutputInterface $output, $name)
126
121
throw new \InvalidArgumentException (sprintf ('The route "%s" does not exist. ' , $ name ));
127
122
}
128
123
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 ' ;
129
128
$ host = '' !== $ route ->getHost () ? $ route ->getHost () : 'ANY ' ;
130
129
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 );
132
132
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 );
137
135
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 );
145
138
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 );
160
166
}
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 );
164
167
}
165
168
166
169
protected function formatValue ($ value )
@@ -175,4 +178,15 @@ protected function formatValue($value)
175
178
176
179
return preg_replace ("/ \n\s*/s " , '' , var_export ($ value , true ));
177
180
}
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
+ }
178
192
}
0 commit comments