-
-
Notifications
You must be signed in to change notification settings - Fork 142
refactor(router): refactor router logic to middleware #1307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
314e412
wip
brendt 8b26c1c
wip
brendt 0edb80c
wip
brendt e36d4db
wip
brendt cc8ffd7
wip
brendt b3e57c8
wip
brendt e012b0c
wip
brendt 1cd911b
wip
brendt b6a67be
wip
brendt 48a861c
refactor: remove `HttpException` custom responses
innocenzi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 0 additions & 43 deletions
43
packages/router/src/Exceptions/HttpErrorResponseProcessor.php
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <?php | ||
|
|
||
| namespace Tempest\Router\Exceptions; | ||
|
|
||
| use Exception; | ||
|
|
||
| final class NoMatchedRoute extends Exception | ||
| { | ||
| public function __construct() | ||
| { | ||
| parent::__construct('No matched route was registered in the container. Did you remove `\Tempest\Router\MatchRouteMiddleware` from the middleware stack by any chance?'); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| <?php | ||
|
|
||
| namespace Tempest\Router; | ||
|
|
||
| use Tempest\Core\Priority; | ||
| use Tempest\Http\HttpException; | ||
| use Tempest\Http\Request; | ||
| use Tempest\Http\Response; | ||
| use Tempest\Http\Responses\Invalid; | ||
| use Tempest\Http\Responses\NotFound; | ||
| use Tempest\Router\Exceptions\NotFoundException; | ||
| use Tempest\Validation\Exceptions\ValidationException; | ||
|
|
||
| #[Priority(Priority::FRAMEWORK - 10)] | ||
| final readonly class HandleRouteExceptionMiddleware implements HttpMiddleware | ||
| { | ||
| public function __construct( | ||
| private RouteConfig $routeConfig, | ||
| ) {} | ||
|
|
||
| public function __invoke(Request $request, HttpMiddlewareCallable $next): Response | ||
| { | ||
| if ($this->routeConfig->throwHttpExceptions === true) { | ||
| $response = $next($request); | ||
|
|
||
| if ($response->status->isServerError() || $response->status->isClientError()) { | ||
brendt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| throw new HttpException( | ||
| status: $response->status, | ||
| cause: $response, | ||
| ); | ||
| } | ||
|
|
||
| return $response; | ||
| } | ||
|
|
||
| try { | ||
brendt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return $next($request); | ||
| } catch (NotFoundException) { | ||
| return new NotFound(); | ||
| } catch (ValidationException $validationException) { | ||
| return new Invalid($validationException->subject, $validationException->failingRules); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| <?php | ||
|
|
||
| namespace Tempest\Router; | ||
|
|
||
| use Tempest\Container\Container; | ||
| use Tempest\Core\Middleware; | ||
| use Tempest\Core\Priority; | ||
| use Tempest\Http\Request; | ||
| use Tempest\Http\Response; | ||
|
|
||
| #[Priority(Priority::LOWEST)] | ||
| final readonly class HandleRouteSpecificMiddleware implements HttpMiddleware | ||
| { | ||
| public function __construct( | ||
| private MatchedRoute $matchedRoute, | ||
| private Container $container, | ||
| ) {} | ||
|
|
||
| public function __invoke(Request $request, HttpMiddlewareCallable $next): Response | ||
brendt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| $middlewareStack = new Middleware(...$this->matchedRoute->route->middleware); | ||
|
|
||
| $callable = new HttpMiddlewareCallable(fn (Request $request) => $next($request)); | ||
|
|
||
| foreach ($middlewareStack->unwrap() as $middlewareClass) { | ||
| $callable = new HttpMiddlewareCallable(function (Request $request) use ($middlewareClass, $callable) { | ||
| /** @var HttpMiddleware $middleware */ | ||
| $middleware = $this->container->get($middlewareClass->getName()); | ||
|
|
||
| return $middleware($request, $callable); | ||
| }); | ||
| } | ||
|
|
||
| return $callable($request); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| <?php | ||
|
|
||
| namespace Tempest\Router; | ||
|
|
||
| use Tempest\Container\Container; | ||
| use Tempest\Core\Priority; | ||
| use Tempest\Http\GenericRequest; | ||
| use Tempest\Http\Mappers\RequestToObjectMapper; | ||
| use Tempest\Http\Request; | ||
| use Tempest\Http\Response; | ||
| use Tempest\Http\Responses\NotFound; | ||
| use Tempest\Router\Routing\Matching\RouteMatcher; | ||
|
|
||
| use function Tempest\map; | ||
|
|
||
| #[Priority(Priority::FRAMEWORK - 9)] | ||
| final readonly class MatchRouteMiddleware implements HttpMiddleware | ||
brendt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| public function __construct( | ||
| private RouteMatcher $routeMatcher, | ||
| private Container $container, | ||
| ) {} | ||
|
|
||
| public function __invoke(Request $request, HttpMiddlewareCallable $next): Response | ||
| { | ||
| $matchedRoute = $this->routeMatcher->match($request); | ||
|
|
||
| if ($matchedRoute === null) { | ||
| return new NotFound(); | ||
| } | ||
|
|
||
| // We register the matched route in the container, some internal framework components will need it | ||
| $this->container->singleton(MatchedRoute::class, fn () => $matchedRoute); | ||
|
|
||
| // Convert the request to a specific request implementation, if needed | ||
| $request = $this->resolveRequest($request, $matchedRoute); | ||
|
|
||
| // We register this newly created request object in the container | ||
| // This makes it so that RequestInitializer is bypassed entirely when the controller action needs the request class | ||
| // Making it so that we don't need to set any $_SERVER variables and stuff like that | ||
| $this->container->singleton(Request::class, fn () => $request); | ||
| $this->container->singleton($request::class, fn () => $request); | ||
|
|
||
| return $next($request); | ||
| } | ||
|
|
||
| private function resolveRequest(Request $request, MatchedRoute $matchedRoute): Request | ||
| { | ||
| // Let's find out if our input request data matches what the route's action needs | ||
| $requestClass = GenericRequest::class; | ||
|
|
||
| // We'll loop over all the handler's parameters | ||
| foreach ($matchedRoute->route->handler->getParameters() as $parameter) { | ||
| // If the parameter's type is an instance of Request… | ||
| if ($parameter->getType()->matches(Request::class)) { | ||
| // We'll use that specific request class | ||
| $requestClass = $parameter->getType()->getName(); | ||
|
|
||
| break; | ||
| } | ||
| } | ||
|
|
||
| if ($requestClass !== Request::class && $requestClass !== GenericRequest::class) { | ||
| $request = map($request)->with(RequestToObjectMapper::class)->to($requestClass); | ||
| } | ||
|
|
||
| return $request; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.