php状态机,PHP 有限状态机使用说明

php 有限状态机使用说明

$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'), // Will be called only for transitions going to this state

'do' => function() { var_dump('guarding to cancelled state'); return false; }

)

),

'before' => array(

'from-checkout' => array(

'from' => array('checkout'), // Will be called only for transitions coming from this state

'do' => function() { var_dump('from checkout transition'); }

)

),

'after' => array(

'on-confirm' => array(

'on' => array('confirm'), // Will be called only on this transition

'do' => function() { var_dump('on confirm transition'); }

),

'to-cancelled' => array(

'to' => array('cancelled'), // Will be called only for transitions going to this state

'do' => function() { var_dump('to cancel transition'); }

),

'cancel-date' => array(

'to' => array('cancelled'),

'do' => array('object', 'setCancelled'),

),

)

)

);

各个属性说明

1'property_path' => 'stateA'

需要在 DomainObject对象中定义,如:

class DomainObject

{

private $stateA = 'checkout';//初始状态

public function getStateA()

{

return $this->stateA;

}

public function setStateA($state)

{

$this->stateA = $state;

}

}

2 states

定义所有可能的状态

'states' => array(

'checkout',

'pending',

'confirmed',

'cancelled'

),

3 transitions

定义了执行动作后的状态变化

'transitions' => array(

'create' => array(

'from' => array('checkout'),

'to' => 'pending'

),

'confirm' => array(

'from' => array('checkout', 'pending'),

'to' => 'confirmed'

),

'cancel' => array(

'from' => array('confirmed'),

'to' => 'cancelled'

)

),

4 callbacks

定义了什么时候执行相对应的回调

'callbacks' => array(

'guard' => array(

'guard-cancel' => array(

'to' => array('cancelled'), // Will be called only for transitions going to this state

'do' => function() { var_dump('guarding to cancelled state'); return false; }

)

),

'before' => array(

'from-checkout' => array(

'from' => array('checkout'), // Will be called only for transitions coming from this state

'do' => function() { var_dump('from checkout transition'); }

)

),

'after' => array(

'on-confirm' => array(

'on' => array('confirm'), // Will be called only on this transition

'do' => function() { var_dump('on confirm transition'); }

),

'to-cancelled' => array(

'to' => array('cancelled'), // Will be called only for transitions going to this state

'do' => function() { var_dump('to cancel transition'); }

),

'cancel-date' => array(

'to' => array('cancelled'),

'do' => array('object', 'setCancelled'),

),

)

)

下面这个定义了是否可以更改到当前状态,guard-cancel修饰作用没有实际意义,执行的时候会扫描整个数组,当to的指向状态是cancelled时执行do回调,当回调返回true的时候可以更改到该状态,类似于中间件

'guard' => array(

'guard-cancel' => array(

'to' => array('cancelled'), // Will be called only for transitions going to this state

'do' => function() { var_dump('guarding to cancelled state'); return false; }

)

),

通过guard之后会先执行before,数组的key其修饰作用,执行的时候会循环这个数组,当from的状态是checkout会执行do回调,

'before' => array(

'from-checkout' => array(

'from' => array('checkout'), // Will be called only for transitions coming from this state

'do' => function() { var_dump('from checkout transition'); }

)

),

before执行完之后,接下来开始扫描after数组,其key起装饰作用。有两种类型一种是on do,另一种是

to do 。on 对应的是transitions的key值,匹配到此key值就执行do回调。to对应的是要转换到的状态,匹配到此状态就执行do回调

'after' => array(

'on-confirm' => array(

'on' => array('confirm'), // Will be called only on this transition

'do' => function() { var_dump('on confirm transition'); }

),

'to-cancelled' => array(

'to' => array('cancelled'), // Will be called only for transitions going to this state

'do' => function() { var_dump('to cancel transition'); }

),

'cancel-date' => array(

'to' => array('cancelled'),

'do' => array('object', 'setCancelled'),

),

)

do回调有两种方式一种是回调,另一种是DomainObject的方法

'do' => array('object', 'setCancelled'),

对应

class DomainObject

{

private $stateA = 'checkout';//初始状态

public function getStateA()

{

return $this->stateA;

}

public function setStateA($state)

{

$this->stateA = $state;

}

public function setConfirmedNow()

{

var_dump('I (the object) am set confirmed at '.date('Y-m-d').'.');

}

}

的setConfirmedNow方法

有三个事件

const PRE_TRANSITION = 'winzou.state_machine.pre_transition';

const POST_TRANSITION = 'winzou.state_machine.post_transition';

const TEST_TRANSITION = 'winzou.state_machine.test_transition';

定义完图后,设置一个初始状态,例如把订单的checkout状态设置进去DomainObject中,然后操作执行相对应的方法(\$stateMachine->apply('create')),可能执行成功($stateMachine->getState(),设置状态进入订单表),也可能执行不成功(错误处理),

本作品采用《CC 协议》,转载必须注明作者和本文链接

NOT IS BECAUSE I WANT TO WRITE,

BUT I WANT TO INCREASE,

SO I GO TO WRITE~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PHP是一种常用的服务器端编程语言,可以用于开发各种web应用程序。对接臻识摄像意味着将PHP应用程序与臻识摄像进行连接与通信。 首先,我们需要确定与臻识摄像的通信方式。通常,臻识摄像会提供SDK或API,用于与其他应用程序进行数据交互。我们可以通过阅读臻识摄像的文档或联系厂商了解可用的通信方式。 一般而言,我们可以使用PHP的网络相关函数和库来与臻识摄像进行通信。例如,可以使用cURL库发送HTTP请求与摄像进行交互。这包括向摄像发送命令、获取图像或视频流等操作。 在进行通信前,我们需要确保服务器端已经安装并配置好PHP、网络环境及相关依赖库。在PHP代码中,我们可以使用cURL函数来实现与摄像的通信。首先,我们需要设置与摄像通信的URL,并通过cURL函数设置参数,例如设置请求方法、请求头、请求体、超时时间等。 然后,我们可以发送请求并获取响应,根据需求对摄像的回应进行处理。可以使用cURL函数提供的方法来获取响应的状态码、响应体等信息。根据返回的数据进行相应的处理,例如解析响应体中的数据、保存图像或视频等。 最后,我们可以将处理后的数据展示在web页面上,例如通过HTML将图像或视频嵌入到页面中,以便用户观看。可以使用PHP的图像处理库对图像进行进一步的编辑或分析。 总之,使用PHP对接臻识摄像需要了解臻识摄像的通信方式,并使用PHP的cURL库来实现与摄像的通信。这样可以实现诸如发送命令、获取图像或视频流等操作,以满足各种应用场景的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值