承接 libriciel/cakephp-state-machine 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

libriciel/cakephp-state-machine

最新稳定版本:3.1.0

Composer 安装命令:

composer require libriciel/cakephp-state-machine

包简介

StateMachineBehavior for CakePHP 3

README 文档

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

统计信息

  • 总下载量: 4.36k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-04-24