refactor(infrastructure): Reorganize routing and messaging architecture with middleware support
This commit is contained in:
24
src/Infrastructure/Messaging/CommandBus.php
Normal file
24
src/Infrastructure/Messaging/CommandBus.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace DistributingCarriers\Infrastructure\Messaging;
|
||||
|
||||
class CommandBus
|
||||
{
|
||||
private $handlers = [];
|
||||
|
||||
public function register(string $commandClass, callable $handler): void
|
||||
{
|
||||
$this->handlers[$commandClass] = $handler;
|
||||
}
|
||||
|
||||
public function dispatch(object $command): void
|
||||
{
|
||||
$commandClass = \get_class($command);
|
||||
if (!isset($this->handlers[$commandClass])) {
|
||||
throw new \Exception("No handler registered for command: $commandClass");
|
||||
}
|
||||
|
||||
$handler = $this->handlers[$commandClass];
|
||||
$handler($command);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user