A simple command handling mechanism.
Install using composer require stratadox/command-handling
<?php
use Stratadox\CommandHandling\CommandBus;
$bus = CommandBus::handling([
SomeCommand::class => new SomeHandler(),
OtherCommand::class => new OtherHandler(),
]);
$bus->handle(new SomeCommand('foo'));
Commands are simple messages (DTO) that represent a request for an operation. These commands are not the same kind as described in the GOF design patterns, but rather command messages from the CQRS realm.
A command handler receives the commands, either accepting them and initiating the operation, or denying them and throwing an exception.
The command bus routes the input command to the appropriate handler.
Middleware can be configured to perform operations before or after handling commands, or on exception cases.
Such middleware can be used to introduce automated logging of all requests, to automatically reject unauthorised access or to handle persistence concerns. Using the middleware functionality in this way grants the benefits of what people call aspect-oriented programming, without most of the downsides that come with.