refactor(infrastructure): Reorganize routing and messaging architecture with middleware support
This commit is contained in:
@@ -4,7 +4,7 @@ namespace DistributingCarriers\Application\Handlers;
|
|||||||
|
|
||||||
use DistributingCarriers\Application\Commands\RegisterCarrierCommand;
|
use DistributingCarriers\Application\Commands\RegisterCarrierCommand;
|
||||||
use DistributingCarriers\Domain\Carrier;
|
use DistributingCarriers\Domain\Carrier;
|
||||||
use DistributingCarriers\Infrastructure\EventStore;
|
use DistributingCarriers\Infrastructure\EventSourcing\EventStore;
|
||||||
|
|
||||||
class RegisterCarrierHandler
|
class RegisterCarrierHandler
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace DistributingCarriers\Infrastructure;
|
namespace DistributingCarriers\Infrastructure\EventSourcing;
|
||||||
|
|
||||||
use Cassandra;
|
use Cassandra;
|
||||||
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace DistributingCarriers\Infrastructure;
|
namespace DistributingCarriers\Infrastructure\EventSourcing;
|
||||||
|
|
||||||
interface EventStore
|
interface EventStore
|
||||||
{
|
{
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace DistributingCarriers\Infrastructure;
|
namespace DistributingCarriers\Infrastructure\Messaging;
|
||||||
|
|
||||||
class CommandBus
|
class CommandBus
|
||||||
{
|
{
|
||||||
@@ -13,9 +13,9 @@ class CommandBus
|
|||||||
|
|
||||||
public function dispatch(object $command): void
|
public function dispatch(object $command): void
|
||||||
{
|
{
|
||||||
$commandClass = get_class($command);
|
$commandClass = \get_class($command);
|
||||||
if (!isset($this->handlers[$commandClass])) {
|
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];
|
$handler = $this->handlers[$commandClass];
|
||||||
@@ -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']);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
8
src/Infrastructure/Routing/IMiddleware.php
Normal file
8
src/Infrastructure/Routing/IMiddleware.php
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace DistributingCarriers\Infrastructure\Routing;
|
||||||
|
|
||||||
|
interface IMiddleware
|
||||||
|
{
|
||||||
|
public function process(IRequestHandler $handler): void;
|
||||||
|
}
|
||||||
8
src/Infrastructure/Routing/IRequestHandler.php
Normal file
8
src/Infrastructure/Routing/IRequestHandler.php
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace DistributingCarriers\Infrastructure\Routing;
|
||||||
|
|
||||||
|
interface IRequestHandler
|
||||||
|
{
|
||||||
|
public function handle(): void;
|
||||||
|
}
|
||||||
11
src/Infrastructure/Routing/IRoute.php
Normal file
11
src/Infrastructure/Routing/IRoute.php
Normal 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;
|
||||||
|
}
|
||||||
39
src/Infrastructure/Routing/Route.php
Normal file
39
src/Infrastructure/Routing/Route.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
63
src/Infrastructure/Routing/Router.php
Normal file
63
src/Infrastructure/Routing/Router.php
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,9 +2,9 @@
|
|||||||
|
|
||||||
require_once __DIR__ . '/../vendor/autoload.php';
|
require_once __DIR__ . '/../vendor/autoload.php';
|
||||||
|
|
||||||
use DistributingCarriers\Infrastructure\CommandBus;
|
use DistributingCarriers\Infrastructure\Messaging\CommandBus;
|
||||||
use DistributingCarriers\Infrastructure\Router;
|
use DistributingCarriers\Infrastructure\Routing\Router;
|
||||||
use DistributingCarriers\Infrastructure\CassandraEventStore;
|
use DistributingCarriers\Infrastructure\EventSourcing\CassandraEventStore;
|
||||||
use DistributingCarriers\Application\Commands\RegisterCarrierCommand;
|
use DistributingCarriers\Application\Commands\RegisterCarrierCommand;
|
||||||
use DistributingCarriers\Application\Handlers\RegisterCarrierHandler;
|
use DistributingCarriers\Application\Handlers\RegisterCarrierHandler;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user