summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Router.php15
-rw-r--r--tests/Unit/RouterTest.php2
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();