runner / finite
简单的PHP5.3+有限状态机
v1.1.1
2017-12-06 10:45 UTC
Requires
- php: >=5.3.9
- symfony/event-dispatcher: ^2.6|^3.0
- symfony/options-resolver: ^2.6|^3.0
- symfony/property-access: ^2.6|^3.0
Requires (Dev)
- phpunit/phpunit: ^4.0
- pimple/pimple: ^1.0
- symfony/dependency-injection: ^2.6|^3.0
- symfony/framework-bundle: ^2.6|^3.0
- symfony/security: ^2.6|^3.0
- twig/twig: ^1.13
Suggests
- pimple/pimple: Needed for use with PimpleFactory
- symfony/security: Needed for using SecurityAwareStateMachine
- symfony/yaml: Yaml allows you to define your State graph in YAML
This package is auto-updated.
Last update: 2024-08-28 23:23:03 UTC
README
Finite是一个简单的状态机,用PHP编写。它可以管理任何有状态的对象,通过定义状态以及这些状态之间的转换。
特性
- 管理对象的状态/转换图
- 定义和检索状态的属性
- 事件可监听的状态转换
- Symfony2集成
- 自定义状态图加载器
- Twig扩展
文档
入门
安装(通过composer)
{ "require": { "yohang/finite": "~1.1" } }
版本说明
如果您在Symfony项目中使用这个库,1.1版本仅与Symfony >=2.6
兼容。1.0版本与Symfony >=2.3, <2.6
兼容。
定义您的有状态对象
您的有状态对象只需要实现StatefulInterface
接口。
use Finite\StatefulInterface; class Document implements StatefulInterface { private $state; public function setFiniteState($state) { $this->state = $state; } public function getFiniteState() { return $this->state; } }
初始化简单的状态机
use Finite\StateMachine\StateMachine; use Finite\State\State; use Finite\State\StateInterface; // $document = retrieve your stateful object $sm = new StateMachine(); // Define states $sm->addState(new State('s1', StateInterface::TYPE_INITIAL)); $sm->addState('s2'); $sm->addState('s3'); $sm->addState(new State('s4', StateInterface::TYPE_FINAL)); // Define transitions $sm->addTransition('t12', 's1', 's2'); $sm->addTransition('t23', 's2', 's3'); $sm->addTransition('t34', 's3', 's4'); $sm->addTransition('t42', 's4', 's2'); // Initialize $sm->setObject($document); $sm->initialize(); // Retrieve current state $sm->getCurrentState(); // Can we process a transition ? $sm->can('t34');