Pretty simple with Composer, run:
composer require gpslab/middleware
MiddlewareChain
contains a middlewares (Middleware
) and sequentially apply them to the message by chain.
There are 3 implementations of the chain, but you can make your own.
DirectBindingMiddlewareChain
- direct binding;ContainerMiddlewareChain
- PSR-11 container;SymfonyContainerMiddlewareChain
- Symfony container (Symfony 3.3 implements a PSR-11).
Example usage middleware for handle Commands in CQRS.
// middleware chain
$chain = new DirectBindingMiddlewareChain();
// add logger middleware
$chain->append(new LoggerMiddleware($logger));
// add validator middleware
$chain->append(new ValidatorMiddleware($validator));
// add middleware for handle command from origin command bus
$chain->append(new CommandMiddleware($command_bus));
// configure command bus
$bus = new MiddlewareCommandBus($chain);
// handle command
try {
$bus->handle($my_command);
} catch(InvalidMessageException $e) {
// show validation errors
var_dump($e->getMessages());
}
Example usage middleware for handle Queries in CQRS.
// middleware chain
$chain = new DirectBindingMiddlewareChain();
// add logger middleware
$chain->append(new LoggerMiddleware($logger));
// add validator middleware
$chain->append(new ValidatorMiddleware($validator));
// add middleware for handle query from origin query bus
$chain->append(new QueryMiddleware($query_bus));
// configure query bus
$bus = new MiddlewareQueryBus($chain);
// handle query
try {
$bus->handle($my_query);
} catch (InvalidMessageException $e) {
// show validation errors
var_dump($e->getMessages());
}
Example usage middleware for handle domain events.
// middleware chain
$chain = new DirectBindingMiddlewareChain();
// add logger middleware
$chain->append(new LoggerMiddleware($logger));
// add middleware for handle event from origin domain event bus
$chain->append(new DomainEventMiddleware($domain_event_bus));
// configure domain event bus
$bus = new MiddlewareDomainEventBus($chain);
// publish domain event
$bus->publish($my_event);
This bundle is under the MIT license. See the complete license in the file: LICENSE