| @@ 9-51 (lines=43) @@ | ||
| 6 | use NilPortugues\MessageBus\CommandBus\Contracts\Command; |
|
| 7 | use NilPortugues\MessageBus\CommandBus\Contracts\CommandBusMiddleware as CommandBusMiddlewareInterface; |
|
| 8 | ||
| 9 | class CommandBus implements CommandBusMiddlewareInterface |
|
| 10 | { |
|
| 11 | /** @var callable[] */ |
|
| 12 | protected $middleware = []; |
|
| 13 | ||
| 14 | /** |
|
| 15 | * StackedCommandBus constructor. |
|
| 16 | * |
|
| 17 | * @param callable[] $middleware |
|
| 18 | */ |
|
| 19 | public function __construct(array $middleware = []) |
|
| 20 | { |
|
| 21 | foreach ($middleware as $commandBusMiddleware) { |
|
| 22 | Assert::isInstanceOf($commandBusMiddleware, CommandBusMiddlewareInterface::class); |
|
| 23 | } |
|
| 24 | ||
| 25 | $this->middleware = $middleware; |
|
| 26 | } |
|
| 27 | ||
| 28 | /** |
|
| 29 | * @param Command $command |
|
| 30 | * @param callable|null $next |
|
| 31 | */ |
|
| 32 | public function __invoke(Command $command, callable $next = null) |
|
| 33 | { |
|
| 34 | $middleware = $this->middleware; |
|
| 35 | $current = array_shift($middleware); |
|
| 36 | ||
| 37 | if (empty($middleware) && !empty($current)) { |
|
| 38 | $current->__invoke($command); |
|
| 39 | ||
| 40 | return; |
|
| 41 | } |
|
| 42 | ||
| 43 | foreach ($middleware as $commandBusMiddleware) { |
|
| 44 | $callable = function ($command) use ($commandBusMiddleware) { |
|
| 45 | return $commandBusMiddleware($command); |
|
| 46 | }; |
|
| 47 | ||
| 48 | $current->__invoke($command, $callable); |
|
| 49 | $current = $commandBusMiddleware; |
|
| 50 | } |
|
| 51 | } |
|
| 52 | } |
|
| 53 | ||
| @@ 12-54 (lines=43) @@ | ||
| 9 | /** |
|
| 10 | * Class EventBus. |
|
| 11 | */ |
|
| 12 | class EventBus implements EventBusMiddlewareInterface |
|
| 13 | { |
|
| 14 | /** @var EventBusMiddleware[] */ |
|
| 15 | protected $middleware; |
|
| 16 | ||
| 17 | /** |
|
| 18 | * StackedEventBus constructor. |
|
| 19 | * |
|
| 20 | * @param array $middleware |
|
| 21 | */ |
|
| 22 | public function __construct(array $middleware = []) |
|
| 23 | { |
|
| 24 | foreach ($middleware as $eventBusMiddleware) { |
|
| 25 | Assert::isInstanceOf($eventBusMiddleware, EventBusMiddlewareInterface::class); |
|
| 26 | } |
|
| 27 | ||
| 28 | $this->middleware = $middleware; |
|
| 29 | } |
|
| 30 | ||
| 31 | /** |
|
| 32 | * @param Event $event |
|
| 33 | * @param callable|null $next |
|
| 34 | */ |
|
| 35 | public function __invoke(Event $event, callable $next = null) |
|
| 36 | { |
|
| 37 | $middleware = $this->middleware; |
|
| 38 | $current = array_shift($middleware); |
|
| 39 | ||
| 40 | if (empty($middleware) && !empty($current)) { |
|
| 41 | $current->__invoke($event); |
|
| 42 | ||
| 43 | return; |
|
| 44 | } |
|
| 45 | ||
| 46 | foreach ($middleware as $eventBusMiddleware) { |
|
| 47 | $callable = function ($event) use ($eventBusMiddleware) { |
|
| 48 | return $eventBusMiddleware($event); |
|
| 49 | }; |
|
| 50 | ||
| 51 | $current->__invoke($event, $callable); |
|
| 52 | $current = $eventBusMiddleware; |
|
| 53 | } |
|
| 54 | } |
|
| 55 | } |
|
| 56 | ||