brenoroosevelt / oni-bus
服务总线:命令总线、查询总线、事件总线
1.0.1
2021-04-30 13:13 UTC
Requires
- php: ^7.1 || ^8.0
- ext-json: *
- psr/container: @stable
- psr/simple-cache: @stable
Requires (Dev)
- brenoroosevelt/habemus: @stable
- phpstan/phpstan: ^0.12.82
- phpunit/phpunit: ^6 || ^9
- squizlabs/php_codesniffer: @stable
- symfony/cache: @stable
Suggests
- brenoroosevelt/habemus: @stable
- symfony/cache: @stable
README
消息总线、命令总线、查询总线以及事件总线。
特性
- 命令总线
- 查询总线
- 事件总线
- 使用PHP 8属性映射处理程序
要求
支持以下版本的PHP: 7.1
, 7.2
, 7.3
, 7.4
, 8.0
.
安装
$ composer require brenoroosevelt/oni-bus
使用方法
命令总线
<?php use OniBus\Command\Command; class UserRegistrationCommand implements Command { public $username; public $password; }
<?php use OniBus\Attributes\CommandHandler; class UserRegistrationHandler { #[CommandHandler] public function register(UserRegistrationCommand $userRegistration) { /* ... */ return new UserDTO(/* ... */); } }
<?php use OniBus\Attributes\CommandHandler; use OniBus\Buses\DispatchToHandler; use OniBus\Command\CommandBus; use OniBus\Handler\Builder\Resolver; $resolver = Resolver::new(new MyPsr11Container()) ->withHandlers([UserRegistrationHandler::class]) ->mapByAttributes(CommandHandler::class); $commandBus = new CommandBus(new DispatchToHandler($resolver)); $userDTO = $commandBus->dispatch(new UserRegistrationCommand('username', 'secret'));
查询总线
<?php use OniBus\Query\Query; class UserByStatus implements Query { protected $status; public function __construct(string $status) { $this->status = $status; } public function getStatus(): string { return $this->status; } }
<?php use OniBus\Attributes\QueryHandler; class FindUserByStatusSQL { #[QueryHandler] public function fetch(UserByStatus $query) { return [/* ... */]; } }
<?php use OniBus\Attributes\QueryHandler; use OniBus\Buses\DispatchToHandler; use OniBus\Query\QueryBus; use OniBus\Handler\Builder\Resolver; $resolver = Resolver::new(new MyPsr11Container()) ->withHandlers([FindUserByStatusSQL::class]) ->mapByAttributes(QueryHandler::class); $queryBus = new QueryBus(new DispatchToHandler($resolver)); $users = $queryBus->dispatch(new UserByStatus('active'));
EventBus总线
<?php use OniBus\Event\Event; class UserCreatedEvent implements Event { protected $username; public function __construct(string $username) { $this->username = $username; } public function getUsername(): string { return $this->username; } }
<?php use OniBus\Attributes\EventListener; class CreateUserProfile { #[EventListener] public function createProfile(UserCreatedEvent $event) { /* ... */ } }
<?php use OniBus\Attributes\EventListener; use OniBus\Buses\DispatchToHandler; use OniBus\Event\EventBus; use OniBus\Handler\Builder\Resolver; $resolver = Resolver::new(new MyPsr11Container()) ->withHandlers([CreateUserProfile::class]) ->mapByAttributes(EventListener::class); $eventBus = new EventBus(new DispatchToHandler($resolver)); $eventBus->dispatch(new UserCreatedEvent('username'));
总线链
<?php /* CONTAINER */ $container = new MyPsr11Container(); /* EVENT BUS */ $eventResolver = Resolver::new($container) ->withHandlers([CreateUserProfile::class]) ->mapByAttributes(EventListener::class); $eventBus = new EventBus(new DispatchToHandler($eventResolver)); /* COMMAND BUS */ $commandResolver = Resolver::new($container) ->withHandlers([UserRegistrationHandler::class]) ->mapByAttributes(CommandHandler::class); $commandBus = new CommandBus( new EventsDispatcher( EventManager::eventProvider(), $eventBus, ), // new ProtectOrder(), // new Transactional(), new DispatchToHandler($commandResolver) );
容器配置
<?php use OniBus\Command\CommandBus; $container->add(CommandBus::class, $commandBus);
控制器
<?php class UserRegistrationController { protected $commandBus; public function __construct(CommandBus $commandBus) { $this->commandBus = $commandBus; } public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $args = []): ResponseInterface { $body = $request->getParsedBody(); $command = new UserRegistrationCommand($body['username'], $body['password']); $user = $this->commandBus->dispatch($command); $response->getBody()->write($user); return $response; }
贡献
请阅读贡献指南以了解如何为此项目做出贡献。
许可证
本项目遵循MIT许可证条款。有关许可证的权利和限制,请参阅LICENSE文件。