feat: improve callable handling in JWT and Scope middlewares
All checks were successful
🧪✨ Tests Workflow / 🛡️ 🔒 License Check (push) Successful in 1m15s
🧪✨ Tests Workflow / 🧪 ✨ Database Migrations (push) Successful in 1m31s
🧪✨ Tests Workflow / 📝 ✨ Code Lint (push) Successful in 1m39s
🧪✨ Tests Workflow / 🛡️ 🔒 Library Audit (push) Successful in 1m49s
🧪✨ Tests Workflow / 🐙 🔍 Code Sniffer (push) Successful in 1m43s
🧪✨ Tests Workflow / 🧪 ✅ Unit Tests (push) Successful in 1m10s

This commit is contained in:
2025-11-07 12:00:07 -05:00
parent 29a71d3b19
commit 9f77e70b25
2 changed files with 33 additions and 6 deletions

View File

@@ -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);

View File

@@ -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);