joselfonseca / laravel-tactician
Requires
- php: >=5.6.0
- league/tactician: 1.0.*
Requires (Dev)
- mockery/mockery: ^1.0
- orchestra/testbench: ~4.0
- phpunit/phpunit: ^8.0
- scrutinizer/ocular: ~1.1
README
Ross Tuck 实现的 Command Bus Tactician 的 Laravel 封装。
安装
要安装此包,请在 composer.json 中运行 composer require joselfonseca/laravel-tactician
>= laravel5.5
ServiceProvider 将自动附加。
其他
下载依赖后,将服务提供者添加到 config/app.php 文件中
'providers' => [ ... Joselfonseca\LaravelTactician\Providers\LaravelTacticianServiceProvider::class ... ]
安装完成!
使用方法
要使用命令总线,可以从 Laravel 容器中解析总线,如下所示
$bus = app('Joselfonseca\LaravelTactician\CommandBusInterface');
或者您可以将其注入到类构造函数中
use Joselfonseca\LaravelTactician\CommandBusInterface; class MyController extends BaseController { public function __construct(CommandBusInterface $bus) { $this->bus = $bus; } }
一旦您有了总线实例,您可以添加您的命令处理器
$bus->addHandler('SomeCommand', 'SomeHandler');
现在您可以使用中间件分发命令
// 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', [], []);
您可以将 Command 的 构造函数 的输入数据数组映射到普通的参数列表或数组本身。例如
// Send parameters in an array of input data ... $bus->dispatch('SomeCommand', [ 'propertyOne' => 'One', 'propertyTwo' => 'Two', 'propertyThree' => 'Three', ], []); // ... and receive them as individual parameters ... Class SomeCommand { public function __construct($propertyOne = 'A', $propertyTwo = 'B', $propertyThree = 'C'){ //... } } // ... or receive array of input data itself Class SomeCommand { public function __construct(array $data = [ 'propertyOne' => 'A', 'propertyTwo' => 'B', 'propertyThree' => 'C', ]){ //... } }
当然,您可以使用默认值!
有关 tactician 命令总线使用方法的更多信息,请访问 http://tactician.thephpleague.com/。
示例
请查看这个在简单创建订单命令中实现的包的示例 https://gist.github.com/joselfonseca/24ee0e96666a06b16f92。
绑定
您可以通过发布配置文件来配置定位器、屈折词、提取器和默认总线的绑定
php artisan vendor:publish
然后您可以修改每个类名,它们将来自 Laravel 容器解析
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' ];
生成器
您可以使用 artisan 自动生成 Commands 和 Handlers
artisan make:tactician:command Foo
artisan make:tactician:handler Foo
这将创建 FooCommand 和 FooHandler 并将它们放在 app/Commands
文件夹中,请注意,Command
和 Handler
将分别添加到类名中,所以在上面的例子中,创建的类将是 FooCommand
和 FooHandler
。
要同时运行它们
artisan make:tactician Foo
包含的中间件
Laravel tactician 包含一些您可以在命令中使用的有用中间件。
- 数据库事务:此中间件将在数据库事务中运行命令,如果抛出任何异常,事务将不会提交,数据库将保持完整,您可以在
Joselfonseca\LaravelTactician\Middleware\DatabaseTransactions
中找到此中间件。
变更日志
请参阅发布页面 https://github.com/joselfonseca/laravel-tactician/releases
测试
要运行此包中的测试,请导航到项目的根目录并运行
composer install
然后
vendor/bin/phpunit
贡献
请参阅CONTRIBUTING以获取详细信息。
安全
如果您发现任何与安全相关的问题,请通过电子邮件jose at ditecnologia dot com联系,而不是使用问题跟踪器。
鸣谢
许可证
MIT许可证(MIT)。请参阅许可证文件以获取更多信息。