tactics / command-bus-bundle
Handle Commands with CommandHandlers through a CommandBus
Installs: 6 995
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 10
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: >=5.5
- symfony/dependency-injection: ~2.3|~3.0
- symfony/framework-bundle: ~2.3|~3.0
This package is not auto-updated.
Last update: 2024-04-29 06:31:33 UTC
README
Say we have a command called RegisterUser.
<?php use Pringles\DomainBundle\CommandBus\Command; class RegisterUser implements Command { public $firstname; public $lastname; }
And we want to handle that command.
<?php use Pringles\DomainBundle\CommandBus\CommandHandler; class RegisterUserHandler implements CommandHandler { private $personRepository; public function __construct(PersonRepository $personRepository) { $this->personRepository = $personRepository; } public function handle(RegisterUser $registerUser) { $person = Person::register($registerUser->firstname, $registerUser->lastname); } }
We can setup the SimpleCommandBus, register the handler and handle the command.
<?php use Pringles\DomainBundle\CommandBus\SimpleCommandBus; function someController() { $bus = new SimpleCommandBus(new ShortNameStrategy()); $bus->registerHandler(new RegisterUserHandler($personRepository)); $cmd = new RegisterUser; $cmd->firstname = 'Aaron'; $cmd->lastname = 'Muylaert'; $bus->handle($cmd); }
SimpleCommandBus finds handlers based on their name. A command named Test requires a registered handler called TestHandler. If no handler is found, nothing happens.
Oh, one small rule, command handlers are not allowed to return values.
There is a command bus service called command_bus
.
You can register a handler by registering your handler as a service and tagging the service as command_handler
. Using it looks like this:
<?php $cmd = new Test; $cmd->value = 'Foo'; $this->get('command_bus')->handle($cmd);
\m/