yohang / finite
简单的PHP有限状态机
1.3.5
2022-04-08 12:40 UTC
Requires
- php: >=7.4
- symfony/event-dispatcher: ^4.0|^5.0|^6.0
- symfony/options-resolver: ^4.0|^5.0|^6.0
- symfony/property-access: ^4.0|^5.0|^6.0
Requires (Dev)
- phpunit/phpunit: ^8.5.25
- pimple/pimple: ^1.1.1
- symfony/dependency-injection: ^4.0|^5.0|^6.0
- symfony/framework-bundle: ^4.0|^5.0|^6.0
- symfony/security-bundle: ^4.0|^5.0|^6.0
- symfony/serializer: ^4.0|^5.0|^6.0
- twig/twig: ^2.0|^3.0
Suggests
- pimple/pimple: Needed for use with PimpleFactory
- symfony/security: Needed for using SecurityAwareStateMachine
- symfony/serializer: Needed for the use of the normalizer
- symfony/yaml: Yaml allows you to define your State graph in YAML
- dev-future / 2.0.x-dev
- dev-master / 1.3.x-dev
- 1.3.5
- 1.3.4
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.x-dev
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- 1.0.0-BETA2
- 1.0.0-BETA1
- dev-symfony-5-php-8
- dev-feature-new-docs
- dev-compatibility-symfony3
- dev-refactor-callback-handler
- dev-apply_additional_param
- dev-scrutinizer-patch-1
- dev-feature-context-graph-aware
This package is auto-updated.
Last update: 2024-08-25 12:07:11 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');