ringierimu/state-workflow
Composer 安装命令:
composer require ringierimu/state-workflow
包简介
Laravel State Workflow provide tools for defining and managing workflows and activities with ease.
README 文档
README
Laravel State workflow provide tools for defining and managing workflows and activities with ease. It offers an object oriented way to define a process or a life cycle that your object goes through. Each step or stage in the process is called a state. You do also define transitions that describe the action to get from one state to another.
A workflow consist of state and actions to get from one state to another. These actions are called transitions which describes how to get from one state to another.
Requirements
| Version | PHP | Laravel |
|---|---|---|
| 5.x | 8.3+ | 11, 12 |
| 4.x | 8.1+ | 10, 11 |
Installation
Requires PHP 8.3+ and Laravel 11+.
composer require ringierimu/state-workflow
Publish config/workflow.php file
php artisan vendor:publish --tag="state-workflow-config"
Publish migration
php artisan vendor:publish --tag="state-workflow-migration"
Run migration
php artisan migrate
Configuration
- Open
config/workflow.phpand configure it
// this should be your model name in camelcase. eg. PropertyListing::Class => propertyListing 'post' => [ // class of your domain object 'class' => \App\Post::class, // Register subscriber for this workflow which contains business rules. Uncomment line below to register subscriber //'subscriber' => \App\Listeners\UserEventSubscriber::class, // property of your object holding the actual state (default is "current_state") //'property_path' => 'current_state', //uncomment this line to override default value // list of all possible states 'states' => [ 'new', 'pending_activation', 'activated', 'deleted', 'blocked' ], // list of all possible transitions 'transitions' => [ 'create' => [ 'from' => ['new'], 'to' => 'pending_activation', ], 'activate' => [ 'from' => ['pending_activation'], 'to' => 'activated', ], 'block' => [ 'from' => ['pending_activation', 'activated'], 'to' => 'blocked' ], 'delete' => [ 'from' => ['pending_activation', 'activated', 'blocked'], 'to' => 'deleted', ], ], ],
- Add
HasWorkflowTraitto your model class to support workflow
<?php namespace App; use Illuminate\Database\Eloquent\Model; use Ringierimu\StateWorkflow\Traits\HasWorkflowTrait; /** * Class Post * @package App */ class Post extends Model { use HasWorkflowTrait; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ ..., 'current_state', // If not using default attribute, update this to match value in workflow.php ] }
Usage
Manage State/Workflow
<?php use App\Post; $post = new Post(); //Apply transition $post->applyTransition("create"); $post = $post->refresh(); //Return current_state value $post->state(); //pending_activation //Check if this transition is allowed $post->canTransition("activate"); // True //Return Model state history $post->stateHistory();
Authenticated User Resolver
Ability to audit and track who action a specific state change for your object. The package leverage the default Laravel auth provider to resolve the authenticated user when applying the state changes.
For a custom authentication mechanism, you should override authenticatedUserId in your object class with your own implementation.
/** * Return authenticated user id. * * @return int|null */ public function authenticatedUserId() { // Implement authenticated user resolver }
Fired Event
Each step has three events that are fired in order:
- An event for every workflow
- An event for the workflow concerned
- An event for the workflow concerned with the specific transition or state name
During state/workflow transition, the following events are fired in the following order:
- Validate whether the transition is allowed at all.
Their event listeners are invoked every time a call to
workflow()->can(),workflow()->apply()orworkflow()->getEnabledTransitions()is executed.Guard Event
workflow.guard workflow.[workflow name].guard workflow.[workflow name].guard.[transition name]
- The subject is about to leave a state.
Leave Event
workflow.leave workflow.[workflow name].leave workflow.[workflow name].leave.[state name]
- The subject is going through this transition.
Transition Event
workflow.transition workflow.[workflow name].transition workflow.[workflow name].transition.[transition name]
- The subject is about to enter a new state.
This event is triggered just before the subject states are updated.
Enter Event
workflow.enter workflow.[workflow name].enter workflow.[workflow name].enter.[state name]
- The subject has entered in the states and is updated.
Entered Event
workflow.entered workflow.[workflow name].entered workflow.[workflow name].entered.[state name]
- The subject has completed this transition.
Completed Event
workflow.completed workflow.[workflow name].completed workflow.[workflow name].completed.[transition name]
Subscriber
Create subscriber class to listen to those events and the class should extends WorkflowSubscriberHandler.
To register method to listen to specific even within subscriber use the following format for method name:
- on[Event] -
onGuard() - on[Event][Transition/State name] -
onGuardActivate()
NB:
- Method name must start with
onkey word otherwise it will be ignored. Subscriberclass must be register insideworkflow.phpconfig file with the appropriate workflow configuration.Subscriberclass must extendsWorkflowSubscriberHandler.Guard,TransitionandCompletedEvent uses of transition name.Leave,EnterandEnteredEvent uses state name.
<?php namespace App\Listeners; use Ringierimu\StateWorkflow\Events\EnteredEvent; use Ringierimu\StateWorkflow\Events\EnterEvent; use Ringierimu\StateWorkflow\Events\GuardEvent; use Ringierimu\StateWorkflow\Events\LeaveEvent; use Ringierimu\StateWorkflow\Events\TransitionEvent; use Ringierimu\StateWorkflow\Subscribers\WorkflowSubscriberHandler; /** * Class PostEventSubscriber * @package App\Listeners */ class UserEventSubscriber extends WorkflowSubscriberHandler { /** * Handle workflow guard events. * * @param GuardEvent $event */ public function onGuardActivate($event) { $user = $event->getOriginalEvent()->getSubject(); if (empty($user->dob)) { // Users with no dob should not be allowed $event->getOriginalEvent()->setBlocked(true); } } /** * Handle workflow leave event. * * @param LeaveEvent $event */ public function onLeavePendingActivation($event) { } /** * Handle workflow transition event. * * @param TransitionEvent $event */ public function onTransitionActivate($event) { } /** * Handle workflow enter event. * * @param EnterEvent $event */ public function onEnterActivated($event) { } /** * Handle workflow entered event. * * @param EnteredEvent $event */ public function onEnteredActivated($event) { } }
Event Methods
Each workflow event has an instance of Event. This means that each event has access to the following information:
getOriginalEvent(): Returns the Parent Event that dispatched the event which has the following children methods:getSubject(): Returns the object that dispatches the event.getTransition(): Returns the Transition that dispatches the event.getWorkflowName(): Returns a string with the name of the workflow that triggered the event.isBlocked(): Returns true/false if transition is blocked.setBlocked(): Sets the blocked value.
Artisan Command
Symfony workflow uses GraphvizDumper to create the workflow image by using the dot command.
The dot command is part of Graphviz.
You will be required to download dot command to make use of this command.
https://graphviz.gitlab.io/download/
Usage
php artisan workflow:dump workflow_name
Run Unit Test
composer test
Credits
ringierimu/state-workflow 适用场景与选型建议
ringierimu/state-workflow 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 70.18k 次下载、GitHub Stars 达 32, 最近一次更新时间为 2018 年 12 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「workflow」 「state machine」 「laravel」 「state management」 「state workflow」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ringierimu/state-workflow 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ringierimu/state-workflow 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ringierimu/state-workflow 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Workflow for NetCommons Plugin
Doctrine implementation of the MetaborStd (Statemachine) for PHP 8.2+
A simple state dropdown field for SilverStripe forms
Workflow logger
A state Machine library for business processes
Metamel Addresses is a polymorphic Laravel package, for address book management. You can add addresses to any eloquent model with ease.
统计信息
- 总下载量: 70.18k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 34
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-12-07