2017 © Pedro Peláez
 

library tactician-module

ZF2 Module to use the League of Extraordinary Packages' Tactician library - flexible command bus implementation

image

code-mine/tactician-module

ZF2 Module to use the League of Extraordinary Packages' Tactician library - flexible command bus implementation

  • Thursday, September 1, 2016
  • by AdamiecRadek
  • Repository
  • 4 Watchers
  • 0 Stars
  • 229 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 8 Forks
  • 0 Open issues
  • 15 Versions
  • 4 % Grown

The README.md

Tactician ZF2 Module

Build Status Scrutinizer Code Quality Code Coverage Dependency Status Latest Stable Version Total Downloads License, (*1)

Wrapper module for easy use of the Tactician Command Bus in your ZF2 applications.

Installation

Best install with Composer:, (*2)

composer require mikemix/tactician-module, (*3)

Register as ZF2 module inside your config/application.config.php file:

    'modules' => [
        'YourApplicationModule',
        'TacticianModule',
    ],

Using

The module presents a Controller Plugin called tacticianCommandBus() for easy use of dispatching commands. If no command object is passed to it, the CommandBus object will be returned. If you pass the command however, it will be passed over to the CommandBus and handled, and the output from the handler will be returned., (*4)

You can type hint this plugin in your controller, for example: @method \League\Tactician\CommandBus|mixed tacticianCommandBus(object $command)., (*5)


// Real life example. // Namespaces, imports, class properties skipped for brevity class LoginController extends AbstractActionController { public function indexAction() { if ($this->request->isPost()) { $this->form->setData($this->request->getPost()); if ($this->form->isValid()) { $command = new UserLoginCommand( $this->form->getLogin(), $this->form->getPassword() )); try { $this->tacticianCommandBus($command); return $this->redirect()->toRoute('home'); } catch (\Some\Kind\Of\Login\Failure $exception) { $this->flashMessenger()->addErrorMessage($exception->getMessage()); return $this->redirect()->refresh(); } } } $view = new ViewModel(); $view->setVariable('form', $this->form); $view->setTemplate('app/login/index'); return $view; } } final class UserLoginCommand { public function __construct($login, $password) { $this->login = $login; $this->password = $password; } } final class UserLoginHandler { // constructor skipped for brevity public function handle(UserLoginCommand $command) { $this->authenticationService->login($command->username, $command->password); } }

If you need to inject the command bus into your service layer or similar, then simply grab from the Service Manager using the FQCN of the CommandBus., (*6)

<?php
namespace MyNamespace;

use League\Tactician\CommandBus;
use Zend\ServiceManager\ServiceManager;
use MyNamespace\Service\MyService;

class Module
{
    public function getServiceConfig()
    {
        return [
            'factories' => [
                MyService::class => function(ServiceManager $serviceManager) {
                    $commandBus = $serviceManager->get(CommandBus::class);
                    return new MyService($commandBus);
                },
            ],
        ];
    }
}

Configuring

The module ships with a ZendLocator and a CommandHandlerMiddleware and a HandlerInflector configured as default. If you wish to override the default locator or default command bus implementations, then simply use the tactician key in the merged config., (*7)

'tactician' => [
    'default-extractor'  => League\Tactician\Handler\CommandNameExtractor\ClassNameExtractor::class,
    'default-locator'    => TacticianModule\Locator\ZendLocator::class,
    'default-inflector'  => League\Tactician\Handler\HandleInflector::class,
    'handler-map'        => [],
    'middleware'         => [
        CommandHandlerMiddleware::class => 0,
    ],
],

default-extractor, default-locator and default-inflector are service manager keys to registered services., (*8)

ZendLocator expects handlers in the handler-map to be commandName => serviceManagerKey or commandName => Handler_FQCN, altough to prevent additional costly checks, use serviceManagerKey and make sure it is available as a Zend Service., (*9)

To add custom middleware to the middleware stack, add it to the middleware array as ServiceName => priority in which the middleware are supposed to be executed (higher the number, earlier it will execute). For example, (*10)

// ... your module config
'tactician' => [
    'middleware'         => [
        YourCustomMiddleware::class  => -100, // execute last
        YourAnotherMiddleware::class => 100, // execute early
    ],
],

Basic usage

Basicly, all you probably will want to do, is to define the handler-map array in your module's configuration. For example:, (*11)

// module.config.php file

    return [
        // other keys
        'tactician' => [
            'handler-map' => [
                App\Command\SomeCommand::class => App\Handler\SomeCommandHandler::class,
            ],
        ],
    ];

Plugins

LockingMiddleware

The LockingMiddleware can now be used out of the box. Simply add the League\Tactician\Plugins\LockingMiddleware FQCN to the TacticianModule's middleware configuration with appropriate priority. You probably want to execute it before the CommandHandlerMiddleware:, (*12)

// module.config.php file

    return [
        // other keys
        'tactician' => [
            'middleware' => [
                \League\Tactician\Plugins\LockingMiddleware::class => 500,
            ],
        ],
    ];

