diff options
| author | Sam Light <sam@lightscale.co.uk> | 2026-06-10 19:00:33 +0100 |
|---|---|---|
| committer | Sam Light <sam@lightscale.co.uk> | 2026-06-10 19:00:33 +0100 |
| commit | fe39ea0aa5a6993ad5edaa6f0f338763ed610ec9 (patch) | |
| tree | a466975d5194c89dd817437654fb5c3c84b7699d | |
| parent | 1a9da09ccc68c7a8fdc8a1d9989e8916ac442f9e (diff) | |
Make it so the router will call the middleware
| -rw-r--r-- | src/Router.php | 15 | ||||
| -rw-r--r-- | tests/Unit/RouterTest.php | 2 |
2 files changed, 16 insertions, 1 deletions
diff --git a/src/Router.php b/src/Router.php index 46eadd8..d70b278 100644 --- a/src/Router.php +++ b/src/Router.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Lightscale\Router; +use Lightscale\Router\Contracts\Middleware; use Lightscale\Router\Contracts\Strategy; use Lightscale\Router\Enums\HttpMethod; use Lightscale\Router\Enums\PathSegmentType; @@ -117,17 +118,29 @@ class Router ); } + /** @return Middleware[] */ + private function getRouteMiddleware(Route $route): array + { + return [ + ...$this->getMiddleware(), + ...($route->getGroup() ?? []), + ...$route->getMiddleware(), + ]; + return []; + } + public function dispatch(RequestInterface $request): ResponseInterface { $uri = $request->getUri(); $match = $this->findRoute($request->getMethod(), $uri->getPath()); + if (null === $match) { return $this->strategy->notFound($request); } return $this->strategy->runMiddleware( $request, - [], + $this->getRouteMiddleware($match->route), fn ($request) => $this->strategy->runRoute(new RouteCall( $request, $match->route, diff --git a/tests/Unit/RouterTest.php b/tests/Unit/RouterTest.php index 4a92381..66b745f 100644 --- a/tests/Unit/RouterTest.php +++ b/tests/Unit/RouterTest.php @@ -340,3 +340,5 @@ it('builds routes in groups', function () { it('can create a group with middleware') ->expect((new Router())->middleware(new TestMiddleware())) ->toBeInstanceOf(Group::class); + +it('runs middleware on dispatch')->todo(); |
