soap / laravel-workflow-process
Provides Laravel workflow guards using Symfony expression and related objects
Fund package maintenance!
Prasit Gebsaap
Requires
- php: ^8.2|^8.3
- illuminate/contracts: ^10.0||^11.0
- spatie/laravel-package-tools: ^1.16
- symfony/expression-language: ^7.1
- zerodahero/laravel-workflow: ^4.0||^5.0
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^9.5
- 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
- spatie/laravel-ray: ^1.35
This package is auto-updated.
Last update: 2024-11-04 17:03:54 UTC
README
Using Zerodahero's Laravel Workflow (based on Symfony Workflow) to handle state-transition workflow is great. However, coding transition guards in events is hard. This package provides a simple way; you can add Symfony Expression Language as a transition guard for each transition. This configuration must be provided in transition metadata using the 'guard' key. The package subscribes for all workflows' transition guard events and uses the provided Symfony Expression Language to allow or block the transition.
Support me
Any recommendation or pull request is welcome. It is great if you sponsor me if you find my work valuable.
Installation
You can install the package via composer:
composer require soap/laravel-workflow-process
You can publish the config file with:
php artisan vendor:publish --tag="laravel-workflow-process-config"
This is the contents of the published config file:
return [
];
Usage
Task that you have to do is providing guard configuration like the following example. This is in laravel-workflow 's configuration file (config/workflow.php). If you want to store workflow configuration in database, please visis my Laravel Workflow Loader package.
// file config/workflow.php use ZeroDaHero\LaravelWorkflow\MarkingStores\EloquentMarkingStore; return [ 'blogPost' => [ 'type' => 'workflow', 'supports' => [App\Models\BlogPost::class], 'marking_store' => [ 'property' => 'state', 'type' => 'single_state', 'class' => EloquentMarkingStore::class, ], 'places' => ['draft', 'pending_for_review', 'approved', 'rejected', 'published', 'archived'], 'transitions' => [ 'submit' => [ 'from' => 'draft', 'to' => 'pending_for_review', 'metadata' => [ 'guard' => 'authenticated and subject.isOwnedBy(user)', ], ], 'approve' => [ 'from' => 'pending_for_review', 'to' => 'approved', ], 'reject' => [ 'from' => 'pending_for_review', 'to' => 'rejected', ], 'publish' => [ 'from' => 'approved', 'to' => 'published', ], 'archive' => [ 'from' => ['draft', 'rejected'], 'to' => 'archived', ], ], ] ];
Currenty these variables/objects were injected into Symfony Expression Language.
- "subject" is the Eloquent model which is subject of a workflow
- "user" is authenticated user.
- "authenticated" boolean, true if user was authenticated.
So you can call any method on the injected object.
Todo
I have a plan to provide document role for user. For example, some users may be assign as "reviewer" or "approver" for Eloquent model. So we can use something like subject.hasActorRole('reviewer') or subject.canBeReviewedBy(user). Any suggestion is welcome.
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
License
The MIT License (MIT). Please see License File for more information.