All Projects → szana8 → laraflow

szana8 / laraflow

Licence: MIT license
State machine and workflow package for Laravel Eloquent

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to laraflow

River Admin
🚀 A shiny admin interface for django-river built with DRF, Vue & Vuetify
Stars: ✭ 55 (+5.77%)
Mutual labels:  state-machine, workflow-engine
Pvm
Build workflows, activities, BPMN like processes, or state machines with PVM.
Stars: ✭ 348 (+569.23%)
Mutual labels:  state-machine, workflow-engine
stateless
Finite State Machine porting from Stateless C#
Stars: ✭ 25 (-51.92%)
Mutual labels:  state-machine, workflow-engine
Django River
Django workflow library that supports on the fly changes ⛵
Stars: ✭ 609 (+1071.15%)
Mutual labels:  state-machine, workflow-engine
nxt state machine
A simple but powerful state machine implementation.
Stars: ✭ 14 (-73.08%)
Mutual labels:  state-machine, workflow-engine
state-machine-demo
A React state machine demo using xstate
Stars: ✭ 18 (-65.38%)
Mutual labels:  state-machine
zenaton-node
⚡ Node.js library to run and orchestrate background jobs with Zenaton Workflow Engine
Stars: ✭ 50 (-3.85%)
Mutual labels:  workflow-engine
pastafarian
A tiny event-based finite state machine
Stars: ✭ 20 (-61.54%)
Mutual labels:  state-machine
M2A01 MuSimpron
Small yet powerful state machine coroutine library
Stars: ✭ 34 (-34.62%)
Mutual labels:  state-machine
dtm
A distributed transaction framework that supports multiple languages, supports saga, tcc, xa, 2-phase message, outbox patterns.
Stars: ✭ 6,110 (+11650%)
Mutual labels:  workflow-engine
hpipe
Workflow engine for various computing systems.
Stars: ✭ 26 (-50%)
Mutual labels:  workflow-engine
simple-state-machine
A simple Java state machine for Spring Boot projects
Stars: ✭ 25 (-51.92%)
Mutual labels:  state-machine
kstatemachine
KStateMachine is a Kotlin DSL library for creating finite state machines (FSM) and hierarchical state machines (HSM).
Stars: ✭ 63 (+21.15%)
Mutual labels:  state-machine
UnityHFSM
A simple yet powerful class based hierarchical finite state machine for Unity3D
Stars: ✭ 243 (+367.31%)
Mutual labels:  state-machine
lightflow
A lightweight, distributed workflow system
Stars: ✭ 67 (+28.85%)
Mutual labels:  workflow-engine
taska
Workflow Management for Biomedical exploration
Stars: ✭ 29 (-44.23%)
Mutual labels:  workflow-engine
cpsr
Cancer Predisposition Sequencing Reporter (CPSR)
Stars: ✭ 44 (-15.38%)
Mutual labels:  workflow-engine
fsm2
FMS2 provides Dart implementation of the core design aspects of the UML state diagrams.
Stars: ✭ 44 (-15.38%)
Mutual labels:  state-machine
fs2-es
Event sourcing utilities for FS2
Stars: ✭ 75 (+44.23%)
Mutual labels:  state-machine
statebot-sh
Statebot for shell-scripts. Write more robust and understandable programs.
Stars: ✭ 14 (-73.08%)
Mutual labels:  state-machine

# Laravel Workflow Package

Laraflow is a standard workflow package for Laravel Eloquent objects. You can define your steps, the transition between them, callbacks, and validators.

Total Downloads Latest Stable Version License

Installation (via composer)

You can install the package via composer. The package require Laravel 5.5 or higher

composer require szana8/Laraflow

You need to crate the necessary table for the historical data:

php artisan migrate

After Laravel 5.5 you don't need to manually add the service provider to the config/app.php. You can find a sample test project with this package in here: https://github.com/szana8/laravel-workflow-package

Configuration file

After the installation you have to publish a configuration file, which contains a lot of necessary data for the package.

php artisan vendor:publish --provider="szana8\Laraflow\LaraflowServiceProvider"

