Michael-Rubel / laravel-enhanced-pipeline
Laravel Pipeline支持数据库事务,事件和额外方法。
4.0.0
2024-03-17 13:39 UTC
Requires
- php: ^8.1
- illuminate/contracts: ^10.0|^11.0
- spatie/laravel-package-tools: ^1.12
Requires (Dev)
- larastan/larastan: ^2.0
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.0|^8.0
- orchestra/testbench: ^8.0|^9.0
- phpunit/phpunit: ^9.5|^10.5
README
Laravel Enhanced Pipeline
Laravel Pipeline支持数据库事务,事件和额外方法。
此包需要PHP 8.1或更高版本以及Laravel 10或更高版本。
#StandWithUkraine
安装
使用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事件
。
可用事件
PipelineStarted
- 当管道开始工作时触发;PipelineFinished
- 当管道完成其工作时触发;PipeExecutionStarted
- 在执行管道之前触发;PipeExecutionFinished
- 在执行管道之后触发。
测试
composer test
致谢
- chefhasteeth 为Pipeline中的DB事务的基础实现。
- rezaamini-ir 为创建带有
onFailure
方法的管道提供了灵感。请参阅#PR
许可证
MIT许可证(MIT)。请参阅许可证文件以获取更多信息。