chrisguitarguy / tactician-symfony-events
Tactician middleware for the Symfony event dispatcher.
Installs: 34 358
Dependents: 1
Suggesters: 0
Security: 0
Stars: 3
Watchers: 3
Forks: 1
Open Issues: 0
Requires
- php: ^7.2
- league/tactician: ^1.0
- symfony/event-dispatcher: ^4.4 || ^5.0
Requires (Dev)
- phpunit/phpunit: ^8.5
- roave/security-advisories: dev-master
This package is auto-updated.
Last update: 2024-11-21 05:08:17 UTC
README
Use the Symfony event dispatcher to get notifications when a command is received, handled, and/or errors in Tactician.
Usage
Add the middleware anywhere anywhere before the command handler middleware.
use Symfony\Component\EventDispatcher\EventDispatcher; use League\Tactician\CommandBus; use Chrisguitarguy\Tactician\SymfonyEvents\EventMiddleware; use Chrisguitarguy\Tactician\SymfonyEvents\CommandRecieved; use Chrisguitarguy\Tactician\SymfonyEvents\CommandHandled; use Chrisguitarguy\Tactician\SymfonyEvents\CommandFailed; $eventMiddleware = new EventMiddlware($dispatcher = new EventDispatcher()); $commandBus = new CommandBus([ // ... $eventMiddleware, $commandHandlerMiddleware, // ... ]);
You can listen for events specific to a single command.
use Acme\Example\SomeCommand; $dispatcher->addListener('command.receieved.'.MyCommand::class, function (CommandReceived $event) { // called before the handler runs on SomeCommand }); $dispatcher->addListener('command.handled.'.MyCommand::class, function (CommandHandled $event) { // called after the handler runs on SomeCommand }); $dispatcher->addListener('command.failed.'.MyCommand::class, function (CommandFailed $event) { // called if one the handler or one of the `received` or `handled` // events throws an exeception from handling SomeCommand }); $commandBus->execute(new SomeCommand());
Or listen to more generic events that fire on all commands.
use Acme\Example\SomeCommand; $dispatcher->addListener('command.receieved', function (CommandReceived $event) { // called before the handler runs on all commands }); $dispatcher->addListener('command.handled', function (CommandHandled $event) { // called after the handler runs on all commands }); $dispatcher->addListener('command.failed', function (CommandFailed $event) { // called if one the handler or one of the `received` or `handled` // events throws an exception from handling any command }); $commandBus->execute(new SomeCommand());
There's also a CommandEvents
class that provides some constants and helpers
for event names.
use Chrisguitarguy\Tactician\SymfonyEvents\CommandEvents;
use Acme\Example\SomeCommand;
// generic events are in constants.
$dispatcher->addListener(CommandEvents::RECEIVED, /*...*/);
// specific have helpers that take an object or class name
$dispatcher->addListener(CommandEvents::received(SomeCommand::class), /*...*/);
$command = new SomeCommand();
$dispatcher->addListener(CommandEvents::received($command), /*...*/);
$dispatcher->addListener(CommandEvents::HANDLED, /*...*/);
$dispatcher->addListener(CommandEvents::handled(SomeCommand::class), /*...*/);
$dispatcher->addListener(CommandEvents::FAILED, /*...*/);
$dispatcher->addListener(CommandEvents::failed(SomeCommand::class), /*...*/);
License
MIT. See the LICENSE
file.