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

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

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

systream/state-machine

Composer 安装命令:

composer require systream/state-machine

包简介

State machine

README 文档

README

Installation

You can install this package via packagist.org with composer.

composer require systream/state-machine

composer.json:

"require": {
    "systream/state-machine": "1.*"
}

Usage examples

Setup

You need to add transitions to state machine.

$stateMachine = new StateMachine(new \Systream\EventDispatcher());

$inStock = new StateMachine\State('In stock');
$ordered = new StateMachine\State('Ordered');
$shippingInProcess = new StateMachine\State('Shipping in process');
$deliveredToClient = new StateMachine\State('Order is at client');

$stateMachine->addTransition(
	new GenericTransition('Order'), $inStock, $ordered
);

$stateMachine->addTransition(
	new GenericTransition('Cancel order'), $ordered, $inStock
);

$stateMachine->addTransition(
	new GenericTransition('Shipping'), $ordered, $shippingInProcess
);

$stateMachine->addTransition(
	new GenericTransition('Handover to client'), $shippingInProcess, $deliveredToClient
);

Custom transitions

You have to implement the \Systream\StateMachine\TransitionInterface interface to create custom transition

Process Transition

StateObject

To use state machine you need an object which has state. process method expect \Systream\StateMachine\State\StateObjectInterface interface.

So you need to implement it, or just use \Systream\StateMachine\State\StateObjectTrait.

Can

Testing, whether it can change the status to the target state

$product = new DummyStateObject();
$product->setState($inStock);
$stateMachine->can($product, $ordered); // will return true
$stateMachine->can($product, $deliveredToClient); // will return false

Process

Set project state to In Stock and process it to Ordered.

$product = new DummyStateObject();
$product->setState($inStock);
$stateMachine->process($product, $ordered);

Set state without transition will trow an \Systream\StateMachine\Exception\CantSetStatusException exception.

$product = new DummyStateObject();
$product->setState($inStock);
$stateMachine->process($product, $deliveredToClient);

Get available states

$states = $stateMachine->getStates();

It will return array of \Systream\StateMachine\State\StateInterface objects

Get next states

This method will return of the next possible states of an state object:

$product = new DummyStateObject();
$states = $stateMachine->getNextStates($product);

It will return array of \Systream\StateMachine\State\StateInterface objects

Visualization

With this library you are able to generate an image with the states and transitions. To get this work you need to install graphviz.

$doFileGenerator = new StateMachine\DotFileGenerator();
$image = $doFileGenerator->getImage($stateMachine);
file_put_contents('my_flow_chart.png', $image);

example-flow

Test

Build Status

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-06-05

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固