jobmetric/laravel-state-machine 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jobmetric/laravel-state-machine

Composer 安装命令:

composer require jobmetric/laravel-state-machine

包简介

This package is for implementing the Eloquent State Machine of various Laravel projects.

README 文档

README

This package is for implementing the Eloquent State Machine of various Laravel projects.

Install via composer

Run the following command to pull in the latest version:

composer require jobmetric/laravel-state-machine

Documentation

When it comes to changing the state of a model field, and we want to change it and then do other things or more clearly react to another action, you can use this package.

Usage

1. Suppose you have an order model with a status field.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Order extends Model
{
    /**
     * @var array
     */
    protected $fillable = [
        'status',
    ];

    /**
     * @var array
     */
    protected $casts = [
        'status' => 'string',
    ];
}

2. Now you need to add a trait called HasStateMachine to the model.

<?php

namespace App\StateMachine;

use Illuminate\Database\Eloquent\Model;
use JobMetric\StateMachine\HasStateMachine;

class OrderStateMachine extends Model
{
    use HasStateMachine;
    
    ...
}

3. When this attribute is added to the model, you will have to add the StateMachineContract interface to the model

<?php

namespace App\StateMachine;

use Illuminate\Database\Eloquent\Model;
use JobMetric\StateMachine\Contracts\StateMachineContract;
use JobMetric\StateMachine\HasStateMachine;

class OrderStateMachine extends Model implements StateMachineContract
{
    use HasStateMachine;
    
    ...
}

4. Now you will again have to use the stateMachineAllowTransition function in the model.

<?php

namespace App\StateMachine;

use Illuminate\Database\Eloquent\Model;
use JobMetric\StateMachine\Contracts\StateMachineContract;
use JobMetric\StateMachine\HasStateMachine;

class OrderStateMachine extends Model implements StateMachineContract
{
    use HasStateMachine;
    
    ...
    
    public function stateMachineAllowTransition(): void
    {
        $this->allowTransition('status', 'pending', 'processing');
        $this->allowTransition('status', 'processing', 'completed');
    }
}

Note: The stateMachineAllowTransition function is used to define the transition of the model field. In the above example, the status field can be changed from pending to processing and from processing to completed.

Why did we do these things?

By doing this, you are defining the states in which status wants to move.

Starting now, manual state manipulation is no longer permitted. Instead, you must navigate between state components using the functions provided by StateMachine. It will handle the update process and a sequence of additional tasks, which will be elaborated upon shortly.

$order = Order::find(1);

$order->status = 'processing';

$order->save();

Note: The above code will not work. Because the status field can only be changed from pending to processing and from processing to completed.

If you want to change the status field from pending to processing, you must use the following code.

$order = Order::find(1);

$order->transitionTo('processing');

Note: If it was a field other than status, you can use the second parameter for the name of that field.

Let's go to the amazing part of our story

When a transitionTo occurs, you can have an action for that event that will be executed automatically if there is one.

Let's go to the actions

To define a StateMachine, the following method must be executed:

php artisan make:state-machine {model} {?state} -f={field}

model: The name of the model you want to create a StateMachine for.

  • The model must be available in the system

state: The name of the state you want to create a StateMachine for.

  • This part should be written like this, for example PendingToProcessing and the word To must be between two situations.

  • If this field is not filled, a status called Common will be created, which will be explained below.

field: The name of the field you want to create a StateMachine for.

  • This is the default option on the status field, and if you want to define another field, use this

When you run this command, a file is created inside app/StateMachines

In the created file, you have two methods, before and after, which tells you that you want to do it before changing the field in the database or after changing the field in the database.

In each of them, you can use different tasks such as sending e-mail or many other things.

The difference between Common and detailed files

The difference between the two Common files and PendingToProcessing mode, for example, is in their execution

The common file is executed for all conditions, but the exact state file is executed only for that specific mode, and the form of their execution is as follows

$common->before();
$exact->before();
// state change
$exact->after();
$common->after();

License

The MIT License (MIT). Please see License File for more information.

jobmetric/laravel-state-machine 适用场景与选型建议

jobmetric/laravel-state-machine 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 6, 最近一次更新时间为 2024 年 01 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「package」 「model」 「state machine」 「laravel」 「eloquent」 「state management」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 jobmetric/laravel-state-machine 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 jobmetric/laravel-state-machine 我们能提供哪些服务?
定制开发 / 二次开发

基于 jobmetric/laravel-state-machine 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 5
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 15
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-01-01