TransactionMiddleware

The TransactionMiddleware can now be used out of the box. Simply add the League\Tactician\Doctrine\ORM\TransactionMiddleware FQCN to the TacticianModule's middleware configuration with appropriate priority. You probably want to execute it before the CommandHandlerMiddleware and after the LockingMiddleware:, (*13)

// module.config.php file

    return [
        // other keys
        'tactician' => [
            'middleware' => [
                \League\Tactician\Doctrine\ORM\TransactionMiddleware::class => 250,
            ],
        ],
    ];

Changing the Handler Locator

ClassnameZendLocator

This locator simply appends the word Handler to the command's FQCN so you don't have to define any handler map. For example, if you request command App\Commands\LoginCommand, locator will try to get App\Command\LoginCommandHandler from the Service Manager., (*14)

Locator will work with FQCN's not registered in the Service Manager, altough to prevent additional costly checks, make sure the locator is registered as a invokable or factory., (*15)

To change the locator from ZendLocator to ClassnameZendLocator simply set it in the config:, (*16)

// ... your module config
'tactician' => [
    'default-locator' => TacticianModule\Locator\ClassnameZendLocator::class,
],

The Versions

01/09 2016

dev-master

9999999-dev

ZF2 Module to use the League of Extraordinary Packages' Tactician library - flexible command bus implementation

  Sources   Download

MIT

The Requires

 

The Development Requires

command bus zf2 zend framework tactician

01/09 2016

2.0.1

2.0.1.0

ZF2 Module to use the League of Extraordinary Packages' Tactician library - flexible command bus implementation

  Sources   Download

MIT

The Requires

 

The Development Requires

command bus zf2 zend framework tactician

01/09 2016

2.0

2.0.0.0

ZF2 Module to use the League of Extraordinary Packages' Tactician library - flexible command bus implementation

  Sources   Download

MIT

The Requires

 

The Development Requires

command bus zf2 zend framework tactician

15/06 2016

1.2.2

1.2.2.0

ZF2 Module to use the League of Extraordinary Packages' Tactician library - flexible command bus implementation

  Sources   Download

MIT

The Requires

 

The Development Requires

command bus zf2 zend framework tactician

20/05 2016

1.2.1

1.2.1.0

ZF2 Module to use the League of Extraordinary Packages' Tactician library - flexible command bus implementation

  Sources   Download

MIT

The Requires

 

The Development Requires

command bus zf2 zend framework tactician

02/05 2016

1.2

1.2.0.0

ZF2 Module to use the League of Extraordinary Packages' Tactician library - flexible command bus implementation

  Sources   Download

MIT

The Requires

 

The Development Requires

command bus zf2 zend framework tactician

07/03 2016

1.1.4

1.1.4.0

ZF2 Module to use the League of Extraordinary Packages' Tactician library - flexible command bus implementation

  Sources   Download

MIT

The Requires

 

command bus zf2 zend framework tactician

25/02 2016

1.1.3

1.1.3.0

ZF2 Module to use the League of Extraordinary Packages' Tactician library - flexible command bus implementation

  Sources   Download

MIT

The Requires

 

The Development Requires

command bus zf2 zend framework tactician

08/02 2016

1.1.2

1.1.2.0

ZF2 Module to use the League of Extraordinary Packages' Tactician library - flexible command bus implementation

  Sources   Download

MIT

The Requires

 

The Development Requires

command bus zf2 zend framework tactician

12/11 2015

1.1.1

1.1.1.0

ZF2 Module to use the League of Extraordinary Packages' Tactician library - flexible command bus implementation

  Sources   Download

MIT

The Requires

 

The Development Requires

command bus zf2 zend framework tactician

14/07 2015

1.1.0

1.1.0.0

ZF2 Module to use the League of Extraordinary Packages' Tactician library - flexible command bus implementation

  Sources   Download

MIT

The Requires

 

The Development Requires

command bus zf2 zend framework tactician

08/07 2015

1.0.3

1.0.3.0

ZF2 Module to use the League of Extraordinary Packages' Tactician library - flexible command bus implementation

  Sources   Download

MIT

The Requires

 

The Development Requires

command bus zf2 zend framework tactician

03/06 2015

1.0.2

1.0.2.0

ZF2 Module to use the League of Extraordinary Packages' Tactician library - flexible command bus implementation

  Sources   Download

MIT

The Requires

 

The Development Requires

command bus zf2 zend framework tactician

21/05 2015

1.0.1

1.0.1.0

ZF2 Module to use the League of Extraordinary Packages' Tactician library

  Sources   Download

MIT

The Requires

 

The Development Requires

command bus zf2 zend framework tactician

20/05 2015

1.0

1.0.0.0

ZF2 Module to use the League of Extraordinary Packages' Tactician library

  Sources   Download

MIT

The Requires

 

The Development Requires

command bus zf2 zend framework tactician