safe_mood/laravel-workflow
Laravel 包,通过清晰的动作定义和事件跟踪简化工作流程。
1.1.0
2024-08-10 15:47 UTC
Requires
- php: ^8.2
- illuminate/contracts: ^10.0||^11.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.16
- mockery/mockery: ^1.6
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^9.0.0||^8.22.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-arch: ^2.7
- pestphp/pest-plugin-laravel: ^2.3
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
README
Laravel Workflow 将功能细节合并到一个类中,允许定义动作和跟踪事件,简化理解并揭示隐藏的逻辑。
安装
您可以通过 Composer 安装此包。
composer require safemood/laravel-workflow
创建工作流程
您可以使用 artisan 命令创建工作流程。
php artisan make:workflow PaymentWorkflow
创建动作
您可以使用 artisan 命令创建动作。
php artisan make:workflow-action ValidateCartItems
<?php namespace App\Actions; use Safemood\Workflow\Action; class ValidateCartItems extends Action { public function handle(array &$context) { // Simulate extra validation logic if (empty($context['cart'])) { throw new \Exception('Cart is empty'); } // you can pass data to the next action if you want $context['validated'] = true; } }
基本示例
一旦您设置了工作流程和动作,您可以在 Laravel 应用程序中定义您的业务逻辑并编排它们。
定义工作流程逻辑
在您的 PaymentWorkflow
类中,您定义构成工作流程的动作和条件的顺序
<?php namespace App\Workflows; use App\Actions\CalculateTotal; use App\Actions\MakePayment; use App\Actions\ValidateCartItems; use App\Events\PaymentProcessed; use App\Jobs\SendEmails; use App\Observers\UserObserver; use App\Models\Order; use Safemood\Workflow\WorkflowManager; class PaymentWorkflow extends WorkflowManager { public function handle() { // Actions to be executed before the main action $this->addBeforeActions([ new ValidateCartItems(), new CalculateTotal() ]); // The main action of the workflow $this->addMainAction(new MakePayment()); // Actions to be executed after the main action $this->addAfterAction(new SendEmails()); // Normal laravel Job in this example // Observers to register for specific entities $this->registerObservers([ Order::class => OrderObserver::class, ]); // Good Debugging or if you want to understand what is happining during the workflow execution: $this->trackEvents([ PaymentProcessed::class ]); // $this->trackAllEvents(); // or // $this->trackEventsIn('App\Events\\'); } }
执行工作流程
<?php namespace App\Http\Controllers; use App\Workflows\PaymentWorkflow; use Illuminate\Http\Request; class PaymentController extends Controller { public function payment(Request $request) { // Example context data representing a user's cart and user information $context = [ 'cart' => [ ['id' => 1, 'name' => 'Product A', 'price' => 100, 'quantity' => 2], ['id' => 2, 'name' => 'Product B', 'price' => 50, 'quantity' => 1] ], 'user_id' => 123 ]; // Execute the PaymentWorkflow with the provided context $paymentWorkflow = (new PaymentWorkflow)->run($context); // Check if the workflow execution was successful $success = $paymentWorkflow->passes(); // Check if the workflow execution failed $failure = $paymentWorkflow->failed(); // Dump the workflow for debugging // $paymentWorkflow->dd(); // Handle the response based on the workflow outcome if ($success) { return $paymentWorkflow->successResponse(); } return $paymentWorkflow->failureResponse(); } }
条件动作执行
您可以使用 when 方法有条件地执行动作。
<?php namespace App\Workflows; use App\Actions\CalculateTotal; use App\Actions\MakePayment; use App\Actions\ValidateCartItems; use App\Actions\SendEmailReceipt; use Safemood\Workflow\WorkflowManager; class PaymentWorkflow extends WorkflowManager { public function handle() { $this->when(false, function () { $this->trackAllEvents(); }); $this->when(true, function () { $this->registerObservers([ DummyModel::class => DummyModelObserver::class, ]); }); $this->when(true, function () { $this->addBeforeActions([ new DummyAction(), new DummyAction(), ]); }); $this->unless( value: false, callback: fn () => $this->trackAllEvents(), default: fn () => $this->doSomething() ); } }