fsm

Finite State Machine builder with transitions, history and enter/exit hooks.

v1.0.0Application & logicbundledimport "fsm";
Overview Installation Documentation Examples

Overview

A lightweight, zero-dependency Finite State Machine (FSM) builder for HolyPHP with named states, transitions, transition history, and onEnter / onExit lifecycle hooks.

bundled — works offline zero dependencies state machine

Quick start

import "fsm";

$door = fsm_create("closed")
    ->addTransition("closed", "open", "open")
    ->addTransition("open", "close", "closed")
    ->addTransition("closed", "lock", "locked")
    ->addTransition("locked", "unlock", "closed")
    ->onEnter("open", function($from, $to, $event, $payload) {
        println("Door opened from " . $from);
    })
    ->onExit("closed", function($from, $to, $event, $payload) {
        println("Leaving closed state via " . $event);
    });

println($door->state()); // "closed"
println($door->can("open")); // true
println($door->can("unlock")); // false

$door->trigger("open", ["by" => "alice"]);
println($door->state()); // "open"
println(count($door->history())); // 1

Installation

In a project

Inside a project (a folder with hphp.yaml, see Projects & packages):

hphp install fsm

This adds the library to hphp.yaml, installs it into .hphp/packages/fsm/ and pins the exact version in hphp.lock:

require:
  fsm: "^1.0"

Use it

import "fsm";

fsm is bundled with the compiler, so import "fsm"; also works in a single file without installing anything, and hphp install works offline.

Outside a project

hphp install fsm          # for your user, shared by all scripts
hphp create myapp                 # or start a project, then: hphp install fsm
Packagefsm
Version1.0.0 (follows the compiler)
Entry filefsm.hphp
Sourcepackages/lib/fsm ↗

Documentation

API Reference

Helper Function

fsm_create(string $initialState): StateMachine

Creates and returns a new StateMachine instance starting in $initialState.

StateMachine Class

new StateMachine(string $initialState)

Constructs a new state machine with the given initial state. Automatically registers $initialState.

addState(string $name): StateMachine

Registers a named state. Returns $this for chaining.

addStates(array $names): StateMachine

Registers multiple named states from an array of strings. Returns $this for chaining.

hasState(string $name): bool

Returns true if $name is a registered state, false otherwise.

states(): array

Returns an array of all registered state names.

addTransition(string $from, string $event, string $to): StateMachine

Defines a transition from state $from to state $to triggered by $event. Automatically registers $from and $to if not already registered. Returns $this for chaining.

onEnter(string $state, $callback): StateMachine

Registers a callback function($from, $to, $event, $payload) invoked whenever the machine enters $state. Returns $this for chaining.

onExit(string $state, $callback): StateMachine

Registers a callback function($from, $to, $event, $payload) invoked whenever the machine exits $state. Returns $this for chaining.

can(string $event): bool

Returns true if $event is a valid transition from the current state, false otherwise.

availableEvents(): array

Returns an array of event names that can currently be triggered from the current state.

transitionsFrom(string $state): array

Returns a map of event => targetState for the given registered state.

trigger(string $event, $payload = null): string

Executes the transition for $event from the current state:

  1. Invokes all onExit hooks for the current state.
  2. Updates the current state to the target state.
  3. Appends ["from" => $from, "to" => $to, "event" => $event, "payload" => $payload] to the history log.
  4. Invokes all onEnter hooks for the target state.
  5. Returns the new state name.

state(): string

Returns the current state name.

is(string $state): bool

Returns true if the current state equals $state.

history(): array

Returns the ordered array of transition records performed since creation or the last reset().

reset(): StateMachine

Resets the current state back to the initial state and clears the transition history. Returns $this for chaining.

Error Handling

All invalid operations throw an Exception with a clear fsm: prefixed message:

Examples

01Quick start

Bundled with the compiler — import "fsm"; works offline in any file, or add it to a project with hphp install fsm.

import "fsm";

$door = fsm_create("closed")
    ->addTransition("closed", "open", "open")
    ->addTransition("open", "close", "closed")
    ->addTransition("closed", "lock", "locked")
    ->addTransition("locked", "unlock", "closed")
    ->onEnter("open", function($from, $to, $event, $payload) {
        println("Door opened from " . $from);
    })
    ->onExit("closed", function($from, $to, $event, $payload) {
        println("Leaving closed state via " . $event);
    });

println($door->state()); // "closed"
println($door->can("open")); // true
println($door->can("unlock")); // false

$door->trigger("open", ["by" => "alice"]);
println($door->state()); // "open"
println(count($door->history())); // 1
Try it
hphp install fsm
hphp run main.hphp

Covered by the official test suite in tests/positive/lib_fsm.hphp.