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
{
26
27
/**
27
- * {@inheritDoc }
28
+ * {@inheritdoc }
28
29
*/
29
30
public function isEnabled ()
30
31
{
@@ -40,7 +41,7 @@ public function isEnabled()
40
41
}
41
42
42
43
/**
43
- * @see Command
44
+ * {@inheritdoc}
44
45
*/
45
46
protected function configure ()
46
47
{
@@ -60,7 +61,9 @@ protected function configure()
60
61
}
61
62
62
63
/**
63
- * @see Command
64
+ * {@inheritdoc}
65
+ *
66
+ * @throws \InvalidArgumentException When route does not exist
64
67
*/
65
68
protected function execute (InputInterface $ input , OutputInterface $ output )
66
69
{
@@ -83,34 +86,28 @@ protected function outputRoutes(OutputInterface $output, $routes = null)
83
86
84
87
$ maxName = strlen ('name ' );
85
88
$ maxMethod = strlen ('method ' );
89
+ $ maxScheme = strlen ('scheme ' );
86
90
$ maxHost = strlen ('host ' );
87
91
88
92
foreach ($ routes as $ name => $ route ) {
89
- $ requirements = $ route ->getRequirements ();
90
- $ method = isset ($ requirements ['_method ' ])
91
- ? strtoupper (is_array ($ requirements ['_method ' ])
92
- ? implode (', ' , $ requirements ['_method ' ]) : $ requirements ['_method ' ]
93
- )
94
- : 'ANY ' ;
93
+ $ method = $ route ->getMethods () ? implode ('| ' , $ route ->getMethods ()) : 'ANY ' ;
94
+ $ scheme = $ route ->getSchemes () ? implode ('| ' , $ route ->getSchemes ()) : 'ANY ' ;
95
95
$ host = '' !== $ route ->getHost () ? $ route ->getHost () : 'ANY ' ;
96
96
$ maxName = max ($ maxName , strlen ($ name ));
97
97
$ maxMethod = max ($ maxMethod , strlen ($ method ));
98
+ $ maxScheme = max ($ maxScheme , strlen ($ scheme ));
98
99
$ maxHost = max ($ maxHost , strlen ($ host ));
99
100
}
100
- $ format = '%- ' .$ maxName .'s %- ' .$ maxMethod .'s %- ' .$ maxHost .'s %s ' ;
101
101
102
- // displays the generated routes
103
- $ format1 = '%- ' .($ maxName + 19 ).'s %- ' .($ maxMethod + 19 ).'s %- ' .($ maxHost + 19 ).'s %s ' ;
104
- $ 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
+
105
106
foreach ($ routes as $ name => $ route ) {
106
- $ requirements = $ route ->getRequirements ();
107
- $ method = isset ($ requirements ['_method ' ])
108
- ? strtoupper (is_array ($ requirements ['_method ' ])
109
- ? implode (', ' , $ requirements ['_method ' ]) : $ requirements ['_method ' ]
110
- )
111
- : 'ANY ' ;
107
+ $ method = $ route ->getMethods () ? implode ('| ' , $ route ->getMethods ()) : 'ANY ' ;
108
+ $ scheme = $ route ->getSchemes () ? implode ('| ' , $ route ->getSchemes ()) : 'ANY ' ;
112
109
$ host = '' !== $ route ->getHost () ? $ route ->getHost () : 'ANY ' ;
113
- $ output ->writeln (sprintf ($ format , $ name , $ method , $ host , $ route ->getPath ()));
110
+ $ output ->writeln (sprintf ($ format , $ name , $ method , $ scheme , $ host , $ route ->getPath ()), OutputInterface:: OUTPUT_RAW );
114
111
}
115
112
}
116
113
@@ -124,41 +121,49 @@ protected function outputRoute(OutputInterface $output, $name)
124
121
throw new \InvalidArgumentException (sprintf ('The route "%s" does not exist. ' , $ name ));
125
122
}
126
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 ' ;
127
128
$ host = '' !== $ route ->getHost () ? $ route ->getHost () : 'ANY ' ;
128
129
129
- $ output ->writeln ($ this ->getHelper ('formatter ' )->formatSection ('router ' , sprintf ('Route "%s" ' , $ name )));
130
+ $ output ->write ('<comment>Name</comment> ' );
131
+ $ output ->writeln ($ name , OutputInterface::OUTPUT_RAW );
130
132
131
- $ output ->writeln (sprintf ('<comment>Name</comment> %s ' , $ name ));
132
- $ output ->writeln (sprintf ('<comment>Pattern</comment> %s ' , $ route ->getPath ()));
133
- $ output ->writeln (sprintf ('<comment>Host</comment> %s ' , $ host ));
134
- $ output ->writeln (sprintf ('<comment>Class</comment> %s ' , get_class ($ route )));
133
+ $ output ->write ('<comment>Path</comment> ' );
134
+ $ output ->writeln ($ route ->getPath (), OutputInterface::OUTPUT_RAW );
135
135
136
- $ defaults = '' ;
137
- $ d = $ route ->getDefaults ();
138
- ksort ($ d );
139
- foreach ($ d as $ name => $ value ) {
140
- $ defaults .= ($ defaults ? "\n" .str_repeat (' ' , 13 ) : '' ).$ name .': ' .$ this ->formatValue ($ value );
141
- }
142
- $ output ->writeln (sprintf ('<comment>Defaults</comment> %s ' , $ defaults ));
136
+ $ output ->write ('<comment>Host</comment> ' );
137
+ $ output ->writeln ($ host , OutputInterface::OUTPUT_RAW );
143
138
144
- $ requirements = '' ;
145
- $ r = $ route ->getRequirements ();
146
- ksort ($ r );
147
- foreach ($ r as $ name => $ value ) {
148
- $ requirements .= ($ requirements ? "\n" .str_repeat (' ' , 13 ) : '' ).$ name .': ' .$ this ->formatValue ($ value );
149
- }
150
- $ requirements = '' !== $ requirements ? $ requirements : 'NONE ' ;
151
- $ output ->writeln (sprintf ('<comment>Requirements</comment> %s ' , $ requirements ));
152
-
153
- $ options = '' ;
154
- $ o = $ route ->getOptions ();
155
- ksort ($ o );
156
- foreach ($ o as $ name => $ value ) {
157
- $ 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 );
158
166
}
159
- $ output ->writeln (sprintf ('<comment>Options</comment> %s ' , $ options ));
160
- $ output ->write ('<comment>Regex</comment> ' );
161
- $ output ->writeln (preg_replace ('/^ / ' , '' , preg_replace ('/^/m ' , ' ' , $ route ->compile ()->getRegex ())), OutputInterface::OUTPUT_RAW );
162
167
}
163
168
164
169
protected function formatValue ($ value )
@@ -173,4 +178,15 @@ protected function formatValue($value)
173
178
174
179
return preg_replace ("/ \n\s*/s " , '' , var_export ($ value , true ));
175
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
+ }
176
192
}
0 commit comments