State Machine for PHP
The State Machine library is aimed to provide a easy use, lightweight, flexible and extensible, and type safe PHP state machine implementation for enterprise usage.
{
"require": {
"dddphp/state-machine": "*"
}
}
State Machine Builder
In order to create a state machine, user need to create state machine builder first. For example:
$builder = StateMachineBuilderFactory::create();
Fluent API After state machine builder was created, we can use fluent API to define state/transition/action of the state machine.
$builder->externalTransition()
->from(self::STATEA)
->to(self::STATEB)
->on(self::EVENTGoToB)
->when($this->checkCondition())
->perform($this->doAction());
An external transition is built between state 'A' to state 'B' and triggered on received event 'GoToB'.
$builder->internalTransition()
->within(self::STATEA)
->on(self::INTERNAL_EVENT)
->when($this->checkCondition())
->perform($this->doAction());
An internal transition with priority set to high is build inside state 'A' on event 'INTERNAL_EVENT' perform '$this->doAction()'. The internal transition means after transition complete, no state is exited or entered. The transition priority is used to override original transition when state machine extended.
$builder->externalTransition()
->from(self::STATEC)
->to(self::STATED)
->on(self::EVENTGoToD)
->when(
new class () implements ConditionInterface {
public function isSatisfied($context): bool
{
echo "Check condition : " . $context . "\n";
return true;
}
public function name(): string
{
return '';
}
})
->perform(
new class () implements ActionInterface {
public function execute($from, $to, $event, $context): void
{
echo $context . " from:" . $from . " to:" . $to . " on:" . $event;
}
};
);
An conditional transition is built from state 'C' to state 'D' on event 'GoToD' when external context satisfied the condition restriction, then call action method.
New State Machine Instance
After user defined state machine behaviour, user could create a new state machine instance through builder. Note, once the state machine instance is created from the builder, the builder cannot be used to define any new element of state machine anymore.
New state machine from state machine builder.
$stateMachine = $builder->build(self::MACHINE_ID);
Trigger Transitions
After state machine was created, user can fire events along with context to trigger transition inside state machine. e.g.
$target = $stateMachine->fire(self::STATE1, self::EVENT1, $this->context);