PHPackages                             noem/state-machine - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. noem/state-machine

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

noem/state-machine
==================

An event-based finite state machine with support for hierarchical and parallel states

2401[1 PRs](https://github.com/NoemPHP/state-machine/pulls)PHPCI passing

Since Mar 1Pushed 4mo ago2 watchersCompare

[ Source](https://github.com/NoemPHP/state-machine)[ Packagist](https://packagist.org/packages/noem/state-machine)[ RSS](/packages/noem-state-machine/feed)WikiDiscussions master Synced 6d ago

READMEChangelogDependenciesVersions (5)Used By (0)

Noem State Machine
==================

[](#noem-state-machine)

[![CI](https://github.com/NoemPHP/state-machine/actions/workflows/ci.yml/badge.svg)](https://github.com/NoemPHP/state-machine/actions/workflows/ci.yml)

[![tmpdbpky87p](https://private-user-images.githubusercontent.com/4208996/309500229-2cebcf16-2548-4ff1-a09f-7ba35593b8b8.png?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Nzg0ODM0MTksIm5iZiI6MTc3ODQ4MzExOSwicGF0aCI6Ii80MjA4OTk2LzMwOTUwMDIyOS0yY2ViY2YxNi0yNTQ4LTRmZjEtYTA5Zi03YmEzNTU5M2I4YjgucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI2MDUxMSUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNjA1MTFUMDcwNTE5WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9ZjdhN2IyYTZhM2E0ZDIyZTI2NjI1ODdlNzdmOTc1NmJjMGRlZGMyOGUxZmFjNWVhZmQyOWFlMGY5ZDU4NzhlMiZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmcmVzcG9uc2UtY29udGVudC10eXBlPWltYWdlJTJGcG5nIn0.PnbS9Nx6X604fyoYErkiaDUklMlYy4svryDCwo5pJbE)](https://private-user-images.githubusercontent.com/4208996/309500229-2cebcf16-2548-4ff1-a09f-7ba35593b8b8.png?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Nzg0ODM0MTksIm5iZiI6MTc3ODQ4MzExOSwicGF0aCI6Ii80MjA4OTk2LzMwOTUwMDIyOS0yY2ViY2YxNi0yNTQ4LTRmZjEtYTA5Zi03YmEzNTU5M2I4YjgucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI2MDUxMSUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNjA1MTFUMDcwNTE5WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9ZjdhN2IyYTZhM2E0ZDIyZTI2NjI1ODdlNzdmOTc1NmJjMGRlZGMyOGUxZmFjNWVhZmQyOWFlMGY5ZDU4NzhlMiZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmcmVzcG9uc2UtY29udGVudC10eXBlPWltYWdlJTJGcG5nIn0.PnbS9Nx6X604fyoYErkiaDUklMlYy4svryDCwo5pJbE)

This library provides an implementation of a Finite State Machine (FSM) in PHP. The benefits of using an FSM architecture include:

1. **Simplified system behavior modeling:** State machines help to represent and organize the behavior of a system in a structured and understandable manner.
2. **Ease of refactoring:** Significant changes to system architecture can be done without affecting business logic
3. **Testability:** State machines allow for easier testing of individual states and transitions, making it simpler to isolate and test specific system behaviors.
4. **Reduced complexity:** By breaking down a complex system into smaller, manageable states, state machines can simplify the overall system design and make it easier to understand.
5. **Predictable behavior:** State machines ensure that a system behaves consistently and predictably, as the transitions between states are explicitly defined.
6. **Documentation:** State machines serve as a form of documentation, as they provide a visual/textual representation of the system's behavior and transitions

Features
--------

[](#features)

- **Nested regions**: One horizontal set of states is called a "region". However, each state can have any number of sub-regions, allowing both *parallel states* and *hierarchical states*
- **Guards**: Enable a given transition only if a predicate returns `true`.
- **Actions**: Dispatch actions to the machine to achieve stateful behaviour. Only the action handlers corresponding to the active state will get called.
- **Entry &amp; Exit events**: Attach arbitrary subscribers to state changes.
- **Region &amp; State context**: Store data relevant to the current application state. Data can be scoped for an individual state - or shared with the entire region
- **State inheritance**: Since regions can be nested, each region can request specific data to be passed down from the parent region.
- **Middleware**: Before creating the final machine, your can augment your definitions with reusable middlewares.

Installation
------------

[](#installation)

Install this package via composer:

`composer require noem/state-machine`

Usage
-----

[](#usage)

### Practical example

[](#practical-example)

This trivial example shows how a product may move through various states during processing:

```
