metabor/statemachine
Composer 安装命令:
composer require metabor/statemachine
包简介
Statemachine in PHP 8.2+
README 文档
README
Statemachine in PHP 8.2+
Upgrading from a previous version? See UPGRADE-3.0.md for breaking changes and migration steps.
Package Information
Packagist
Open Issues
Other
Quickstart examples
Once installed, let's use a sample statemachine:
<?php require_once 'vendor/autoload.php'; use Metabor\Statemachine\Process; use Metabor\Statemachine\State; use Metabor\Statemachine\Statemachine; use Metabor\Statemachine\Transition; $closed = new State('closed'); $opened = new State('opened'); $eventOpen = 'open'; $eventClose = 'close'; $closed->addTransition(new Transition($opened, $eventOpen)); $closed->addTransition(new Transition($closed, $eventClose)); $opened->addTransition(new Transition($opened, $eventOpen)); $opened->addTransition(new Transition($closed, $eventClose)); // adding some action to events // the parts of observing the event and executing the command are separated in this example // normaly it could be put all together into your own command (base)class $openCommand = new \Metabor\Callback\Callback( function () { echo 'motor is opening door' . PHP_EOL; }); $observerForOpenEvent = new \Metabor\Observer\Callback($openCommand); $closed->getEvent($eventOpen)->attach($observerForOpenEvent); $closeCommand = new \Metabor\Callback\Callback( function () { echo 'motor is closing door' . PHP_EOL; }); $observerForCloseEvent = new \Metabor\Observer\Callback($closeCommand); $opened->getEvent($eventClose)->attach($observerForCloseEvent); // stateful subject that belongs to this statemachine $subject = new stdClass(); // start process with closed status; $initialState = $closed; $process = new Process('process name', $initialState); $statemachine = new Statemachine($subject, $process); echo 'Status:' . $statemachine->getCurrentState()->getName() . PHP_EOL; echo 'Event:' . $eventOpen . PHP_EOL; $statemachine->triggerEvent($eventOpen); echo 'Status:' . $statemachine->getCurrentState()->getName() . PHP_EOL; // opening an open door would not activate the motor echo 'Event:' . $eventOpen . PHP_EOL; $statemachine->triggerEvent($eventOpen); echo 'Status:' . $statemachine->getCurrentState()->getName() . PHP_EOL; echo 'Event:' . $eventClose . PHP_EOL; $statemachine->triggerEvent($eventClose); echo 'Status:' . $statemachine->getCurrentState()->getName() . PHP_EOL;
Features
This library implements a finite-state machine in PHP 8.2+.
It was first developed for a talk at a conference. The example from my talk is available on Github and Packagist as metabor/statemachine-example.
In the namespace MetaborStd are abstract types defined that are exemplified implemented in this project. If you have to implement or use a statemachine in your project, feel free to either use this libary at all or replace the parts that didn't fit your needs by using the MetaborStd Interfaces.
Process Graph Drawing
The library supports visualizing of the process graph by using clue/graph and GraphViz "Graph Visualization Software".
Install
The recommended way to install this library is through composer. New to composer?
{
"require": {
"php": ">=8.2",
"metabor/statemachine": "~3.0"
}
}
Optional recommendation:
In order to be able to use the process graph drawing feature you'll have to
install GraphViz (dot executable). Users of Debian/Ubuntu-based distributions may simply
invoke sudo apt-get install graphviz, Windows users have to
download GraphViZ for Windows and remaining
users should install from GraphViz homepage.
To use this feature you also have to add this to your composer.json:
{
"require": {
"php": ">=8.2",
"graphp/graphviz": "*",
"clue/graph": "*",
"metabor/statemachine": "~3.0"
}
}
An example how to draw and display the graph, can be found in metabor/statemachine-example.
Tests
This library uses phpunit for its extensive testsuite.
You can either use a global installation or rely on the one composer installs
when you first run $ composer install.
This sets up the developer environment, so that you
can now run it from the project root directory:
$ php vendor/bin/phpunit
Contributing
If you encounter any issues, please don't hesitate to drop us a line, file a bug report or even best provide us with a patch / pull request and/or unit test to reproduce your problem.
Besides directly working with the code, any additional documentation, additions to our readme or even fixing simple typos are appreciated just as well.
Any feedback and/or contribution is welcome!
License
Released under the terms of the permissive MIT license.
metabor/statemachine 适用场景与选型建议
metabor/statemachine 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 150.9k 次下载、GitHub Stars 达 102, 最近一次更新时间为 2013 年 11 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「process」 「state machine」 「statemachine」 「status」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 metabor/statemachine 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 metabor/statemachine 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 metabor/statemachine 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Doctrine implementation of the MetaborStd (Statemachine) for PHP 8.2+
Shell command module for PHP.
A simple state dropdown field for SilverStripe forms
Swoole server process management
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.
统计信息
- 总下载量: 150.9k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 103
- 点击次数: 30
- 依赖项目数: 2
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2013-11-08