willdurand/propel-statemachine-behavior
最新稳定版本:1.0.0
Composer 安装命令:
composer require willdurand/propel-statemachine-behavior
包简介
Adds a finite state machine to your model.
README 文档
README
This behavior adds a finite state machine to your model.
Configuration
<behavior name="state_machine"> <parameter name="states" value="draft, rejected, unpublished, published" /> <parameter name="initial_state" value="draft" /> <parameter name="transition" value="draft to published with publish" /> <parameter name="transition" value="draft to rejected with reject" /> <parameter name="transition" value="published to unpublished with unpublish" /> <parameter name="transition" value="unpublished to published with publish" /> <!-- Optional parameters --> <parameter name="state_column" value="state" /> <parameter name="timestampable" value="true" /> </behavior>
The state_machine behavior requires three parameters to work:
states: a finite set of states as comma separated values;initial_state: the initial state, part of set of states;transition: a set of transitions. As you can see, you can add as manytransitionparameters as you want.
Each transition has to follow this pattern:
STATE_1 to STATE_2 with SYMBOL
A symbol, which is part of the Finite State Machine's terminology, can be considered as an event triggered
on your model object.
The timestampable option can be added to log in the_given_state_at column the date when the state is setted
### ActiveRecord API ###
The behavior will generate the following constants which represent the available states of your object:
ObjectModel::STATE_DRAFTObjectModel::STATE_REJECTEDObjectModel::STATE_UNPUBLISHEDObjectModel::STATE_PUBLISHED
You can get the current state of your object:
getState()
Or get an array with all available states:
getAvailableStates()
Most of the time, you would like to display these states. Thanks to two convenient methods, it's really easy:
getHumanizedState() // 'Draft', or 'Rejected', or 'Unpublished', or 'Published'
ObjectModel::getHumanizedStates()
// array(
// 0 => 'Draft',
// 1 => 'Rejected',
// 2 => 'Unpublished',
// 3 => 'Published',
// )
The behavior will also generate a set of issers:
isDraft()
isRejected()
isPublished()
isUnpublished()
But the most interesting part is the implemenation of the FSM itself. First you have methods to determine whether you can perform, or not a transition based on the current model's state:
canPublish()
canReject()
canUnpublish()
It will also generate a set of methods for each symbol:
publish(PropelPDO $con = null)
unpublish(PropelPDO $con = null)
reject(PropelPDO $con = null)
To handle custom logic, new hooks are created. The methods below should return a boolean value, and can act as guards (which is not part of the FSM's terminology).
prePublish(PropelPDO $con = null)
preUnpublish(PropelPDO $con = null)
preReject(PropelPDO $con = null)
The methods below should contain your own logic depending on each state, and your business.
onPublish(PropelPDO $con = null)
onUnpublish(PropelPDO $con = null)
onReject(PropelPDO $con = null)
The methods below allow to execute code once the transition is executed.
postPublish(PropelPDO $con = null)
postUnpublish(PropelPDO $con = null)
postReject(PropelPDO $con = null)
ActiveQuery API
To be defined.
Usage
Let's say we have a Post model class which represents an entry in a blog engine.
When we create a new post, its initial state is draft because we don't want to publish
it immediately.
As a draft, you can decide to publish your new post. Its state is now published.
Once published, you may want to unpublish it for some reasons. Then, its state is unpublished.
The last possibily is to republish an unpublished post. The new state is published.
We have three different states (draft, published, unpublished), and three transitions:
drafttopublishedpublishedtounpublishedunpublishedtopublished
We can define the following configuration:
<table name="post"> <!-- some columns --> <behavior name="state_machine"> <parameter name="states" value="draft, unpublished, published" /> <parameter name="initial_state" value="draft" /> <parameter name="transition" value="draft to published with publish" /> <parameter name="transition" value="published to unpublished with unpublish" /> <parameter name="transition" value="unpublished to published with publish" /> </behavior> </table>
Here is a workflow:
<?php $post = new Post(); $post->getState(); // Post::STATE_DRAFT $post->getAvailableStates(); // Post::STATE_DRAFT, Post::STATE_UNPUBLISHED, Post::STATE_PUBLISHED $post->isDraft(); // true $post->isPublished(); // false $post->isUnpublished(); // false $post->canPublish(); // true $post->canUnpublish(); // false $post->unpublish(); // throw a LogicException, no transition found from draft to unpublished // Let's publish this post // This is the first transition in the scenario above $post->publish()->save(); $post->isDraft(); // false $post->isPublished(); // true $post->isUnpublished(); // false $post->canPublish(); // false $post->canUnpublish(); // true $post->publish(); // throw a LogicException, the post is already published // Let's unpublish this post // This is the second transition in the scenario above $post->unpublish()->save(); $post->isDraft(); // false $post->isPublished(); // false $post->isUnpublished(); // true $post->canPublish(); // true $post->canUnpublish(); // false $post->unpublish(); // throw a LogicException, the post is already unpublished // Let's (re)publish this post // This is the last transition in the scenario above $post->publish()->save(); $post->isDraft(); // false $post->isPublished(); // true $post->isUnpublished(); // false $post->canPublish(); // false $post->canUnpublish(); // true
Now imagine we have authors linked to each post, and once a post is published, we notify the post's author by email. Thanks to new hooks, it's really easy to extend things:
<?php class Post extends BasePost { // Assuming we have a mail manager which is able to send emails, // and that we injected it before. private $mailManager; public function onPublish(PropelPDO $con = null) { $this->mailManager->postPublished( $this->getAuthor(), $this->getTitle() ); } }
Use case in a controller:
<?php class PostController extends Controller { public function newAction() { // handle a form, etc to create a new Post } public function publishAction(Post $post) { try { $post->publish()->save(); } catch (\LogicException $e) { // handle the exception as you wish } } public function unpublishAction(Post $post) { try { $post->unpublish()->save(); } catch (\LogicException $e) { // handle the exception as you wish } } }
Known Limitations
- You cannot use the
deletedstate; - You cannot use the
save, ordeletesymbols.
At the moment, there is no built-in solution to handle these cases.
Combining Archivable Behavior
The Archivable behavior is useful to copy a model object to an archival table. In other words, it acts as a soft delete behavior but with better performance.
In your workflow, you may want to destroy your object for some reason. I say
"destroy" because you can't use the deleted status, nor the delete symbol,
but it doesn't matter. Destroying an object is fine, but instead of hard
deleting it, you may want to soft delete it. That means you will rely on the
archivable behavior.
Just add it to your XML schema, rebuild both SQL, and model classes, and you're
done. At first glance, when you destroy your object, you will expect it
to be hidden, but it's not the case. It just has the destroyed state.
Just call the delete() termination method as usual, and your object will be
automatically archived. No much to do.
willdurand/propel-statemachine-behavior 适用场景与选型建议
willdurand/propel-statemachine-behavior 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 25.14k 次下载、GitHub Stars 达 39, 最近一次更新时间为 2012 年 05 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「propel」 「Behavior」 「state machine」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 willdurand/propel-statemachine-behavior 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 willdurand/propel-statemachine-behavior 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 willdurand/propel-statemachine-behavior 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A CakePHP plugin to provide easy access to zumba/swivel
Doctrine implementation of the MetaborStd (Statemachine) for PHP 8.2+
This is a set of Yii2 behaviors extensions
A simple state dropdown field for SilverStripe forms
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.
统计信息
- 总下载量: 25.14k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 39
- 点击次数: 29
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2012-05-14