Laravel State Workflow provide tools for defining and managing workflows and activities with ease.
ringierimu/state-workflow is a Laravel package for laravel state workflow provide tools for defining and managing workflows and activities with ease..
It currently has 32 GitHub stars and 68.670 downloads on Packagist (latest version 5.0.0).
Install it with composer require ringierimu/state-workflow.
Discover more Laravel packages by ringierimu
or browse all Laravel packages to compare alternatives.
Last updated
Laravel State workflow provide tools for defining and managing workflows and activities with ease. It offers an object oriented way to define a process or a life cycle that your object goes through. Each step or stage in the process is called a state. You do also define transitions that describe the action to get from one state to another.
A workflow consist of state and actions to get from one state to another. These actions are called transitions which describes how to get from one state to another.
| Version | PHP | Laravel | |---------|-----|---------| | 5.x | 8.3+ | 11, 12 | | 4.x | 8.1+ | 10, 11 |
Requires PHP 8.3+ and Laravel 11+.
composer require ringierimu/state-workflow
Publish config/workflow.php file
php artisan vendor:publish --tag="state-workflow-config"
Publish migration
php artisan vendor:publish --tag="state-workflow-migration"
Run migration
php artisan migrate
config/workflow.php and configure it// this should be your model name in camelcase. eg. PropertyListing::Class => propertyListing
'post' => [
// class of your domain object
'class' => \App\Post::class,
// Register subscriber for this workflow which contains business rules. Uncomment line below to register subscriber
//'subscriber' => \App\Listeners\UserEventSubscriber::class,
// property of your object holding the actual state (default is "current_state")
//'property_path' => 'current_state', //uncomment this line to override default value
// list of all possible states
'states' => [
'new',
'pending_activation',
'activated',
'deleted',
'blocked'
],
// list of all possible transitions
'transitions' => [
'create' => [
'from' => ['new'],
'to' => 'pending_activation',
],
'activate' => [
'from' => ['pending_activation'],
'to' => 'activated',
],
'block' => [
'from' => ['pending_activation', 'activated'],
'to' => 'blocked'
],
'delete' => [
'from' => ['pending_activation', 'activated', 'blocked'],
'to' => 'deleted',
],
],
],
HasWorkflowTrait to your model class to support workflow<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Ringierimu\StateWorkflow\Traits\HasWorkflowTrait;
/**
* Class Post
* @package App
*/
class Post extends Model
{
use HasWorkflowTrait;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
...,
'current_state', // If not using default attribute, update this to match value in workflow.php
]
}
<?php
use App\Post;
$post = new Post();
//Apply transition
$post->applyTransition("create");
$post = $post->refresh();
//Return current_state value
$post->state(); //pending_activation
//Check if this transition is allowed
$post->canTransition("activate"); // True
//Return Model state history
$post->stateHistory();
Ability to audit and track who action a specific state change for your object. The package leverage the default Laravel auth provider to resolve the authenticated user when applying the state changes.
For a custom authentication mechanism, you should override authenticatedUserId in your object class with your own implementation.
/**
* Return authenticated user id.
*
* @return int|null
*/
public function authenticatedUserId()
{
// Implement authenticated user resolver
}
Each step has three events that are fired in order:
During state/workflow transition, the following events are fired in the following order:
workflow()->can(), workflow()->apply() or workflow()->getEnabledTransitions() is executed. Guard Eventworkflow.guard
workflow.[workflow name].guard
workflow.[workflow name].guard.[transition name]
Leave Eventworkflow.leave
workflow.[workflow name].leave
workflow.[workflow name].leave.[state name]
Transition Eventworkflow.transition
workflow.[workflow name].transition
workflow.[workflow name].transition.[transition name]
Enter Eventworkflow.enter
workflow.[workflow name].enter
workflow.[workflow name].enter.[state name]
Entered Eventworkflow.entered
workflow.[workflow name].entered
workflow.[workflow name].entered.[state name]
Completed Eventworkflow.completed
workflow.[workflow name].completed
workflow.[workflow name].completed.[transition name]
Create subscriber class to listen to those events and the class should extends WorkflowSubscriberHandler.
To register method to listen to specific even within subscriber use the following format for method name:
onGuard()onGuardActivate()NB:
on key word otherwise it will be ignored.Subscriber class must be register inside workflow.php config file with the appropriate workflow configuration.Subscriber class must extends WorkflowSubscriberHandler.Guard, Transition and Completed Event uses of transition name.Leave, Enter and Entered Event uses state name.<?php namespace App\Listeners;
use Ringierimu\StateWorkflow\Events\EnteredEvent;
use Ringierimu\StateWorkflow\Events\EnterEvent;
use Ringierimu\StateWorkflow\Events\GuardEvent;
use Ringierimu\StateWorkflow\Events\LeaveEvent;
use Ringierimu\StateWorkflow\Events\TransitionEvent;
use Ringierimu\StateWorkflow\Subscribers\WorkflowSubscriberHandler;
/**
* Class PostEventSubscriber
* @package App\Listeners
*/
class UserEventSubscriber extends WorkflowSubscriberHandler
{
/**
* Handle workflow guard events.
*
* @param GuardEvent $event
*/
public function onGuardActivate($event)
{
$user = $event->getOriginalEvent()->getSubject();
if (empty($user->dob)) {
// Users with no dob should not be allowed
$event->getOriginalEvent()->setBlocked(true);
}
}
/**
* Handle workflow leave event.
*
* @param LeaveEvent $event
*/
public function onLeavePendingActivation($event)
{
}
/**
* Handle workflow transition event.
*
* @param TransitionEvent $event
*/
public function onTransitionActivate($event)
{
}
/**
* Handle workflow enter event.
*
* @param EnterEvent $event
*/
public function onEnterActivated($event)
{
}
/**
* Handle workflow entered event.
*
* @param EnteredEvent $event
*/
public function onEnteredActivated($event)
{
}
}
Each workflow event has an instance of Event. This means that each event has access to the following information:
getOriginalEvent(): Returns the Parent Event that dispatched the event which has the following children methods:
getSubject(): Returns the object that dispatches the event.getTransition(): Returns the Transition that dispatches the event.getWorkflowName(): Returns a string with the name of the workflow that triggered the event.isBlocked(): Returns true/false if transition is blocked.setBlocked(): Sets the blocked value.Symfony workflow uses GraphvizDumper to create the workflow image by using the dot command.
The dot command is part of Graphviz.
You will be required to download dot command to make use of this command.
https://graphviz.gitlab.io/download/
php artisan workflow:dump workflow_name
composer test