zinovyev/php-fsm
Composer 安装命令:
composer require zinovyev/php-fsm
包简介
PHP Finite-state Machine
README 文档
README
Finite-state machine allows you to create an object, containing different states and transitions between them, that can change its behaivour according to the current state.
Install:
For installing the FSM machine use Composer. Simply add following to your composer.json file:
"require": { "zinovyev/php-fsm": "0.6.5" }
Creating a State machine steps:
- Create State classes and define states:
class StateA extends State { public function foo($name) { printf("Hello, %s!", $name); } }
- Create a new FSM\Client instance (StateMachine).
- Bind your States to the Client instance, apply transactions and set initial state:
class StateMachine extends Client { public function __construct() { parent::__construct(); /* ... */ $this ->addState($stateA) /* ... */ ->createTransition('fourth', 'stateA', 'stateB') /* ... */
or
$stateMachine = new Client; $stateMachine ->addState($stateA) ->addState($stateB) ->createTransition('fourth', 'stateA', 'stateB') ;
- Use Memento if you want do store current State and parameters and restore them later:
$memento = $stateMachine->createMemento();
and restore:
$stateMachine = new StateMachine(); $stateMachine->applyMemento($memento);
- That's all =) Your simple State machine is now configured and ready for use!
Example code:
<?php require_once('vendor/autoload.php'); use FSM\Client; use FSM\State\State; use FSM\State\StateInterface; /** * StateA class */ class StateA extends State { public function foo($f) { return $f; } } /** * StateB class */ class StateB extends State { public function bar($b) { return $b * 2; } } /** * StateC class */ class StateC extends State { public function fooBar($fb) { return strrev($fb); } public function shuffle(array $array) { shuffle($array); return $array; } } /** * StateMachine example class */ class StateMachine extends Client { public function __construct() { parent::__construct(); // Create StateA initial type instance of class StateA $stateA = new StateA(); $stateA->setName('stateA'); $stateA->setType(StateInterface::TYPE_INITIAL); // Create StateB finite type instance of class StateB $stateB = new StateB(); $stateB->setName('stateB'); $stateB->setType(StateInterface::TYPE_FINITE); // Create StateC regular type instance of class StateC $stateC = new StateC(); $stateC->setName('stateC'); $stateC->setType(StateInterface::TYPE_REGULAR); // Create StateD regular type instance of class StateB $stateD = new StateB(); $stateD->setName('stateD'); $stateD->setType(StateInterface::TYPE_REGULAR); // Attach states and transitions $this ->addState($stateA) ->addState($stateB) ->addState($stateC) ->addState($stateD) ->setInitialState($stateA) ->createTransition('initial', 'stateA', 'stateA') ->createTransition('second', 'stateA', 'stateC') ->createTransition('secondAlternative', 'stateA', 'stateD') ->createTransition('third', 'stateC', 'stateD') ->createTransition('thirdAlternative', 'stateD', 'stateC') ->createTransition('fourth', 'stateD', 'stateB') ->createTransition('fourthAlternative', 'stateC', 'stateB') ->createTransition('shortWay', 'stateA', 'stateB') ; } } // Create new StateMachine instance $stateMachine = new StateMachine(); $stateMachine->foo = 'bar'; // Add public property // Test StateA state printf("%d) State machine is at state: '%s'. Test function call result is: '%s'\n", 1, $stateMachine->getCurrentState()->getName(), // Get State Name $stateMachine->callAction('foo', $properties = [100]) // Call State function ); // Accept transition "initial" $stateMachine->acceptTransitionByName('initial'); printf("%d) State machine is at SAME state: '%s'. Test function call result: '%s'\n", 2, $stateMachine->getCurrentState()->getName(), // Get State Name $stateMachine->callAction('foo', $properties = [200]) // Call State function ); // Accept transition "second" $stateMachine->acceptTransitionByName('second'); printf("%d) State machine is at state: '%s'. Test function call result: '%s'\n", 3, $stateMachine->getCurrentState()->getName(), // Get State Name $stateMachine->fooBar('foo bar') // Call State function ); // Create a memento (snapshot) of the current state $memento = $stateMachine->createMemento(); // Unset StateMachine $stateMachine = null; unset($stateMachine); // Restore StateMachine from a snapshot (Memento) $stateMachine = new StateMachine(); $stateMachine->applyMemento($memento); printf("=*= Check property value after restore: \$foo='%s' =*=\n", $stateMachine->foo); // Accept transition "third" $stateMachine->acceptTransitionByName('third'); printf("%d) State machine is at state '%s'. Test function call result: '%s'\n", 4, $stateMachine->getCurrentState()->getName(), // Get State Name $stateMachine->bar(1) // Call State function ); // Accept transition "fourth" $stateMachine->acceptTransitionByName('fourth'); printf("%d) State machine is at state '%s'. Test function call result: '%s'\n", 5, $stateMachine->getCurrentState()->getName(), // Get State Name $stateMachine->bar(2) // Call State function );
Example code execution result:
If you run the code in your console, you'll see the following output:
1) State machine is at state: 'stateA'. Test function call result is: '100'
2) State machine is at SAME state: 'stateA'. Test function call result: '200'
3) State machine is at state: 'stateC'. Test function call result: 'rab oof'
=*= Check property value after restore: $foo='bar' =*=
4) State machine is at state 'stateD'. Test function call result: '2'
5) State machine is at state 'stateB'. Test function call result: '4'
zinovyev/php-fsm 适用场景与选型建议
zinovyev/php-fsm 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.4k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2014 年 05 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「state」 「statemachine」 「transition」 「machine」 「finite-state」 「automaton」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 zinovyev/php-fsm 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 zinovyev/php-fsm 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 zinovyev/php-fsm 相关的其它包
同方向 / 同关键字的高下载量 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.
统计信息
- 总下载量: 7.4k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 11
- 点击次数: 30
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPLv2
- 更新时间: 2014-05-17