exabyssus/laravel-finite-state
最新稳定版本:v1.0.6
Composer 安装命令:
composer require exabyssus/laravel-finite-state
包简介
Mini Laravel Finite State Machine.
关键字:
README 文档
README
Package gives an easy way to add StateMachine to your Eloquent Models.
StateMachine helps you control state flow and records state history.
Installation
Add package to your composer.
composer require exabyssus/laravel-finite-state
Publish package to copy config and migration files.
php artisan vendor:publish --provider="Exabyssus\StateMachine\StateMachineServiceProvider"
Add Traits to Models you want to add State Machine
use Exabyssus\StateMachine\Traits\HasStateHistory;
use Exabyssus\StateMachine\Traits\HasStateMachine;
class YourModel extends Model
{
use HasStateMachine;
use HasStateHistory;
...
}
Configure State Machine transitions
config/state-machine.php
Configuration
<?php
return [
'order' => [ // Name of your object
'state_property_name' => 'state', // Objects current state property name
'states' => [ // All available states
'pending',
'confirmed',
],
'transitions' => [ // Transition mapping
'confirm' => [
'from' => ['pending'],
'to' => 'confirmed'
],
'cancel' => [
'from' => ['pending', 'confirmed'],
'to' => 'canceled'
],
]
],
];
Usage
Check if transition is allow for Object
transitionAllowed (strign $status): bool
Apply transition
transition(string $transition): void
Get available transitions
getPossibleTransitions(): array
Events
To catch Object before state is change add afterStateChange method to your class.
function beforeStateChange($transition, $stateFrom, $stateTo)
If function returns false state won't be changed.
After state is changed afterStateChange method is called.
function afterStateChange($transition, $stateTo)
And StateChanged event is dispatched.
统计信息
- 总下载量: 2.54k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-01-31