2017 © Pedro Peláez
 

library state-machine

state machine

image

weiwenhao/state-machine

state machine

  • Monday, July 2, 2018
  • by weiwenhao
  • Repository
  • 0 Watchers
  • 8 Stars
  • 14 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 8 Versions
  • 0 % Grown

The README.md

介绍

基于laravel model的状态管理扩展, (*1)

开发中经常需要为实体定义一些状态 如对于文章实体可能会有 new/draft/deleted 对于支付实体可能会有 awaiting_payment/cancelled/paid/refunded, (*2)

对于订单实体↓, (*3)

, (*4)

对于小程序的的迭代周期实体可能会有 ↓ , (*5)

面对开发过程中繁杂的状态,我们需要一个状态管理工具. 而state-machine就是管理状态之间转换的一个laravel扩展,拥有优雅的语法与回调机制., (*6)

安装

composer require weiwenhao/state-machine

支持laravel5.5+版本, (*7)

创建graph

php artisan make:graph TestGraph, (*8)

运行该命令将会在app\Graphs目录下创建一个TestGraph文件. 如果你不明白如何配置 graph,则可以运行 php artisan make:graph TestGraph -demo 来生成一个demo., (*9)

<?php

namespace App\Graphs;

use Illuminate\Database\Eloquent\Model;
use Weiwenhao\StateMachine\Graph;

class TestGraph extends Graph
{
    /**
     * 所有可选的状态
     * @var array
     */
    protected $states = [
        'cart',
        'new',
        'cancelled',
        'fulfilled'
    ];

    /**
     * 初始状态
     * @var string
     */
    protected $initState = 'cart';

    /**
     * 定义状态之间的转换
     * @var array
     */
    protected $graph= [
        'create' => [
            'from' => ['cart'],
            'to' => 'new',
        ],
        'cancel' => [
            'from' => ['new'],
            'to' => 'cancel',
        ],
        'fulfilled' => [
            'from' => ['new'],
            'to' => 'fulfilled'
        ]
    ];

    /**
     * Model中用于进行状态转换的key 默认使用state字段
     * @var string
     */
    protected $key = 'state';

    /**
     * 转换发生时调用的回调(后置回调)
     * @param $object
     * @return string
     */
    public function onCreate(Model $object)
    {
        return 'created';
    }
}

其中对于state更推荐使用一个enum类型来处理,所以我通常会在laravel的app目录下创建一个Enums目录来存放枚举类型, (*10)

<?php

namespace App\Enums;

interface OrderState
{
    const NEW = 0;
    const CANCELLED = 1;
    const FULFILLED = 2;
}

使用

init graph

$testGraph = new TestGraph($order)$testGraph = TestGraph::with($order), (*11)

init state

TestGraph::with($order)->init(), (*12)

使用Graph中定义的initState对$order的state属性进行初始化, (*13)

apply transition

TestGraph::with($order)->apply('cancel'), (*14)

对$order的state属性应用cancel这个转换,如果不允许应用cancel则会抛出一个 StateMachineException, (*15)

由于apply方法严格遵守TestGraph中$graph中定义的状态转换.如果存在可能会出现异常的apply(),并且想要处理失败后的结果则可以像下面这样处理, (*16)

    use Weiwenhao\StateMachine\StateMachineException

    try {
        TestGraph::with($order)->apply('cancel')
    } cache (StateMachineException $e) {
        // handle...
    }

如果你觉得这样太过麻烦, 只需要在apply的时候传递第二个参数为true, 则对于不允许apply的情况则不会抛出异常, 而是返回一个false;, (*17)

state-machine只是进行了state的状态转换,不会去进行model的save操作, (*18)

apply event

在apply transition后,state-machine 回去对应的Graph中查找是否存在对应的transition回调,有则调用, 并且将对应的model作为参数传递, (*19)

TestGraph::with($order)->apply('cancel')则会调用TestGraph下的onCancel, (*20)

<?php

# TestGraph.php

public function onCancel($order)
{
    // cancel..
}

laravel model的观察者模式已经是一种很优雅的事件处理机制了,可能有个同学会觉得两者存在冲突. 但是我认为并不会, model event专注于整个模型实体的增删改事件, 而state则是更加细颗粒的,更加专注于处理state的事件机制., (*21)

can transition

TestGraph::with($order)->can('cancel'), (*22)

返回一个bool值,在apply()的时候会先调用can进行检测, (*23)

get state

TestGraph::with($order)->getState(), (*24)

在model中使用 $order->state 是一种更加方便的选择, (*25)

get possible transitions

TestGraph::with($order)->getPossibleTransitions(), (*26)

返回一个array,包含当前状态下可以使用的transitions, (*27)

其他可选方案

winzou/state-machine, (*28)

The Versions

02/07 2018

dev-master

9999999-dev

state machine

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar weiwenhao

laravel state state-machine

02/07 2018

0.2.4

0.2.4.0

state machine

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar weiwenhao

laravel state state-machine

01/07 2018

dev-develop

dev-develop

state machine

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar weiwenhao

laravel state state-machine

01/07 2018

0.2.3

0.2.3.0

state machine

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar weiwenhao

laravel state state-machine

28/06 2018

0.2.2

0.2.2.0

state machine

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar weiwenhao

laravel state state-machine

22/06 2018

0.2.1

0.2.1.0

state machine

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar weiwenhao

laravel state state-machine

22/06 2018

0.2.0

0.2.0.0

state machine

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar weiwenhao

laravel state state-machine

21/06 2018

0.1.0

0.1.0.0

state machine

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar weiwenhao

laravel state state-machine