Symfony2 Bundle for the Tactician library https://github.com/thephpleague/tactician/
If you are looking for a Laravel Provider or want to help: https://github.com/xtrasmal/TacticianProvider
First add this bundle to your composer dependencies:
> composer require xtrasmal\tactician-bundle
Then register it in your AppKernel.php.
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
new Xtrasmal\TacticianBundle\TacticianBundle(),
// ...
That's it!
The most common use case with Tactician is passing a Command to the Command Bus and having it routed to the Command Bus.
Since handlers often have extra dependencies and are best lazily-loaded, you'll want to register them in the service container.
Let's say we have two classes, RegisterUserCommand
and RegisterUserHandler
. We'll register the Handler in the service container, along with a repository it needs.
foo.user.register_user_handler:
class: Foo\User\RegisterUserHandler
arguments:
- @foo.user.user_repository
However, we still need to map the Command to the Handler. We can do this by adding a tag to the Handler's DI definition.
The tag should have two attributes: the tag name, which should always be tactician.handler
, and the command, which should be FQCN of a Command it can handle.
foo.user.register_user_handler:
class: Foo\User\RegisterUserHandler
arguments:
- @foo.user.user_repository
tags:
- { name: tactician.handler, command: Foo\User\RegisterUserCommand }
Everything inside Tactician is a middleware plugin. Without any middleware configured, nothing will happen when you pass a command to handle()
.
By default, the only Middleware enabled is the Command Handler support. You can override this and add your own middleware in the app/config.yml
.
tactician:
commandbus:
default:
middleware:
# service ids for all your middlewares, top down. First in, first out.
- tactician.middleware.locking
- my.custom.middleware.plugin
- tactician.middleware.command_handler
Important: Adding your own middleware is absolutely encouraged, just be sure to always add tactician.middleware.command_handler
as the final middleware. Otherwise, your commands won't actually be executed.
Check the Tactician docs for more info and a complete list of middleware.
The bundle is pre-configured with a command bus called "default". Which has the service name tactician.commandbus
.
Some users want to configure more than one command bus though. You can do this via configuration, like so:
tactician:
commandbus:
default:
middleware:
- tactician.middleware.command_handler
queued:
middleware:
- tactician.middleware.queued_command_handler
The configuration defines two buses: "default" and "queued". These buses will be registered as the
tactician.commandbus.default
and tactician.commandbus.queued
services respectively.
If you want, you can also change which command handler is registered under tactician.commandbus
. You can do this by
setting the default_bus
value in the configuration, like so:
tactician:
default_bus: queued
commandbus:
default:
middleware:
# ...
queued:
middleware:
# ...
This bundles ships with a few pre-configured middlewares, they can be enabled using the method above by just listing their ids.
The validator middleware will plug into Symfony's Validator (@validator) and will throw and exception if the command is not valid.
Constraints can be added via configuration or annotations like in default Symfony practices, please refer to their docs.
The middleware will throw an InvalidCommand
Exception that will contain the command and the ContraintViolationList
returned by the validator.
This middleware is bundled in Tactician, please refer to the official documentation for details.
Always ensure this is the last middleware listed
While not listed this is the core of Tactician and handles executing commands, it should always be enabled.
Create a service and inject the command bus:
services:
your.controller:
class: %your.controller.class%
arguments:
- @tactician.commandbus
Then party like it's 1994
<?php namespace YourName\Controller;
use League\Tactician\CommandBus;
use YourName\Commands\DoSomethingCommand;
class YourNameController
{
private $commandbus;
public function __construct( CommandBus $commandbus )
{
$this->commandbus = $commandbus;
}
public function doSomething()
{
$command = new DoSomethingCommand();
$this->commandbus->handle($command);
}
}