- Notifications
You must be signed in to change notification settings - Fork104
Use the Symfony Workflow component in Laravel
License
brexis/laravel-workflow
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Use the Symfony Workflow component in Laravel
composer require brexis/laravel-workflowAdd a ServiceProvider to your providers array inconfig/app.php:
<?php'providers' => [ ...Brexis\LaravelWorkflow\WorkflowServiceProvider::class,]
Add theWorkflow facade to your facades array:
<?php...'Workflow' =>Brexis\LaravelWorkflow\Facades\WorkflowFacade::class,
Publish the config file
php artisan vendor:publish --provider="Brexis\LaravelWorkflow\WorkflowServiceProvider"Configure your workflow inconfig/workflow.php
<?phpreturn ['straight' => ['type' =>'workflow',// or 'state_machine''marking_store' => ['type' =>'multiple_state','arguments' => ['currentPlace'] ],'supports' => ['App\BlogPost'],'places' => ['draft','review','rejected','published'],'transitions' => ['to_review' => ['from' =>'draft','to' =>'review' ],'publish' => ['from' =>'review','to' =>'published' ],'reject' => ['from' =>'review','to' =>'rejected' ] ], ]];
Use theWorkflowTrait inside supported classes
<?phpnamespaceApp;useIlluminate\Database\Eloquent\Model;useBrexis\LaravelWorkflow\Traits\WorkflowTrait;class BlogPostextends Model{use WorkflowTrait;}
<?phpuseApp\BlogPost;useWorkflow;$post = BlogPost::find(1);$workflow = Workflow::get($post);// if more than one workflow is defined for the BlogPost class$workflow = Workflow::get($post,$workflowName);$workflow->can($post,'publish');// False$workflow->can($post,'to_review');// True$transitions =$workflow->getEnabledTransitions($post);// Apply a transition$workflow->apply($post,'to_review');$post->save();// Don't forget to persist the state// Using the WorkflowTrait$post->workflow_can('publish');// True$post->workflow_can('to_review');// False// Get the post transitionsforeach ($post->workflow_transitions()as$transition) {echo$transition->getName();}// if more than one workflow is defined for the BlogPost classforeach ($post->workflow_transitions($workflowName)as$transition) {echo$transition->getName();}// Apply a transition$post->workflow_apply('publish');$post->save();
This package provides a list of events fired during a transition
Brexis\LaravelWorkflow\Events\GuardBrexis\LaravelWorkflow\Events\LeaveBrexis\LaravelWorkflow\Events\Transition Brexis\LaravelWorkflow\Events\EnterBrexis\LaravelWorkflow\Events\Entered
You can subscribe to an event
<?phpnamespaceApp\Listeners;useBrexis\LaravelWorkflow\Events\GuardEvent;class BlogPostWorkflowSubscriber{/** * Handle workflow guard events. */publicfunctiononGuard(GuardEvent$event) {/** Symfony\Component\Workflow\Event\GuardEvent */$originalEvent =$event->getOriginalEvent();/** @var App\BlogPost $post */$post =$originalEvent->getSubject();$title =$post->title;if (empty($title)) {// Posts with no title should not be allowed$originalEvent->setBlocked(true); } }/** * Handle workflow leave event. */publicfunctiononLeave($event) {}/** * Handle workflow transition event. */publicfunctiononTransition($event) {}/** * Handle workflow enter event. */publicfunctiononEnter($event) {}/** * Handle workflow entered event. */publicfunctiononEntered($event) {}/** * Register the listeners for the subscriber. * * @param Illuminate\Events\Dispatcher $events */publicfunctionsubscribe($events) {$events->listen('Brexis\LaravelWorkflow\Events\GuardEvent','App\Listeners\BlogPostWorkflowSubscriber@onGuard' );$events->listen('Brexis\LaravelWorkflow\Events\LeaveEvent','App\Listeners\BlogPostWorkflowSubscriber@onLeave' );$events->listen('Brexis\LaravelWorkflow\Events\TransitionEvent','App\Listeners\BlogPostWorkflowSubscriber@onTransition' );$events->listen('Brexis\LaravelWorkflow\Events\EnterEvent','App\Listeners\BlogPostWorkflowSubscriber@onEnter' );$events->listen('Brexis\LaravelWorkflow\Events\EnteredEvent','App\Listeners\BlogPostWorkflowSubscriber@onEntered' ); }}
Symfony workflow uses GraphvizDumper to create the workflow image. You may need to install thedot command ofGraphviz
php artisan workflow:dump workflow_name --class App\\BlogPostYou can change the image format with the--format option. By default the format is png.
php artisan workflow:dump workflow_name --format=jpgAbout
Use the Symfony Workflow component in Laravel
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Languages
- PHP100.0%