ibrostudio/laravel-piped-tasks
Composer 安装命令:
composer require ibrostudio/laravel-piped-tasks
包简介
Manage tasks workflows through Laravel Pipes
关键字:
README 文档
README
Manage tasks workflows through Laravel Pipes.
Concept
A process defines the order of tasks executed through a pipe.
Each process is associated with a payload. Payload is a mutable object passed to each task to retrieve, add, or update data.
Each steps of a process can be loggued through Spatie laravel-activitylog.
You can use external tasks inside a process with the Resumable feature.
Installation
Install the package via composer:
composer require ibrostudio/laravel-piped-tasks
Then create the tables:
php artisan piped-tasks:install
Usage
1. Create process
First you need to generate a process:
php artisan make:piped-process CreateOrderProcess
Name your process like this : <Action><Domain>Process
Note: It is possible to generate processes and tasks for a package.
2. Define Payload
You'll find the associated payload to your process in App\Processes\Payloads and its interface in App\Processes\Payloads\Contracts.
Add properties and methods according to your workflow:
<?php namespace App\Processes\Payloads\Contracts; use App\Models\Cart; use App\Models\Invoice; use App\Models\Order; use App\Models\Payment; interface TenantPayload { public function getCart(): Cart; public function setOrder(Order $order): void; public function getOrder(): Order|null; public function setPayment(Payment $payment): void; public function getPayment(): Payment|null; public function setInvoice(Invoice $invoice): void; public function getInvoice(): Invoice|null } --------------------------- namespace App\Processes\Payloads; use App\Models\Cart; use App\Models\Invoice; use App\Models\Order; use App\Models\Payment; use IBroStudio\PipedTasks\PayloadAbstract; final class CreateOrderPayload extends PayloadAbstract implements OrderPayload { public function __construct( protected Cart $cart, protected ?Order $order = null, protected ?Payment $payment = null, protected ?Invoice $invoice = null, ) {} public function getCart(): Cart { return $this->cart; } public function setOrder(Order $order): void { $this->order = $order; } public function getOrder(): Order|null { return $this->order; } public function setPayment(Payment $payment): void { $this->payment = $payment; } public function getPayment(): Payment|null { return $this->payment; } public function setInvoice(Invoice $invoice): void { $this->invoice = $invoice; } public function getInvoice(): Invoice|null { return $this->invoice; } }
For reusability, methods can be shared by payloads by placing them in traits:
<?php namespace App\Processes\Payloads\Concerns; use App\Models\Cart; use App\Models\Invoice; use App\Models\Order; use App\Models\Payment; trait OrderPayloadMethods { public function getCart(): Cart { return $this->cart; } public function setOrder(Order $order): void { $this->order = $order; } public function getOrder(): Order|null { return $this->order; } (...) --------------------------- namespace App\Processes\Payloads; use App\Processes\Payloads\Concerns\OrderPayloadMethods; use App\Models\Cart; use App\Models\Invoice; use App\Models\Order; use App\Models\Payment; use IBroStudio\PipedTasks\PayloadAbstract; final class CreateOrderPayload extends PayloadAbstract implements OrderPayload { use OrderPayloadMethods; public function __construct( protected Cart $cart, protected ?Order $order = null, protected ?Payment $payment = null, protected ?Invoice $invoice = null, ) {} } --------------------------- namespace App\Processes\Payloads; use App\Processes\Payloads\Concerns\OrderPayloadMethods; use App\Models\Cart; use App\Models\Invoice; use App\Models\Order; use App\Models\Payment; use IBroStudio\PipedTasks\PayloadAbstract; final class RebillOrderPayload extends PayloadAbstract implements OrderPayload { use OrderPayloadMethods; public function __construct( protected Order $order, protected ?Payment $payment = null, protected ?Invoice $invoice = null, ) {} }
3. Create tasks
Generate your tasks with this command:
php artisan make:piped-task MakePaymentTask
Name your process like this : <Action><Domain>Task
For convenience and reusability, tasks use actions (from Spatie's Laravel Queuable Action):
<?php namespace App\Processes\Tasks; use App\Actions\MakePaymentAction; use App\Processes\Payloads\Contracts\OrderPayload; use IBroStudio\User\Actions\CreateUserAction; use IBroStudio\User\Processes\Payloads\Contracts\UserPayload; use Closure; final readonly class MakePaymentTask { public function __construct( private MakePaymentAction $action, ) {} public function __invoke(OrderPayload $payload, Closure $next): mixed { $payload->setPayment( $this->action->execute($payload->getOrder()) ); return $next($payload); } } --------------------------- namespace App\Actions; use App\Models\Order; use App\Models\Payment; use Spatie\QueueableAction\QueueableAction; final class MakePaymentAction { use QueueableAction; public function execute(Order $order): Payment { $payment = 'Process payment and return model'; return $payment; } }
4. Add tasks to the process
Under the hood, process uses Michael Rubel's Laravel Enhanced Pipeline and supports all features from it like DB transaction or events:
<?php namespace App\Processes; use App\Processes\Tasks; use IBroStudio\PipedTasks\Models\Process; class CreateOrderProcess extends Process { protected array $tasks = [ Tasks\CreateOrderTask::class, Tasks\MakePaymentTask::class, Tasks\GenerateInvoiceTask::class, Tasks\SendInvoiceToCustomerTask::class, Tasks\NewOrderNotificationTask::class, ]; }
5. Execute process
<?php use App\Processes\CreateOrderProcess; use App\Processes\Payloads\CreateOrderPayload; $process = CreateOrderProcess::process(['cart' => $cart]); $process->getOrder();
Argument passed to the process static method is an array used to build the Payload.
Processable models
You can link processes to any Eloquent model implementing the Processable interface and using the IsProcessable trait:
<?php namespace App\Models; use IBroStudio\PipedTasks\Concerns\IsProcessable; use IBroStudio\PipedTasks\Contracts\Processable; use Illuminate\Database\Eloquent\Model; class Cart extends Model implements Processable { use IsProcessable; }
It allows to call process from the model:
<?php $cart->process(CreateOrderProcess::class);
And permits to access to the model in tasks:
<?php namespace App\Processes\Tasks; use App\Processes\Payloads\Contracts\OrderPayload; use Closure; class CreateOrderTask { public function __invoke(OrderPayload $payload, Closure $next): mixed { $cart = $payload->getProcess()->processable; return $next($payload); } }
Adding processable during process
If the processable model is created during a process, you can assign it to the process in a task:
<?php use App\Models\Order; use App\Processes\CreateOrderProcess; Order::callProcess(CreateOrderProcess::class)
<?php namespace App\Processes\Tasks; use App\Models\Order; use App\Processes\Payloads\Contracts\OrderPayload; use Closure; class CreateOrderTask { public function __invoke(OrderPayload $payload, Closure $next): mixed { $payload->getProcess()->addProcessable( Order::create([...]); ); return $next($payload); } }
Pause and resume processes
Sometimes an external task needs to be performed to complete a process. You can include it in your workflow by using PauseProcess and the resumeUrl:
- Define a task in your process where you want to make your external call :
<?php namespace App\Processes\Tasks; use App\Models\Order;use App\Processes\Payloads\Contracts\MyProcessPayload;use Closure;use IBroStudio\PipedTasks\Exceptions\PauseProcessException; class CallExternalTask { public function __invoke(MyProcessPayload $payload, Closure $next): mixed { // Here call your external service allowing to include a webhook url // Webhook url to use to resume the process can be retrieved with $payload->getProcess()->resumeUrl() return new PauseProcessException; } }
The $process->resumeUrl() method returns a Laravel signed url.
Process within process
A process can be added as a task if it shares the same payload base.
Process logs
To enable process logs, set log_processes key to true in config/piped-tasks.php.
Spatie laravel-activitylog methods are available to retrieve logs:
<?php use Spatie\Activitylog\Models\Activity; $log = Activity::all()->last(); $logs = Activity::inLog('process-name')->get();
By default, the Process name is used to name the log but you can customize it by adding $logName property to the Process:
<?php namespace App\Processes; use App\Processes\Tasks; use IBroStudio\PipedTasks\Models\Process; class CreateOrderProcess extends Process { public static ?string $logName = 'orders'; }
Append / prepend tasks
Via the tasks array in the config file piped-tasks.php, it is possible to add tasks to a process. It allows you to dynamically modify a process using to the Config::set() method:
First, publish the config file:
php artisan vendor:publish --tag=piped-tasks
Add your process class and append or prepend your(s) task(s) class(es):
<?php declare(strict_types=1); return [ 'tasks' => [ Process::class => [ 'prepend' => [ FirstTask::class, SecondTask::class, ], 'append' => [ LastTask::class, ], ] ], ];
Skip task, abort process
To skip a task and keep the process running, use SkipTaskException.
To abort a process, use AbortProcessException.
Testing
composer test
License
The MIT License (MIT). Please see License File for more information.
ibrostudio/laravel-piped-tasks 适用场景与选型建议
ibrostudio/laravel-piped-tasks 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 99 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 07 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「Tasks」 「laravel」 「pipe」 「iBroStudio」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ibrostudio/laravel-piped-tasks 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ibrostudio/laravel-piped-tasks 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ibrostudio/laravel-piped-tasks 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Functional programming utilities for composing, decorating, and controlling function execution
Minimalist and Typed Immutable Collections
Tasks for NetCommons Plugin
CLI tooling with zero configuration required.
php proc_open wrapper class for shell command process management.
PIPE - ORM en Español.
统计信息
- 总下载量: 99
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-07-05