louishrg/state-flow
Composer 安装命令:
composer require louishrg/state-flow
包简介
Simple implementation of state machine for Laravel
README 文档
README
Simple state machine / resilient states for your Laravel application!
Installation
You can install the package via composer:
composer require louishrg/state-flow
Create your states classes with artisan :
Creating files for every available states is repetitive, that's why this package provide an artisan command to speed up the process :
php artisan states:new
Simple states AKA Stack
A Stack is a simple state machine that doesn't need to register transitions. It's a very convenient way to add a hardcoded type or category to your model.
How to use :
You need at least 1 variable: key which is the real value of the column in the database.
If you want to use other variables as key, you can give the name of the variable in you stateStack creation (see below)
- First parameter is all your available state as an array.
- Second parameter is the default value when creating a model (optional).
- Third parameter is to override the default key-value (optional).
new Stack(self::$status, Pending::class, 'key'),
Declare your states classes in the directory of your choice, for example :
namespace App\Models\States\User; use Louishrg\StateFlow\StateAbstract; class Active extends StateAbstract { public $key = 'active'; public $label = 'Active'; public $color = 'green'; // and everything you want ! // public function computedField() { return $this->$label.$this->color; } }
Now, add all the needed declaration in your model :
<?php ... // Import all your states use App\Models\States\Active; use App\Models\States\Banned; use App\Models\States\Inactive; // Import the classes use Louishrg\StateFlow\Traits\WithState; use Louishrg\StateFlow\Casts\StateCast; use Louishrg\StateFlow\Stack; class User { // Add WithState trait use WithState, ...; ... // You can register all available states for a namespace in a var for example protected static $status = [ Active::class, Banned::class, Inactive::class, ]; // register your states as a Stack for the namespace "status" protected static function registerStates(){ return [ 'status' => new Stack(self::$status), ]; } ... // Add the cast for your column and that's it ! protected $casts = [ 'status' => StateCast::class, ]; }
Now you can get your state like so :
$user->status; // It'll give you the state object with all your defined constants in it.
If you want to update/create an object with a state:
$user = new User; // Simply pass the state class and that's it. $user->status = Pending::class;
Laravel Nova:
If you want to use the package in nova, you should use it as following :
Example with a select:
Select::make('Statut', 'status') ->options(GearRequest::getState('status')->pluck('label', 'key')->toArray()) ->displayUsing(fn($item) => $item->label) ->resolveUsing(fn($item) => $item->key) // Use the magic setter in the fillUsing method ->fillUsing(fn($request, $model) => ($model->_status = $request->status)),
Useful Methods :
If you want to compare the current value of a state with another one, you can use
$user->status->equal(Banned::class);
Also, you can directly get the class of your current state :
$user->status->is();
If you want to retrieve all your states registered in a namespace as a collection :
User::getState('status')
Complex States AKA Flow:
If you want to use the real state machine pattern in your app you can add register like so:
// Import the Flow class use Louishrg\StateFlow\Flow; ... protected static function registerStates(){ return [ // use a custom method in your model for better readability 'status' => self::myFlow(), ]; } // You need to use the Flow class protected static function myFlow(){ // We'll use the data from above return (new Flow(self::$status)) // Add a transition, here your state can go from Pending to either Accepted or Refused. ->add(Pending::class, [ Accepted::class, Refused::class ]) ->add(Refused::class, [ Pending::class ]) ->add(Accepted::class, [ Pending::class, Canceled::class, CanceledByAdmin::class ]) ->default(Pending::class); // You can specify a default class, when creating you don't need to provide value. }
Methods for flows :
When using flows, you can check if you can transition to another state like so :
$user->status->canBe(Banned::class);
Or you can get all the possible transitions for your current state :
$user->status->allowedTo();
Magic methods for your state :
When you are retrieving rows from your database, you new to instanciate your state to get the key :
$users = User::where('status', (new Active)->key)->get();
In order to simplify the syntax, every State values extend a StateAbstract that provide magic methods :
$users = User::where('status', Active::key())->get();
The magic methods can get you every property defined in your State
Features to come:
- Possibility of using getter & setters in your state classes
- Tests
Testing
composer test
Changelog
Please see CHANGELOG for more information about what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security-related issues, please email dev@narah.io instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
Laravel Package Boilerplate
This package was generated using the Laravel Package Boilerplate.
louishrg/state-flow 适用场景与选型建议
louishrg/state-flow 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.83k 次下载、GitHub Stars 达 14, 最近一次更新时间为 2020 年 10 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「state-machine」 「louishrg」 「state-flow」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 louishrg/state-flow 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 louishrg/state-flow 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 louishrg/state-flow 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Control your state using enums
A workflow manager plugin for FilamentPHP with PHP enum support.
A robust, enum-aware state machine for Laravel Eloquent models
A database-backed alternative to Horizon & Laravel Queues — durable jobs, sophisticated batches, high observability & scheduling that survive worker and host crashes, with no Redis to operate.
A simple generic library to implement FSMs (Finite-State Machines) and the State Design Pattern in PHP.
A simple state machine to handle model transactions, based on a list of pre-defined rules, for Laravel.
统计信息
- 总下载量: 1.83k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 14
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-10-01