承接 defstudio/actions 相关项目开发

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

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

defstudio/actions

Composer 安装命令:

composer require defstudio/actions

包简介

Helpers methods for Laravel Actions

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status GitHub Static Analysis Action Status Total Downloads

an opinionated lightweight package for creating Action classes

Installation

You can install the package via composer:

composer require defstudio/actions

Usage

Add use DefStudio\Actions\ActAsAction; trait to your action class or extend DefStudio\Actions\Action

(optional) add a dockblock to hint the static run method parameters and return types

/**
 * @method static void run(Report|int $report)
 */
class DeleteReport
{
    use ActsAsAction;

    public function handle(Report|int $report): void
    {
        if (is_int($report)) {
            $report = Report::findOrFail($report);
        }

        DB::transaction(function () use ($report) {
            $report->delete_data();
            $report->delete();
        });
    }
}

class DeleteReport extends \DefStudio\Actions\Action
{
    public function handle(Report|int $report): bool
    {
        if (is_int($report)) {
            $report = Report::findOrFail($report);
        }

        return DB::transaction(function () use ($report) {
            $report->delete_data();
            return $report->delete();
        });
    }
}

Use the new methods:

$result = DeleteReport::run($report->id); //true

$result = DeleteReport::make()->handle($report->id); //true

Run multiple actions as once

an action can be run multiple times by calling its runMany() method

$results = DeleteReport::runMany($report1->id, $report2->id, $report3->id); //[true, false, true]

each run's result will be collected in an array and returned by runMany()

notes

if multiple parameters are required, they can be wrapped in an array (associative array keys will be treated as named arguments):

class{
    use InjectsItself;
 
    public function handle($name = 'guest', $title = 'Mr.'): string
    {
        return "$title $name";
    }
}

$result = MyAwesomeAction::runMany(['Elizabeth', "Ms."], ['Fabio'],  ['title' => 'Mrs.']);

// $result = ["Ms. Elizabeth", "Mr. Fabio", "Mrs. guest"] 

Mockable actions

Also, you can define a mock for the action (it will be authomatically bound to the app container):

FindTheAnswerToLifeTheUniverseAndEverything::mock(fn ($report_id) => 42);

FindTheAnswerToLifeTheUniverseAndEverything::run() // 42

if you are interested in only mocking the return value, you can write:

FindTheAnswerToLifeTheUniverseAndEverything::mock(42);

if your action has public methods other than handle, they can be mocked as well:

MyWeirdAction::mock(handle: fn() => 5, handleForAdmin: fn() => 42);

without arguments, mocks returns a MockInterface instance ready to be used

MyAction::mock()->shouldNotReceive('handle');

a partial mock (i.e. for actions with more than a single method)

BuildOrder::partial_mock(fromRequest: fn() => true);

//this will not be mocked
BuildOrder::make()->fromJson($data);

along with mocks, actions can also be spied:

$spiedAction = MyAction::spy();

$spiedAction->handle();
$spiedAction->handle();

$spiedAction->shouldHaveReceived()->handle()->twice()

Dispatchable actions

An action can be made dispatchable as a job with the ActsAsJob trait (or extending the Action class)

a job can be created by calling the job() static method:

dispatch(LongRunningAction::job($argument_1, $argument_2));

or can be dispatched with its dedicated methods:

LongRunningAction::dispatch($argument_1, $argument_2);

LongRunningAction::dispatchSync($argument_1, $argument_2);

LongRunningAction::dispatchAfterResponse($argument_1, $argument_2);

The action will be dispatched wrapped in a ActionJob decorator that will proxy properties as needed:

use DefStudio\Actions\Concerns\ActsAsJob;

class LongRunningAction{
    use ActsAsJob;
    
    public int $timeout = 2 * 60 * 60;
    public int $tries = 4;
    public array $backoff = [60, 120, 300, 600];
    public string $queue = 'long-running';
    
    public function handle(){...}
}

Cleaning up after failed action job

Failed action jobs can be handled by defining a jobFailed() method:

class LongRunningAction{
    use ActsAsJob;
       
    public function handle(){..}
    
    public function jobFailed($exception)
    {
        $this->handleFailure();
    }
    
    private function handleFailure(){..}
}

Batches and Chains of action jobs

Similarly to the runMany() method, a new batch/chain of action jobs can be created starting from an array of parameters:

MyAction::batch([$name1, $title1], [$name2, $title2])->dispatch();

MyAction::chain([$name1, $title1], [$name2, $title2])->dispatch();

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

This project was inspired and is built as an opinionated and simplified implementation of Loris Leiva's Laravel Actions. For a more powerful tool, you should take a look at it.

License

The MIT License (MIT). Please see License File for more information.

defstudio/actions 适用场景与选型建议

defstudio/actions 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.97k 次下载、GitHub Stars 达 11, 最近一次更新时间为 2021 年 11 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 defstudio/actions 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 9.97k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 11
  • 点击次数: 17
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 11
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-11-23