1 Star 1 Fork 0

NorthWind / state-machine

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

一个非常轻量级但功能强大的PHP状态机

定义状态,定义转换和回调:剩下的由我们来做。 硬编码状态的时代已经结束了!

Build Status

安装 (通过 composer)

{
    "require": {
        "winzou/state-machine": "~0.1"
    }
}

使用

配置状态机图

为了使用状态机,首先需要定义一个图。图是状态、转换和可选的回调的定义; 所有都连接到域的对象上。 多个图形可以附加到同一个对象。

让我们为DomainObject对象定义一个名为myGraphA的图:

$config = array(
    'graph'         => 'myGraphA', // Name of the current graph - there can be many of them attached to the same object
    'property_path' => 'stateA',  // Property path of the object actually holding the state
    'states'        => array(
        'checkout',
        'pending',
        'confirmed',
        'cancelled'
    ),
    'transitions' => array(
        'create' => array(
            'from' => array('checkout'),
            'to'   => 'pending'
        ),
        'confirm' => array(
            'from' => array('checkout', 'pending'),
            'to'   => 'confirmed'
        ),
        'cancel' => array(
            'from' => array('confirmed'),
            'to'   => 'cancelled'
        )
    ),
    'callbacks' => array(
        'guard' => array(
            'guard-cancel' => array(
                'to' => array('cancelled'), // 是否仅在转换到此状态时才调用
                'do' => function() { var_dump('guarding to cancelled state'); return false; }
            )
        ),
        'before' => array(
            'from-checkout' => array(
                'from' => array('checkout'), // 是否只对来自此状态的转换调用
                'do'   => function() { var_dump('from checkout transition'); }
            )
        ),
        'after' => array(
            'on-confirm' => array(
                'on' => array('confirm'), // 只会在交接时被调用吗
                'do' => function() { var_dump('on confirm transition'); }
            ),
            'to-cancelled' => array(
                'to' => array('cancelled'), // 是否仅在转换到此状态时才调用
                'do' => function() { var_dump('to cancel transition'); }
            ),
            'cancel-date' => array(
                'to' => array('cancelled'),
                'do' => array('object', 'setCancelled'),
            ),
        )
    )
);

所以,在前面的例子中,图形有6种可能的状态,这些状态可以通过对对象应用一些转换来实现。例如,当创建一个新 DomainObject, 你可以应用 'create' 转换到对象,然后它的状态变为 pending.

使用状态机

定义

状态机是实际操作对象的对象。 通过使用状态机,您可以测试是否可以应用转换,实际应用转换,检索当前状态,等等。 状态机是特定于一对 对象 + 图 这意味着如果你想操纵其它对象, 或者用这些对象的其它图, 您需要另一个状态机.

工厂帮助您获得这些对象+图的状态机。 你给它一个对象和一个图名称, 它会把这对状态机对象返回给你。如果你想在你的Symfony2应用程序中使用这个工厂服务,请看 corresponding StateMachineBundle.

用法

请参阅examples文件夹中的几个例子

回调

回调用于保护转换或在应用转换之前或之后执行一些代码。

Guarding 回调必须返回 bool. 如果守卫返回 false, 不能执行转换。

Credits

This library has been highly inspired by https://github.com/yohang/Finite, but has taken another direction.

Copyright (c) 2014 Alexandre Bacco Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

暂无描述 展开 收起
PHP
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
PHP
1
https://gitee.com/xskit/state-machine.git
git@gitee.com:xskit/state-machine.git
xskit
state-machine
state-machine
master

搜索帮助