Download the PHP package stateflow/engine without Composer
On this page you can find all versions of the php package stateflow/engine. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download stateflow/engine
More information about stateflow/engine
Files in stateflow/engine
Package engine
Short Description A clean, auditable state machine for PHP 8.2+
License MIT
Informations about the package engine
StateFlow Engine
A clean, auditable state machine for PHP 8.2+
Why StateFlow?
Stop scattering state logic across your codebase:
| Feature | Ad-hoc if statements | StateFlow |
|---|---|---|
| See all states | Scattered | One place |
| See all transitions | Scattered | One place |
| Invalid transitions | Silent bugs | Throws exception |
| Typos | Silent bugs | Throws exception |
| Side effects | Easy to forget | Defined with hooks |
| Audit trail | Build it yourself | Built-in events |
| Testing | Hard | Easy |
| New developer onboarding | "Read the whole codebase" | "Read this class" |
Installation
Requires PHP 8.2 or higher.
Quick Start
1. Define your states and transitions
2. Create and use the state machine
That's it! The library enforces valid transitions. You handle persistence.
Core Concepts
States
States are the possible values your subject can have. Define them as a const array:
Transitions
Transitions define how to move between states. Each transition has:
- A name (e.g.,
'submit') - One or more
fromstates - One
tostate
Subjects
A subject is the thing that has state. It can be:
- An object with a public property
- An object with getter/setter methods
- An array
- An Eloquent model
- A Doctrine entity
- Anything
API Reference
Creating a State Machine
Applying Transitions
Reading State
Initializing New Subjects
Introspection
Validation
Guards
Guards are conditions that must pass before a transition is allowed.
Defining Guards
Create a workflow class with guard methods:
Using Guards
Multiple Guards
You can have multiple guards for the same transition. All must pass:
Hooks
Hooks run code at specific points during a transition.
Available Hooks
| Hook | Triggers on | When |
|---|---|---|
#[Before('transition')] |
Transition name | After guards, before state change |
#[OnExit('state')] |
State name | Before leaving the state |
#[OnEnter('state')] |
State name | After entering the state |
#[After('transition')] |
Transition name | After everything |
Execution Order
Defining Hooks
Hook Signature
All hooks receive the same parameters:
Guards must return bool. Other hooks return void.
Events & Audit Trail
Listen to transition events for logging, auditing, or triggering side effects.
Registering Listeners
TransitionEvent Properties
Multiple Listeners
Note: Listeners are only called for successful transitions. Failed transitions (invalid state, guard rejected) do not trigger listeners.
Custom State Accessor
By default, StateFlow reads/writes state using common patterns:
- Public properties (
$subject->status) - Getter/setter methods (
$subject->getStatus()) - Array access (
$subject['status']) - Magic methods (
__get,__set)
For special cases, provide a custom accessor:
Or implement StateAccessorInterface:
Visualizing Your State Machine
Export to Mermaid for documentation:
Output:
Renders as:
Paste this into GitHub, GitLab, Notion, or any Mermaid-compatible tool.
Framework Integration
Laravel / Eloquent
With Service Container
Symfony / Doctrine
Error Handling
Exception Types
Testing
StateFlow makes testing easy:
Complete Example
License
MIT License. See LICENSE for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Credits
Created by Enmanuel Varela Pividori.