Download the PHP package star/state-machine without Composer
On this page you can find all versions of the php package star/state-machine. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download star/state-machine
More information about star/state-machine
Files in star/state-machine
Package state-machine
Short Description PHP implementation of a configurable State Machine, Workflow, transitions for your domain models
License MIT
Informations about the package state-machine
PHP State machine
This package help you build a workflow for a designated context, that can be encapsulated inside the given context.
It was designed to avoid having a hard dependency to the package. The library do not require you to implement any method. All the code you need to write can be encapsulated inside your context class, and it stays hidden from your other object.
Installation
composer require star/state-machine
Features
States
A state is just a name in which a context can find itself in. The state is usually kept in a persistence platform, or kept in the model using a string representation or a StateMetadata class.
Transitions
A transition is an action on the context that will move from one state to the other. A transition can only have one destination state, since there is no way for the machine to know which state to go to. On the other hand, a transition may have multiple starting states.
If no transition contains the context's current state as a start start, an exception will be raised (unless another TransitionCallback is given).
Attributes
Attributes are used to mark a state as having a meaning to the context.
Ie. Given you need a state to be considered active or closed while another state should not,
you just need to add the is_active
and is_closed
attributes to the states that needs them.
Examples of usage
Given you have a Post
context that can have the following states:
- Draft: The post is visible only to the creator and moderators
- Published: The post is visible to all users
- Archived: The post is visible only to the creator
The post's allowed workflow should be as follow:
Transitions | draft | published | archived |
---|---|---|---|
draft | N/A | publish | N/A |
published | N/A | N/A | archive |
archived | N/A | unarchive | N/A |
You Post
class can be defined as one of the following pattern.
Using the builder in your model
Wrap the workflow in a class
If you have multiple models that can have the same workflow, defining a class that wraps the workflow can be done using the StateMetadata.
Persistence of state
The package supports the following persistence engine:
- Doctrine: Can be used using
@Embeddable
, see Example of usage.
Events
The state machine has an internal event handling systems.
Multiple events are triggered at different places, which enables you to hook into the system to add behavior on certain transitions.
Subscribers that listens to these events will have their configured callback(s) called for any transitions.
StateEventStore::BEFORE_TRANSITION
: The event is performed before any transition on the context. SeeTransitionWasRequested
.StateEventStore::AFTER_TRANSITION
: This event is performed after any transition is executed on the context. SeeTransitionWasSuccessful
.StateEventStore::FAILURE_TRANSITION
: This event is performed before the transition exception is triggered. SeeTransitionWasFailed
.
Subscribing a listener in the machine
Transition callbacks
When requesting a transition, another way to hook in the process is to pass a TransitionCallback.
Transition callbacks allow to perform an action before, after or when the transition is not allowed. By default, an exception is triggered. see AlwaysThrowExceptionOnFailure.
Callback on a transition
All versions of state-machine with dependencies
webmozart/assert Version ^1.0
symfony/event-dispatcher Version ^4.0|^5.0|^6.0