承接 robotusers/cakephp-tactician 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

robotusers/cakephp-tactician

Composer 安装命令:

composer require robotusers/cakephp-tactician

包简介

CakePHP Tactician plugin

README 文档

README

Software License Build Status codecov

CakePHP plugin for league/tactician.

NOTE: The plugin is under development.

Installation

composer require robotusers/cakephp-tactician
bin/cake plugin load Robotusers/Tactician

Using the plugin

CakePHP integration

This plugin provides Controller and Model integration through Commander library.

Commander is a command bus abstraction library for PHP which enables you to decouple your code from a concrete command bus implementation.

Using the Commander (PHP 7.1+)

Install robotusers/commander:

composer require robotusers/commander

Set up your controllers:

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);
        // ...
   }

}

For more information, read the docs.

Next you should configure the command bus which will be injected into your controllers and models that implement the CommandBusAwareInterface.

Console (CakePHP 3.6+)

For console integration Tactician plugin provides a CommandFactory that injects a command bus into compatible console shells and commands.

Set up your CommandRunner as below:

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));

Application hook (CakePHP 3.3+)

If your application supports middleware you can configure the command bus using an application hook.

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;
    }
}

You can use helper factory methods for building CommandBus or CakePHP convention enabled CommandHandlerMiddleware:

use Robotusers\Tactician\Bus\Factory;

public function commandBus()
{
    return Factory::createCommandBus([
        // your middleware
        Factory::createCommandHandlerMiddleware();
    ]);
}

The command bus configured here will be injected into controllers and models in Model.initialize and Controller.initialize event listener.

Bootstrap

If you're still on pre 3.3 stack you can set up the listener in your bootstrap.php file.

You can use build in quick start class:

// bootstrap.php

use Robotusers\Tactician\Event\QuickStart;

QuickStart::setUp($commandBus);

QuickStart can load simple CakePHP convention enabled bus if it hasn't been provided:

// bootstrap.php

use Robotusers\Tactician\Event\QuickStart;

QuickStart::setUp();

Conventions locator

CakePHP Conventions locator will look for command handlers based on a convention, that commands should reside under App\Model\Command\ namespace and be suffixed with Command string and handlers should reside under App\Model\Handler\ namespace and be suffixed with Handler string.

//CakePHP convention locator
$locator = new ConventionsLocator();
$extractor = new ClassNameExtractor();
$inflector = new HandleClassNameInflector();

$commandBus = new CommandBus(
    [
        new CommandHandlerMiddleware($extractor, $locator, $inflector)
    ]
);

In this example App\Model\Command\MakeOrderCommand command will map to App\Model\Handler\MakeOrderHandler handler.

You can change default namespace and suffix using configuration options:

$locator = new ConventionsLocator([
    'commandNamespace' => 'Command',
    'commandSuffix' => '',
    'handlerNamespace' => 'Handler',
    'handlerSuffix' => '',
]);

In this example App\Command\MakeOrder command will map to App\Handler\MakeOrder handler. Note a different namespace and no suffix.

Transaction middleware

Transaction middleware is a wrapper for CakePHP ConnectionInterface::transactional(). You need to provide a list of supported commands.

A list supports FQCN or convention supported name (eq Plugin.Name).

This will wrap only Foo and Bar commands in a transaction:

//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
    ]
);

You can include all commands by setting the $commands argument to true and exclude only some commands.

This will wrap all commands in a transaction with an exception for Foo and Bar commands:

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

robotusers/cakephp-tactician 适用场景与选型建议

robotusers/cakephp-tactician 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.9k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2017 年 07 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 robotusers/cakephp-tactician 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 robotusers/cakephp-tactician 我们能提供哪些服务?
定制开发 / 二次开发

基于 robotusers/cakephp-tactician 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 4.9k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 9
  • 点击次数: 6
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 9
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-07-24