@@ -147,6 +147,7 @@ public function match(Request $request, int $pathOffset = 0, array $options = []
147
147
{
148
148
$ partialResult = $ this ->route ->partialMatch ($ request , $ pathOffset , $ options );
149
149
150
+ // no match, bail out
150
151
if ($ partialResult ->isFailure () && ! $ partialResult ->isMethodFailure ()) {
151
152
return RouteResult::fromRouteFailure ();
152
153
}
@@ -162,54 +163,54 @@ public function match(Request $request, int $pathOffset = 0, array $options = []
162
163
return RouteResult::fromMethodFailure ($ partialResult ->getAllowedMethods ());
163
164
}
164
165
166
+ // @TODO get rid of lazy routes
165
167
if ($ this ->childRoutes !== null ) {
166
168
$ this ->addRoutes ($ this ->childRoutes );
167
169
$ this ->childRoutes = null ;
168
170
}
169
171
170
- $ nextOffset = $ pathOffset + $ partialResult ->getMatchedPathLength ();
171
-
172
- if (isset ($ options ['translator ' ])
173
- && ! isset ($ options ['locale ' ])
174
- && $ partialResult ->isSuccess ()
175
- && null !== ($ locale = $ partialResult ->getMatchedParams ()['locale ' ] ?? null )
176
- ) {
177
- $ options ['locale ' ] = $ locale ;
172
+ // pass matched params to child routes.
173
+ // Could be used for obtaining locale from parent route match.
174
+ if ($ partialResult ->isSuccess ()) {
175
+ $ options ['parent_match_params ' ] = $ options ['parent_match_params ' ] ?? [];
176
+ $ options ['parent_match_params ' ] += $ partialResult ->getMatchedParams ();
178
177
}
179
178
180
- $ gatherMethods = $ options ['gather_allowed_methods ' ] ?? false ;
181
179
if ($ partialResult ->isMethodFailure ()) {
180
+ // we got partial method failure, keep matching to find all allowed methods
182
181
$ options ['gather_allowed_methods ' ] = true ;
183
182
}
184
183
184
+ // match child routes
185
+ $ nextOffset = $ pathOffset + $ partialResult ->getMatchedPathLength ();
185
186
$ result = parent ::match ($ request , $ nextOffset , $ options );
186
187
if ($ result ->isFailure () && ! $ result ->isMethodFailure ()) {
187
188
return $ result ;
188
189
}
189
- if ($ partialResult ->isMethodFailure ()) {
190
- $ allowed = $ partialResult ->getAllowedMethods ();
191
- if (! empty ($ methods = $ result ->getAllowedMethods ())) {
192
- $ allowed = \array_intersect ($ allowed , $ methods );
190
+
191
+ // gather allowed methods
192
+ $ partialAllowed = $ partialResult ->getAllowedMethods ();
193
+ $ childAllowed = $ result ->getAllowedMethods ();
194
+ if (null !== $ partialAllowed && null !== $ childAllowed ) {
195
+ $ allowed = \array_intersect ($ partialAllowed , $ childAllowed );
196
+ } else {
197
+ $ allowed = $ partialAllowed ?? $ childAllowed ;
198
+ }
199
+
200
+ // was it a method failure?
201
+ if ($ partialResult ->isMethodFailure () || $ result ->isMethodFailure ()) {
202
+ if (empty ($ allowed )) {
203
+ return RouteResult::fromRouteFailure ();
193
204
}
194
205
return RouteResult::fromMethodFailure ($ allowed );
195
206
}
196
- if ($ result ->isMethodFailure ()) {
197
- return $ result ;
198
- }
207
+
208
+ // we got success
199
209
$ return = RouteResult::fromRouteMatch (
200
210
\array_merge ($ partialResult ->getMatchedParams (), $ result ->getMatchedParams ()),
201
211
$ result ->getMatchedRouteName ()
202
212
);
203
- $ allowed = $ partialResult ->getAllowedMethods ();
204
- $ nested = $ result ->getAllowedMethods ();
205
- if (! empty ($ allowed ) && ! empty ($ nested )) {
206
- $ allowed = \array_intersect ($ allowed , $ nested );
207
- }
208
- $ allowed = $ allowed ?? $ nested ;
209
- if (! empty ($ allowed )) {
210
- $ return ->withAllowedMethods ($ allowed );
211
- }
212
- return $ return ;
213
+ return $ return ->withAllowedMethods ($ allowed );
213
214
}
214
215
215
216
/**
0 commit comments