A simple command bus that uses the middleware pattern for processing commands.
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
- All Dependancies are injected at construction
- All Dependencies are based on interfaces
- Route Commands differently based on custom middleware
- Use simple arrays for middleware configuration
- Uses Dependency Injection Container when loading middleware
- Uses Dependency Injection Container when loading handlers
- Does not inject dependencies for methods other than __constructor
- Does not inject dependencies for setters
- PHP 7
- Container-Interop - Dependancy Injection Container
- GunnaPHP\Invoker
- KodCube\DependencyInjection - Dependancy Injection Container
- KodCube\MessageBus - Dispatching commands to background processes
This will create a command bus with the default handler detection
$commandbus = new KodCube\CommandBus\CommandBus(
new KodCube\CommandBus\MiddlewareManager(
new KodCube\DependencyInjection\Container()
)
);
or using dependancy injection container
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
Command Class: MyCommand
Handler Class: MyHandler
$command = new MyCommand();
$response = $commandbus($command);
or
$response = $commandbus->handle($command);
is the equivalant to
$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
$commandbus = new KodCube\CommandBus\CommandBus(
new KodCube\CommandBus\MiddlewareManager(
new KodCube\DependencyInjection\Container(),
new KodCube\CommandBus\Middleware\Handler(
[
MyCommand::class => MyNewHandler::class
]
)
)
);
is the equivalant to
$command = new MyCommand();
$handler = new MyNewHandler();
$response = $handler($command);
or using dependancy injection container
DI Config
[
MiddlewareManagerInterface::class => MiddlewareManager::class,
ContainerInterface::class => Container::class
Handler::class => [
[
MyCommand::class => MyNewHandler::class
]
]
]
$commandbus = $di->get(KodCube\CommandBus\CommandBus::class);
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.
$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
DI Config
[
MiddlewareManagerInterface::class => MiddlewareManager::class,
MiddlewareManager::class => [
Middleware\Queue::class,
Middleware\Handler::class
],
ContainerInterface::class => Container::class,
Handler::class => [
[
MyCommand::class => MyNewHandler::class
]
]
]
$commandbus = $di->get(KodCube\CommandBus\CommandBus::class);