1. Go to this page and download the library: Download ringierimu/state-workflow library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
// 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',
],
],
],
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
]
}
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();
/**
* Return authenticated user id.
*
* @return int|null
*/
public function authenticatedUserId()
{
// Implement authenticated user resolver
}
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)
{
}
}
php artisan workflow:dump workflow_name
$ php artisan migrate
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.