Laravel Tactician in an implementation of the Command Bus Tactician by Ross Tuck and based on: joselfonseca/laravel-tactician
Simply do a composer require:
composer require victormln/laravel-tactician:1.0.*
Or add this line to your composer.json file
"victormln/laravel-tactician" : "1.0.*"
Once the dependencies have been downloaded, add the service provider to your config/app.php file
'providers' => [
...
Victormln\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('Victormln\LaravelTactician\CommandBusInterface');
Or you can inject it into a class constructor
use Victormln\LaravelTactician\CommandBusInterface;
class MyController extends BaseController
{
/** @var CommandBusInterface */
private $bus;
public function __construct(CommandBusInterface $bus)
{
$this->bus = $bus;
}
}
After inject the commandBus, you can dispatch the command as simple as this:
// First parameter expects the command
$bus->dispatch(new SimpleCommand());
NOTE: This package is build to automatically grab the CommandHandler from the same path as the Command, so you don't have to do anything to bind the two files. But if you want, you can bind the command handler manually calling the addHandler method
$bus->addHandler('Path\SimpleCommand', 'Path\SimpleCommandHandler');
$bus->dispatch(new SimpleCommand());
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/victormln/f06a86f7204b251d6d6c876d5e516a67
You can configure the bindings for the locator, inflector, extractor and default bus publishing the config file like so
php artisan vendor:publish --provider="Victormln\LaravelTactician\Providers\LaravelTacticianServiceProvider"
Then you can modify each class name and they will be resolved from the laravel container
return [
// The locator to bind
'locator' => 'Victormln\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' => 'Victormln\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 FooCommandHandler 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
Victormln\LaravelTactician\Middleware\DatabaseTransactions
.
Please see the releases page https://github.com/victormln/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.