定制 kphoen/doctrine-state-machine-bundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

kphoen/doctrine-state-machine-bundle

最新稳定版本:1.1.3

Composer 安装命令:

composer require kphoen/doctrine-state-machine-bundle

包简介

Integrates DoctrineStateMachineBehavior in Symfony2.

README 文档

README

Doctrine2 behavior adding a finite state machine in your entities.

The state machine implementation used is Finite.

Status

This project is DEPRECATED and should NOT be used.

If someone magically appears and wants to maintain this project, I'll gladly give access to this repository.

Configuration

In your app/config/config.yml file, define your state machines:

k_phoen_doctrine_state_machine:
    auto_injection:     true    # should we automatically inject state machines into hydrated objects?
    auto_validation:    true    # should we validate any status change before the persistence happens?

    state_machines:
        article_state_machine:
            class:      \Acme\FooBundle\Entity\Article
            property:   state
            states:
                new:        {type: initial}
                reviewed:   ~
                accepted:   ~
                published:  {type: final, properties: {printable: true}}
                rejected:   {type: final}
            transitions:
                review:     {from: [new], to: reviewed}
                accept:     {from: [reviewed], to: accepted}
                publish:    {from: [accepted], to: published}
                reject:     {from: [new, reviewed, accepted, published], to: rejected}

The state machines configuration is pretty straightforward. In addition to the states and transitions, you just have to define the entity class and the column used to store the state.

Important: the entity has to implement the Stateful interface.

To ease the implementation, you can use the StatefulTrait that comes bundled with the behavior.

Usage

Stateful entities have access to their own state machine. See Finite's documentation for more details about it.

The Article entity below is ready to be used as a Stateful entity.

<?php

namespace Acme\FooBundle\Entity;

use KPhoen\DoctrineStateMachineBehavior\Entity\Stateful;
use KPhoen\DoctrineStateMachineBehavior\Entity\StatefulTrait;

class Article implements Stateful
{
    use StatefulTrait;

    /**
     * define your fields here
     */


    /**
     * @var string
     */
    protected $state = 'new';

    /**
     * Set state
     *
     * @param  string  $state
     * @return Article
     */
    public function setState($state)
    {
        $this->state = $state;

        return $this;
    }

    /**
     * Get state
     *
     * @return string
     */
    public function getState()
    {
        return $this->state;
    }

    /**
     * Sets the object state.
     * Used by the StateMachine behavior
     *
     * @return string
     */
    public function getFiniteState()
    {
        return $this->getState();
    }

    /**
     * Sets the object state.
     * Used by the StateMachine behavior
     *
     * @param string $state
     */
    public function setFiniteState($state)
    {
        return $this->setState($state);
    }
}

Entities using the StatefulTrait see the setStateMachine() and getStateMachine() methods implemented and gain access to the following methods:

  • can($transition): indicating if the given transition is allowed ;
  • and a few magic methods, based on the transition allowed by the state-machine:
    • {TransitionName}(): apply the transition {TransitionName} (ie: accept(), reject(), etc) ;
    • can{TransitionName}(): test if the transition {TransitionName} can be applied (ie: canAccept(), canReject(), etc).
    • is{StateName}(): test if the current state is {StatusName} (ie: isAccepted(), isRejected(), etc).
<?php

$article = new Article();

$article->canAccept();
$article->canReject();
$article->can('accept');

$article->accept();
$article->publish();

$article->isAccepted();
$article->isRejected();

Lifecyle callbacks

If you use the event-aware state-machine (which is the default one used by the bundle), the extension provides a listener implementing "lifecyle callbacks" for stateful entities.

For each available transition, three methods can be executed:

  • pre{TransitionName}(): called before the transition {TransitionName} is applied ;
  • post{TransitionName}(): called after the transition {TransitionName} is applied ;
  • can{TransitionName}(): called when the state-machine tests if the transition {TransitionName} can be applied.
<?php

namespace Acme\FooBundle\Entity;

use KPhoen\DoctrineStateMachineBehavior\Entity\Stateful;
use KPhoen\DoctrineStateMachineBehavior\Entity\StatefulTrait;

class Article implements Stateful
{
    // previous code

    public function preAccept()
    {
        // your logic here
    }

    public function postAccept()
    {
        // your logic here
    }

    public function onCanAccept()
    {
        // your logic here
    }
}

Using a service

You can put the state logic outside of the entity using a listener on Finite events. The extension provides an abstract EventSubcriber with the same methods as the "lifecyle callbacks" listener.

You need to declare a service and extend the AbstractSubcriber.

services:
    article_workflow_subscriber:
        class: Acme\FooBundle\Workflow\ArticleSubscriber
        tags:
            - { name: kernel.event_subscriber }
<?php

namespace Acme\FooBundle\Workflow;

use KPhoen\DoctrineStateMachineBundle\Listener\AbstractSubscriber;

class ArticleSubscriber extends AbstractSubscriber
{
    public function supportsObject($object)
    {
        return $object instanceof \Acme\FooBundle\Entity\Article;
    }

    public function preAccept()
    {
        // your logic here
    }

    public function postAccept()
    {
        // your logic here
    }

    public function canAccept()
    {
        // your logic here
    }
}

Twig

The bundle also exposes a few Twig helpers:

{# your template ... #}

{% if article|can('reject') %}
    <a class="btn btn-danger" href="{{ path('article_delete', article) }}">
        <i class="icon-trash"></i>
        {{ 'link_reject'|trans }}
    </a>
{% endif %}

{# this is strictly equivalent #}
{% if can(article, 'reject') %}
    <a class="btn btn-danger" href="{{ path('article_delete', article) }}">
        <i class="icon-trash"></i>
        {{ 'link_reject'|trans }}
    </a>
{% endif %}

{% if current_state(article).isFinal %}
    blabla
{% endif %}

{% if article|is_status('rejected') %}
    blabla
{% endif %}

{# this is strictly equivalent #}
{% if is_status(article, 'rejected') %}
    blabla
{% endif %}

{% if article|has_property('printable') %}
    {{ article|property('printable') ? 'I can print' : 'I CANNOT print' }}
{% endif %}

{# this is strictly equivalent #}
{% if has_property(article, 'printable') %}
    {{ property(article, 'printable') ? 'I can print' : 'I CANNOT print' }}
{% endif %}

Installation

Install the behavior adding kphoen/doctrine-state-machine-bundle to your composer.json or from CLI:

composer require kphoen/doctrine-state-machine-bundle

Than register the bundle in your app/AppKernel.php file:

// app/AppKernel.php
public function registerBundles()
{
    $bundles = array(
        // ...
        new KPhoen\DoctrineStateMachineBundle\KPhoenDoctrineStateMachineBundle(),
        // ...
    );
}

License

This bundle is released under the MIT license.

kphoen/doctrine-state-machine-bundle 适用场景与选型建议

kphoen/doctrine-state-machine-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 218.59k 次下载、GitHub Stars 达 48, 最近一次更新时间为 2013 年 10 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「doctrine2」 「extension」 「Behavior」 「statemachine」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 kphoen/doctrine-state-machine-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 kphoen/doctrine-state-machine-bundle 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 218.59k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 48
  • 点击次数: 28
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 48
  • Watchers: 3
  • Forks: 19
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-10-15