Skip to content

Commit

Permalink
feat: add support for Leaf MVC class middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
mychidarko committed Oct 15, 2024
1 parent 9ad7890 commit a6d6a06
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,9 @@ protected static function mapHandler(

if (is_array($handler)) {
if (is_string($handler['middleware'] ?? null)) {
$parsedOptions['middleware'] = static::$namedMiddleware[$handler['middleware']] ?? null;
$parsedOptions['middleware'] = (class_exists($handler['middleware'])) ? function () use ($handler) {
(new $handler['middleware']())->call();
} : static::$namedMiddleware[$handler['middleware']] ?? null;
}

if (is_array($handler['middleware'] ?? null)) {
Expand Down Expand Up @@ -591,9 +593,11 @@ public static function use($middleware)
// if (in_array($middleware, static::$middleware)) {
// throw new \RuntimeException('Circular Middleware setup detected. Tried to queue the same Middleware twice.');
// }

if (is_string($middleware)) {
$middleware = static::$namedMiddleware[$middleware];
$middleware = class_exists($middleware) ? function () use ($middleware) {
(new $middleware())->call();
} : static::$namedMiddleware[$middleware];
}

$methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD'];
Expand Down

0 comments on commit a6d6a06

Please sign in to comment.