From 9f77e70b25b43e503f683a0c4dc16b15053788d5 Mon Sep 17 00:00:00 2001 From: Ron Rise Date: Fri, 7 Nov 2025 12:00:07 -0500 Subject: [PATCH] feat: improve callable handling in JWT and Scope middlewares --- src/Http/Middleware/JwtMiddleware.php | 20 +++++++++++++++++--- src/Http/Middleware/ScopeMiddleware.php | 19 ++++++++++++++++--- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/Http/Middleware/JwtMiddleware.php b/src/Http/Middleware/JwtMiddleware.php index 67d5671..110377f 100644 --- a/src/Http/Middleware/JwtMiddleware.php +++ b/src/Http/Middleware/JwtMiddleware.php @@ -36,6 +36,11 @@ class JwtMiddleware implements MiddlewareInterface */ public function process(ServerRequestInterface $request, RequestHandlerInterface|Dispatcher $handler): ResponseInterface { + + if (!$handler instanceof Dispatcher) { + return $handler->handle($request); + } + /** @var Route | null $lastSegment */ $lastSegment = array_last($handler->getMiddlewareStack()); @@ -43,10 +48,19 @@ class JwtMiddleware implements MiddlewareInterface return $handler->handle($request); } - [$class, $method] = $lastSegment->getCallable(); + $callable = $lastSegment->getCallable(); + $class = null; + $method = null; - if (class_exists($class::class)) { - $reflectionClass = new \ReflectionClass($class::class); + if (is_array($callable) && count($callable) === 2) { + [$class, $method] = $callable; + } elseif (is_string($callable)) { + // Handle the case where the callable is a string (e.g., 'ClassName::methodName') + [$class, $method] = explode('::', $callable); + } + + if (class_exists($class)) { + $reflectionClass = new \ReflectionClass($class); if ($reflectionClass->hasMethod($method)) { $reflectionMethod = $reflectionClass->getMethod($method); diff --git a/src/Http/Middleware/ScopeMiddleware.php b/src/Http/Middleware/ScopeMiddleware.php index 13348fe..0b30133 100644 --- a/src/Http/Middleware/ScopeMiddleware.php +++ b/src/Http/Middleware/ScopeMiddleware.php @@ -21,6 +21,10 @@ class ScopeMiddleware implements MiddlewareInterface */ public function process(ServerRequestInterface $request, RequestHandlerInterface | Dispatcher $handler): ResponseInterface { + if (!$handler instanceof Dispatcher) { + return $handler->handle($request); + } + /** @var Route | null $lastSegment */ $lastSegment = array_last($handler->getMiddlewareStack()); @@ -28,10 +32,19 @@ class ScopeMiddleware implements MiddlewareInterface return $handler->handle($request); } - [$class, $method] = $lastSegment->getCallable(); + $callable = $lastSegment->getCallable(); + $class = null; + $method = null; - if (class_exists($class::class)) { - $reflectionClass = new \ReflectionClass($class::class); + if (is_array($callable) && count($callable) === 2) { + [$class, $method] = $callable; + } elseif (is_string($callable)) { + // Handle the case where the callable is a string (e.g., 'ClassName::methodName') + [$class, $method] = explode('::', $callable); + } + + if (class_exists($class)) { + $reflectionClass = new \ReflectionClass($class); if ($reflectionClass->hasMethod($method)) { $reflectionMethod = $reflectionClass->getMethod($method); $attributes = $reflectionMethod->getAttributes(Scope::class);