Wallogit.com
2017 © Pedro Peláez
Middleware Command Bus
A simple command bus that uses the middleware pattern for processing commands., (*1)
It has been designed to leaverage Dependency Injection with Autowiring support. All of it's dependencies and be configured & overridden by a dependency injection container, (*2)
This will create a command bus with the default handler detection, (*3)
``` PHP $commandbus = new KodCube\CommandBus\CommandBus( new KodCube\CommandBus\MiddlewareManager( new KodCube\DependencyInjection\Container() ) );, (*4)
or using dependancy injection container ``` PHP DI Config [ MiddlewareManagerInterface::class => MiddlewareManager::class, ContainerInterface::class => Container::class ] $commandbus = $di->get(KodCube\CommandBus\CommandBus::class);
Assuming you have the following command and handler, (*5)
Command Class: MyCommand, (*6)
Handler Class: MyHandler, (*7)
``` PHP $command = new MyCommand();, (*8)
$response = $commandbus($command); or $response = $commandbus->handle($command);, (*9)
is the equivalant to ``` PHP $command = new MyCommand(); $handler = new MyHandler(); $response = $handler($command);
The default handler middleware allows you to override which handler is responsible for handling the command, (*10)
``` PHP $commandbus = new KodCube\CommandBus\CommandBus( new KodCube\CommandBus\MiddlewareManager( new KodCube\DependencyInjection\Container(), new KodCube\CommandBus\Middleware\Handler( [ MyCommand::class => MyNewHandler::class ] ) ) );, (*11)
is the equivalant to ``` PHP $command = new MyCommand(); $handler = new MyNewHandler(); $response = $handler($command);
or using dependancy injection container, (*12)
``` PHP DI Config [ MiddlewareManagerInterface::class => MiddlewareManager::class, ContainerInterface::class => Container::class Handler::class => [ [ MyCommand::class => MyNewHandler::class ] ] ], (*13)
$commandbus = $di->get(KodCube\CommandBus\CommandBus::class);, (*14)
### Multiple Middlerware Handlers Using the middleware capablies we can easily add multiple command handlers, to process some commands differently from others. In this example we add a Queueable Middleware Handler, that will look for commands that implement a *CommandQueueInterface::class*. Theses commands will be routed to a message bus for processing on a background process, were as all other command will be handled by the final middleware handler. ``` PHP $commandbus = new KodCube\CommandBus\CommandBus( new KodCube\CommandBus\MiddlewareManager( [ KodCube\CommandBus\Middleware\Queue::class, KodCube\CommandBus\Middleware\Handler::class ], new KodCube\DependencyInjection\Container(), ) );
or using dependancy injection container, (*15)
``` PHP DI Config [ MiddlewareManagerInterface::class => MiddlewareManager::class, MiddlewareManager::class => [ Middleware\Queue::class, Middleware\Handler::class ], ContainerInterface::class => Container::class, Handler::class => [ [ MyCommand::class => MyNewHandler::class ] ] ], (*16)
$commandbus = $di->get(KodCube\CommandBus\CommandBus::class); ```, (*17)