Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
F

Finite

> 编程语言
Open source

A Simple PHP Finite State Machine

1.3K stars0 likes3 views
WebsiteGitHub

About

A Simple PHP Finite State Machine

Finite, A Simple PHP Finite State Machine

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.

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)

bash
 $ composer req yohang/finite

Define your state enum

php
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

php
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

php
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.

php
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.

php
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.

…

Symfony Integration

Register the bundle in config/bundles.php:

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

This automatically registers the StateMachine service and the Twig extension.

Twig Helper

twig
{# 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:

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

License

This library is licensed under the MIT License.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHP

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

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