Michael-Rubel/laravel-enhanced-pipeline

Laravel Pipeline支持数据库事务,事件和额外方法。

4.0.0 2024-03-17 13:39 UTC

This package is auto-updated.

Last update: 2024-09-17 14:55:54 UTC


README

Enhanced Pipeline in Laravel

Laravel Enhanced Pipeline

Latest Version on Packagist Total Downloads Code Quality Code Coverage GitHub Tests Action Status PHPStan

Laravel Pipeline支持数据库事务,事件和额外方法。

此包需要PHP 8.1或更高版本以及Laravel 10或更高版本。

#StandWithUkraine

SWUbanner

安装

使用Composer安装包

composer require michael-rubel/laravel-enhanced-pipeline

用法

将修改后的管道导入您的类

use MichaelRubel\EnhancedPipeline\Pipeline;

然后使用管道

Pipeline::make()
    ->withEvents()
    ->withTransaction()
    ->send($data)
    ->through([
        // your pipes
    ])
    ->onFailure(function ($data, $exception) {
        // do something when exception caught

        return $data;
    })->then(function ($data) {
        // do something when all pipes completed their work

        return $data;
    });

您还可以使用服务容器或手动实例化管道

app(Pipeline::class)
    ...

(new Pipeline(app()))
    ...

(new Pipeline)
    ->setContainer(app())
    ...

您可以使用run方法执行单个管道

$pipeline = Pipeline::make();

$pipeline->run(Pipe::class, $data);

默认情况下,run使用您的类中的handle方法作为入口点,但如果您的管道中使用了不同的方法名,您可以在ServiceProvider中添加代码来修复它

$this->app->resolving(Pipeline::class, function ($pipeline) {
    return $pipeline->via('execute');
});

如果您想覆盖通过IoC容器解析的原始Pipeline,您可以在ServiceProvider的register方法中添加绑定

$this->app->singleton(\Illuminate\Pipeline\Pipeline::class, \MichaelRubel\EnhancedPipeline\Pipeline::class);

事务

使用withTransaction方法将在管道执行过程中启用手动数据库事务

事件

使用withEvents方法将在管道执行过程中启用Laravel事件

可用事件

测试

composer test

致谢

  • chefhasteeth 为Pipeline中的DB事务的基础实现。
  • rezaamini-ir 为创建带有onFailure方法的管道提供了灵感。请参阅#PR

许可证

MIT许可证(MIT)。请参阅许可证文件以获取更多信息。