sofa/state-machine
Composer 安装命令:
composer require sofa/state-machine
包简介
Easy to use implementation of the Finite State Machine
README 文档
README
Finite State Machine implementation
Installation
Add package to your project:
composer require sofa/state-machine
Usage
State machine helps you eliminate switch and/or if/else statements in your code to determine available actions in given state.
Let's use a naive example from a Laravel's Blade view template and underlying Eloquent Order model:
@foreach($orders as $order) {{ $order->reference }} status: {{ $order->status }} @if($order->status === 'new') <button>start processing</button> @elseif($order->status === 'awaiting_payment') <button>record payment</button> @elseif($order->status === 'awaiting_shipment') <button>save tracking number</button> @elseif($order->status === 'in_delivery') <button>record delivery</button> <button>open claim</button> @elseif($order->status === 'complete') <button>open claim</button> @elseif($order->status === 'processing_claim') <button>refund</button> <button>close claim</button> @endif @endforeach
This quickly gets out of hand, especially when a new status is introduced or the processing order changes.
To streamline it, we can implement state machine for the Order entity:
-
implement interface on the
Ordermodelclass Order extends Model implements \Sofa\StateMachine\StateMachineInterface { //... public function getCurrentState() : string { return $this->status; } public function setState(string $state) : void { $this->status = $state; $this->save(); } }
-
define available transitions and prepare data for the template:
$transitions = [ Transition::make(/*from_state*/ 'new', /*action*/ 'start processing', /*to_state*/ 'awaiting_payment'), Transition::make('awaiting_payment', 'record payment', 'awaiting_shipment'), Transition::make('awaiting_shipment', 'save tracking number', 'in_delivery'), Transition::make('in_delivery', 'record delivery', 'complete'), Transition::make('in_delivery', 'open claim', 'processing_claim'), Transition::make('complete', 'open claim', 'processing_claim'), Transition::make('processing_claim', 'close claim', 'complete'), Transition::make('processing_claim', 'refund', 'refunded'), ]; foreach ($orders as $order) { $order_state = new \Sofa\StateMachine\Fsm($order, $transitions); $order->available_actions = $order_state->getAvailableActions(); }
-
and we end up with controller & template code decoupled from the Process logic & order:
@foreach($orders as $order) {{ $order->reference }} status: {{ $order->status }} @foreach($order->available_actions as $action) <button>{{ $action }}</button> @endforeach @endforeach
-
finally let's process the actions
// controller handling the action public function handleAction($order_id, Request $request) { $order_state = new \Sofa\StateMachine\Fsm(Order::find($order_id), $transitions); $this->validate($request, [ 'action' => Rule::in($order_state->getAvailableActions()), // ... ]); $order_state->process($request->get('action')); return Redirect::to('some/place'); }
With this setup we no longer have to change our controllers or views, whenever business requirements change. Instead we add a new transition to the state machine definition.
I need more control during transition - how to?
The above example assumes very simple transition process, ie. $order->status = $new_status. This can be enough sometimes, but often we will need more flexibility during transitions. To address this need you can customize your Transition definitions, so they turn from simple POPO into callable that will be invoked, when state machine processes appropriate action:
class Refund extends \Sofa\StateMachine\Transition { public function __invoke(StateMachineInterface $order, $payload) { // $payload is any object you pass to the process method: // $order_state->process('refund', $anything_you_need_here); $order->refunded_at = $payload['time']; $order->refunded_by = $payload['user_id']; $order->setState($this->to_state); } } // Then our transitions definition would like something like: $transitions = [ // ... Transition::make('processing_claim', 'close claim', 'complete'), Refund::make('processing_claim', 'refund', 'refunded'), ];
Happy Coding!
Contribution
All contributions are welcome. Make your PR PSR-2 compliant and tested.
sofa/state-machine 适用场景与选型建议
sofa/state-machine 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 221.82k 次下载、GitHub Stars 达 8, 最近一次更新时间为 2018 年 07 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「state」 「fsm」 「statemachine」 「finite-state-machine」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 sofa/state-machine 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sofa/state-machine 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 sofa/state-machine 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Doctrine implementation of the MetaborStd (Statemachine) for PHP 8.2+
A simple PHP7.2+ Finite State Machine
A simple state dropdown field for SilverStripe forms
Finite state machine for php.
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.
统计信息
- 总下载量: 221.82k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 8
- 点击次数: 35
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-07-21