gaia_winzou/state-machine-bundle

轻量级且功能强大的PHP状态机组件

安装: 2

依赖者: 0

建议者: 0

安全性: 0

星星: 0

关注者: 1

分支: 43

类型:symfony-bundle

0.5.1 2021-03-21 11:59 UTC

This package is auto-updated.

Last update: 2024-09-29 05:56:32 UTC


README

定义您的状态、定义您的转换和回调:我们将完成剩下的工作。硬编码状态的年代已经结束!

Build Status

安装

安装(通过composer)

composer require winzou/state-machine-bundle

注册组件

// app/AppKernel.php
public function registerBundles()
{
    return array(
        // ...
        new winzou\Bundle\StateMachineBundle\winzouStateMachineBundle(),
    );
}

用法

配置状态机图

为了使用此组件的状态机,您首先需要定义一个图。图是状态、转换以及可选回调的定义,所有这些都与您领域中的对象相关联。可以将多个图附加到同一对象。

让我们为我们的 Article 对象定义一个名为 simple 的图

# app/config/config.yml

winzou_state_machine:
    my_bundle_article:
        class: My\Bundle\Entity\Article # class of your domain object
        property_path: state            # property of your object holding the actual state (default is "state")
        graph: simple                   # name of the graph (default is "default")
        # list of all possible states:
        states:
            - new
            - pending_review
            - awaiting_changes
            - accepted
            - published
            - rejected
        # list of all possible transitions:
        transitions:
            create:
                from: [new]
                to: pending_review
            ask_for_changes:
                from: [pending_review, accepted]
                to: awaiting_changes
            submit_changes:
                from: [awaiting_changes]
                to: pending_review
            approve:
                from: [pending_review, rejected]
                to: accepted
            publish:
                from: [accepted]
                to: published
        # list of all callbacks
        callbacks:
            # will be called when testing a transition
            guard:
                guard_on_submitting:
                    on:   'submit_changes'                        # call the callback on a specific transition
                    do:   ['@my.awesome.service', 'isSubmittable']  # will call the method of this Symfony service
                    args: ['object']                              # arguments for the callback
            # will be called before applying a transition
            before:
                update_reviewer:
                    on:   'create'
                    do:   ['@my.awesome.service', 'update']
                    args: ['object']
            # will be called after applying a transition
            after:
                email_on_publish:
                    on:   'publish'
                    do:   ['@my.awesome.service', 'sendEmail']
                    args: ['object', '"Email title"']

因此,在前面的例子中,对象 Article 有 6 个可能的状态,这些状态可以通过对实体应用一些转换来实现。例如,当创建一个新的 Article 时,您会向实体应用 'create' 转换,之后它的状态将变为 pending_review

现在让我们想象一下,经过彻底的审查后,有人决定 Article 不够好,因此想要您进行一些修改。因此,他们会应用 ask_for_changes 转换,此时状态将变为 awaiting_changes

使用状态机

定义

状态机是实际操作您的对象的那个对象。通过使用状态机,您可以测试是否可以应用转换,实际上应用转换,检索当前状态等。 状态机是针对特定的对象 + 图的。 这意味着如果您想操作另一个对象,或者用另一个图操作同一个对象,您需要另一个状态机

工厂帮助您获取这些对象 + 图的状态机。您向它提供一个对象和图名称,它将返回这对的状态机。工厂是一个名为 SM\Factory\Factory 的服务。

用法

public function myAwesomeAction($id, \SM\Factory\Factory $factory)
{
    // Get your domain object
    $article = $this->getRepository('MyAwesomeBundle:Article')->find($id);
    
    // Get the state machine for this object, and graph called "simple"
    $articleSM = $factory->get($article, 'simple');
}

现在,$articleSM 有很多方法,这些方法将允许您检查是否可以应用所需的转换,给定我们传递给它(在我们的例子中是 $article)的对象的状态。例如,我们可以

// Check if a transition can be applied: returns true or false
$articleSM->can('a_transition_name');

// Apply a transition
$articleSM->apply('a_transition_name');

// Get the actual state of the object
$articleSM->getState();

// Get all available transitions
$articleSM->getPossibleTransitions();

回调

回调用于保护转换或在应用转换前后执行一些代码。此组件增加了在回调中使用Symfony2服务的能力。