raoul2000/yii2-workflow
Composer 安装命令:
composer require raoul2000/yii2-workflow
包简介
A simple workflow engine for Yii2
README 文档
README
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist raoul2000/yii2-workflow "*"
or add
"raoul2000/yii2-workflow": "*"
to the require section of your composer.json file.
Quick Start
Configuration
For this "Quick start Guide" we will be using default configuration settings, but remember that yii2-workflow is designed to be highly flexible so to adapt to a lot of execution contexts... well at least that was my goal.
Create A Workflow
A workflow is defined as a PHP class that implements the \raoul2000\workflow\source\file\IWorkflowDefinitionProvider interface. which
declares the getDefinition() method. This method must return an array representing the workflow definition.
Let's define a very simple workflow that will be used to manage posts in a basic blog system.
Here is the PHP class that implements the definition for our workflow :
@app/models/PostWorkflow.php
namespace app\models; class PostWorkflow implements \raoul2000\workflow\source\file\IWorkflowDefinitionProvider { public function getDefinition() { return [ 'initialStatusId' => 'draft', 'status' => [ 'draft' => [ 'transition' => ['publish','deleted'] ], 'publish' => [ 'transition' => ['draft','deleted'] ], 'deleted' => [ 'transition' => ['draft'] ] ] ]; } }
Attach To The Model
Now let's have a look to our Post model: we store the status of a post in a column named status of type STRING(40).
The last step is to associate the workflow definition with posts models. To do so we must declare the SimpleWorkflowBehavior behavior in the Post model class and let the default configuration settings do the rest.
@app/models/Post.php
namespace app\models; /** * @property integer $id * @property string $title * @property string $body * @property string $status column used to store the status of the post */ class Post extends \yii\db\ActiveRecord { public function behaviors() { return [ \raoul2000\workflow\base\SimpleWorkflowBehavior::className() ]; } // ...
That's it ! We are ready to play with SimpleWorkflowBehavior.
Use It !
Now that we are all setup, we can use the SimpleWorkflowBehavior methods to set/get the status of our posts : the SimpleWorkflowBehavior will take care that the post doesn't reach a status where it is not supposed to go to, depending on the workflow definition that we have provided.
$post = new Post(); $post->status = 'draft'; $post->save(); echo 'post status is : '. $post->workflowStatus->label;
This will print the following message :
post status is : Draft
If you do the same thing but instead of draft set the status to publish and try to save it, the following exception is thrown :
Not an initial status : PostWorkflow/publish ("PostWorkflow/draft" expected)
That's because in your workflow definition the initial status is set to draft and not publish.
Ok, one more example for the fun ! This time we are not going to perform the transition when the Post is saved (like we did in the previous
example), but immediately, by invoking the sendToStatus method. Our Post is going to try to reach status publish passing through deleted
which is strictly forbidden by the workflow. Will it be successful in this risky attempt to break workflow rules ?
$post = new Post(); $post->sendToStatus('draft'); $post->sendToStatus('deleted'); $post->sendToStatus('publish'); // danger zone !
Game Over ! There is no transition between deleted and publish, and that's what SimpleWorkflow tries to explain to our fearless post object.
Workflow Exception – raoul2000\workflow\base\WorkflowException
No transition found between status PostWorkflow/deleted and PostWorkflow/publish
Yes, that's severe, but there was many ways to avoid this exception like for instance by first validating that the transition was possible.
What's Next ?
This is just one way of using the SimpleWorkflowBehavior but there's much more and hopefully enough to assist you in workflow management inside your Yii2 web app.
You will find additional information there :
- yii2-workflow Usage Guide
- yii2-workflow Class Reference
- Demo : a simple example based on the Post use case.
You may also be interested in the following projects developed around yii2-workflow :
- yii2-workflow-view : A Widget to display workflows (demo)
- yii2-workflow-manager : A Module to manage workflows
- yii2-wizflow : a proof of concept that mixes the Wizard UI pattern with workflow (Demo)
- ..and more to come
License
yii2-workflow is released under the BSD 3-Clause License. See the bundled LICENSE.md for details.
raoul2000/yii2-workflow 适用场景与选型建议
raoul2000/yii2-workflow 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 92.37k 次下载、GitHub Stars 达 171, 最近一次更新时间为 2015 年 02 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「workflow」 「yii2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 raoul2000/yii2-workflow 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 raoul2000/yii2-workflow 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 raoul2000/yii2-workflow 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Workflow for NetCommons Plugin
A custom URL rule class for Yii 2 which allows to create translated URL rules
Workflow logger
Approval Workflow Engine for Filament
Operational approvals engine for Laravel applications.
SCEditor, a lightweight WYSIWYG BBCode & HTML editor extension for Yii 2.0 Framework
统计信息
- 总下载量: 92.37k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 173
- 点击次数: 12
- 依赖项目数: 34
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2015-02-25
