msnisha/laraflow

Laravel Eloquent 模型工作流包。

v0.16 2019-04-26 18:46 UTC

This package is auto-updated.

Last update: 2024-09-27 06:54:23 UTC


README

分支以更改支持的 Laravel 版本,希望为该包带来更多功能

Laraflow 工作流包

Laraflow 是 Laravel Eloquent 对象的标准工作流包。您可以定义步骤、步骤之间的转换、回调和验证器。

StyleCI

安装(通过 composer)

您可以通过 composer 安装此包。该包需要 Laravel 5.5 或更高版本

composer require msnisha/Laraflow

您需要创建必要的表以存储历史数据

php artisan migrate

在 Laravel 5.5 之后,您不需要手动将服务提供者添加到 config/app.php。

配置数组

在您使用 Laraflow 工作流之前,需要配置一个数组。例如

[
    'property_path' => 'last_step',
    'steps' => [
        [
            'text' => 'Open',
            'extra' => []
        ],
        [
            'text' => 'In Progress',
            'extra' => []
        ],
        [
            'text' => 'Resolved',
            'extra' => []
        ],
        [
            'text' => 'Reopen',
            'extra' => []
        ],
        [
            'text' => 'Closed',
            'extra' => []
        ],
    ],
    'transitions' => [
        [
            'from' =>  0,
            'to' => 1,
            'text' => 'Start Progress',
            'extra' => [],
            'callbacks' => [
                'pre' => [
                    'App\\TestPreCallback'
                ],
                'post' => [
                    'App\\TestPostCallback'
                ]
            ],
            'validators' => [
                [
                    'title' => 'numeric',
                    'assignee_id' => 'required'
                ]
            ]
        ],
        [
            'from' => 1,
            'to' => 0,
            'text' => 'Stop Progress',
            'extra' => [],
            'callbacks' => [
                'pre' => [],
                'post' => []
            ],
            'validators' => []
        ],
        [
            'from' => 1,
            'to' =>  2,
            'text' => 'Resolve Issue',
            'extra' => [],
            'callbacks' => [
                'pre' => [],
                'post' => []
            ],
            'validators' => []
        ],
        [
            'from' => 2,
            'to' =>  3,
            'text' => 'Reopen Issue',
            'extra' => [],
            'callbacks' => [
                'pre' => [],
                'post' => []
            ],
            'validators' => []
        ],
        [
            'from' => 3,
            'to' =>  2,
            'text' => 'Resolve Issue',
            'extra' => [
                'fromPort' => 'R',
                'toPort' => 'R',
                'points' => []
            ],
            'callbacks' => [
                'pre' => [],
                'post' => []
            ],
            'validators' => []
        ],
        [
            'from' => 1,
            'to' =>  4,
            'text' => 'Close Issue',
            'extra' => [],
            'callbacks' => [
                'pre' => [],
                'post' => []
            ],
            'validators' => []
        ],
        [
            'from' => 3,
            'to' =>  4,
            'text' => 'Close Issue',
            'extra' => [],
            'callbacks' => [
                'pre' => [],
                'post' => []
            ],
            'validators' => []
        ],
    ],

此配置包含一个示例工作流。每个步骤都有一个文本,您可以使用它来显示它们。所有其他必要的属性都必须添加到额外数组中。
property_path 包含存储记录实际步骤的表中的列名。

用法

步骤 1

首先,您需要将新列添加到您的 Eloquent 模型表中,例如称为:last_step/status 或您想要的任何名称。

 $table->string('last_state');

在您的配置文件中,property_path 属性必须与列名相同。

步骤 2

您必须将 Flowable 特性添加到您的 Eloquent 模型以使用工作流。

use szana8\Laraflow\Traits\Flowable;

class SampleClass extends Model {

    use Flowable;
    
步骤 3

然后,您必须添加一个名为 getLaraflowStates() 的函数到您的 Eloquent 模型。
此函数必须返回配置的数组!

步骤 4

如果您想更改 Eloquent 对象的状态,可以使用

$object->transiton($new_status);
$object->save();

来自 Flowable 特性的方法。$new_status 参数是来自 getPossibleTransitions() 函数的 key 属性的值。

事件

您可以监听在每次状态更改时都会触发的 '全局' 事件。

LaraflowEvents::PRE_TRANSITION
LaraflowEvents::POST_TRANSITION
LaraflowEvents::CAN_TRANSITION

PRE_TRANSITION 在状态更改之前触发,POST_TRANSITION 在状态更改后触发。当包检查从实际步骤转换是否可能时,会触发 CAN_TRANSITION。您可以为指定的转换定义回调。

验证器

该包附带一个默认的验证器类,它使用 Laravel 验证器类。您可以在转换中添加验证规则,以便在转换之前,包可以使用给定的规则检查 Eloquent 对象的属性。如果验证失败,则抛出包含错误消息数组的 LaraflowValidatorException 异常。

如果您创建了一个实现 LaraflowValidatorInterface 的类,则可以定义自己的验证器。

致谢

此库高度受 https://github.com/winzou/state-machine 的启发。

许可

该包是开源软件,许可协议为 MIT 许可