From 923484c7accc1b6c3c829c791944c1611aec3834 Mon Sep 17 00:00:00 2001 From: flashwave Date: Fri, 8 Sep 2023 00:09:23 +0000 Subject: [PATCH] Added middleware support to attribute registry. --- VERSION | 2 +- src/Routing/Route.php | 44 +++++++++++++++++++++++++++++-------------- tests/RouterTest.php | 13 ++++++++++++- 3 files changed, 43 insertions(+), 16 deletions(-) diff --git a/VERSION b/VERSION index 907753d..860e4be 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2309.72356 +0.2309.80009 diff --git a/src/Routing/Route.php b/src/Routing/Route.php index b6a51e6..fac985a 100644 --- a/src/Routing/Route.php +++ b/src/Routing/Route.php @@ -1,7 +1,7 @@ method = null; + $this->path = $pathOrMethod; + } else { + $this->method = $pathOrMethod; + $this->path = $path; + } + } /** * Returns the target method name. * - * @return string + * @return ?string */ - public function getMethod(): string { + public function getMethod(): ?string { return $this->method; } + /** + * Whether this route should be used as middleware. + * + * @return bool + */ + public function isMiddleware(): bool { + return $this->method === null; + } + /** * Returns the target path. * @@ -52,13 +69,12 @@ class Route { foreach($attrInfos as $attrInfo) { $routeInfo = $attrInfo->newInstance(); - $router->add( - $routeInfo->getMethod(), - $routeInfo->getPath(), - $methodInfo->getClosure( - $methodInfo->isStatic() ? null : $handler - ) - ); + $closure = $methodInfo->getClosure($methodInfo->isStatic() ? null : $handler); + + if($routeInfo->isMiddleware()) + $router->use($routeInfo->getPath(), $closure); + else + $router->add($routeInfo->getMethod(), $routeInfo->getPath(), $closure); } } } diff --git a/tests/RouterTest.php b/tests/RouterTest.php index f47cd77..3e047f3 100644 --- a/tests/RouterTest.php +++ b/tests/RouterTest.php @@ -1,7 +1,7 @@ assertEquals('static', $router->resolve('PUT', '/static')->run()); $this->assertEquals('meow', $router->resolve('GET', '/meow')->run()); $this->assertEquals('meow', $router->resolve('POST', '/meow')->run()); + $this->assertEquals('this intercepts', $router->resolve('GET', '/mw')->run()); } }