百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
S

StateMachineBundle

> 编程语言
开源

一个非常轻量级但功能强大的 PHP 状态机包 ======================================================== 定义您的状态,定义您的行为,并将其集成到您的应用程序中。

339 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

一个非常轻量级但功能强大的 PHP 状态机包 ======================================================== 定义您的状态,定义您的行为,并将其集成到您的应用程序中。

A very lightweight yet powerful PHP state machine bundle

Define your states, define your transitions and your callbacks: we do the rest. The era of hard-coded states is over! Installation

Installation (via composer)

bash
composer require winzou/state-machine-bundle

Register the bundle

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

Usage

Configure a state machine graph

In order to use the state machine of this bundle, you first need to define a graph. A graph is a definition of states, transitions and optionally callbacks ; all attached on an object from your domain. Multiple graphs can be attached to the same object.

Let's define a graph called simple for our Article object:

…

So, in the previous example, the object Article has 6 possible states, and those can be achieved by applying some transitions to the entity. For example, when creating a new Article, you would apply the 'create' transition to the entity, and after that the state of it would become pending_review.

Let's imagine now that, after an exhaustive review, someone decides the Article was not good enough, so it would like to ask you for some changes. Therefore, they would apply the ask_for_changes transition, and now the state would be awaiting_changes.

Using the state machine

Definitions

The state machine is the object actually manipulating your object. By using the state machine you can test if a transition can be applied, actually apply a transition, retrieve the current state, etc. A state machine is specific to a couple object + graph. It means that if you want to manipulate another object, or the same object with another graph, you need another state machine.

The factory helps you to get the state machine for these couples object + graph. You give an object and a graph name to it, and it will return you the state machine for this couple. The factory is a service named SM\Factory\Factory.

Usage

php

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');
}

Now, the $articleSM has a bunch of methods that will allow you to check if the desired transitions are possible, given the state of the object we have passed to it ($article in our case). For example, we can:

php
// 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();

Callbacks

Callbacks are used to guard transitions or execute some code before or after applying transitions. This bundle adds the ability to use Symfony2 services in the callbacks.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHP

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言