笼子 / laravel-workflow
将Symfony Workflow组件集成到Laravel中。
dev-master / 1.0.x-dev
2021-10-28 09:30 UTC
Requires
- php: ^7.3|^8.0
- illuminate/console: 5.3.* || 5.4.* || 5.5.* || 5.6.* || 5.7.* || 5.8.* || 6.* || 7.* || 8.*
- illuminate/support: 5.3.* || 5.4.* || 5.5.* || 5.6.* || 5.7.* || 5.8.* || 6.* || 7.* || 8.*
- symfony/event-dispatcher: ^3.3 || ^4.0 || ^5.0
- symfony/process: ^3.3 || ^4.0 || ^5.0
- symfony/workflow: ^3.3 || ^4.0 || ^5.0
Requires (Dev)
- mockery/mockery: ^1.0
- phpunit/phpunit: ^6.0 || ~7.0 || ^8.0 || ^9.0
This package is auto-updated.
Last update: 2024-09-28 16:00:03 UTC
README
在Laravel8、PHP7、PHP8中使用Symfony Workflow组件。此存储库基于@brexis的项目,自2019-09起不再维护。因此,我修改了代码并发布了它。
安装
composer require cage/laravel-workflow
配置
发布配置文件
php artisan vendor:publish --provider="Cage\LaravelWorkflow\WorkflowServiceProvider"
在config/workflow.php
中配置您的流程
在受支持的类中使用WorkflowTrait
<?php namespace App; use Illuminate\Database\Eloquent\Model; use Cage\LaravelWorkflow\Traits\WorkflowTrait; class BlogPost extends Model { use WorkflowTrait; }
用法
<?php use App\Models\BlogPost; use Workflow; $post = BlogPost::find(1); $workflow = $post->workflow_get();// or $workflow = Workflow::get($post); $workflow->can($post, 'publish'); // False $workflow->can($post, 'to_review'); // True $transitions = $workflow->getEnabledTransitions($post);// Get the transitions $definition = $workflow->getDefinition();// Get the definition $places = $workflow->getMarking($post)->getPlaces();// // Get the current places $metadata = $workflow->getMetadataStore();// Get the metadata // Apply a transition $workflow->apply($post, 'to_review'); $post->save(); // Don't forget to persist the state
使用事件
在app/Providers/EventServiceProvider.php
中注册
protected $subscribe = [ BlogPostWorkflowSubscriber::class, ];
然后您就可以订阅一个事件了。在app/Listeners/BlogPostWorkflowSubscriber.php
中创建监听器
<?php namespace App\Listeners; use Cage\LaravelWorkflow\Events\GuardEvent; class BlogPostWorkflowSubscriber { public function onGuard(GuardEvent $event){} public function onLeave($event) { // The event can also proxy to the original event $subject = $event->getOriginalEvent()->getSubject(); } public function onTransition($event) {} public function onEnter($event) {} public function onEntered($event) {} public function subscribe($events) { $events->listen( 'Cage\LaravelWorkflow\Events\GuardEvent', 'App\Listeners\BlogPostWorkflowSubscriber@onGuard' ); $events->listen( 'Cage\LaravelWorkflow\Events\LeaveEvent', 'App\Listeners\BlogPostWorkflowSubscriber@onLeave' ); $events->listen( 'Cage\LaravelWorkflow\Events\TransitionEvent', 'App\Listeners\BlogPostWorkflowSubscriber@onTransition' ); $events->listen( 'Cage\LaravelWorkflow\Events\EnterEvent', 'App\Listeners\BlogPostWorkflowSubscriber@onEnter' ); $events->listen( 'Cage\LaravelWorkflow\Events\EnteredEvent', 'App\Listeners\BlogPostWorkflowSubscriber@onEntered' ); } }
输出工作流
Symfony工作流使用GraphvizDumper创建工作流图像。您可能需要安装Graphviz的dot
命令
php artisan workflow:dump straight --class App\\Models\\BlogPost --path workflows --format=svg
更多信息
https://symfony.com.cn/doc/current/workflow.html
https://github.com/brexis/laravel-workflow