robotusers/cakephp-tactician

CakePHP Tactician 插件

安装数: 4,469

依赖项: 0

建议者: 0

安全性: 0

星星: 8

关注者: 2

分支: 2

开放问题: 3

类型:cakephp-plugin

0.4.0 2022-11-21 14:15 UTC

This package is auto-updated.

Last update: 2024-09-21 18:14:45 UTC


README

Software License Build Status codecov

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.initializeController.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)。

这将仅将 FooBar 命令包装在事务中

//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 来包含所有命令,或仅排除一些命令。

这将将所有命令包装在事务中,但 FooBar 命令除外

$middleware = new TransactionMiddleware($connection, true, [
    FooCommand::class,
    'My/Plugin.Bar',
]),