@@ -55,6 +55,7 @@ protected function configure(): void
55
55
new InputOption ('show-aliases ' , null , InputOption::VALUE_NONE , 'Show aliases in overview ' ),
56
56
new InputOption ('format ' , null , InputOption::VALUE_REQUIRED , \sprintf ('The output format ("%s") ' , implode ('", " ' , $ this ->getAvailableFormatOptions ())), 'txt ' ),
57
57
new InputOption ('raw ' , null , InputOption::VALUE_NONE , 'To output raw route(s) ' ),
58
+ new InputOption ('method ' , null , InputOption::VALUE_REQUIRED , 'Filter by HTTP method ' , null , ['GET ' , 'POST ' , 'PUT ' , 'DELETE ' , 'PATCH ' ]),
58
59
])
59
60
->setHelp (<<<'EOF'
60
61
The <info>%command.name%</info> displays the configured routes:
@@ -76,6 +77,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
76
77
{
77
78
$ io = new SymfonyStyle ($ input , $ output );
78
79
$ name = $ input ->getArgument ('name ' );
80
+ $ methodOption = $ input ->getOption ('method ' );
81
+ $ method = $ methodOption ? strtoupper ($ methodOption ) : null ;
79
82
$ helper = new DescriptorHelper ($ this ->fileLinkFormatter );
80
83
$ routes = $ this ->router ->getRouteCollection ();
81
84
$ container = null ;
@@ -85,7 +88,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
85
88
86
89
if ($ name ) {
87
90
$ route = $ routes ->get ($ name );
88
- $ matchingRoutes = $ this ->findRouteNameContaining ($ name , $ routes );
91
+ $ matchingRoutes = $ this ->findRouteNameContaining ($ name , $ routes, $ method );
89
92
90
93
if (!$ input ->isInteractive () && !$ route && \count ($ matchingRoutes ) > 1 ) {
91
94
$ helper ->describe ($ io , $ this ->findRouteContaining ($ name , $ routes ), [
@@ -94,6 +97,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
94
97
'show_controllers ' => $ input ->getOption ('show-controllers ' ),
95
98
'show_aliases ' => $ input ->getOption ('show-aliases ' ),
96
99
'output ' => $ io ,
100
+ 'method ' => $ method ,
97
101
]);
98
102
99
103
return 0 ;
@@ -124,24 +128,45 @@ protected function execute(InputInterface $input, OutputInterface $output): int
124
128
'show_aliases ' => $ input ->getOption ('show-aliases ' ),
125
129
'output ' => $ io ,
126
130
'container ' => $ container ,
131
+ 'method ' => $ method ,
127
132
]);
128
133
}
129
134
130
135
return 0 ;
131
136
}
132
137
133
- private function findRouteNameContaining (string $ name , RouteCollection $ routes ): array
138
+ private function findRouteNameContaining (string $ name , RouteCollection $ routes, ? string $ method ): array
134
139
{
135
140
$ foundRoutesNames = [];
136
141
foreach ($ routes as $ routeName => $ route ) {
137
- if (false !== stripos ($ routeName , $ name )) {
142
+ if (
143
+ false !== stripos ($ routeName , $ name )
144
+ && (null === $ method || $ this ->isMethodValidForRoute ($ method , $ route ->getMethods ()))
145
+ ) {
138
146
$ foundRoutesNames [] = $ routeName ;
139
147
}
140
148
}
141
149
142
150
return $ foundRoutesNames ;
143
151
}
144
152
153
+ /**
154
+ * Checks if the given HTTP method is valid for the route or if the route has no method restrictions.
155
+ *
156
+ * @param string $method The HTTP method to check (e.g., 'GET', 'POST'). This parameter is required and cannot be null.
157
+ * @param array $routeMethods An array of HTTP methods that the route supports (e.g., ['GET', 'POST']). If the array is empty, the route has no method restrictions.
158
+ *
159
+ * @return bool returns `true` if the method is valid for the route, or if the route has no specific method restrictions; `false` otherwise
160
+ */
161
+ private function isMethodValidForRoute (string $ method , array $ routeMethods ): bool
162
+ {
163
+ if (0 === \count ($ routeMethods )) {
164
+ return true ;
165
+ }
166
+
167
+ return \in_array ($ method , $ routeMethods , true );
168
+ }
169
+
145
170
public function complete (CompletionInput $ input , CompletionSuggestions $ suggestions ): void
146
171
{
147
172
if ($ input ->mustSuggestArgumentValuesFor ('name ' )) {
0 commit comments