chore: added documentation #16

Merged
rrise merged 1 commits from feat/documenation into master 2025-11-13 04:14:17 +00:00
28 changed files with 514 additions and 84 deletions

View File

@@ -83,6 +83,7 @@ services:
postgres: postgres:
condition: service_healthy condition: service_healthy
environment: environment:
QUEUE_BROKER: kafka
PHP_IDE_CONFIG: serverName=localhost PHP_IDE_CONFIG: serverName=localhost
WORKERS: 1 WORKERS: 1
DEBUG: 1 DEBUG: 1

View File

@@ -6,16 +6,28 @@ namespace Siteworxpro\App\Annotations\Async;
use Attribute; use Attribute;
/**
* Attribute to mark a class as a handler for a specific message class in an async workflow.
*
* Repeatable: attach multiple times to handle multiple message classes.
*/
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)] #[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
readonly class HandlesMessage readonly class HandlesMessage
{ {
/**
* Create a new HandlesMessage attribute.
*
* @param class-string $messageClass Fully-qualified class name of the message handled.
*/
public function __construct( public function __construct(
public string $messageClass, public string $messageClass,
) { ) {
} }
/** /**
* @return string * Get the fully-qualified message class this handler processes.
*
* @return class-string
*/ */
public function getMessageClass(): string public function getMessageClass(): string
{ {

View File

@@ -6,9 +6,22 @@ namespace Siteworxpro\App\Annotations\Events;
use Attribute; use Attribute;
/**
* Attribute to mark a class as an event listener for a specific event class.
*
* Apply this attribute to classes that subscribe to domain or application events.
* Repeatable: can be attached multiple times to the same class to listen for multiple events.
*
* Targets: class only.
*/
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)] #[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
readonly class ListensFor readonly class ListensFor
{ {
/**
* Initialize the ListensFor attribute.
*
* @param class-string $eventClass Fully-qualified class name of the event to listen for.
*/
public function __construct(public string $eventClass) public function __construct(public string $eventClass)
{ {
} }

View File

@@ -7,22 +7,47 @@ namespace Siteworxpro\App\Annotations\Guards;
use Attribute; use Attribute;
use Siteworxpro\App\Services\Facades\Config; use Siteworxpro\App\Services\Facades\Config;
/**
* Attribute to guard classes or methods with JWT claim requirements.
*
* Apply this attribute to a class or method to declare the expected JWT issuer and/or audience.
* If either the issuer or audience is an empty string, the value will be resolved from configuration:
* - `jwt.issuer`
* - `jwt.audience`
*
* Targets: class or method.
*/
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)] #[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)]
readonly class Jwt readonly class Jwt
{ {
/**
* Initialize the Jwt attribute with optional overrides for expected JWT claims.
*
* @param string $issuer Optional expected JWT issuer (`iss`). Empty string uses `Config::get('jwt.issuer')`.
* @param string $audience Optional expected JWT audience (`aud`). Empty string uses `Config::get('jwt.audience')`.
*/
public function __construct( public function __construct(
private string $issuer = '', private string $issuer = '',
private string $audience = '', private string $audience = '',
) { ) {
} }
/**
* Get the required audience from configuration, ignoring any local override.
*
* @return string The globally configured audience or an empty string if not set.
*/
public function getRequiredAudience(): string public function getRequiredAudience(): string
{ {
return Config::get('jwt.audience') ?? ''; return Config::get('jwt.audience') ?? '';
} }
/** /**
* @return string * Get the expected audience for validation.
*
* Returns the constructor-provided audience when non-empty; otherwise falls back to `jwt.audience` config.
*
* @return string The audience value to enforce.
*/ */
public function getAudience(): string public function getAudience(): string
{ {
@@ -33,6 +58,13 @@ readonly class Jwt
return $this->audience; return $this->audience;
} }
/**
* Get the expected issuer for validation.
*
* Returns the constructor-provided issuer when non-empty; otherwise falls back to `jwt.issuer` config.
*
* @return string The issuer value to enforce.
*/
public function getIssuer(): string public function getIssuer(): string
{ {
if ($this->issuer === '') { if ($this->issuer === '') {

View File

@@ -11,8 +11,8 @@ readonly class Scope
{ {
public function __construct( public function __construct(
private array $scopes = [] private array $scopes = []
) {} ) {
}
public function getScopes(): array public function getScopes(): array
{ {

View File

@@ -60,6 +60,13 @@ class Kafka extends Broker
return null; return null;
} }
if ($kafkaMessage->err === RD_KAFKA_RESP_ERR_UNKNOWN_TOPIC_OR_PART) {
throw new \RuntimeException(
"Topic '{$queue->queueName()}' or partition does not exist. Kafka does not auto-create topics" .
" unless configured to do so."
);
}
/** @var string | null $messageData */ /** @var string | null $messageData */
$messageData = $kafkaMessage->payload; $messageData = $kafkaMessage->payload;
if ($messageData !== null) { if ($messageData !== null) {

View File

@@ -5,87 +5,96 @@ declare(ticks=1);
namespace Siteworxpro\App\Async; namespace Siteworxpro\App\Async;
use Siteworxpro\App\Annotations\Async\HandlesMessage; use Siteworxpro\App\Annotations\Async\HandlesMessage;
use Siteworxpro\App\Async\Messages\Message;
use Siteworxpro\App\Async\Queues\Queue; use Siteworxpro\App\Async\Queues\Queue;
use Siteworxpro\App\Services\Facades\Broker;
use Siteworxpro\App\Services\Facades\Logger; use Siteworxpro\App\Services\Facades\Logger;
/**
* Long-running process that listens to queues, pops messages, and dispatches them to handlers.
*/
class Consumer class Consumer
{ {
private static bool $shutDown = false; private static bool $shutDown = false;
/** @var array<string,string> */
private const array QUEUES = [ private const array QUEUES = [
'default' => Queues\DefaultQueue::class, 'default' => Queues\DefaultQueue::class,
]; ];
/** @var Queue[] */
private array $queues = []; private array $queues = [];
/** @var array<string, string[]> message FQCN => handler FQCNs */
private array $handlers = []; private array $handlers = [];
private const string HANDLER_NAMESPACE = 'Siteworxpro\\App\\Async\\Handlers\\'; private const string HANDLER_NAMESPACE = 'Siteworxpro\\App\\Async\\Handlers\\';
/**
* @param string[] $queues Optional list of queue names (keys from self::QUEUES)
*/
public function __construct(array $queues = []) public function __construct(array $queues = [])
{ {
if ($queues === []) { $queueClasses = $queues === []
$queues = self::QUEUES; ? array_values(self::QUEUES)
} else { : array_map(
$mappedQueues = []; static function (string $name): string {
foreach ($queues as $queueName) { if (!isset(self::QUEUES[$name])) {
if (isset(self::QUEUES[$queueName])) { throw new \InvalidArgumentException("Queue '$name' is not defined.");
$mappedQueues[] = self::QUEUES[$queueName];
} else {
throw new \InvalidArgumentException("Queue '$queueName' is not defined.");
}
}
$queues = $mappedQueues;
} }
return self::QUEUES[$name];
},
$queues
);
foreach ($queueClasses as $class) {
foreach ($queues as $queueClass) { $this->queues[] = new $class();
$this->queues[] = new $queueClass();
} }
$this->registerHandlers(); $this->registerHandlers();
} }
/**
* Discover handler classes under `Handlers` and register them via HandlesMessage attributes.
*/
private function registerHandlers(): void private function registerHandlers(): void
{ {
$recursiveIterator = new \RecursiveIteratorIterator( $it = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator(__DIR__ . '/Handlers/') new \RecursiveDirectoryIterator(__DIR__ . '/Handlers/')
); );
foreach ($recursiveIterator as $file) { /** @var \SplFileInfo $file */
if ($file->isFile() && $file->getExtension() === 'php') { foreach ($it as $file) {
$relativePath = str_replace(__DIR__ . '/Handlers/', '', $file->getPathname()); if (!$file->isFile() || $file->getExtension() !== 'php') {
$className = self::HANDLER_NAMESPACE . str_replace('/', '\\', substr($relativePath, 0, -4)); continue;
if (class_exists($className)) {
$reflection = new \ReflectionClass($className);
$attributes = $reflection->getAttributes(HandlesMessage::class);
foreach ($attributes as $attribute) {
$instance = $attribute->newInstance();
$messageClass = $instance->getMessageClass();
$this->handlers[$messageClass][] = $className;
} }
$relative = str_replace(__DIR__ . '/Handlers/', '', $file->getPathname());
$class = self::HANDLER_NAMESPACE . str_replace('/', '\\', substr($relative, 0, -4));
if (!class_exists($class)) {
continue;
} }
$ref = new \ReflectionClass($class);
foreach ($ref->getAttributes(HandlesMessage::class) as $attr) {
$messageClass = $attr->newInstance()->getMessageClass();
$this->handlers[$messageClass][] = $class;
} }
} }
} }
/** /**
* @param $signal * Signal handler used to initiate graceful or immediate shutdown.
*/ */
public static function handleSignal($signal): void public static function handleSignal(int $signal): void
{ {
switch ($signal) { switch ($signal) {
// Graceful
case SIGINT: case SIGINT:
case SIGTERM: case SIGTERM:
case SIGHUP: case SIGHUP:
self::$shutDown = true; self::$shutDown = true;
return;
break;
// Not Graceful
case SIGKILL: case SIGKILL:
exit(9); exit(9);
} }
@@ -96,6 +105,9 @@ class Consumer
return self::$shutDown; return self::$shutDown;
} }
/**
* Start the consumer main loop.
*/
public function start(): void public function start(): void
{ {
if (!\function_exists('pcntl_signal')) { if (!\function_exists('pcntl_signal')) {
@@ -103,10 +115,11 @@ class Consumer
} }
Logger::info('Starting queue consumer...'); Logger::info('Starting queue consumer...');
Logger::info('Using Broker: ' . Broker::getFacadeRoot()::class);
\pcntl_signal(SIGINT, [self::class, 'handleSignal']); foreach ([SIGINT, SIGTERM, SIGHUP] as $sig) {
\pcntl_signal(SIGTERM, [self::class, 'handleSignal']); \pcntl_signal($sig, [self::class, 'handleSignal']);
\pcntl_signal(SIGHUP, [self::class, 'handleSignal']); }
while (true) { while (true) {
if ($this->shouldShutDown()) { if ($this->shouldShutDown()) {
@@ -118,40 +131,43 @@ class Consumer
foreach ($this->queues as $queue) { foreach ($this->queues as $queue) {
Logger::info('Listening to queue: ' . $queue->queueName()); Logger::info('Listening to queue: ' . $queue->queueName());
$message = $queue->pop(); $message = $queue->pop();
if ($message) { if (!$message) {
continue;
}
Logger::info('Processing message of type: ' . get_class($message)); Logger::info('Processing message of type: ' . get_class($message));
$handlers = $this->getHandlerForMessage($message); foreach ($this->getHandlersForMessage($message) as $handler) {
foreach ($handlers as $handler) {
$handler($message); $handler($message);
} }
}
// Continue polling from the top of the loop after processing a message.
continue 2;
} }
// Avoid busy-looping when no messages are available.
sleep(1); sleep(1);
} }
} }
private function getHandlerForMessage($message): array /**
* @return callable[] Handler instances invokable with the message
*/
private function getHandlersForMessage(Message $message): array
{ {
$callables = [];
$messageClass = get_class($message); $messageClass = get_class($message);
if (isset($this->handlers[$messageClass])) {
$handlerClasses = $this->handlers[$messageClass];
foreach ($handlerClasses as $handlerClass) { if (!isset($this->handlers[$messageClass])) {
throw new \RuntimeException("No handler found for message class: $messageClass");
}
$callables = [];
foreach ($this->handlers[$messageClass] as $handlerClass) {
if (class_exists($handlerClass)) { if (class_exists($handlerClass)) {
$handlerInstance = new $handlerClass(); $callables[] = new $handlerClass();
$callables[] = $handlerInstance;
} }
} }
return $callables; return $callables;
} }
throw new \RuntimeException("No handler found for message class: $messageClass");
}
} }

View File

@@ -7,6 +7,7 @@ namespace Siteworxpro\App\Cli;
use Ahc\Cli\Application; use Ahc\Cli\Application;
use Siteworxpro\App\Cli\Commands\DemoCommand; use Siteworxpro\App\Cli\Commands\DemoCommand;
use Siteworxpro\App\Cli\Commands\Queue\Start; use Siteworxpro\App\Cli\Commands\Queue\Start;
use Siteworxpro\App\Cli\Commands\Queue\TestJob;
use Siteworxpro\App\Kernel; use Siteworxpro\App\Kernel;
use Siteworxpro\App\Services\Facades\Config; use Siteworxpro\App\Services\Facades\Config;
@@ -24,6 +25,7 @@ class App
$this->app->add(new DemoCommand()); $this->app->add(new DemoCommand());
$this->app->add(new Start()); $this->app->add(new Start());
$this->app->add(new TestJob());
} }
public function run(): int public function run(): int

View File

@@ -24,8 +24,6 @@ class Start extends Command implements CommandInterface
$queues = explode(',', $this->values()['queues']); $queues = explode(',', $this->values()['queues']);
} }
SayHelloMessage::dispatch("hello from queue consumer!");
$consumer = new Consumer($queues); $consumer = new Consumer($queues);
$consumer->start(); $consumer->start();

View File

@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace Siteworxpro\App\Cli\Commands\Queue;
use Ahc\Cli\Input\Command;
use Siteworxpro\App\Async\Messages\SayHelloMessage;
use Siteworxpro\App\Cli\Commands\CommandInterface;
/**
* Class TestJob
*
* A CLI command to schedule a demo job that dispatches a SayHelloMessage.
*/
class TestJob extends Command implements CommandInterface
{
public function __construct()
{
parent::__construct('queue:demo', 'Schedule a demo job.');
}
/**
* Execute the command to dispatch a SayHelloMessage.
*
* @return int Exit code
*/
public function execute(): int
{
SayHelloMessage::dispatch('World from TestJob Command!');
return 0;
}
}

View File

@@ -8,6 +8,13 @@ use League\Route\Http\Exception\NotFoundException;
use Nyholm\Psr7\ServerRequest; use Nyholm\Psr7\ServerRequest;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
/**
* Class Controller
*
* An abstract base controller providing default implementations for HTTP methods.
*
* @package Siteworxpro\App\Controllers
*/
abstract class Controller implements ControllerInterface abstract class Controller implements ControllerInterface
{ {
/** /**

View File

@@ -7,6 +7,11 @@ namespace Siteworxpro\App\Controllers;
use Nyholm\Psr7\ServerRequest; use Nyholm\Psr7\ServerRequest;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
/**
* Interface ControllerInterface
*
* Defines the contract for handling HTTP requests in a controller.
*/
interface ControllerInterface interface ControllerInterface
{ {
/** /**

View File

@@ -12,6 +12,13 @@ use Siteworxpro\App\Models\Model;
use Siteworxpro\App\Services\Facades\Redis; use Siteworxpro\App\Services\Facades\Redis;
use Siteworxpro\HttpStatus\CodesEnum; use Siteworxpro\HttpStatus\CodesEnum;
/**
* Class HealthcheckController
*
* Handles health check requests to verify database and cache connectivity.
*
* @package Siteworxpro\App\Controllers
*/
class HealthcheckController extends Controller class HealthcheckController extends Controller
{ {
/** /**

View File

@@ -10,6 +10,10 @@ use Siteworxpro\App\Annotations\Events\ListensFor;
use Siteworxpro\App\Events\Listeners\Listener; use Siteworxpro\App\Events\Listeners\Listener;
use Siteworxpro\App\Services\Facades\Logger; use Siteworxpro\App\Services\Facades\Logger;
/**
* Class Connected
* @package Siteworxpro\App\Events\Listeners\Database
*/
#[ListensFor(ConnectionEstablished::class)] #[ListensFor(ConnectionEstablished::class)]
class Connected extends Listener class Connected extends Listener
{ {

View File

@@ -4,6 +4,11 @@ declare(strict_types=1);
namespace Siteworxpro\App\Events\Listeners; namespace Siteworxpro\App\Events\Listeners;
/**
* Class Listener
*
* @package Siteworxpro\App\Events\Listeners
*/
abstract class Listener implements ListenerInterface abstract class Listener implements ListenerInterface
{ {
} }

View File

@@ -4,7 +4,16 @@ declare(strict_types=1);
namespace Siteworxpro\App\Events\Listeners; namespace Siteworxpro\App\Events\Listeners;
/**
* Interface ListenerInterface
* @package Siteworxpro\App\Events\Listeners
*/
interface ListenerInterface interface ListenerInterface
{ {
/**
* @param mixed $event
* @param array $payload
* @return mixed
*/
public function __invoke(mixed $event, array $payload = []): mixed; public function __invoke(mixed $event, array $payload = []): mixed;
} }

View File

@@ -4,6 +4,10 @@ declare(strict_types=1);
namespace Siteworxpro\App\Helpers; namespace Siteworxpro\App\Helpers;
/**
* Class Env
* @package Siteworxpro\App\Helpers
*/
abstract class Env abstract class Env
{ {
/** /**

View File

@@ -4,8 +4,17 @@ declare(strict_types=1);
namespace Siteworxpro\App\Helpers; namespace Siteworxpro\App\Helpers;
/**
* Class Ulid
* @package Siteworxpro\App\Helpers
*/
class Ulid class Ulid
{ {
/**
* Generate a ULID string
*
* @return string
*/
public static function generate(): string public static function generate(): string
{ {
return \Ulid\Ulid::generate()->getRandomness(); return \Ulid\Ulid::generate()->getRandomness();

View File

@@ -27,18 +27,44 @@ use Siteworxpro\App\Http\JsonResponseFactory;
use Siteworxpro\App\Services\Facades\Config; use Siteworxpro\App\Services\Facades\Config;
use Siteworxpro\HttpStatus\CodesEnum; use Siteworxpro\HttpStatus\CodesEnum;
/**
* JWT authorization middleware.
*
* Applies JWT validation to controller actions annotated with `Jwt` attribute.
* Flow:
* - Resolve the targeted controller and method for the current route.
* - If the method has `Jwt`, read the `Authorization` header and parse the Bearer token.
* - Validate signature, time constraints, issuer\(\) and audience\(\) based on attribute and config.
* - On success, attach all token claims to the request as attributes.
* - On failure, return a 401 JSON response with validation errors.
*
* Configuration:
* - `jwt.signing_key`: key material or `file://` path to key.
* - `jwt.strict_validation`: bool toggling strict vs loose time validation.
*/
class JwtMiddleware extends Middleware class JwtMiddleware extends Middleware
{ {
/** /**
* @throws \JsonException * Process the incoming request.
* @throws \Exception *
* If the matched controller method is annotated with `Jwt`, validates the token and
* augments the request with claims on success. Otherwise, just delegates to the next handler.
*
* @param ServerRequestInterface $request PSR-7 request instance.
* @param RequestHandlerInterface|Dispatcher $handler Next middleware or route dispatcher.
*
* @return ResponseInterface Response produced by the next handler or a 401 JSON response.
*
* @throws \JsonException On JSON error response encoding issues.
* @throws \Exception On unexpected reflection or JWT parsing issues.
*/ */
public function process( public function process(
ServerRequestInterface $request, ServerRequestInterface $request,
RequestHandlerInterface|Dispatcher $handler RequestHandlerInterface|Dispatcher $handler
): ResponseInterface { ): ResponseInterface {
$callable = $this->extractRouteCallable($request, $handler); // Resolve the callable \[Controller, method] for the current route.
$callable = $this->extractRouteCallable($handler);
if ($callable === null) { if ($callable === null) {
return $handler->handle($request); return $handler->handle($request);
} }
@@ -51,12 +77,15 @@ class JwtMiddleware extends Middleware
if ($reflectionClass->hasMethod($method)) { if ($reflectionClass->hasMethod($method)) {
$reflectionMethod = $reflectionClass->getMethod($method); $reflectionMethod = $reflectionClass->getMethod($method);
// Read `Jwt` attribute on the controller method.
$attributes = $reflectionMethod->getAttributes(Jwt::class); $attributes = $reflectionMethod->getAttributes(Jwt::class);
// If no `Jwt` attribute, do not enforce auth here.
if (empty($attributes)) { if (empty($attributes)) {
return $handler->handle($request); return $handler->handle($request);
} }
// Extract Bearer token from Authorization header.
$token = str_replace('Bearer ', '', $request->getHeaderLine('Authorization')); $token = str_replace('Bearer ', '', $request->getHeaderLine('Authorization'));
if (empty($token)) { if (empty($token)) {
@@ -66,6 +95,7 @@ class JwtMiddleware extends Middleware
], CodesEnum::UNAUTHORIZED); ], CodesEnum::UNAUTHORIZED);
} }
// Aggregate required issuers and audience from attributes.
$requiredIssuers = []; $requiredIssuers = [];
$requiredAudience = ''; $requiredAudience = '';
@@ -81,6 +111,7 @@ class JwtMiddleware extends Middleware
} }
try { try {
// Parse and validate the token with signature, time, issuer and audience constraints.
$jwt = new JwtFacade()->parse( $jwt = new JwtFacade()->parse(
$token, $token,
$this->getSignedWith(), $this->getSignedWith(),
@@ -91,6 +122,7 @@ class JwtMiddleware extends Middleware
new PermittedFor($requiredAudience) new PermittedFor($requiredAudience)
); );
} catch (RequiredConstraintsViolated $exception) { } catch (RequiredConstraintsViolated $exception) {
// Collect human-readable violations to return to the client.
$violations = []; $violations = [];
foreach ($exception->violations() as $violation) { foreach ($exception->violations() as $violation) {
$violations[] = $violation->getMessage(); $violations[] = $violation->getMessage();
@@ -102,12 +134,14 @@ class JwtMiddleware extends Middleware
'errors' => $violations 'errors' => $violations
], CodesEnum::UNAUTHORIZED); ], CodesEnum::UNAUTHORIZED);
} catch (InvalidTokenStructure) { } catch (InvalidTokenStructure) {
// Token could not be parsed due to malformed structure.
return JsonResponseFactory::createJsonResponse([ return JsonResponseFactory::createJsonResponse([
'status_code' => 401, 'status_code' => 401,
'message' => 'Unauthorized: Invalid token', 'message' => 'Unauthorized: Invalid token',
], CodesEnum::UNAUTHORIZED); ], CodesEnum::UNAUTHORIZED);
} }
// Expose all token claims as request attributes for downstream consumers.
foreach ($jwt->claims()->all() as $item => $value) { foreach ($jwt->claims()->all() as $item => $value) {
$request = $request->withAttribute($item, $value); $request = $request->withAttribute($item, $value);
} }
@@ -117,6 +151,17 @@ class JwtMiddleware extends Middleware
return $handler->handle($request); return $handler->handle($request);
} }
/**
* Build the signature validation constraint from configured key.
*
* - If the configured key content includes the string `PUBLIC KEY`, use RSA SHA-256.
* - Otherwise assume an HMAC SHA-256 shared secret.
* - Supports raw key strings or `file://` paths.
*
* @return SignedWith Signature constraint used during JWT parsing.
*
* @throws \RuntimeException When no signing key is configured.
*/
private function getSignedWith(): SignedWith private function getSignedWith(): SignedWith
{ {
$key = Config::get('jwt.signing_key'); $key = Config::get('jwt.signing_key');
@@ -125,12 +170,14 @@ class JwtMiddleware extends Middleware
throw new \RuntimeException('JWT signing key is not configured.'); throw new \RuntimeException('JWT signing key is not configured.');
} }
// Load key either from file or raw text.
if (str_starts_with($key, 'file://')) { if (str_starts_with($key, 'file://')) {
$key = InMemory::file(substr($key, 7)); $key = InMemory::file(substr($key, 7));
} else { } else {
$key = InMemory::plainText($key); $key = InMemory::plainText($key);
} }
// Heuristic: if PEM public key content is detected, use RSA; otherwise use HMAC.
if (str_contains($key->contents(), 'PUBLIC KEY')) { if (str_contains($key->contents(), 'PUBLIC KEY')) {
return new SignedWith(new Sha256(), $key); return new SignedWith(new Sha256(), $key);
} }

View File

@@ -9,30 +9,60 @@ use League\Route\Route;
use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface; use Psr\Http\Server\RequestHandlerInterface;
/**
* Base middleware helper for extracting route callables.
*
* This abstract middleware provides a utility method to inspect a League\Route
* dispatcher and obtain the underlying route callable as a [class, method] tuple.
*
* @package Siteworxpro\App\Http\Middleware
*/
abstract class Middleware implements MiddlewareInterface abstract class Middleware implements MiddlewareInterface
{ {
/**
protected function extractRouteCallable($request, RequestHandlerInterface | Dispatcher $handler): array|null * Extract the route callable [class, method] from a League\Route dispatcher.
{ *
* When the provided handler is a League\Route\Dispatcher, this inspects its
* middleware stack, looks at the last segment (the resolved Route), and
* attempts to normalize its callable into a [class, method] pair.
*
* Supported callable forms:
* - array callable: [object|class-string, method-string]
* - string callable: "ClassName::methodName"
*
* Returns null when the handler is not a Dispatcher, the stack is empty,
* or the callable cannot be parsed.
*
* @param RequestHandlerInterface|Dispatcher $handler The downstream handler or dispatcher.
*
* @return array{0: class-string|object|null, 1: string|null}|null Tuple of [class|object, method] or null.
*/
protected function extractRouteCallable(
RequestHandlerInterface|Dispatcher $handler
): array|null {
// Only proceed if this is a League\Route dispatcher.
if (!$handler instanceof Dispatcher) { if (!$handler instanceof Dispatcher) {
return null; return null;
} }
/** @var Route | null $lastSegment */ /** @var Route | null $lastSegment */
// Retrieve the last middleware in the stack, which should be the Route.
$lastSegment = array_last($handler->getMiddlewareStack()); $lastSegment = array_last($handler->getMiddlewareStack());
if ($lastSegment === null) { if ($lastSegment === null) {
return null; return null;
} }
// Obtain the callable associated with the route.
$callable = $lastSegment->getCallable(); $callable = $lastSegment->getCallable();
$class = null; $class = null;
$method = null; $method = null;
// Handle array callable: [object|class-string, 'method']
if (is_array($callable) && count($callable) === 2) { if (is_array($callable) && count($callable) === 2) {
[$class, $method] = $callable; [$class, $method] = $callable;
} elseif (is_string($callable)) { } elseif (is_string($callable)) {
// Handle the case where the callable is a string (e.g., 'ClassName::methodName') // Handle string callable: 'ClassName::methodName'
[$class, $method] = explode('::', $callable); [$class, $method] = explode('::', $callable);
} }

View File

@@ -13,36 +13,65 @@ use Siteworxpro\App\Controllers\Controller;
use Siteworxpro\App\Http\JsonResponseFactory; use Siteworxpro\App\Http\JsonResponseFactory;
use Siteworxpro\HttpStatus\CodesEnum; use Siteworxpro\HttpStatus\CodesEnum;
/**
* Middleware that enforces scope-based access control on controller actions.
*
* It inspects PHP 8 attributes of type \`Scope\` applied to the resolved controller method,
* compares the required scopes with the user scopes provided on the request attribute \`scopes\`,
* and returns a 403 JSON response when any required scope is missing.
*
* If the route callable cannot be resolved, or no scope is required, the request is passed through.
*
* @see Scope
*/
class ScopeMiddleware extends Middleware class ScopeMiddleware extends Middleware
{ {
/** /**
* @throws \JsonException * Resolve the route callable, read any \`Scope\` attributes, and enforce required scopes.
*
* Expected user scopes are provided on the request under the attribute name \`scopes\`
* as an array of strings.
*
* @param ServerRequestInterface $request Incoming PSR-7 request (expects \`scopes\` attribute).
* @param RequestHandlerInterface|Dispatcher $handler Next handler or League\Route dispatcher.
*
* @return ResponseInterface A 403 JSON response when scopes are insufficient; otherwise the handler response.
*
* @throws \JsonException If encoding the JSON error response fails.
* @throws \ReflectionException If reflection on the controller or method fails.
*/ */
public function process( public function process(
ServerRequestInterface $request, ServerRequestInterface $request,
RequestHandlerInterface | Dispatcher $handler RequestHandlerInterface | Dispatcher $handler
): ResponseInterface { ): ResponseInterface {
$callable = $this->extractRouteCallable($request, $handler); // Attempt to resolve the route's callable [Controller instance, method name].
$callable = $this->extractRouteCallable($handler);
if ($callable === null) { if ($callable === null) {
// If no callable is available, delegate to the next handler.
return $handler->handle($request); return $handler->handle($request);
} }
/** @var Controller $class */ /** @var Controller $class Controller instance resolved from the route. */
[$class, $method] = $callable; [$class, $method] = $callable;
// Ensure the controller exists and the method is defined before reflecting.
if (class_exists($class::class)) { if (class_exists($class::class)) {
$reflectionClass = new \ReflectionClass($class); $reflectionClass = new \ReflectionClass($class);
if ($reflectionClass->hasMethod($method)) { if ($reflectionClass->hasMethod($method)) {
$reflectionMethod = $reflectionClass->getMethod($method); $reflectionMethod = $reflectionClass->getMethod($method);
// Fetch all Scope attributes declared on the method.
$attributes = $reflectionMethod->getAttributes(Scope::class); $attributes = $reflectionMethod->getAttributes(Scope::class);
foreach ($attributes as $attribute) { foreach ($attributes as $attribute) {
/** @var Scope $scopeInstance */ /** @var Scope $scopeInstance Concrete Scope attribute instance. */
$scopeInstance = $attribute->newInstance(); $scopeInstance = $attribute->newInstance();
$requiredScopes = $scopeInstance->getScopes(); $requiredScopes = $scopeInstance->getScopes();
// Retrieve user scopes from the request (defaults to an empty array).
$userScopes = $request->getAttribute('scopes', []); $userScopes = $request->getAttribute('scopes', []);
// Deny if any required scope is missing from the user's scopes.
if ( if (
array_any( array_any(
$requiredScopes, $requiredScopes,
@@ -59,6 +88,7 @@ class ScopeMiddleware extends Middleware
} }
} }
// All checks passed; continue down the middleware pipeline.
return $handler->handle($request); return $handler->handle($request);
} }
} }

View File

@@ -14,8 +14,21 @@ use Siteworxpro\App\Services\ServiceProviders\DispatcherServiceProvider;
use Siteworxpro\App\Services\ServiceProviders\LoggerServiceProvider; use Siteworxpro\App\Services\ServiceProviders\LoggerServiceProvider;
use Siteworxpro\App\Services\ServiceProviders\RedisServiceProvider; use Siteworxpro\App\Services\ServiceProviders\RedisServiceProvider;
/**
* Class Kernel
*
* The Kernel class is responsible for bootstrapping the application by
* initializing service providers and setting up the database connection.
*
* @package Siteworxpro\App
*/
class Kernel class Kernel
{ {
/**
* List of service providers to be registered during bootstrapping.
*
* @var array
*/
private static array $serviceProviders = [ private static array $serviceProviders = [
LoggerServiceProvider::class, LoggerServiceProvider::class,
RedisServiceProvider::class, RedisServiceProvider::class,

View File

@@ -11,12 +11,44 @@ use Psr\Log\LogLevel;
use RoadRunner\Logger\Logger as RRLogger; use RoadRunner\Logger\Logger as RRLogger;
use Spiral\Goridge\RPC\RPC; use Spiral\Goridge\RPC\RPC;
/**
* Logger implementation that conforms to PSR-3 (`Psr\Log\LoggerInterface`).
*
* Behavior:
* - If environment indicates RoadRunner RPC (`$_SERVER['RR_RPC']`), logs are forwarded
* to a RoadRunner RPC logger (`RoadRunner\Logger\Logger`) created via Goridge RPC.
* - Otherwise, logs are written to `php://stdout` using Monolog with a JSON formatter.
* - Messages below the configured threshold are ignored (level filtering).
*
* Supported PSR-3 levels are mapped to an internal numeric ordering in `$levels`.
* When using the RPC logger, levels are translated to the respective RPC methods
* (debug, info, warning, error). When using Monolog, the numeric mapping is used
* as the numeric level passed to Monolog's `log` method.
*/
class Logger implements LoggerInterface class Logger implements LoggerInterface
{ {
/**
* RoadRunner RPC logger instance when running under RoadRunner.
*
* @var RRLogger|null
*/
private ?RRLogger $rpcLogger = null; private ?RRLogger $rpcLogger = null;
/**
* Monolog logger used as a fallback to write JSON-formatted logs to stdout.
*
* @var \Monolog\Logger
*/
private \Monolog\Logger $monologLogger; private \Monolog\Logger $monologLogger;
/**
* Numeric ordering for PSR-3 log levels.
*
* Lower numbers represent higher severity. This mapping is used for filtering
* messages according to the configured minimum level and for Monolog numeric level.
*
* @var array<string,int>
*/
private array $levels = [ private array $levels = [
LogLevel::EMERGENCY => 0, LogLevel::EMERGENCY => 0,
LogLevel::ALERT => 1, LogLevel::ALERT => 1,
@@ -28,10 +60,21 @@ class Logger implements LoggerInterface
LogLevel::DEBUG => 7, LogLevel::DEBUG => 7,
]; ];
/**
* Create a new Logger.
*
* @param string $level Minimum level to log (PSR-3 level string). Messages with
* a higher numeric value in `$levels` will be ignored.
*
* The default is `LogLevel::DEBUG` (log everything).
*
* If `$_SERVER['RR_RPC']` is set, an RPC connection will be attempted at
* $_SERVER['RR_RPC'] and a RoadRunner RPC logger will be used.
*/
public function __construct(private readonly string $level = LogLevel::DEBUG) public function __construct(private readonly string $level = LogLevel::DEBUG)
{ {
if (isset($_SERVER['RR_RPC'])) { if (isset($_SERVER['RR_RPC'])) {
$rpc = RPC::create('tcp://127.0.0.1:6001'); $rpc = RPC::create($_SERVER['RR_RPC']);
$this->rpcLogger = new RRLogger($rpc); $this->rpcLogger = new RRLogger($rpc);
} }
@@ -40,46 +83,113 @@ class Logger implements LoggerInterface
$this->monologLogger->pushHandler(new StreamHandler('php://stdout')->setFormatter($formatter)); $this->monologLogger->pushHandler(new StreamHandler('php://stdout')->setFormatter($formatter));
} }
/**
* System is unusable.
*
* @param \Stringable|string $message
* @param array $context
*/
public function emergency(\Stringable|string $message, array $context = []): void public function emergency(\Stringable|string $message, array $context = []): void
{ {
$this->log(LogLevel::EMERGENCY, $message, $context); $this->log(LogLevel::EMERGENCY, $message, $context);
} }
/**
* Action must be taken immediately.
*
* @param \Stringable|string $message
* @param array $context
*/
public function alert(\Stringable|string $message, array $context = []): void public function alert(\Stringable|string $message, array $context = []): void
{ {
$this->log(LogLevel::ALERT, $message, $context); $this->log(LogLevel::ALERT, $message, $context);
} }
/**
* Critical conditions.
*
* @param \Stringable|string $message
* @param array $context
*/
public function critical(\Stringable|string $message, array $context = []): void public function critical(\Stringable|string $message, array $context = []): void
{ {
$this->log(LogLevel::CRITICAL, $message, $context); $this->log(LogLevel::CRITICAL, $message, $context);
} }
/**
* Runtime errors that do not require immediate action but should typically be logged and monitored.
*
* @param \Stringable|string $message
* @param array $context
*/
public function error(\Stringable|string $message, array $context = []): void public function error(\Stringable|string $message, array $context = []): void
{ {
$this->log(LogLevel::ERROR, $message, $context); $this->log(LogLevel::ERROR, $message, $context);
} }
/**
* Exceptional occurrences that are not errors.
*
* @param \Stringable|string $message
* @param array $context
*/
public function warning(\Stringable|string $message, array $context = []): void public function warning(\Stringable|string $message, array $context = []): void
{ {
$this->log(LogLevel::WARNING, $message, $context); $this->log(LogLevel::WARNING, $message, $context);
} }
/**
* Normal but significant events.
*
* @param \Stringable|string $message
* @param array $context
*/
public function notice(\Stringable|string $message, array $context = []): void public function notice(\Stringable|string $message, array $context = []): void
{ {
$this->log(LogLevel::NOTICE, $message, $context); $this->log(LogLevel::NOTICE, $message, $context);
} }
/**
* Interesting events.
*
* @param \Stringable|string $message
* @param array $context
*/
public function info(\Stringable|string $message, array $context = []): void public function info(\Stringable|string $message, array $context = []): void
{ {
$this->log(LogLevel::INFO, $message, $context); $this->log(LogLevel::INFO, $message, $context);
} }
/**
* Detailed debug information.
*
* @param \Stringable|string $message
* @param array $context
*/
public function debug(\Stringable|string $message, array $context = []): void public function debug(\Stringable|string $message, array $context = []): void
{ {
$this->log(LogLevel::DEBUG, $message, $context); $this->log(LogLevel::DEBUG, $message, $context);
} }
/**
* Logs with an arbitrary level.
*
* Behavior details:
* - If the provided `$level` maps to a numeric value greater than the configured
* minimum level, the message is discarded (filtered).
* - If an RPC logger is available, the message is forwarded to the RPC logger
* using a method chosen by level (debug, info, warning, error).
* - Otherwise, the message is written to Monolog using the numeric mapping.
*
* Notes:
* - `$level` should be a PSR-3 level string (values defined in `Psr\Log\LogLevel`).
* - If an unknown level string is passed, accessing `$this->levels[$level]` may
* trigger a PHP notice or undefined index. Ensure callers use valid PSR-3 levels.
*
* @param mixed $level PSR-3 log level (string)
* @param \Stringable|string $message
* @param array $context
*/
public function log($level, \Stringable|string $message, array $context = []): void public function log($level, \Stringable|string $message, array $context = []): void
{ {
if ($this->levels[$level] > $this->levels[$this->level]) { if ($this->levels[$level] > $this->levels[$this->level]) {

View File

@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Siteworxpro\App\Services; namespace Siteworxpro\App\Services;
use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Container\Container;
use Illuminate\Support\HigherOrderTapProxy;
use Illuminate\Support\Testing\Fakes\Fake; use Illuminate\Support\Testing\Fakes\Fake;
use Mockery; use Mockery;
use Mockery\Expectation; use Mockery\Expectation;
@@ -57,9 +58,9 @@ class Facade
/** /**
* Convert the facade into a Mockery spy. * Convert the facade into a Mockery spy.
* *
* @return MockInterface * @return HigherOrderTapProxy | MockInterface
*/ */
public static function spy(): MockInterface public static function spy(): HigherOrderTapProxy | MockInterface
{ {
if (! static::isMock()) { if (! static::isMock()) {
$class = static::getMockableClass(); $class = static::getMockableClass();

View File

@@ -8,8 +8,25 @@ use Illuminate\Support\ServiceProvider;
use Siteworxpro\App\Async\Brokers\Broker; use Siteworxpro\App\Async\Brokers\Broker;
use Siteworxpro\App\Services\Facades\Config; use Siteworxpro\App\Services\Facades\Config;
/**
* Class BrokerServiceProvider
*
* This service provider is responsible for binding the Broker implementation
* to the Laravel service container based on configuration settings.
*
* @package Siteworxpro\App\Services\ServiceProviders
*/
class BrokerServiceProvider extends ServiceProvider class BrokerServiceProvider extends ServiceProvider
{ {
/**
* Register services.
*
* This method binds the Broker interface to a specific implementation
* based on the configuration defined in 'queue.broker' and 'queue.broker_config'.
*
* @return void
* @throws \RuntimeException if the specified broker class does not exist.
*/
public function register(): void public function register(): void
{ {
$this->app->singleton(Broker::class, function (): Broker { $this->app->singleton(Broker::class, function (): Broker {

View File

@@ -7,6 +7,11 @@ namespace Siteworxpro\App\Services\ServiceProviders;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use Siteworxpro\App\Events\Dispatcher; use Siteworxpro\App\Events\Dispatcher;
/**
* Class DispatcherServiceProvider
*
* @package Siteworxpro\App\Services\ServiceProviders
*/
class DispatcherServiceProvider extends ServiceProvider class DispatcherServiceProvider extends ServiceProvider
{ {
public function register(): void public function register(): void

View File

@@ -7,6 +7,11 @@ namespace Siteworxpro\App\Services\ServiceProviders;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use Siteworxpro\App\Log\Logger; use Siteworxpro\App\Log\Logger;
/**
* Class LoggerServiceProvider
*
* @package Siteworxpro\App\Services\ServiceProviders
*/
class LoggerServiceProvider extends ServiceProvider class LoggerServiceProvider extends ServiceProvider
{ {
public function register(): void public function register(): void

View File

@@ -8,6 +8,13 @@ use Illuminate\Support\ServiceProvider;
use Predis\Client; use Predis\Client;
use Siteworxpro\App\Services\Facades\Config; use Siteworxpro\App\Services\Facades\Config;
/**
* Class RedisServiceProvider
*
* This service provider registers a Redis client as a singleton in the application container.
*
* @package Siteworxpro\App\Services\ServiceProviders
*/
class RedisServiceProvider extends ServiceProvider class RedisServiceProvider extends ServiceProvider
{ {
public function register(): void public function register(): void