jeyroik / extas-m
此包最新版本(4.0.3)没有可用的许可信息。
可扩展状态机。
4.0.3
2020-03-26 10:47 UTC
Requires
This package is auto-updated.
Last update: 2024-09-08 21:33:02 UTC
README
Extas 的状态机包。
安装
composer require jeyroik/extas-m:*
使用
使用演示状态机
安装后,可以立即运行演示状态机
use extas\components\SystemContainer as Container; use extas\interfaces\machines\IMachineRepository; use extas\interfaces\machines\IMachine; $machineRepo = Container::getItem(IMachineRepository::class); $machine = $machineRepo->one([IMachine::FIELD__NAME => 'extas.demo']); $machine->run('init', ['anything' => 'you want']);
你应该看到以下输出
Initialized demo machine with the context:
[
'anything' => 'you want'
]
Set to context initialized = true
Finished demo machine with the context:
[
'anything' => 'you want',
'initialized' => true
]
此外,还可以查看状态之间的转换序列
echo '<pre>' . print_r($machine->getDump(), true) . '</pre>;
你应该看到以下内容
[
[
'state_from' => 'extas.demo.not initialized yet',
'state_to' => 'extas.demo.init',
'context' => [
'anything' => 'you want'
]
],
[
'state_from' => 'extas.demo.init',
'state_to' => 'extas.demo.end',
'context' => [
'anything' => 'you want',
'initialized' => true
]
]
]
创建自己的状态机
创建状态机配置
首先需要配置机器和状态。需要在 extas 兼容的配置中指定它们。
{ "machine_states": [ { "name": "init", "title": "Инициализация", "description": "Состояние инициализации машины" }, { "name": "hello", "title": "Привет", "description": "Приветствие", "parameters": [{"name": "text"}] }, { "name": "space", "title": "Пробел", "description": "Состояние пробела", "parameters": [{"name": "text"}] }, { "name": "end", "title": "Конец", "description": "завершающее состояние машины", "parameters": [{"name": "text"}] }, { "name": "someone", "title": "Некто", "description": "Состояние незнакомца", "parameters": [{"name": "text"}, {"name": "user"}] }, { "name": "print_html", "title": "Вывести HTML", "description": "Вывод HTML", "parameters": [{"name": "data"}] }, { "name": "print_json", "title": "Вывод JSON", "description": "Вывод JSON", "parameters": [{"name": "data"}] } ], "machines": [ { "name": "hello_world", "title": "Привет мир", "description": "Данная машина выводит приветственное сообщение миру", "states": [ { "name": "init", "on_success": {"state": "hello"}, "on_failure": {"state": "end"} }, { "name": "hello", "on_success": {"state": "space"}, "on_failure": {"state": "end"} }, { "name": "space", "on_success": {"state": "world"}, "on_failure": {"state": "end"} }, { "name": "world", "on_success": {"state": "end"}, "on_failure": {"state": "someone"} }, { "name": "end", "on_success": {"machine": "sub_machine", "state": "print_html"} }, { "name": "someone" } ] }, { "name": "sub_machine", "parameters": [{"name": "text"}], "states": [ { "name": "print_html", "on_failure": {"state": "print_json"} }, { "name": "print_json" } ] } ] }
状态参数
是状态所需的上下文参数。如果上下文中缺少任何一个参数,则状态被认为是无效的,其启动将被取消。机器参数
是该机器中所有状态使用的参数。
为状态创建插件
接下来需要创建用于处理状态的插件。
use extas\components\plugins\Plugin; class PluginStateHello extends Plugin { public function __invoke($state, &$context, $machine, &$isSuccess) { $context['text'] = $context['text'] . 'hello'; $isSuccess = true; } } class PluginStateSpace extends Plugin { public function __invoke($state, &$context, $machine, &$isSuccess) { $context['text'] = $context['text'] . ' '; $isSuccess = true; } } class PluginStateWorld extends Plugin { public function __invoke($state, &$context, $machine, &$isSuccess) { $context['text'] = $context['text'] . 'world'; $isSuccess = true; } }
安装机器、状态和插件
/vendor/bin/extas i
运行状态机
$machine = $machineRepo->one([IMachine::FIELD__NAME => 'hello_world']) $machine->run('hello', ['text' => '']); // "hello world" /** * [ * [ * "state_from" => "hello_world.init", * "state_to" => "hello_world.hello", * "context" => [ * "text" => "" * ] * ], * [ * "state_from" => "hello_world.hello", * "state_to" => "hello_world.hello", * "context" => [ * "text" => "hello" * ] * ], * [ * "state_from" => "hello_world.space", * "state_to" => "hello_world.hello", * "context" => [ * "text" => "hello " * ] * ], * [ * "state_from" => "hello_world.world", * "state_to" => "hello_world.end", * "context" => [ * "text" => "hello world" * ] * ], * [ * "state_from" => "hello_world.end", * "state_to" => "sub_machine.print_html", * "context" => [ * "text" => "hello world" * ] * ] * ] */ $machine->dump();