11
11
12
12
namespace Symfony \Bundle \FrameworkBundle \Command ;
13
13
14
+ use Symfony \Bundle \FrameworkBundle \Console \Helper \DescriptorHelper ;
14
15
use Symfony \Component \Console \Input \InputArgument ;
15
16
use Symfony \Component \Console \Input \InputInterface ;
16
17
use Symfony \Component \Console \Input \InputOption ;
@@ -50,7 +51,9 @@ protected function configure()
50
51
->setName ('router:debug ' )
51
52
->setDefinition (array (
52
53
new InputArgument ('name ' , InputArgument::OPTIONAL , 'A route name ' ),
53
- new InputOption ('show-controllers ' , null , InputOption::VALUE_NONE , 'Show assigned controllers in overview ' )
54
+ new InputOption ('show-controllers ' , null , InputOption::VALUE_NONE , 'Show assigned controllers in overview ' ),
55
+ new InputOption ('format ' , null , InputOption::VALUE_REQUIRED , 'To output route(s) in other formats ' ),
56
+ new InputOption ('raw ' , null , InputOption::VALUE_NONE , 'To output raw route(s) ' ),
54
57
))
55
58
->setDescription ('Displays current routes for an application ' )
56
59
->setHelp (<<<EOF
@@ -70,151 +73,21 @@ protected function configure()
70
73
protected function execute (InputInterface $ input , OutputInterface $ output )
71
74
{
72
75
$ name = $ input ->getArgument ('name ' );
76
+ $ helper = new DescriptorHelper ();
73
77
74
78
if ($ name ) {
75
- $ this ->outputRoute ($ output , $ name );
76
- } else {
77
- $ this ->outputRoutes ($ output , null , $ input ->getOption ('show-controllers ' ));
78
- }
79
- }
80
-
81
- protected function outputRoutes (OutputInterface $ output , $ routes = null , $ showControllers = false )
82
- {
83
- if (null === $ routes ) {
84
- $ routes = $ this ->getContainer ()->get ('router ' )->getRouteCollection ()->all ();
85
- }
86
-
87
- $ output ->writeln ($ this ->getHelper ('formatter ' )->formatSection ('router ' , 'Current routes ' ));
88
-
89
- $ maxName = strlen ('name ' );
90
- $ maxMethod = strlen ('method ' );
91
- $ maxScheme = strlen ('scheme ' );
92
- $ maxHost = strlen ('host ' );
93
- $ maxPath = strlen ('path ' );
94
-
95
- foreach ($ routes as $ name => $ route ) {
96
- $ method = $ route ->getMethods () ? implode ('| ' , $ route ->getMethods ()) : 'ANY ' ;
97
- $ scheme = $ route ->getSchemes () ? implode ('| ' , $ route ->getSchemes ()) : 'ANY ' ;
98
- $ host = '' !== $ route ->getHost () ? $ route ->getHost () : 'ANY ' ;
99
- $ path = $ route ->getPath ();
100
- $ maxName = max ($ maxName , strlen ($ name ));
101
- $ maxMethod = max ($ maxMethod , strlen ($ method ));
102
- $ maxScheme = max ($ maxScheme , strlen ($ scheme ));
103
- $ maxHost = max ($ maxHost , strlen ($ host ));
104
- $ maxPath = max ($ maxPath , strlen ($ path ));
105
- }
106
-
107
- $ format = '%- ' .$ maxName .'s %- ' .$ maxMethod .'s %- ' .$ maxScheme .'s %- ' .$ maxHost .'s %s ' ;
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
- }
120
-
121
- foreach ($ routes as $ name => $ route ) {
122
- $ method = $ route ->getMethods () ? implode ('| ' , $ route ->getMethods ()) : 'ANY ' ;
123
- $ scheme = $ route ->getSchemes () ? implode ('| ' , $ route ->getSchemes ()) : 'ANY ' ;
124
- $ host = '' !== $ route ->getHost () ? $ route ->getHost () : 'ANY ' ;
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 );
79
+ $ route = $ this ->getContainer ()->get ('router ' )->getRouteCollection ()->get ($ name );
80
+ if (!$ route ) {
81
+ throw new \InvalidArgumentException (sprintf ('The route "%s" does not exist. ' , $ name ));
138
82
}
83
+ $ helper ->describe ($ output , $ route , $ input ->getOption ('format ' ), $ input ->getOption ('raw ' ), array ('name ' => $ name ));
84
+ } else {
85
+ $ routes = $ this ->getContainer ()->get ('router ' )->getRouteCollection ();
86
+ $ helper ->describe ($ output , $ routes , array (
87
+ 'format ' => $ input ->getOption ('format ' ),
88
+ 'raw_text ' => $ input ->getOption ('raw ' ),
89
+ 'show_controllers ' => $ input ->getOption ('show-controllers ' ),
90
+ ));
139
91
}
140
92
}
141
-
142
- /**
143
- * @throws \InvalidArgumentException When route does not exist
144
- */
145
- protected function outputRoute (OutputInterface $ output , $ name )
146
- {
147
- $ route = $ this ->getContainer ()->get ('router ' )->getRouteCollection ()->get ($ name );
148
- if (!$ route ) {
149
- throw new \InvalidArgumentException (sprintf ('The route "%s" does not exist. ' , $ name ));
150
- }
151
-
152
- $ output ->writeln ($ this ->getHelper ('formatter ' )->formatSection ('router ' , sprintf ('Route "%s" ' , $ name )));
153
-
154
- $ method = $ route ->getMethods () ? implode ('| ' , $ route ->getMethods ()) : 'ANY ' ;
155
- $ scheme = $ route ->getSchemes () ? implode ('| ' , $ route ->getSchemes ()) : 'ANY ' ;
156
- $ host = '' !== $ route ->getHost () ? $ route ->getHost () : 'ANY ' ;
157
-
158
- $ output ->write ('<comment>Name</comment> ' );
159
- $ output ->writeln ($ name , OutputInterface::OUTPUT_RAW );
160
-
161
- $ output ->write ('<comment>Path</comment> ' );
162
- $ output ->writeln ($ route ->getPath (), OutputInterface::OUTPUT_RAW );
163
-
164
- $ output ->write ('<comment>Host</comment> ' );
165
- $ output ->writeln ($ host , OutputInterface::OUTPUT_RAW );
166
-
167
- $ output ->write ('<comment>Scheme</comment> ' );
168
- $ output ->writeln ($ scheme , OutputInterface::OUTPUT_RAW );
169
-
170
- $ output ->write ('<comment>Method</comment> ' );
171
- $ output ->writeln ($ method , OutputInterface::OUTPUT_RAW );
172
-
173
- $ output ->write ('<comment>Class</comment> ' );
174
- $ output ->writeln (get_class ($ route ), OutputInterface::OUTPUT_RAW );
175
-
176
- $ output ->write ('<comment>Defaults</comment> ' );
177
- $ output ->writeln ($ this ->formatConfigs ($ route ->getDefaults ()), OutputInterface::OUTPUT_RAW );
178
-
179
- $ output ->write ('<comment>Requirements</comment> ' );
180
- // we do not want to show the schemes and methods again that are also in the requirements for BC
181
- $ requirements = $ route ->getRequirements ();
182
- unset($ requirements ['_scheme ' ], $ requirements ['_method ' ]);
183
- $ output ->writeln ($ this ->formatConfigs ($ requirements ) ?: 'NO CUSTOM ' , OutputInterface::OUTPUT_RAW );
184
-
185
- $ output ->write ('<comment>Options</comment> ' );
186
- $ output ->writeln ($ this ->formatConfigs ($ route ->getOptions ()), OutputInterface::OUTPUT_RAW );
187
-
188
- $ output ->write ('<comment>Path-Regex</comment> ' );
189
- $ output ->writeln ($ route ->compile ()->getRegex (), OutputInterface::OUTPUT_RAW );
190
-
191
- if (null !== $ route ->compile ()->getHostRegex ()) {
192
- $ output ->write ('<comment>Host-Regex</comment> ' );
193
- $ output ->writeln ($ route ->compile ()->getHostRegex (), OutputInterface::OUTPUT_RAW );
194
- }
195
- }
196
-
197
- protected function formatValue ($ value )
198
- {
199
- if (is_object ($ value )) {
200
- return sprintf ('object(%s) ' , get_class ($ value ));
201
- }
202
-
203
- if (is_string ($ value )) {
204
- return $ value ;
205
- }
206
-
207
- return preg_replace ("/ \n\s*/s " , '' , var_export ($ value , true ));
208
- }
209
-
210
- private function formatConfigs (array $ array )
211
- {
212
- $ string = '' ;
213
- ksort ($ array );
214
- foreach ($ array as $ name => $ value ) {
215
- $ string .= ($ string ? "\n" .str_repeat (' ' , 13 ) : '' ).$ name .': ' .$ this ->formatValue ($ value );
216
- }
217
-
218
- return $ string ;
219
- }
220
93
}
0 commit comments