Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/brexis/laravel-workflow
Use the Symfony Workflow component in Laravel
https://github.com/brexis/laravel-workflow
laravel laravel5 symfony workflow
Last synced: 9 days ago
JSON representation
Use the Symfony Workflow component in Laravel
- Host: GitHub
- URL: https://github.com/brexis/laravel-workflow
- Owner: brexis
- License: mit
- Fork: true (finicprint/laravel-workflow)
- Created: 2017-02-09T22:57:34.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2022-06-20T12:44:57.000Z (over 2 years ago)
- Last Synced: 2024-04-26T03:22:13.224Z (9 months ago)
- Topics: laravel, laravel5, symfony, workflow
- Language: PHP
- Size: 99.6 KB
- Stars: 279
- Watchers: 24
- Forks: 105
- Open Issues: 19
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Laravel workflow [![Build Status](https://travis-ci.org/brexis/laravel-workflow.svg?branch=1.1.2)](https://travis-ci.org/brexis/laravel-workflow)
Use the Symfony Workflow component in Laravel
### Installation
composer require brexis/laravel-workflow
#### For laravel <= 5.4
Add a ServiceProvider to your providers array in `config/app.php`:
```php
[
...
Brexis\LaravelWorkflow\WorkflowServiceProvider::class,]
```Add the `Workflow` facade to your facades array:
```php
Brexis\LaravelWorkflow\Facades\WorkflowFacade::class,
```### Configuration
Publish the config file
```
php artisan vendor:publish --provider="Brexis\LaravelWorkflow\WorkflowServiceProvider"
```Configure your workflow in `config/workflow.php`
```php
[
'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 the `WorkflowTrait` inside supported classes
```php
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 transitions
foreach ($post->workflow_transitions() as $transition) {
echo $transition->getName();
}
// if more than one workflow is defined for the BlogPost class
foreach ($post->workflow_transitions($workflowName) as $transition) {
echo $transition->getName();
}// Apply a transition
$post->workflow_apply('publish');
$post->save();
```### Use the events
This package provides a list of events fired during a transition```php
Brexis\LaravelWorkflow\Events\Guard
Brexis\LaravelWorkflow\Events\Leave
Brexis\LaravelWorkflow\Events\Transition
Brexis\LaravelWorkflow\Events\Enter
Brexis\LaravelWorkflow\Events\Entered
```You can subscribe to an event
```php
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.
*/
public function onLeave($event) {}/**
* Handle workflow transition event.
*/
public function onTransition($event) {}/**
* Handle workflow enter event.
*/
public function onEnter($event) {}/**
* Handle workflow entered event.
*/
public function onEntered($event) {}/**
* Register the listeners for the subscriber.
*
* @param Illuminate\Events\Dispatcher $events
*/
public function subscribe($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'
);
}}
```### Dump Workflows
Symfony workflow uses GraphvizDumper to create the workflow image. You may need to install the `dot` command of [Graphviz](http://www.graphviz.org/)php artisan workflow:dump workflow_name --class App\\BlogPost
You can change the image format with the `--format` option. By default the format is png.
php artisan workflow:dump workflow_name --format=jpg