diff --git a/src/Application/Handlers/RegisterCarrierHandler.php b/src/Application/Handlers/RegisterCarrierHandler.php index 6084706..90b1d3b 100644 --- a/src/Application/Handlers/RegisterCarrierHandler.php +++ b/src/Application/Handlers/RegisterCarrierHandler.php @@ -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 { diff --git a/src/Infrastructure/CassandraEventStore.php b/src/Infrastructure/EventSourcing/CassandraEventStore.php similarity index 97% rename from src/Infrastructure/CassandraEventStore.php rename to src/Infrastructure/EventSourcing/CassandraEventStore.php index 8a9e09a..ca54981 100644 --- a/src/Infrastructure/CassandraEventStore.php +++ b/src/Infrastructure/EventSourcing/CassandraEventStore.php @@ -1,6 +1,6 @@ handlers[$commandClass])) { - throw new \Exception("No handler registered for command: " . $commandClass); + throw new \Exception("No handler registered for command: $commandClass"); } $handler = $this->handlers[$commandClass]; diff --git a/src/Infrastructure/Router.php b/src/Infrastructure/Router.php deleted file mode 100644 index 7b45e2c..0000000 --- a/src/Infrastructure/Router.php +++ /dev/null @@ -1,30 +0,0 @@ -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']); - } -} diff --git a/src/Infrastructure/Routing/IMiddleware.php b/src/Infrastructure/Routing/IMiddleware.php new file mode 100644 index 0000000..0b3a0a1 --- /dev/null +++ b/src/Infrastructure/Routing/IMiddleware.php @@ -0,0 +1,8 @@ +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; + } +} diff --git a/src/Infrastructure/Routing/Router.php b/src/Infrastructure/Routing/Router.php new file mode 100644 index 0000000..83d0864 --- /dev/null +++ b/src/Infrastructure/Routing/Router.php @@ -0,0 +1,63 @@ +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(); + } +} diff --git a/src/index.php b/src/index.php index 27d029b..7a18061 100644 --- a/src/index.php +++ b/src/index.php @@ -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;