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);
Test
systream/state-machine 适用场景与选型建议
systream/state-machine 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 108 次下载、GitHub Stars 达 1, 最近一次更新时间为 2016 年 06 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「state」 「statemachine」 「transition」 「machine」 「state-machine」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 systream/state-machine 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 systream/state-machine 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 systream/state-machine 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
simple state machine with annotations for PHP, inspired by AASM known as a Ruby state machine.
Doctrine implementation of the MetaborStd (Statemachine) for PHP 8.2+
A simple PHP7.2+ Finite State Machine
A simple state dropdown field for SilverStripe forms
A state Machine library for business processes
Metamel Addresses is a polymorphic Laravel package, for address book management. You can add addresses to any eloquent model with ease.
统计信息
- 总下载量: 108
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-06-05
