ext-rgonzalez /laraflow
Laravel Eloquent模型的工作流包。
Requires
- php: ^5.6.4 || ^8.1
- illuminate/support: ^v10.15.0
Requires (Dev)
- mockery/mockery: ^0.9.6
- phpunit/phpunit: ^6.0 || ^7.0 || ^8.0 || ^9.0
README
Laraflow是Laravel Eloquent对象的标准工作流包。您可以定义步骤、步骤之间的转换、回调和验证器。
安装(通过composer)
您可以通过composer安装此包。该包需要Laravel 5.5或更高版本
composer require ext-rgonzalez/Laraflow
您需要创建必要的表以存储历史数据
php artisan migrate
从Laravel 5.5开始,您不需要手动将服务提供者添加到config/app.php。
配置文件
安装后,您必须发布一个配置文件,其中包含许多必要的包数据。
php artisan vendor:publish --provider="ext-rgonzalez\Laraflow\LaraflowServiceProvider"
配置数组
在使用Laraflow工作流之前,您需要一个配置数组。
使用方法
步骤1
首先,您需要向您的Eloquent模型表添加一个新列,例如:last_step/status或您想要的任何名称。
$table->string('status');
在您的配置文件中,property_path属性必须与列名相同。
步骤2
您必须将Flowable特质添加到您的Eloquent模型中,以使用工作流。
use laraflow\Traits\Flowable; class SampleClass extends Model { use Flowable;
步骤3
然后,您必须向Eloquent模型添加一个名为getLaraflowStates()的函数。
此函数必须返回配置数组!
当您在模型中仅使用1个状态机时,配置数组必须具有以下形式
[
'default' => [
//... see published configuration example in config directory.
]
];
当您在模型中具有多个状态机时,请使用以下形式
[
'statemachine_name1' => [ // might also be called 'default' !
// ... see published configuration example in config directory
],
'statemachine_name2' => [
// ... see published configuration example in config directory
],
]
步骤4
如果您想更改Eloquent对象的状态,可以使用来自Flowable特质的
// when using only 1 or at least 'default' named state machine. $object->transiton($new_status); // when using only 1 not named state machine. $statemachineName1 = 'statemachine_name1' $object->transiton($new_status, $statemachineName1); $statemachineName2 = 'default' $object->transiton($new_status, $statemachineName2); $object->save();
方法。$new_status参数是来自getPossibleTransitions()函数的key属性值。
历史记录
您可以通过在模型中调用history函数来查询记录的历史记录,如下所示
/** * Return historical records. * * @return string */ public function getFlowHistoryAttribute() { return $this->history(); }
事件
您可以监听在每次状态更改时触发的'全局'事件。
LaraflowEvents::PRE_TRANSITION LaraflowEvents::POST_TRANSITION LaraflowEvents::CAN_TRANSITION
PRE_TRANSITION在状态更改之前触发,POST_TRANSITION在状态更改之后触发。当包检查从当前步骤转换是否可能时,将触发CAN_TRANSITION。您可以定义在指定的转换中将被调用的回调函数。
验证器
该包附带了一个默认的验证器类,它使用Laravel Validator类。您可以将验证规则添加到转换中,以便在转换之前,包可以检查Eloquent对象的属性是否符合给定的规则。如果验证失败,则抛出带有错误消息数组的LaraflowValidatorException异常。
如果您创建了一个实现了LaraflowValidatorInterface的类,您可以定义自己的验证器。
class TestValidator extends Rule implements LaraflowValidatorInterface { /** * Validate the attributes with the given rules. * * @param array $attributes * @param array $rules * @return mixed */ public function validate(array $attributes, array $rules) { if (1 == 2) { return true; } throw LaraflowValidatorException::withMessages(['Validation error']); } }
命令
订阅者生成
您可以使用Artisan命令创建默认Laraflow事件的订阅者。
foo@bar: php artian laraflow:make subscriber --NameOfTheSubscriber
运行后,您可以在App\Listener目录中找到新的订阅者类。要创建回调类,您必须创建一个实现了LaraflowCallbackInterface的类,并将其添加到订阅者中的必要事件。
示例
class TestPreCallback implements LaraflowCallbackInterface { public function handle(LaraflowTransitionEvents $event) { CallbackTest::insert(['message' => json_encode($event->convertToArray())]); } } class LaraflowEventSubscriber { /** * Register the listeners for the subscriber. * * @param \Illuminate\Events\Dispatcher $events */ public function subscribe($events) { $events->listen( LaraflowEvents::PRE_TRANSITION, 'App\TestPreCallback@handle' ); } }
自定义验证器生成
要为自定义验证生成验证器类骨架,请使用此命令
foo@bar: php artian laraflow:make validator --NameOfTheValidator
运行后,您可以在App\Validators目录中找到新类。
致谢
此库受到了https://github.com/winzou/state-machine的高度启发。
许可证
该包是开源软件,许可协议为MIT许可证。