refactor(infrastructure): Reorganize routing and messaging architecture with middleware support

This commit is contained in:
m.jalmoudy
2025-12-02 16:56:10 +03:30
parent 5326f40b2b
commit a014c36f63
11 changed files with 138 additions and 39 deletions

View File

@@ -4,7 +4,7 @@ namespace DistributingCarriers\Application\Handlers;
use DistributingCarriers\Application\Commands\RegisterCarrierCommand;
use DistributingCarriers\Domain\Carrier;
use DistributingCarriers\Infrastructure\EventStore;
use DistributingCarriers\Infrastructure\EventSourcing\EventStore;
class RegisterCarrierHandler
{

View File

@@ -1,6 +1,6 @@
<?php
namespace DistributingCarriers\Infrastructure;
namespace DistributingCarriers\Infrastructure\EventSourcing;
use Cassandra;

View File

@@ -1,6 +1,6 @@
<?php
namespace DistributingCarriers\Infrastructure;
namespace DistributingCarriers\Infrastructure\EventSourcing;
interface EventStore
{

View File

@@ -1,6 +1,6 @@
<?php
namespace DistributingCarriers\Infrastructure;
namespace DistributingCarriers\Infrastructure\Messaging;
class CommandBus
{
@@ -13,9 +13,9 @@ class CommandBus
public function dispatch(object $command): void
{
$commandClass = get_class($command);
$commandClass = \get_class($command);
if (!isset($this->handlers[$commandClass])) {
throw new \Exception("No handler registered for command: " . $commandClass);
throw new \Exception("No handler registered for command: $commandClass");
}
$handler = $this->handlers[$commandClass];

View File

@@ -1,30 +0,0 @@
<?php
namespace DistributingCarriers\Infrastructure;
class Router
{
private $routes = [];
public function add(string $method, string $path, callable $handler): void
{
$this->routes[] = [
'method' => $method,
'path' => $path,
'handler' => $handler
];
}
public function dispatch(string $method, string $uri)
{
foreach ($this->routes as $route) {
if ($route['method'] === $method && $route['path'] === $uri) {
return \call_user_func($route['handler']);
}
}
http_response_code(404);
header('Content-Type: application/json');
echo json_encode(['error' => 'Not Found']);
}
}

View File

@@ -0,0 +1,8 @@
<?php
namespace DistributingCarriers\Infrastructure\Routing;
interface IMiddleware
{
public function process(IRequestHandler $handler): void;
}

View File

@@ -0,0 +1,8 @@
<?php
namespace DistributingCarriers\Infrastructure\Routing;
interface IRequestHandler
{
public function handle(): void;
}

View File

@@ -0,0 +1,11 @@
<?php
namespace DistributingCarriers\Infrastructure\Routing;
interface IRoute
{
public function getMethod(): string;
public function getPath(): string;
public function getHandler(): IRequestHandler;
public function getMiddlewares(): array;
}

View File

@@ -0,0 +1,39 @@
<?php
namespace DistributingCarriers\Infrastructure\Routing;
class Route implements IRoute
{
private string $method;
private string $path;
private IRequestHandler $handler;
private array $middlewares;
public function __construct(string $method, string $path, IRequestHandler $handler, array $middlewares = [])
{
$this->method = $method;
$this->path = $path;
$this->handler = $handler;
$this->middlewares = $middlewares;
}
public function getMethod(): string
{
return $this->method;
}
public function getPath(): string
{
return $this->path;
}
public function getHandler(): IRequestHandler
{
return $this->handler;
}
public function getMiddlewares(): array
{
return $this->middlewares;
}
}

View File

@@ -0,0 +1,63 @@
<?php
namespace DistributingCarriers\Infrastructure\Routing;
class Router
{
private array $routes = [];
private array $globalMiddlewares = [];
public function add(string $method, string $path, IRequestHandler $handler, array $middlewares = []): self
{
$this->routes[] = new Route($method, $path, $handler, $middlewares);
return $this;
}
public function addGlobalMiddleware(IMiddleware $middleware): self
{
$this->globalMiddlewares[] = $middleware;
return $this;
}
public function dispatch(string $method, string $uri): void
{
foreach ($this->routes as $route) {
if ($route->getMethod() === $method && $route->getPath() === $uri) {
$middlewares = array_merge($this->globalMiddlewares, $route->getMiddlewares());
$this->executeMiddlewareStack($middlewares, $route->getHandler());
return;
}
}
http_response_code(404);
header('Content-Type: application/json');
echo json_encode(['error' => 'Not Found']);
}
private function executeMiddlewareStack(array $middlewares, IRequestHandler $handler): void
{
$next = $handler;
foreach (array_reverse($middlewares) as $middleware) {
$next = new class($middleware, $next) implements IRequestHandler {
private $middleware;
private $next;
public function __construct(IMiddleware $middleware, IRequestHandler $next)
{
$this->middleware = $middleware;
$this->next = $next;
}
public function handle(): void
{
$this->middleware->process($this->next);
}
};
}
$next->handle();
}
}

View File

@@ -2,9 +2,9 @@
require_once __DIR__ . '/../vendor/autoload.php';
use DistributingCarriers\Infrastructure\CommandBus;
use DistributingCarriers\Infrastructure\Router;
use DistributingCarriers\Infrastructure\CassandraEventStore;
use DistributingCarriers\Infrastructure\Messaging\CommandBus;
use DistributingCarriers\Infrastructure\Routing\Router;
use DistributingCarriers\Infrastructure\EventSourcing\CassandraEventStore;
use DistributingCarriers\Application\Commands\RegisterCarrierCommand;
use DistributingCarriers\Application\Handlers\RegisterCarrierHandler;