PHPackages                             systream/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. systream/state-machine

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

systream/state-machine
======================

State machine

1.1.2(10y ago)1108MITPHPPHP &gt;=5.4.0

Since Jun 5Pushed 9y ago1 watchersCompare

[ Source](https://github.com/systream/state-machine)[ Packagist](https://packagist.org/packages/systream/state-machine)[ RSS](/packages/systream-state-machine/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (5)Dependencies (3)Versions (6)Used By (0)

State machine
=============

[](#state-machine)

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

[](#installation)

You can install this package via [packagist.org](https://packagist.org/packages/systream/state-machine) with [composer](https://getcomposer.org/).

`composer require systream/state-machine`

composer.json:

```
"require": {
    "systream/state-machine": "1.*"
}
```

Usage examples
--------------

[](#usage-examples)

### Setup

[](#setup)

You need to add transitions to state machine.

```
$stateMachine = new StateMachine(new \Systream\EventDispatcher());

$inStock = new StateMachine\State('In stock');
$ordered = new StateMachine\State('Ordered');
$shippingInProcess = new StateMachine\State('Shipping in process');
$deliveredToClient = new StateMachine\State('Order is at client');

$stateMachine->addTransition(
	new GenericTransition('Order'), $inStock, $ordered
);

$stateMachine->addTransition(
	new GenericTransition('Cancel order'), $ordered, $inStock
);

$stateMachine->addTransition(
	new GenericTransition('Shipping'), $ordered, $shippingInProcess
);

$stateMachine->addTransition(
	new GenericTransition('Handover to client'), $shippingInProcess, $deliveredToClient
);
```

### Custom transitions

[](#custom-transitions)

You have to implement the `\Systream\StateMachine\TransitionInterface` interface to create custom transition

### Process Transition

[](#process-transition)

#### StateObject

[](#stateobject)

To use state machine you need an object which has state. `process` method expect `\Systream\StateMachine\State\StateObjectInterface` interface.

So you need to implement it, or just use `\Systream\StateMachine\State\StateObjectTrait`.

#### Can

[](#can)

Testing, whether it can change the status to the target state

```
$product = new DummyStateObject();
$product->setState($inStock);
$stateMachine->can($product, $ordered); // will return true
$stateMachine->can($product, $deliveredToClient); // will return false
```

#### Process

[](#process)

Set project state to `In Stock` and process it to `Ordered`.

```
$product = new DummyStateObject();
$product->setState($inStock);
$stateMachine->process($product, $ordered);
```

Set state without transition will trow an `\Systream\StateMachine\Exception\CantSetStatusException` exception.

```
$product = new DummyStateObject();
$product->setState($inStock);
$stateMachine->process($product, $deliveredToClient);
```

### Get available states

[](#get-available-states)

```
$states = $stateMachine->getStates();
```

It will return array of `\Systream\StateMachine\State\StateInterface` objects

#### Get next states

[](#get-next-states)

This method will return of the next possible states of an state object:

```
$product = new DummyStateObject();
$states = $stateMachine->getNextStates($product);
```

It will return array of `\Systream\StateMachine\State\StateInterface` objects

Visualization
=============

[](#visualization)

With this library you are able to generate an image with the states and transitions. To get this work you need to install `graphviz`.

```
$doFileGenerator = new StateMachine\DotFileGenerator();
$image = $doFileGenerator->getImage($stateMachine);
file_put_contents('my_flow_chart.png', $image);
```

[![example-flow](https://github.com/systream/state-machine/raw/master/assets/my_flow_chart.png)](https://github.com/systream/state-machine/blob/master/assets/my_flow_chart.png)

Test
----

[](#test)

[![Build Status](https://camo.githubusercontent.com/5aac463a8c41b4ebf5dd7a790951fef396b82e921cdd4a521b16f06103e36458/68747470733a2f2f7472617669732d63692e6f72672f737973747265616d2f73746174652d6d616368696e652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/systream/state-machine)

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~4 days

Total

5

Last Release

3655d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2c5ea6e7d8143a1fba541e1f41419cf36a4f970806d4bd91094d3b46177aea6d?d=identicon)[systream](/maintainers/systream)

---

Top Contributors

[![systream](https://avatars.githubusercontent.com/u/1583029?v=4)](https://github.com/systream "systream (19 commits)")

---

Tags

statestatemachinetransitionmachinestate-machine

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/systream-state-machine/health.svg)

```
[![Health](https://phpackages.com/badges/systream-state-machine/health.svg)](https://phpackages.com/packages/systream-state-machine)
```

###  Alternatives

[symfony/workflow

Provides tools for managing a workflow or finite state machine

62944.6M238](/packages/symfony-workflow)[yohang/finite

A simple PHP Finite State Machine

1.3k3.6M10](/packages/yohang-finite)[winzou/state-machine

A very lightweight yet powerful PHP state machine

52413.9M18](/packages/winzou-state-machine)[sebdesign/laravel-state-machine

Winzou State Machine service provider for Laravel

3411.4M1](/packages/sebdesign-laravel-state-machine)[rolfvreijdenberger/izzum-statemachine

A superior statemachine library php &gt;= 5.3. Integrates with your domain models perfectly.

6425.9k](/packages/rolfvreijdenberger-izzum-statemachine)[iben12/laravel-statable

Statable trait for Laravel Eloquent models

96312.6k1](/packages/iben12-laravel-statable)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
