A Command Bus implementation for Mezzio applications, providing a clean way to handle commands through a middleware pipeline.
- CmdBus - The main command bus implementation
- CmdBusInterface - Command bus contract
- MiddlewarePipe - Middleware pipeline management
- MiddlewarePipelineInterface - Pipeline contract
- CommandHandlerFactory - Factory for command handlers
- ConfigProvider - Laminas configuration provider
- Next - Pipeline delegation helper
- CommandInterface - Command contract
- CommandHandlerInterface - Handler contract
- MiddlewareInterface - Middleware contract
- CmdBusFactory - Service factory for CmdBus
- CommandHandlerFactoryFactory - Factory for CommandHandlerFactory
- CommandHandlerMiddlewareFactory - Factory for CommandHandlerMiddleware
- MiddlewarePipeFactory - Factory for MiddlewarePipe
- PreCommandHandlerMiddleware - Middleware for pre-processing commands
- CommandHandlerMiddleware - Final middleware for command execution
- PostCommandHandlerMiddleware - Middleware for post-processing results
- EmptyPipelineHandler - Default handler for empty pipelines
- CommandException - Base command exception
- InvalidConfigurationException - Configuration errors
- NextHandlerAlreadyCalledException - Pipeline state errors
- ServiceNotFoundException - Service resolution errors
composer require php-cmd/cmd-bus// config/config.php
return [
PhpCmd\CmdBus\ConfigProvider::class => [
'command-map' => [
App\Command\CreateUserCommand::class => App\Handler\CreateUserHandler::class,
],
'middleware_pipeline' => [
['middleware' => \PhpCmd\CmdBus\Middleware\CommandHandlerMiddleware::class, 'priority' => 1],
],
],
];// In a request handler or middleware
class UserHandler
{
public function __construct(
private \PhpCmd\CmdBus\CmdBusInterface $commandBus
) {}
public function handle(ServerRequestInterface $request): ResponseInterface
{
$data = $request->getParsedBody();
$command = new CreateUserCommand(
email: $data['email'],
username: $data['username']
);
$user = $this->commandBus->handle($command);
return new JsonResponse(['user' => $user->toArray()]);
}
}The cmd-bus library follows these key principles:
- Commands are data objects that implement
CommandInterface - Handlers process commands and implement
CommandHandlerInterface - Middleware can intercept and potentially modify command processing
- Pipeline manages middleware execution order
- Factory classes integrate with Laminas ServiceManager/ Psr\Container\ContainerInterface
See the project repository for contribution guidelines and development setup instructions.