承接 astrotechlabs/yii2-tactician 相关项目开发

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

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

astrotechlabs/yii2-tactician

Composer 安装命令:

composer create-project astrotechlabs/yii2-tactician

包简介

Yii 2 Command Bus pattern implementation

README 文档

README

Packagist PHP Version Support GitHub stars GitHub forks GitHub repo size GitHub license

This is a Yii Framework 2 Wrapper/Adapter for Tactician Command Bus Library. It provides an easy way to use the command bus pattern in Yii2 Based apps.

When should I use Command Bus?

Tactician is a great fit if you’ve got a service layer. If you’re not sure what a service layer is, Martin Fowler’s PoEAA is a good starting point. Tactician’s author also did a talk on the subject.

Commands really help capture user intent. They’re also a great stand-in for the models when it comes to forms or serializer libraries that expect getter/setter objects.

The command bus itself is really easy to decorate with extra behaviors, like locking or database transactions so it’s very easy to extend with plugins.

By: tactician.thephpleague.com

When should I NOT use it?

If you’ve got a very small app that doesn’t need a service layer, then Tactician won’t offer much to you.

If you’re already using a tool that provides a command bus (like Broadway), you’re probably okay there too.

By: tactician.thephpleague.com

Installation

$ composer require astrotechlabs/yii2-tactician

Setup

First you must have configure your Yii Dependency Injection Container to be able to use Command and Handler classes within it.

You should have something like this in your config/web.php:

$config = [
    'id' => 'your-app-id',
    //...
    'container' => [
        'definitions' => [
            MyClassCommand::class => MyClassHandler::class 
        ]
    ],
    'components' => [
        //...
    ]
];

IMPORTANT: you must follow the conventions below:

  • Your Command class must be suffixed by Command;
  • Your Handler class must be: Same Command Class Name + Without Command Suffix + Handler.

The last one is register Yii2TacticianCommandBus component in your config/web.php file, as below:

$config = [
    'id' => 'your-app-id',
    //...
    'container' => [
        'definitions' => [
            MyClassCommand::class => MyClassHandler::class 
        ]
    ],
    'components' => [
        'commandBus' => [
            'class' => AstrotechLabs\Yii2Tactician\Yii2TacticianCommandBus::class
        ],
        // other components...
    ]
];

How to Use

Mapping Command and Handler Classes

Define a Command class somewhere in your application, for example:

class MyClassCommand
{
    public $someParam;
    public $someOtherParam;

    public function __construct($someParam, $someOtherParam = 'defaultValue')
    {
    	$this->someParam = $someParam;
        $this->someOtherParam = $someOtherParam;
    }
}

Define your Handler class should be something like:

class MyClassHandler
{
    public function handle(MyClassCommand $command)
    {
    	// do command stuff here!
        // we can use $command->someParam and $this->someOtherParam
    }
}

Now we can use this command in controllers or wherever you want:

public function actionDoSomething()
{
    $queryParam = Yii::$app->getRequest()->get('some_param');
    
    // Here the magic happens! =)
    $result = Yii::$app->commandBus->handle(new MyClassCommand($queryParam));

    if ($result === true) {
    	return $this->redirect(['go/to/some/place']);
    }

    return $this->render('some-awesome-view');
}

Using String Path

You can use a command class as String Path instead of concrete object. For this case, just do:

$config = [
    'id' => 'your-app-id',
    //...
    'container' => [
        'definitions' => [
            // you can use any string here.
            'awesome.alias.to.be.called.anywhere' => MyClassHandler::class 
        ]
    ],
    'components' => [
        //...
    ]
];

Your Handle Class should be as follows:

class MyClassHandler implements AstrotechLabs\Yii2Tactician\Handler
{
    public function handle(MyClassCommand $command)
    {
    	// do command stuff here!
        // we can use $command->someParam and $this->someOtherParam
    }
    
    public function commandClassName(): string
    {
        return MyClassCommand::class;
    }
}

IMPORTANT: in this way your Handler Class MUST implements the AstrotechLabs\Yii2Tactician\Handler interface and your Command Class MUST implements AstrotechLabs\Yii2Tactician\Command.

Your controller action or anywhere else:

public function actionDoSomething()
{
    $result = Yii::$app->commandBus->handle('awesome.alias.to.be.called.anywhere', [
        'someParam' => 'abc',
        'someOtherParam' => 'def'
    ]);
    
    // .. other logics
}

Under the hood the Command Bus System will call:

MyClassHandler::handle($command);

where $command argument is created inside that like:

MyClassCommand::create([someParam' => 'abc', 'someOtherParam' => 'def']);

Enjoy =).

Authors

See also the list of contributors who participated in this project.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

Licence

This package is released under the MIT License. See the bundled LICENSE for details.

References

astrotechlabs/yii2-tactician 适用场景与选型建议

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

它主要适用于以下技术方向: 「framework」 「service layer」 「cqrs」 「yii2」 「command bus」 「command handler」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 6
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-12-24