2017 © Pedro Peláez
 

library tactician-laravel

Integrate tactician with Laravel 5

image

madewithlove/tactician-laravel

Integrate tactician with Laravel 5

  • Monday, May 7, 2018
  • by bramdevries
  • Repository
  • 11 Watchers
  • 19 Stars
  • 10,400 Installations
  • PHP
  • 0 Dependents
  • 1 Suggesters
  • 4 Forks
  • 1 Open issues
  • 7 Versions
  • 13 % Grown

The README.md

madewithlove/tactician-laravel

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

Introduction

This packages is a replacement for Laravel 5's default command bus using tactician., (*2)

Default middleware

  • LockingMiddleware (block commands from running inside commands)
  • TransactionMiddleware (Run all commands in a database transaction and rolls back incase of failure)

Command Handling

By default commands will be resolved as followed:, (*3)

Acme\Jobs\Foo => Acme\Listeners\Foo
Acme\Foo\Jobs\Bar => Acme\Foo\Listeners\Bar

All command handlers are resolved out of the container which mean you can use all kind of Laravel goodies., (*4)

Install

``` bash $ composer require madewithlove/tactician-laravel, (*5)


### Laravel <= 5.4 Add the service provider to `config/app.php`: ```php Madewithlove\Tactician\ServiceProvider::class,

In case you want to tweak the middleware you should publish the package configuration:, (*6)

php artisan vendor:publish --provider="Madewithlove\Tactician\ServiceProvider"

Usage

Writing commands

A command always consists out of two parts: the command and the handler., (*7)

// Products\Jobs\CalculatePriceForQuantity
class CalculatePriceForQuantity
{
    public $price;

    public $amount;

    public function __construct($price, $amount = 1)
    {
        $this->price = $price;
        $this->amount = $amount;
    }
}

use Products\Jobs\CalculatePriceForQuantity as Job;

// Products\Listeners\CalculatePriceForQuantity
class CalculatePriceForQuantity
{
    public function handle(Job $job)
    {
        return $job->amount * $job->price;
    }
}

Overriding the command handling logic

If you're not happy with the default logic shipped in this package you can overwrite it easily by rebinding the League\Tactician\Handler\CommandHandlerMiddleware. You do this by adding the following to your application's service provider, refer to Tactician's documentation for options., (*8)

public function register()
{
    $this->app->bind(CommandHandlerMiddleware::class, function () {
        // Return your own implementation of CommandHandlerMiddleware here.
    });
}

Middleware

This package includes a couple middleware specific to Laravel, you can choose to use these., (*9)

TransactionMiddleware

This middleware is included by default. It means all your commands are handled inside of a database transaction, and if an error occurs it will rollback the transaction., (*10)

It's quite common for a command to throw an exception that is caught higher up the chain so a certain action can be performed (such as displaying an error message) but still perform some kind of database interaction. For cases such as this you can make use of the Madewithlove\Tactician\Contracts\IgnoresRollback interface. Simply implement it on your exception and no rollbacks will be performed!, (*11)

Note: This middleware only runs the main database connection in a transaction, if you use multiple connections you will need to come up with a custom solution., (*12)

Testing

bash $ composer test, (*13)

License

The MIT License (MIT). Please see License File for more information., (*14)

The Versions

07/05 2018