caner/state-machine

Laravel Simple State Machine System

1.1.0 2022-02-19 16:09 UTC

README

Latest Version on Packagist Total Downloads run-tests

This package helps you to create State Machine data model-based services.

Installation

You can install the package via composer:

composer require caner/state-machine  

Usage

In config/app.php;

\Caner\StateMachine\StateMachineServiceProvider::class,  

and publish config file;

php artisan vendor:publish --tag=caner-state-machine-config  

Step by step documentation

Introducing Concept

A state machine is a mathematical abstraction used to design algorithms. A state machine reads a set of inputs and changes to a different state based on those inputs.

A state is a description of the status of a system waiting to execute a transition. A transition is a set of actions to execute when a condition is fulfilled or an event received. In a state diagram, circles represent each possible state and arrows represent transitions between states.

Basically, we are building a state for each status and running related transition when changing the status.

Sample State Change Workflow

Each Transition should consist of 3 parts. These are the guards, action and afterActions methods.

Sample Transition Workflow

I prefer to use package in complex status changes.

Preferred Directory Tree;

  • app
    • Services
      • YourStateMachine
        • AfterActions
        • Guards
        • States
        • Transitions
        • YourStateMachine.php
      • AnotherStateMachine
        • AfterActions
        • Guards
        • States
        • Transitions
        • AnotherStateMachine.php

In future versions, we will be added, create state machine parts in artisan commands. This time commands run like this directory tree.

1-) Create First State Machine
2-) Create First State
3-) Create First Transition
4-) Create First Guard
5-) Create First AfterAction
6-) Example Transition in Created State Machine
7-) Create Another State Machine

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please create an issue on github issues section.

Credits

Special thanks for Tarfin Labs

License

The MIT License (MIT). Please see License File for more information.