datomatic/laravel-enum-state-machine
Composer 安装命令:
composer require datomatic/laravel-enum-state-machine
包简介
A simple state machine for enums in Laravel
README 文档
README
Laravel enum state machine
This package it's simple state transitions control for enums in Laravel, this is not an implementation of state machine pattern. Allowing you to prevent unlogically transition and also controlling the initial state of the enum fields on your models.
Installation
Laravel 10+ and PHP 8.2+ are required.
You can install the package via composer:
composer require datomatic/laravel-enum-state-machine
You can publish and run the migrations with:
php artisan vendor:publish --tag="enum-state-machine-migrations"
php artisan migrate
You can publish the config file with:
php artisan vendor:publish --tag="enum-state-machine-config"
This is the contents of the published config file:
return [ /* |-------------------------------------------------------------------------- | Soft Mode Configuration |-------------------------------------------------------------------------- | | The 'soft_mode' configuration allows for handling errors without | interrupting the application's execution. When this option is enabled, | no exceptions are thrown during state transitions and logged instead. | This helps prevent unexpected crashes, ensuring | greater application resilience, especially in scenarios where a failure | in the state machine should not disrupt the main program flow. | You can configure this modality for each model casting | */ 'soft_mode' => env('LARAVEL_ENUM_STATE_MACHINE_SOFT_MODE', false), ];
Using Laravel IDE Helper?
If you are using Laravel IDE Helper, you need to run the following command:
php artisan vendor:publish --tag="enum-state-machine-ide-helper-hooks"
and add LaravelEnumStateMachineModelIdeHelperHook::class on model_hooks array in config/ide-helper.php
'model_hooks' => [
...,
LaravelEnumStateMachineModelIdeHelperHook::class,
],
Usage
Laravel enum state machine it's a simple state transitions control for enums in Laravel, this is not an implementation of state machine pattern.
In the default mode, if the transition is not allowed, an exception StatusTransitionDenied will be thrown.
In the soft mode, if the transition is not allowed, an error message will be logged.
Setting the model
You need to define the casts in your model and the transition control function. The first param on the casting is the enum class and the optional second param is the soft mode modality (if no second param is passed, the default mode configured in config file is used). You can cast multiple fields if needed. The transition method name is composed by the enum field name (camelCase) + Transitions and serve to define whether a transition is allowed or not.
class TestModel extends Model { //Laravel 10 protected $casts = [ 'status' => AsEnumStateMachine::class.':'.StatusEnum::class, // ',true' for soft mode ]; //Laravel 11 protected function casts(): array { return [ 'status' => AsEnumStateMachine::of(FieldEnum::class, false), ]; } /** * This method name is composed by the enum field name (camelCase) + Transitions */ public function statusTransitions(?StatusEnum $from, ?StatusEnum $to): bool { return match ($from) { null => true, // initial state permitted to all states StatusEnum::PUBLIC => match ($to) { StatusEnum::PRIVATE => true, StatusEnum::PROTECTED => true, null => false, default => false }, StatusEnum::PROTECTED => match ($to) { StatusEnum::PRIVATE => true, StatusEnum::PUBLIC => false, default => false }, StatusEnum::PRIVATE => false, //final state default => true }; }
Use the model
$model = new TestModel; $model->status = StatusEnum::PUBLIC; // OK $model->save(); $model = TestModel::find(1); $model->status; // StatusEnum::PUBLIC $model->status = StatusEnum::PRIVATE; // OK $model->status = StatusEnum::PUBLIC; // thrown `StatusTransitionDenied`
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
datomatic/laravel-enum-state-machine 适用场景与选型建议
datomatic/laravel-enum-state-machine 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.46k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 11 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「Datomatic」 「laravel-enum-state-machine」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 datomatic/laravel-enum-state-machine 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 datomatic/laravel-enum-state-machine 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 datomatic/laravel-enum-state-machine 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel wrapper for Fatture in Cloud API v2
Un pacchetto per poter integrare la Carta del Docente in un progetto PHP
A package to save a collection of Enums in a Eloquent field and interact with him
Laravel PHP Soap wrapper of Carta del Docente
Alfabank REST API integration
Use spatie/opening-hours package and save it on database to query it
统计信息
- 总下载量: 1.46k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-11-18