Configuration array

You need a configuration array before you use the Laraflow workflow.

Usage

Step 1

First you need to add a new column to your Eloquent model table for example called: last_step/status or whatever you want.

 $table->string('status');

In your config file property_path attribute has to be the same value than the column name.

Step 2

You have to add the Flowable trait to your Eloquent model to use the workflow.

use szana8\Laraflow\Traits\Flowable;

class SampleClass extends Model {

    use Flowable;
Step 3

Than you have to add a function to your eloquent model called getLaraflowStates() .

This function has to return the array of the configuration!

The configuration array, when just using 1 state machine in you model must of the form:

[
    'default' => [
        //... see published configuration example in config directory.
    ]
];

When you have multiple statemachines in your model use the following form:

[
    'statemachine_name1' => [ // might also be called 'default' !
        // ...  see published configuration example in config directory
    ],
    'statemachine_name2' => [
        // ...  see published configuration example in config directory
    ],
]
Step 4

If you want to change the status of the Eloquent object you can use the

// when using only 1 or at least 'default' named state machine.
$object->transiton($new_status);
// when using only 1 not named state machine.
$statemachineName1 = 'statemachine_name1'
$object->transiton($new_status, $statemachineName1);
$statemachineName2 = 'default'
$object->transiton($new_status, $statemachineName2);
$object->save();

method which comes from the Flowable trait. The $new_status parameter is the value of the key attribute which comes from the getPossibleTransitions() function.

History

You can query the history of the record just call the history function in your model like this:

/**
* Return historical records.
*
* @return string
*/
public function getFlowHistoryAttribute()
{
return $this->history();
}

Events

You can listen to the 'global' events which fires in every status changes.

LaraflowEvents::PRE_TRANSITION
LaraflowEvents::POST_TRANSITION
LaraflowEvents::CAN_TRANSITION

The PRE_TRANSITION fires before the status change, the POST_TRANSITION fires after. The CAN_TRANSITION fires when the package checks the transition is possible from the actual step. You can define callback(s) which will be call from the specified transition.

Validators

The package comes with a default validator class, which use the Laravel Validator class. You can add validation rules to the transitions, so the package can checks the Eloquent object attributes with the given rules before the transition. If the validation fails throws a LaraflowValidatorException exception with the error message(s) array.

You can define your own validator if you create a class which implements the LaraflowValidatorInterface.

class TestValidator extends Rule implements LaraflowValidatorInterface
{
    /**
     * Validate the attributes with the given rules.
     *
     * @param array $attributes
     * @param array $rules
     * @return mixed
     */
    public function validate(array $attributes, array $rules)
    {
        if (1 == 2) {
            return true;
        }

        throw LaraflowValidatorException::withMessages(['Validation error']);
    }
}

Commands

Subscriber generation

You can create subscribers for the default Laraflow events with artisan command.

foo@bar: php artian laraflow:make subscriber --NameOfTheSubscriber

After the run you can find the new subscriber class in the App\Listener directory. To create a callback class you have to create a class which implements the LaraflowCallbackInterface and add the class to the neessary event in the subscriber.

Example:

class TestPreCallback implements LaraflowCallbackInterface
{
    public function handle(LaraflowTransitionEvents $event)
    {
        CallbackTest::insert(['message' => json_encode($event->convertToArray())]);
    }
}

class LaraflowEventSubscriber
{
    /**
     * Register the listeners for the subscriber.
     *
     * @param  \Illuminate\Events\Dispatcher  $events
     */
    public function subscribe($events)
    {
        $events->listen(
            LaraflowEvents::PRE_TRANSITION,
            'App\TestPreCallback@handle'
        );
    }
}

Custom validator generation

To generate a validator class skeleton for the custom validation you use this command:

foo@bar: php artian laraflow:make validator --NameOfTheValidator

After the run you can find the new class in the App\Validators directory.

Credits

This library has been highly inspired by https://github.com/winzou/state-machine.

License

The package is open-sourced software licensed under the MIT license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].