feat: refactor middleware classes to extend a base Middleware class and improve route callable extraction
Some checks failed
🧪✨ Tests Workflow / 🧪 ✨ Database Migrations (push) Failing after 1m32s
🧪✨ Tests Workflow / 🛡️ 🔒 License Check (push) Successful in 1m31s
🧪✨ Tests Workflow / 📝 ✨ Code Lint (push) Successful in 1m37s
🧪✨ Tests Workflow / 🛡️ 🔒 Library Audit (push) Successful in 1m48s
🧪✨ Tests Workflow / 🐙 🔍 Code Sniffer (push) Failing after 1m48s
🧪✨ Tests Workflow / 🧪 ✅ Unit Tests (push) Successful in 1m24s

This commit is contained in:
2025-11-11 09:40:56 -05:00
parent 8ccaaadf03
commit c6567bd3a5
3 changed files with 63 additions and 48 deletions

View File

@@ -18,48 +18,35 @@ use Lcobucci\JWT\Validation\Constraint\SignedWith;
use Lcobucci\JWT\Validation\Constraint\StrictValidAt;
use Lcobucci\JWT\Validation\RequiredConstraintsViolated;
use League\Route\Dispatcher;
use League\Route\Route;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Siteworxpro\App\Annotations\Guards\Jwt;
use Siteworxpro\App\Controllers\Controller;
use Siteworxpro\App\Http\JsonResponseFactory;
use Siteworxpro\App\Services\Facades\Config;
use Siteworxpro\HttpStatus\CodesEnum;
class JwtMiddleware implements MiddlewareInterface
class JwtMiddleware extends Middleware
{
/**
* @throws \JsonException
* @throws \Exception
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface|Dispatcher $handler): ResponseInterface
{
public function process(
ServerRequestInterface $request,
RequestHandlerInterface|Dispatcher $handler
): ResponseInterface {
if (!$handler instanceof Dispatcher) {
$callable = $this->extractRouteCallable($request, $handler);
if ($callable === null) {
return $handler->handle($request);
}
/** @var Route | null $lastSegment */
$lastSegment = array_last($handler->getMiddlewareStack());
/** @var $class Controller */
[$class, $method] = $callable;
if ($lastSegment === null) {
return $handler->handle($request);
}
$callable = $lastSegment->getCallable();
$class = null;
$method = null;
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)) {
if (class_exists($class::class)) {
$reflectionClass = new \ReflectionClass($class);
if ($reflectionClass->hasMethod($method)) {

View File

@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace Siteworxpro\App\Http\Middleware;
use League\Route\Dispatcher;
use League\Route\Route;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
abstract class Middleware implements MiddlewareInterface
{
protected function extractRouteCallable($request, RequestHandlerInterface | Dispatcher $handler): array|null
{
if (!$handler instanceof Dispatcher) {
return null;
}
/** @var Route | null $lastSegment */
$lastSegment = array_last($handler->getMiddlewareStack());
if ($lastSegment === null) {
return $handler->handle($request);
}
$callable = $lastSegment->getCallable();
$class = null;
$method = null;
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);
}
return [$class, $method];
}
}

View File

@@ -5,45 +5,32 @@ declare(strict_types=1);
namespace Siteworxpro\App\Http\Middleware;
use League\Route\Dispatcher;
use League\Route\Route;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Siteworxpro\App\Annotations\Guards\Scope;
use Siteworxpro\App\Controllers\Controller;
use Siteworxpro\App\Http\JsonResponseFactory;
use Siteworxpro\HttpStatus\CodesEnum;
class ScopeMiddleware implements MiddlewareInterface
class ScopeMiddleware extends Middleware
{
/**
* @throws \JsonException
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface | Dispatcher $handler): ResponseInterface
{
if (!$handler instanceof Dispatcher) {
public function process(
ServerRequestInterface $request,
RequestHandlerInterface | Dispatcher $handler
): ResponseInterface {
$callable = $this->extractRouteCallable($request, $handler);
if ($callable === null) {
return $handler->handle($request);
}
/** @var Route | null $lastSegment */
$lastSegment = array_last($handler->getMiddlewareStack());
/** @var $class Controller */
[$class, $method] = $callable;
if ($lastSegment === null) {
return $handler->handle($request);
}
$callable = $lastSegment->getCallable();
$class = null;
$method = null;
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)) {
if (class_exists($class::class)) {
$reflectionClass = new \ReflectionClass($class);
if ($reflectionClass->hasMethod($method)) {
$reflectionMethod = $reflectionClass->getMethod($method);