3838 */
3939class TextDescriptor extends Descriptor
4040{
41+ private const VERB_COLORS = [
42+ 'ANY ' => 'default ' ,
43+ 'GET ' => 'blue ' ,
44+ 'QUERY ' => 'blue ' ,
45+ 'HEAD ' => 'magenta ' ,
46+ 'OPTIONS ' => 'blue ' ,
47+ 'POST ' => 'green ' ,
48+ 'PUT ' => 'yellow ' ,
49+ 'PATCH ' => 'yellow ' ,
50+ 'DELETE ' => 'red ' ,
51+ ];
52+
4153 public function __construct (
4254 private ?FileLinkFormatter $ fileLinkFormatter = null ,
4355 ) {
4456 }
4557
4658 protected function describeRouteCollection (RouteCollection $ routes , array $ options = []): void
4759 {
48- $ showControllers = isset ($ options ['show_controllers ' ]) && $ options ['show_controllers ' ];
49-
50- $ tableHeaders = ['Name ' , 'Method ' , 'Scheme ' , 'Host ' , 'Path ' ];
51- if ($ showControllers ) {
52- $ tableHeaders [] = 'Controller ' ;
53- }
54-
55- if ($ showAliases = $ options ['show_aliases ' ] ?? false ) {
56- $ tableHeaders [] = 'Aliases ' ;
57- }
60+ $ showAliases = $ options ['show_aliases ' ] ?? false ;
61+ $ showControllers = $ options ['show_controllers ' ] ?? false ;
5862
5963 $ tableRows = [];
64+ $ shouldShowScheme = false ;
65+ $ shouldShowHost = false ;
6066 foreach ($ routes ->all () as $ name => $ route ) {
6167 $ controller = $ route ->getDefault ('_controller ' );
6268
69+ $ scheme = $ route ->getSchemes () ? implode ('| ' , $ route ->getSchemes ()) : 'ANY ' ;
70+ $ shouldShowScheme = $ shouldShowScheme || 'ANY ' !== $ scheme ;
71+
72+ $ host = '' !== $ route ->getHost () ? $ route ->getHost () : 'ANY ' ;
73+ $ shouldShowHost = $ shouldShowHost || 'ANY ' !== $ host ;
74+
6375 $ row = [
64- $ name ,
65- $ route -> getMethods () ? implode ( ' | ' , $ route ->getMethods ()) : ' ANY ' ,
66- $ route -> getSchemes () ? implode ( ' | ' , $ route -> getSchemes ()) : ' ANY ' ,
67- '' !== $ route -> getHost () ? $ route -> getHost () : ' ANY ' ,
68- $ this -> formatControllerLink ( $ controller , $ route ->getPath (), $ options [ ' container ' ] ?? null ),
76+ ' Name ' => $ name ,
77+ ' Methods ' => $ this -> formatMethods ( $ route ->getMethods ()),
78+ ' Scheme ' => $ scheme ,
79+ 'Host ' => $ host ,
80+ ' Path ' => $ route ->getPath (),
6981 ];
7082
7183 if ($ showControllers ) {
72- $ row [] = $ controller ? $ this ->formatControllerLink ($ controller , $ this ->formatCallable ($ controller ), $ options ['container ' ] ?? null ) : '' ;
84+ $ row [' Controller ' ] = $ controller ? $ this ->formatControllerLink ($ controller , $ this ->formatCallable ($ controller ), $ options ['container ' ] ?? null ) : '' ;
7385 }
7486
7587 if ($ showAliases ) {
76- $ row [] = implode ('| ' , ( $ reverseAliases ??= $ this ->getReverseAliases ($ routes) )[$ name ] ?? []);
88+ $ row [' Aliases ' ] = implode ('| ' , $ this ->getReverseAliases ($ routes )[$ name ] ?? []);
7789 }
7890
7991 $ tableRows [] = $ row ;
8092 }
8193
94+ $ tableHeaders = ['Name ' , 'Method ' ];
95+
96+ if ($ shouldShowScheme ) {
97+ $ tableHeaders [] = 'Scheme ' ;
98+ } else {
99+ array_walk ($ tableRows , function (&$ row ) { unset($ row ['Scheme ' ]); });
100+ }
101+
102+ if ($ shouldShowHost ) {
103+ $ tableHeaders [] = 'Host ' ;
104+ } else {
105+ array_walk ($ tableRows , function (&$ row ) { unset($ row ['Host ' ]); });
106+ }
107+
108+ $ tableHeaders [] = 'Path ' ;
109+
110+ if ($ showControllers ) {
111+ $ tableHeaders [] = 'Controller ' ;
112+ }
113+
114+ if ($ showAliases ) {
115+ $ tableHeaders [] = 'Aliases ' ;
116+ }
117+
82118 if (isset ($ options ['output ' ])) {
83119 $ options ['output ' ]->table ($ tableHeaders , $ tableRows );
84120 } else {
@@ -103,7 +139,7 @@ protected function describeRoute(Route $route, array $options = []): void
103139 ['Host ' , '' !== $ route ->getHost () ? $ route ->getHost () : 'ANY ' ],
104140 ['Host Regex ' , '' !== $ route ->getHost () ? $ route ->compile ()->getHostRegex () : '' ],
105141 ['Scheme ' , $ route ->getSchemes () ? implode ('| ' , $ route ->getSchemes ()) : 'ANY ' ],
106- ['Method ' , $ route -> getMethods () ? implode ( ' | ' , $ route ->getMethods ()) : ' ANY ' ],
142+ ['Method ' , $ this -> formatMethods ( $ route ->getMethods ())],
107143 ['Requirements ' , $ route ->getRequirements () ? $ this ->formatRouterConfig ($ route ->getRequirements ()) : 'NO CUSTOM ' ],
108144 ['Class ' , $ route ::class],
109145 ['Defaults ' , $ this ->formatRouterConfig ($ defaults )],
@@ -576,6 +612,21 @@ private function formatRouterConfig(array $config): string
576612 return trim ($ configAsString );
577613 }
578614
615+ /**
616+ * @param array<string> $methods
617+ */
618+ private function formatMethods (array $ methods ): string
619+ {
620+ if ([] === $ methods ) {
621+ $ methods = ['ANY ' ];
622+ }
623+
624+ return implode ('| ' , array_map (
625+ fn (string $ method ): string => \sprintf ('<fg=%s>%s</> ' , self ::VERB_COLORS [$ method ] ?? 'default ' , $ method ),
626+ $ methods
627+ ));
628+ }
629+
579630 /**
580631 * @param (callable():ContainerBuilder)|null $getContainer
581632 */
0 commit comments