webware/command-bus
最新稳定版本:0.3.0
Composer 安装命令:
composer require webware/command-bus
包简介
CommandBus for Mezzio
README 文档
README
A Command Bus implementation for Mezzio applications, providing a clean way to handle commands through a middleware pipeline.
Table of Contents
Core Components
- CommandBus - The main command bus implementation
- CommandBusInterface - Command bus contract
- MiddlewarePipe - Middleware pipeline management
- MiddlewarePipelineInterface - Pipeline contract
- CommandHandlerFactory - Factory for command handlers
- ConfigProvider - Laminas configuration provider
- Next - Pipeline delegation helper
Interfaces
- CommandInterface - Command contract
- CommandHandlerInterface - Handler contract
- MiddlewareInterface - Middleware contract
Container Factories
- CommandBusFactory - Service factory for CmdBus
- CommandHandlerFactoryFactory - Factory for CommandHandlerFactory
- CommandHandlerMiddlewareFactory - Factory for CommandHandlerMiddleware
- MiddlewarePipeFactory - Factory for MiddlewarePipe
Middleware
- PreCommandHandlerMiddleware - Middleware for pre-processing commands
- CommandHandlerMiddleware - Final middleware for command execution
- PostCommandHandlerMiddleware - Middleware for post-processing results
Handlers
- EmptyPipelineHandler - Default handler for empty pipelines
Exceptions
- CommandException - Base command exception
- InvalidConfigurationException - Configuration errors
- NextHandlerAlreadyCalledException - Pipeline state errors
- ServiceNotFoundException - Service resolution errors
Quick Start
Installation
composer require php-cmd/cmd-bus
Basic Configuration
// config/config.php return [ Webware\CommandBus\ConfigProvider::class => [ 'command-map' => [ App\Command\CreateUserCommand::class => App\Handler\CreateUserHandler::class, ], 'middleware_pipeline' => [ ['middleware' => \Webware\CommandBus\Middleware\CommandHandlerMiddleware::class, 'priority' => 1], ], ], ];
Usage in Mezzio
// In a request handler or middleware class UserHandler { public function __construct( private \Webware\CommandBus\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()]); } }
Architecture Overview
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
Contributing
See the project repository for contribution guidelines and development setup instructions.
统计信息
- 总下载量: 76
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2026-01-04