Laravel Tactician in an implementation of the Command Bus Tactician by Ross Tuck.
To install this update your composer.json file to require
"joselfonseca/laravel-tactician" : "0.5.*"
ServiceProvider will be attached automatically
Once the dependencies have been downloaded, add the service provider to your config/app.php file
'providers' => [
...
Joselfonseca\LaravelTactician\Providers\LaravelTacticianServiceProvider::class
...
]
You are done with the installation!
To use the command bus you can resolve the bus from the laravel container like so
$bus = app('Joselfonseca\LaravelTactician\CommandBusInterface');
Or you can inject it into a class constructor
use Joselfonseca\LaravelTactician\CommandBusInterface;
class MyController extends BaseController
{
public function __construct(CommandBusInterface $bus)
{
$this->bus = $bus;
}
}
Once you have the bus instance you can add your handler for the command to be dispatched
$bus->addHandler('SomeCommand', 'SomeHandler');
Now you can dispatch the command with the middleware.
// first parameter is the class name of the command
// Second parameter is an array of input data to be mapped to the command
// Third parameter is an array of middleware class names to be added to the stack
$bus->dispatch('SomeCommand', [], []);
You can map the input data array of the Command's constructor with plain list of arguments or the array itself. For example:
// Send parameters in an array of input data ...
$bus->dispatch('SomeCommand', [
'propertyOne' => 'One',
'propertyTwo' => 'Two',
'propertyThree' => 'Three',
], []);
// ... and recive them as individual parameters ...
Class SomeCommand {
public function __construct($propertyOne = 'A', $propertyTwo = 'B', $propertyThree = 'C'){
//...
}
}
// ... or recive array of input data itself
Class SomeCommand {
public function __construct(array $data = [
'propertyOne' => 'A',
'propertyTwo' => 'B',
'propertyThree' => 'C',
]){
//...
}
}
Of course, you can use default values!
For more information about the usage of the tactician command bus please visit http://tactician.thephpleague.com/
Check out this example of the package implemented in a simple create order command https://gist.github.com/joselfonseca/24ee0e96666a06b16f92
You can configure the bindings for the locator, inflector, extractor and default bus publishing the config file like so
php artisan vendor:publish
Then you can modify each class name and they will be resolved from the laravel container
return [
// The locator to bind
'locator' => 'Joselfonseca\LaravelTactician\Locator\LaravelLocator',
// The inflector to bind
'inflector' => 'League\Tactician\Handler\MethodNameInflector\HandleInflector',
// The extractor to bind
'extractor' => 'League\Tactician\Handler\CommandNameExtractor\ClassNameExtractor',
// The bus to bind
'bus' => 'Joselfonseca\LaravelTactician\Bus'
];
You can generate Commands and Handlers automatically using artisan
artisan make:tactician:command Foo
artisan make:tactician:handler Foo
This will create FooCommand and FooHandler and place them in the app/CommandBus/Commands and app/CommandBus/Handlers respectively
To run both at once
artisan make:tactician Foo
Laravel tactician includes some useful middleware you can use in your commands
- Database Transactions: This Middleware will run the command inside a database transaction, if any exception is thrown the transaction won't be committed and the database will stay intact, you can find this middleware in
Joselfonseca\LaravelTactician\Middleware\DatabaseTransactions
.
Please see the releases page https://github.com/joselfonseca/laravel-tactician/releases
To run the test in this package, navigate to the root folder of the project and run
composer install
Then
vendor/bin/phpunit
Please see CONTRIBUTING for details.
If you discover any security related issues, please email jose at ditecnologia dot com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.