承接 willdurand/propel-statemachine-behavior 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

willdurand/propel-statemachine-behavior

最新稳定版本:1.0.0

Composer 安装命令:

composer require willdurand/propel-statemachine-behavior

包简介

Adds a finite state machine to your model.

README 文档

README

Build Status

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 many transition parameters 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_DRAFT
  • ObjectModel::STATE_REJECTED
  • ObjectModel::STATE_UNPUBLISHED
  • ObjectModel::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:

  • draft to published
  • published to unpublished
  • unpublished to published

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 deleted state;
  • You cannot use the save, or delete symbols.

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 willdurand/propel-statemachine-behavior 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 25.14k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 39
  • 点击次数: 29
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 39
  • Watchers: 4
  • Forks: 9
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2012-05-14