runner / heshen
有限状态机
v1.0.3
2018-07-18 10:05 UTC
Requires
- symfony/event-dispatcher: ^4.0
Requires (Dev)
- phpunit/phpunit: ^5.0
This package is auto-updated.
Last update: 2024-09-08 19:43:29 UTC
README
PHP中的有限状态机
特性
- 基于 Stateful 对象绑定 Graph
- 过渡事件监听
- 便捷的过渡检查器
- 以上都是胡说八道
文档
无
使用方法
先定义 Stateful 对象
<?php use Runner\Heshen\Contracts\StatefulInterface; class Document implements StatefulInterface { protected $state = 'a'; public function getState(): string { return $this->state; } public function setState(string $state): void { echo "\nsetting\n"; $this->state = $state; } }
然后定义一个 Blueprint 来配置过渡及状态
<?php use Runner\Heshen\Blueprint; use Runner\Heshen\State; use Runner\Heshen\Contracts\StatefulInterface; class Graph extends Blueprint { protected function configure(): void { $this->addState('a', State::TYPE_INITIAL); $this->addState('b', State::TYPE_NORMAL); $this->addState('c', State::TYPE_NORMAL); $this->addState('d', State::TYPE_FINAL); $this->addTransition('one', 'a', 'b'); $this->addTransition('two', 'b', 'c', function (StatefulInterface $stateful, array $parameters) { return ($parameters['number'] ?? 0) > 5; }); } protected function preOne(StatefulInterface $stateful, array $parameters = []) { echo "before apply transition 'one'\n"; } protected function postOne(StatefulInterface $stateful, array $parameters = []) { echo "after apply transition 'one'\n"; } }
开始使用!
<?php use Runner\Heshen\Machine; $machine = new Machine(new Document, new Graph); var_dump($machine->can('one')); // output: bool(true) var_dump($machine->can('two')); // output: bool(false) $machine->apply('one'); /* * output: * before apply transition 'one' * after apply transition 'one' */ var_dump($machine->can('two', ['number' => 1])); // output: bool(false) var_dump($machine->can('two', ['number' => 6])); // output: bool(true)
通过 Factory 获取 Machine
<?php use Runner\Heshen\Factory; $factory = new Factory([ Document::class => Graph::class, ]); $document = new Document; $machine = $factory->make($document); var_dump($machine->can('one')); // output: bool(true)
提示
在实现 StatefulInterface::setState()
时,如有需要,应当使用锁避免冲突。