innmind/command-bus-bundle

This package is abandoned and no longer maintained. No replacement package was suggested.

Command Bus Bundle

2.0.0 2017-02-19 16:29 UTC

This package is auto-updated.

Last update: 2022-02-01 13:00:09 UTC


README

master develop
Scrutinizer Code Quality Scrutinizer Code Quality
Code Coverage Code Coverage
Build Status Build Status

Symfony integration of innmind/command-bus that ease stacking command buses.

Installation

composer require innmind/command-bus-bundle

In your AppKernel.php add the following line:

//app/AppKernel.php

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Innmind\CommandBusBundle\InnmindCommandBusBundle,
        );
        // ...
    }
    // ...
}

Usage

$container->get('innmind_command_bus');

In order to handle your commands you need to define the handlers as services with the tag innmind_command_bus.handler with the attribte handles that will contain the command FQCN.

Advanced configuration

You may want to add extra capabilities to the command bus to do some specific stuff before or after each command is executed (like flushing the doctrine manager or logging each command). To do so you need to create a class that implements CommandBusInterface. The class must at least have one argument in its contructor type hinted with this interface.

Then you declare this command bus as a service with a tag innmind_command_bus and an attribute alias. Then the alias must be placed in the innminc_command_bus.stack configuration array.

Example:

use Innmind\CommandBus\CommandBusInterface;
use Psr\Log\LoggerInterface;

final class LoggingCommandBus implements CommandBusInterface
{
    private $bus;
    private $logger;

    public function __construct(LoggerInterface $logger, CommandBusInterface $bus)
    {
        $this->logger = $logger;
        $this->bus = $bus;
    }

    public function handle($command)
    {
        $this->bus->handle($command);
        $this->logger->debug(
            'A command has been executed',
            ['class' => get_class($command)]
        );
    }
}
#app/config/services.yml
services:
    logging_command_bus:
        class: LoggingCommandBus
        arguments:
            - '@logger'
            - ~ #this is important to declare this argument as null
        tags:
            - { name: innmind_command_bus, alias: logging }
#app/config/config.yml
innmind_command_bus:
    stack:
        - queue
        - logging
        - default

With all this each time you handle a command it will look if there's a handle being handled (in such case it will queue it; this happens if you handle a command inside a handler), then if it can be executed it will log the command (this is your class) and finally it will call the handler associated to the command.