yohang/finite 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

yohang/finite

Composer 安装命令:

composer require yohang/finite

包简介

A simple PHP Finite State Machine

README 文档

README

Finite is a Simple State Machine, written in PHP. It can manage any Stateful object by defining states and transitions between these states.

As of version 2, Finite is a low-deps and lightweight state machine library, thanks to the use of PHP Enums.

CI Status Mutation testing badge

Features

  • Manage State/Transition graph for an object
  • Attach business logic to states
  • Listen to transitions between state to trigger your domain code
  • Symfony integration
  • Twig Extension

Getting started

Installation (via composer)

 $ composer req yohang/finite

Define your state enum

enum DocumentState: string implements State
{
    case DRAFT = 'draft';
    case PUBLISHED = 'published';
    case REPORTED = 'reported';
    case DISABLED = 'disabled';

    public static function getTransitions(): array
    {
        return [
            new Transition('publish', [self::DRAFT], self::PUBLISHED),
            new Transition('clear', [self::REPORTED, self::DISABLED], self::PUBLISHED),
            new Transition('report', [self::PUBLISHED], self::REPORTED),
            new Transition('disable', [self::REPORTED, self::PUBLISHED], self::DISABLED),
        ];
    }
}

Define your Stateful Object

Your stateful object just need to have a state property

class Document
{
    private DocumentState $state = DocumentState::DRAFT;

    public function getState(): DocumentState
    {
        return $this->state;
    }

    public function setState(DocumentState $state): void
    {
        $this->state = $state;
    }
}

Initializing a simple StateMachine

use Finite\StateMachine;

$document = new Document;

$sm = new StateMachine;

// Can we process a transition ?
$sm->can($document, 'publish');

// Apply a transition
$sm->apply($document, 'publish'); 

Add business logic to states

Finite < 2.0 had properties on states. A metadata mechanism that allowed to add business properties on states to define the behavior of on object, depending on its state.

The idea behind this was to avoid to test the state in your domain code (A controller must not throw a 404 if the state is draft. But it can throw a 404 if the current state does not have a "visible" property. That was the idea).

Finite 2 does not needs this. PHP Enums can have methods. So, replace your properties with simple methods on your state.

enum DocumentState: string implements State
{
    // ...

    public function isDeletable(): bool
    {
        return in_array($this, [self::DRAFT, self::DISABLED]);
    }

    public function isPrintable(): bool
    {
        return in_array($this, [self::PUBLISHED, self::REPORTED]);
    }
}

After that, you can use theses methods on your object, even without instantiating the state machine.

var_dump($document->getState()->isDeletable());
var_dump($document->getState()->isPrintable());

Events & Guards

Finite dispatches PSR-14 events. You can listen to them to add custom logic or block transitions.

use Finite\Event\CanTransitionEvent;
use Finite\Event\PostTransitionEvent;

$dispatcher = $stateMachine->getDispatcher();

// 1. Guard: Prevent a transition dynamically
$dispatcher->addEventListener(CanTransitionEvent::class, function (CanTransitionEvent $event) {
    if ('publish' === $event->getTransition()->getName() && !$event->getObject()->title) {
        // Block the transition if the title is empty
        $event->blockTransition(); 
    }
});

// 2. Post-Action: Do something after a transition
$dispatcher->addEventListener(PostTransitionEvent::class, function (PostTransitionEvent $event) {
    // e.g. Send an email, log activity...
    echo 'Transition ' . $event->getTransition()->getName() . ' completed!';
});

Symfony Integration

Register the bundle in config/bundles.php:

return [
    // ...
    Finite\Extension\Symfony\Bundle\FiniteBundle::class => ['all' => true],
];

This automatically registers the StateMachine service and the Twig extension.

Twig Helper

{# Check if a transition is available #}
{% if finite_can(document, 'publish') %}
    <a href="...">Publish</a>
{% endif %}

{# List all reachable transitions #}
<ul>
    {% for transition in finite_reachable_transitions(document) %}
        <li>{{ transition.name }}</li>
    {% endfor %}
</ul>

Console Command

Dump your graph to visualize it:

php bin/console finite:state-machine:dump "App\State\DocumentState" mermaid

License

This library is licensed under the MIT License.

yohang/finite 适用场景与选型建议

yohang/finite 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.66M 次下载、GitHub Stars 达 1.35k, 最近一次更新时间为 2012 年 10 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 yohang/finite 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 3.66M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1353
  • 点击次数: 39
  • 依赖项目数: 19
  • 推荐数: 3

GitHub 信息

  • Stars: 1347
  • Watchers: 51
  • Forks: 185
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2012-10-08