Skip to content

Commit 8b26c1c

Browse files
committed
wip
1 parent 314e412 commit 8b26c1c

File tree

4 files changed

+25
-27
lines changed

4 files changed

+25
-27
lines changed

packages/router/src/HandleRouteExceptionMiddleware.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tempest\Router;
44

55
use Tempest\Core\Priority;
6+
use Tempest\Http\HttpException;
67
use Tempest\Http\Request;
78
use Tempest\Http\Response;
89
use Tempest\Http\Responses\Invalid;
@@ -21,6 +22,8 @@ public function __invoke(Request $request, HttpMiddlewareCallable $next): Respon
2122
return new NotFound();
2223
} catch (ValidationException $validationException) {
2324
return new Invalid($validationException->subject, $validationException->failingRules);
25+
// } catch(HttpException $httpException) {
26+
// TODO?
2427
}
2528
}
2629
}

packages/router/src/MatchRouteMiddleware.php

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use 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
}

packages/router/src/Routing/Matching/GenericRouteMatcher.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44

55
namespace Tempest\Router\Routing\Matching;
66

7-
use Psr\Http\Message\ServerRequestInterface as PsrRequest;
7+
use Tempest\Http\Request;
88
use Tempest\Router\Exceptions\InvalidEnumParameterException;
9-
use Tempest\Router\Exceptions\NotFoundException;
109
use Tempest\Router\MatchedRoute;
1110
use Tempest\Router\RouteConfig;
1211
use Tempest\Router\Routing\Construction\DiscoveredRoute;
@@ -17,7 +16,7 @@ public function __construct(
1716
private RouteConfig $routeConfig,
1817
) {}
1918

20-
public function match(PsrRequest $request): ?MatchedRoute
19+
public function match(Request $request): ?MatchedRoute
2120
{
2221
// Try to match routes without any parameters
2322
if (($staticRoute = $this->matchStaticRoute($request)) !== null) {
@@ -28,9 +27,9 @@ public function match(PsrRequest $request): ?MatchedRoute
2827
return $this->matchDynamicRoute($request);
2928
}
3029

31-
private function matchStaticRoute(PsrRequest $request): ?MatchedRoute
30+
private function matchStaticRoute(Request $request): ?MatchedRoute
3231
{
33-
$staticRoute = $this->routeConfig->staticRoutes[$request->getMethod()][$request->getUri()->getPath()] ?? null;
32+
$staticRoute = $this->routeConfig->staticRoutes[$request->method->value][$request->path] ?? null;
3433

3534
if ($staticRoute === null) {
3635
return null;
@@ -39,19 +38,19 @@ private function matchStaticRoute(PsrRequest $request): ?MatchedRoute
3938
return new MatchedRoute($staticRoute, []);
4039
}
4140

42-
private function matchDynamicRoute(PsrRequest $request): ?MatchedRoute
41+
private function matchDynamicRoute(Request $request): ?MatchedRoute
4342
{
4443
// If there are no routes for the given request method, we immediately stop
45-
$routesForMethod = $this->routeConfig->dynamicRoutes[$request->getMethod()] ?? null;
44+
$routesForMethod = $this->routeConfig->dynamicRoutes[$request->method->value] ?? null;
4645
if ($routesForMethod === null) {
4746
return null;
4847
}
4948

5049
// Get matching regex for route
51-
$matchingRegexForMethod = $this->routeConfig->matchingRegexes[$request->getMethod()];
50+
$matchingRegexForMethod = $this->routeConfig->matchingRegexes[$request->method->value];
5251

5352
// Then we'll use this regex to see whether we have a match or not
54-
$matchResult = $matchingRegexForMethod->match($request->getUri()->getPath());
53+
$matchResult = $matchingRegexForMethod->match($request->path);
5554

5655
if ($matchResult === null) {
5756
return null;

packages/router/src/Routing/Matching/RouteMatcher.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace Tempest\Router\Routing\Matching;
66

7-
use Psr\Http\Message\ServerRequestInterface as PsrRequest;
7+
use Tempest\Http\Request;
88
use Tempest\Router\MatchedRoute;
99

1010
interface RouteMatcher
1111
{
12-
public function match(PsrRequest $request): ?MatchedRoute;
12+
public function match(Request $request): ?MatchedRoute;
1313
}

0 commit comments

Comments
 (0)