brenoroosevelt / oni-bus
Service Bus: Command Bus, Query Bus, Event 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
Message Bus, Command Bus, Query Bus and Event Bus.
Features
- Command Bus
- Query Bus
- Event Bus
- Map Handlers using PHP 8 Attributes
Requirements
The following versions of PHP are supported: 7.1
, 7.2
, 7.3
, 7.4
, 8.0
.
Install
$ composer require brenoroosevelt/oni-bus
Usage
Command 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'));
Query Bus
<?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 Bus
<?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'));
Bus Chain
<?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) );
Container Configuration
<?php use OniBus\Command\CommandBus; $container->add(CommandBus::class, $commandBus);
Controller
<?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; }
Contributing
Please read the Contributing guide to learn about contributing to this project.
License
This project is licensed under the terms of the MIT license. See the LICENSE file for license rights and limitations.