robotusers / cakephp-tactician
CakePHP Tactician 插件
Requires
- php: >=7.2
- cakephp/core: ~4.0
- cakephp/datasource: ~4.0
- league/tactician: ^1.0
Requires (Dev)
- cakephp/cakephp: ~4.0.0
- cakephp/cakephp-codesniffer: *
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^0.12.6
- phpunit/phpunit: ~8.0
- robotusers/commander: ^0.2.0
Suggests
- cakephp/cakephp: Install ^3.6 if you want to use CommandFactory.
- cakephp/event: Install ^3.4 if you want to use BusListener.
- robotusers/commander: Install ^0.2 if you want to use BusListener.
This package is auto-updated.
Last update: 2024-09-21 18:14:45 UTC
README
CakePHP 插件,用于 league/tactician
。
注意:该插件处于开发中。
安装
composer require robotusers/cakephp-tactician
bin/cake plugin load Robotusers/Tactician
使用插件
CakePHP 集成
此插件通过 Commander 库提供 Controller 和 Model 集成。
Commander 是一个 PHP 命令总线抽象库,它使您能够将代码与具体的命令总线实现解耦。
使用 Commander(PHP 7.1+)
安装 robotusers/commander
composer require robotusers/commander
设置您的控制器
use Cake\Controller\Controller; use Robotusers\Commander\CommandBusAwareInterface; use Robotusers\Tactician\Bus\CommandBusAwareTrait; class OrdersController extends Controller implements CommandBusAwareInterface { use CommandBusAwareTrait; public function makeOrder() { // ... $command = new MakeOrderCommand($data); $this->handleCommand($command); // ... // or using quick convention enabled command handling: $this->handleCommand('MakeOrder', $data); // ... } }
有关更多信息,请参阅文档。
接下来,您应该配置命令总线,该总线将被注入到实现 CommandBusAwareInterface
的控制器和模型中。
控制台(CakePHP 3.6+)
对于控制台集成,Tactician 插件提供 CommandFactory
,它将命令总线注入到兼容的控制台外壳和命令中。
按以下方式设置您的 CommandRunner
use App\Application; use Cake\Console\CommandRunner; use Cake\Console\CommandFactory; use Tactician\Console\CommandFactory as TacticianCommandFactory; $application = new Application(dirname(__DIR__) . '/config'); $cakeFactory = new CommandFactory(); // or any other custom factory (ie. CakePHP DI plugin DIC-compatible factory) $factory = new TacticianCommandFactory($cakeFactory, $application); $runner = new CommandRunner($application, 'cake', $factory); exit($runner->run($argv));
应用程序钩子(CakePHP 3.3+)
如果您的应用程序支持中间件,您可以使用应用程序钩子来配置命令总线。
use Cake\Http\BaseApplication; use League\Tactician\CommandBus; use Robotusers\Tactician\Core\BusApplicationInterface; use Robotusers\Tactician\Core\BusMiddleware; class Application extends BaseApplication implements BusApplicationInterface { public function commandBus() { $bus = new CommandBus([ // your middleware ]); return $bus; } public function middleware($middleware) { // ... $middleware->add(new BusMiddleware($this)); // ... return $middleware; } }
您可以使用辅助工厂方法来构建 CommandBus
或启用 CakePHP 约定的 CommandHandlerMiddleware
。
use Robotusers\Tactician\Bus\Factory; public function commandBus() { return Factory::createCommandBus([ // your middleware Factory::createCommandHandlerMiddleware(); ]); }
在此处配置的命令总线将在 Model.initialize
和 Controller.initialize
事件监听器中将注入到控制器和模型中。
引导
如果您仍在 3.3 之前的版本上,您可以在 bootstrap.php
文件中设置监听器。
您可以使用内置的 快速入门 类
// bootstrap.php use Robotusers\Tactician\Event\QuickStart; QuickStart::setUp($commandBus);
QuickStart
可以加载简单的 CakePHP 约定启用总线,如果尚未提供
// bootstrap.php use Robotusers\Tactician\Event\QuickStart; QuickStart::setUp();
约定定位器
CakePHP 约定定位器将根据约定查找命令处理器,命令应位于 App\Model\Command\
命名空间下,并以 Command
字符串结尾,处理器应位于 App\Model\Handler\
命名空间下,并以 Handler
字符串结尾。
//CakePHP convention locator $locator = new ConventionsLocator(); $extractor = new ClassNameExtractor(); $inflector = new HandleClassNameInflector(); $commandBus = new CommandBus( [ new CommandHandlerMiddleware($extractor, $locator, $inflector) ] );
在此示例中,App\Model\Command\MakeOrderCommand
命令将映射到 App\Model\Handler\MakeOrderHandler
处理器。
您可以使用配置选项更改默认命名空间和后缀
$locator = new ConventionsLocator([ 'commandNamespace' => 'Command', 'commandSuffix' => '', 'handlerNamespace' => 'Handler', 'handlerSuffix' => '', ]);
在此示例中,App\Command\MakeOrder
命令将映射到 App\Handler\MakeOrder
处理器。注意命名空间不同且没有后缀。
事务中间件
事务中间件是 CakePHP ConnectionInterface::transactional()
的包装器。您需要提供支持的事务命令列表。
列表支持 FQCN 或约定支持名称(例如 Plugin.Name
)。
这将仅将 Foo
和 Bar
命令包装在事务中
//default connection $connection = ConnectionManager::get('default'); $commandBus = new CommandBus( [ //CakePHP transaction middleware with a connection and a list of commands. new TransactionMiddleware($connection, [ FooCommand::class, 'My/Plugin.Bar', ]), $commandHandlerMiddleware ] );
您可以通过将 $commands
参数设置为 true
来包含所有命令,或仅排除一些命令。
这将将所有命令包装在事务中,但 Foo
和 Bar
命令除外
$middleware = new TransactionMiddleware($connection, true, [ FooCommand::class, 'My/Plugin.Bar', ]),