chrisguitarguy/tactician-symfony-events

Tactician middleware for the Symfony event dispatcher.

v2.0.0-RC1 2020-07-20 20:07 UTC

This package is auto-updated.

Last update: 2024-10-21 05:00:50 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.