mini\Hooks\StateMachine
class
Documentation
A state machine which validates that states only transition to legal target states.
Provides hooks for entering, exiting, entered, and exited states, allowing fine-grained control over state transitions. Prevents invalid transitions and re-entrant state changes.
Example Usage
$machine = new StateMachine([
[Phase::Bootstrap, Phase::Request], // Bootstrap can only go to Request
[Phase::Request], // Request is terminal
]);
$machine->onEnteringState(Phase::Request, function($old, $new) {
echo "Entering request phase";
});
$machine->trigger(Phase::Request); // Transitions and fires hooks
Inheritance
Extends: mini\Hooks\Dispatcher
Implements:
Stringable
Properties (9)
array $transitions
Valid state transitions for the state machine.
UnitEnum|string|int $state
The current state
UnitEnum|string|int|null $transitioningTo
State being transitioned to (null if not transitioning)
array $listeners
Subscribers to all state changes
array $enteringListeners
Subscribers for entering a particular state
array $exitingListeners
Subscribers for exiting a particular state
array $enteredListeners
Subscribers for completed entering a particular state
array $exitedListeners
Subscribers for completed exiting a particular state
array $exitCurrentListeners
Subscribers to get notified ONCE when the current state exits
Methods (27)
Configure the states and valid transitions
Return the current state
String representation of current state
Transition to a new state
Register a listener that fires once when the current state exits
Subscribe to when a particular state is about to be entered
Subscribe to when a particular state has been entered
Subscribe to when a particular state is about to be exited
Subscribe to when a particular state has been exited
Subscribe to get notified about all state transitions
Unsubscribe from all state transition events
Assert we're not currently transitioning
Assert that the provided states exist in the state machine
Assert that the provided state is a valid target from current state
Normalize state arguments (handles arrays, recursion)
Convert state to scalar value for array keys
public
final
getDescription()
inherited from Hooks\Dispatcher
Get a description of the event dispatcher
public
final
getFile()
inherited from Hooks\Dispatcher
Get the filename where this event dispatcher was created
public
final
getLine()
inherited from Hooks\Dispatcher
Get the line number where this event dispatcher was created
public
static
final
configure()
inherited from Hooks\Dispatcher
Configure event loop integration
protected
invokeAll()
inherited from Hooks\Dispatcher
Invoke all listeners in an array
protected
static
handleException()
inherited from Hooks\Dispatcher
Handle an exception from a listener
protected
static
filterArray()
inherited from Hooks\Dispatcher
Filter array to remove specific values
protected
static
defer()
inherited from Hooks\Dispatcher
Schedule a function to run when runEvents() is called
protected
static
runEvents()
inherited from Hooks\Dispatcher
Run all scheduled events
protected
filterArrays()
inherited from Hooks\Dispatcher
Remove values from multiple arrays by reference
protected
static
invoke()
inherited from Hooks\Dispatcher
Invoke a listener with exception handling