abarbod/laravel-state-machine
Composer 安装命令:
composer require abarbod/laravel-state-machine
包简介
A production-ready state machine package for Laravel Eloquent models
README 文档
README
A production-ready state machine package for Laravel Eloquent models. Enforce state transitions, prevent invalid state changes, and maintain a complete audit trail.
Features
- Formal State Machine: Define states and transitions with a fluent API
- Guard Conditions: Prevent invalid transitions with guard callbacks
- Hooks: Execute code before, after, or on failure of transitions
- History Tracking: Complete audit trail of all state changes
- Concurrency Safety: Optimistic locking prevents race conditions
- Strict Mode: Prevent direct state assignment (must use transitions)
- Multi-State Support: Multiple independent state machines per model
- Diagram Generation: Visualize state machines as Mermaid or DOT diagrams
- Testing Helpers: Assertions for testing state transitions
Installation
composer require abarbod/laravel-state-machine
Publish the configuration file:
php artisan vendor:publish --tag=state-machine-config
Run migrations:
php artisan migrate
Quick Start
1. Define a State Machine
In your AppServiceProvider or a dedicated service provider:
use Laravel\StateMachine\Facades\StateMachine; use App\Models\Order; public function boot(): void { StateMachine::define(Order::class, 'status', function ($machine) { $machine->states(['draft', 'paid', 'shipped', 'delivered', 'canceled']) ->initial('draft') ->transition('pay') ->from('draft') ->to('paid') ->guard(fn(Order $order) => $order->total > 0) ->after(fn(Order $order) => event(new OrderPaid($order))) ->transition('ship') ->from('paid') ->to('shipped') ->guard(fn(Order $order) => $order->items()->count() > 0) ->transition('deliver') ->from('shipped') ->to('delivered') ->transition('cancel') ->from(['draft', 'paid']) ->to('canceled'); }); }
2. Use the Trait in Your Model
use Illuminate\Database\Eloquent\Model; use Laravel\StateMachine\HasStateMachine; class Order extends Model { use HasStateMachine; protected $fillable = ['status', 'total']; }
3. Execute Transitions
$order = Order::create(['status' => 'draft', 'total' => 100]); // Check if transition is allowed if ($order->canTransition('pay')) { $order->transition('pay'); } // Or directly transition (throws exception if not allowed) $order->transition('pay');
Configuration
Edit config/state-machine.php:
return [ 'history' => env('STATE_MACHINE_HISTORY', true), 'strict' => env('STATE_MACHINE_STRICT', false), 'transactional' => env('STATE_MACHINE_TRANSACTIONAL', true), 'actor_resolver' => fn() => auth()->id(), 'concurrency' => [ 'enabled' => env('STATE_MACHINE_CONCURRENCY_ENABLED', true), 'retry_attempts' => env('STATE_MACHINE_RETRY_ATTEMPTS', 3), ], ];
Options
- history: Enable/disable transition history logging (default:
true) - strict: Prevent direct state assignment (default:
false) - transactional: Run transitions in database transactions (default:
true) - actor_resolver: Callback to determine the user/actor for history (default:
auth()->id()) - concurrency.enabled: Enable optimistic locking (default:
true) - concurrency.retry_attempts: Number of retry attempts on concurrent conflicts (default:
3)
Advanced Usage
Guards
Guards prevent transitions when conditions aren't met:
->transition('ship') ->from('paid') ->to('shipped') ->guard(fn(Order $order) => $order->items()->count() > 0) ->guard(fn(Order $order) => $order->shipping_address !== null)
If any guard returns false or throws an exception, the transition is prevented.
Hooks
Execute code at different stages of a transition:
->transition('pay') ->from('draft') ->to('paid') ->before(function ($order, $from, $to, $transition, $context) { // Runs before state change (in transaction) logger("About to pay order {$order->id}"); }) ->after(function ($order, $from, $to, $transition, $context) { // Runs after successful state change (outside transaction) event(new OrderPaid($order)); }) ->onFailure(function ($order, $from, $to, $transition, $context) { // Runs if transition fails logger("Failed to pay order {$order->id}"); })
Hook parameters:
$model: The model instance$from: The current state$to: The target state$transition: The transition name$context: Additional context data
Multiple Source States
Transitions can originate from multiple states:
->transition('cancel') ->from(['draft', 'paid', 'shipped']) ->to('canceled')
Wildcard Transitions
Allow transitions from any state:
->transition('reset') ->from('*') ->to('draft')
Transition History
Access the transition history:
$history = $order->getStateHistory(); // Returns collection of StateTransition models // Or use the relationship $transitions = $order->stateTransitions;
Multiple State Machines
Support multiple independent state machines per model:
// In your model class Order extends Model { use HasStateMachine; protected $stateMachineField = 'status'; // Default field // For multiple fields, define machines separately } // Define machines for different fields StateMachine::define(Order::class, 'status', function ($machine) { // status state machine }); StateMachine::define(Order::class, 'payment_status', function ($machine) { // payment_status state machine });
Strict Mode
Enable strict mode to prevent direct state assignment:
STATE_MACHINE_STRICT=true
With strict mode enabled:
// This will throw an exception: $order->status = 'paid'; // Must use transitions: $order->transition('pay');
Diagram Generation
Generate visual diagrams of your state machines:
php artisan state-machine:diagram Order php artisan state-machine:diagram Order --field=status php artisan state-machine:diagram Order --format=dot --output=order.dot php artisan state-machine:diagram Order --format=mermaid --output=order.md
Supported formats:
mermaid(default): Mermaid flowchart syntaxdot: Graphviz DOT format
Testing
The package includes testing helpers:
use Laravel\StateMachine\Tests\TestCase; class OrderTest extends TestCase { public function test_order_can_be_paid(): void { $order = Order::create(['status' => 'draft', 'total' => 100]); $this->assertCanTransition($order, 'pay'); $this->assertCannotTransition($order, 'ship'); $order->transition('pay'); $this->assertState($order, 'paid'); $this->assertTransitionHistory($order, ['pay']); } }
Available assertions:
assertCanTransition($model, $transition): Assert transition is allowedassertCannotTransition($model, $transition): Assert transition is not allowedassertState($model, $expectedState, $field = null): Assert current stateassertTransitionHistory($model, $expectedTransitions, $field = null): Assert transition history
Error Handling
The package throws specific exceptions:
StateMachineNotDefinedException: No state machine defined for model/fieldInvalidTransitionException: Transition not allowed from current stateTransitionGuardFailedException: Guard condition failedConcurrentTransitionException: Concurrent state change detected
use Laravel\StateMachine\Exceptions\InvalidTransitionException; try { $order->transition('invalid'); } catch (InvalidTransitionException $e) { // Handle invalid transition }
Performance Considerations
- State machine definitions are stored in memory (can be cached in production)
- Transition execution is O(1) - no reflection in hot paths
- Optimistic locking uses efficient WHERE clauses
- History logging is optional and can be disabled
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
The MIT License (MIT). Please see the license file for more information.
abarbod/laravel-state-machine 适用场景与选型建议
abarbod/laravel-state-machine 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 01 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「workflow」 「state machine」 「fsm」 「laravel」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 abarbod/laravel-state-machine 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 abarbod/laravel-state-machine 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 abarbod/laravel-state-machine 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Workflow for NetCommons Plugin
Doctrine implementation of the MetaborStd (Statemachine) for PHP 8.2+
A simple state dropdown field for SilverStripe forms
Workflow logger
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.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 30
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-02