libriciel/cakephp-state-machine

StateMachineBehavior for CakePHP 3

3.1.0 2020-10-21 15:13 UTC

README

Build Status Coverage Status Scrutinizer Quality Score Latest Stable Version Total Downloads

Documentation is not finished yet either. See the tests if you want to learn something, as all aspects of the state machine is tested there.

What is a State Machine?

http://en.wikipedia.org/wiki/State_machine

Installation

First you need to alter the tables of the models you want to use StateMachine:

ALTER TABLE `vehicles` ADD `state` VARCHAR(50);
ALTER TABLE `vehicles` ADD `previous_state` VARCHAR(50);

Features

  • Callbacks on states and transitions
  • Custom methods may be added to your model
  • is($entity, $state), can($entity, $transition), on($transition, 'before|after', callback) and when($state, callback) methods allows you to control the whole flow. transition($entity, $transition) is used to move between two states.
  • Graphviz
  • (removed: Roles and rules, feel free to commit it)

Callbacks

You can add callbacks that will fire before/after a transition, and before/after a state change. This can either be done manually with $this->on('mytransition', 'before', funtion() {}), or you can add a method to your model:

public function onBeforeTransition($entity, $currentState, $previousState, $transition) {
    // will fire on all transitions
}

public function onAfterIgnite($entity, $currentState, $previousState, $transition) {
    // will fire after the ignite transition
}

The state callbacks are a little different:

public function onStateChange($entity, $newState) {
    // will fire on all state changes
}

public function onStateIdling($entity, $newState) {
    // will fire on the idling state
}

Naming conventions

  • Transitions and states in $transitions should be lowercased and underscored. The method names are in turn camelized.

    Example:

    shift_up   => can($entity, 'ShiftUp') => transition($entity, 'ShiftUp')
    first_gear => is($entity, 'FirstGear')

How to Use

namespace App\Model\Table;

use Cake\ORM\Table;
use Cake\ORM\Entity;

class VehiclesTable extends Table
{
    public function initialize(array $config)
    {
        $this->addBehavior('StateMachine.StateMachine');
        $this->on('ignite', 'after', function(Entity $entity, $prevState, $nextState, $transition) {
            // the car just ignited!
        });
    }

    public $initialState = 'parked';

    public $transitions = array(
            'ignite' => array(
                    'parked' => 'idling',
                    'stalled' => 'stalled'
            ),
            'park' => array(
                    'idling' => 'parked',
                    'first_gear' => 'parked'
            ),
            'shift_up' => array(
                    'idling' => 'first_gear',
                    'first_gear' => 'second_gear',
                    'second_gear' => 'third_gear'
            ),
            'shift_down' => array(
                    'first_gear' => 'idling',
                    'second_gear' => 'first_gear',
                    'third_gear' => 'second_gear'
            ),
            'crash' => array(
                    'first_gear' => 'stalled',
                    'second_gear' => 'stalled',
                    'third_gear' => 'stalled'
            ),
            'repair' => array(
                    'stalled' => 'parked'
            ),
            'idle' => array(
                    'first_gear' => 'idling'
            ),
            'turn_off' => array(
                    'all' => 'parked'
            )
    );

    // a shortcut method for checking if the vehicle is moving
    public function isMoving(Entity $entity) {
        return in_array($this->getCurrentState($entity), array('first_gear', 'second_gear', 'third_gear'));
    }

}
class Controller .... {
    public function method() {
        $this->loadModel('Vehicles');
        $entity = $this->Vehicles->newEntity();
        $entity->title = 'Toybota';
        $this->Vehicles->save($entity);

        // $this->Vehicles->getCurrentState($entity) == 'parked'
	if ($this->Vehicles->can($entity, 'Ignite')) {
       	 	$this->Vehicles->transition($entity, 'Ignite');
       	 	$this->Vehicles->transition($entity, 'shiftUp');
        	// $this->Vehicles->getCurrentState($entity) == 'first_gear'
        }
    }
}

Graphviz

Here's how to state machine of the Vehicle would look like if you saved:

$table->toDot()

into fsm.gv and ran:

dot -Tpng -ofsm.png fsm.gv