1616use function Tempest \map ;
1717
1818#[Priority(Priority::FRAMEWORK - 10 )]
19- final class MatchRouteMiddleware implements HttpMiddleware
19+ final readonly class MatchRouteMiddleware implements HttpMiddleware
2020{
2121 public function __construct (
2222 private RouteMatcher $ routeMatcher ,
@@ -25,22 +25,28 @@ public function __construct(
2525
2626 public function __invoke (Request $ request , HttpMiddlewareCallable $ next ): Response
2727 {
28- $ psrRequest = map ($ request )->with (RequestToPsrRequestMapper::class)->do ();
29-
30- $ matchedRoute = $ this ->routeMatcher ->match ($ psrRequest );
28+ $ matchedRoute = $ this ->routeMatcher ->match ($ request );
3129
3230 if ($ matchedRoute === null ) {
3331 return new NotFound ();
3432 }
3533
34+ // We register the matched route in the container, some internal framework components will need it
3635 $ this ->container ->singleton (MatchedRoute::class, fn () => $ matchedRoute );
3736
38- $ request = $ this ->resolveRequest ($ psrRequest , $ matchedRoute );
37+ // Convert the request to a specific request implementation, if needed
38+ $ request = $ this ->resolveRequest ($ request , $ matchedRoute );
39+
40+ // We register this newly created request object in the container
41+ // This makes it so that RequestInitializer is bypassed entirely when the controller action needs the request class
42+ // Making it so that we don't need to set any $_SERVER variables and stuff like that
43+ $ this ->container ->singleton (Request::class, fn () => $ request );
44+ $ this ->container ->singleton ($ request ::class, fn () => $ request );
3945
4046 return $ next ($ request );
4147 }
4248
43- private function resolveRequest (PsrRequest $ psrRequest , MatchedRoute $ matchedRoute ): Request
49+ private function resolveRequest (Request $ request , MatchedRoute $ matchedRoute ): Request
4450 {
4551 // Let's find out if our input request data matches what the route's action needs
4652 $ requestClass = GenericRequest::class;
@@ -56,20 +62,10 @@ private function resolveRequest(PsrRequest $psrRequest, MatchedRoute $matchedRou
5662 }
5763 }
5864
59- // We map the original request we got into this method to the right request class
60- /** @var \Tempest\Http\GenericRequest $request */
61- $ request = map ($ psrRequest )->with (PsrRequestToGenericRequestMapper::class)->do ();
62-
6365 if ($ requestClass !== Request::class && $ requestClass !== GenericRequest::class) {
6466 $ request = map ($ request )->with (RequestToObjectMapper::class)->to ($ requestClass );
6567 }
6668
67- // Next, we register this newly created request object in the container
68- // This makes it so that RequestInitializer is bypassed entirely when the controller action needs the request class
69- // Making it so that we don't need to set any $_SERVER variables and stuff like that
70- $ this ->container ->singleton (Request::class, fn () => $ request );
71- $ this ->container ->singleton ($ request ::class, fn () => $ request );
72-
7369 return $ request ;
7470 }
7571}
0 commit comments