定制 pablopetr/laravel-state-machine 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

pablopetr/laravel-state-machine

最新稳定版本:v1.0.0

Composer 安装命令:

composer require pablopetr/laravel-state-machine

包简介

Laravel-friendly state machine with class-based transitions and attribute binding

README 文档

README

Laravel State Machine is a package that provides a elegant and easy way to create State Machines.

Installation

composer require laravel/state-machine

Usage

To use the State Machine, you need to create a state machine class that extends the StateMachine class. You can define your states and transitions in this class.

use Laravel\StateMachine\StateMachine;

class OrderStateMachine extends StateMachine
{
    public static function field(): string
    {
        return 'state';
    }

    public static function initialState(): string
    {
        return 'pending';
    }

    protected function states(): array
    {
        return [
            'pending',
            'processing',
            'shipped',
            'delivered',
            'cancelled',
        ];
    }

    protected function transitions(): array
    {
        return [
            'process' => ['from' => 'pending', 'to' => 'processing'],
            'ship' => ['from' => 'processing', 'to' => 'shipped'],
            'deliver' => ['from' => 'shipped', 'to' => 'delivered'],
            'cancel' => ['from' => ['pending', 'processing'], 'to' => 'cancelled'],
        ];
    }
    
    public static function callbacks(): array
    {
        return [];
    }
     
    public static function guessModel(): string
    {
        return Order::class;
    }
}

You can then use this state machine in your application to manage the state of your orders.

$orderStateMachine = new OrderStateMachine();
$orderStateMachine->setState('pending');
$orderStateMachine->process(); // Changes state to 'processing'
$orderStateMachine->ship(); // Changes state to 'shipped'
$orderStateMachine->deliver(); // Changes state to 'delivered'

You can also check if a transition is allowed:

$orderStateMachine = new OrderStateMachine();
$orderStateMachine->setState('pending');
$isAllowed = $orderStateMachine->can('process'); // Returns true
$isAllowed = $orderStateMachine->can('ship'); // Returns false
$isAllowed = $orderStateMachine->can('deliver'); // Returns false
$isAllowed = $orderStateMachine->can('cancel'); // Returns true

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-06-27