13
13
14
14
use Symfony \Component \Console \Input \InputArgument ;
15
15
use Symfony \Component \Console \Input \InputInterface ;
16
+ use Symfony \Component \Console \Input \InputOption ;
16
17
use Symfony \Component \Console \Output \OutputInterface ;
17
18
use Symfony \Component \Routing \RouterInterface ;
18
19
@@ -49,6 +50,7 @@ protected function configure()
49
50
->setName ('router:debug ' )
50
51
->setDefinition (array (
51
52
new InputArgument ('name ' , InputArgument::OPTIONAL , 'A route name ' ),
53
+ new InputOption ('show-controllers ' , null , InputOption::VALUE_NONE , 'Show assigned controllers in overview ' )
52
54
))
53
55
->setDescription ('Displays current routes for an application ' )
54
56
->setHelp (<<<EOF
@@ -72,11 +74,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
72
74
if ($ name ) {
73
75
$ this ->outputRoute ($ output , $ name );
74
76
} else {
75
- $ this ->outputRoutes ($ output );
77
+ $ this ->outputRoutes ($ output, null , $ input -> getOption ( ' show-controllers ' ) );
76
78
}
77
79
}
78
80
79
- protected function outputRoutes (OutputInterface $ output , $ routes = null )
81
+ protected function outputRoutes (OutputInterface $ output , $ routes = null , $ showControllers = false )
80
82
{
81
83
if (null === $ routes ) {
82
84
$ routes = $ this ->getContainer ()->get ('router ' )->getRouteCollection ()->all ();
@@ -88,26 +90,52 @@ protected function outputRoutes(OutputInterface $output, $routes = null)
88
90
$ maxMethod = strlen ('method ' );
89
91
$ maxScheme = strlen ('scheme ' );
90
92
$ maxHost = strlen ('host ' );
93
+ $ maxPath = strlen ('path ' );
91
94
92
95
foreach ($ routes as $ name => $ route ) {
93
96
$ method = $ route ->getMethods () ? implode ('| ' , $ route ->getMethods ()) : 'ANY ' ;
94
97
$ scheme = $ route ->getSchemes () ? implode ('| ' , $ route ->getSchemes ()) : 'ANY ' ;
95
98
$ host = '' !== $ route ->getHost () ? $ route ->getHost () : 'ANY ' ;
99
+ $ path = $ route ->getPath ();
96
100
$ maxName = max ($ maxName , strlen ($ name ));
97
101
$ maxMethod = max ($ maxMethod , strlen ($ method ));
98
102
$ maxScheme = max ($ maxScheme , strlen ($ scheme ));
99
103
$ maxHost = max ($ maxHost , strlen ($ host ));
104
+ $ maxPath = max ($ maxPath , strlen ($ path ));
100
105
}
101
106
102
107
$ 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
+ }
105
120
106
121
foreach ($ routes as $ name => $ route ) {
107
122
$ method = $ route ->getMethods () ? implode ('| ' , $ route ->getMethods ()) : 'ANY ' ;
108
123
$ scheme = $ route ->getSchemes () ? implode ('| ' , $ route ->getSchemes ()) : 'ANY ' ;
109
124
$ 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
+ }
111
139
}
112
140
}
113
141
0 commit comments