<?phprequire_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
robertripoll / automatic-finite-state-machine example snippets
useRobertRipoll\Definition;
useRobertRipoll\FiniteStateMachine;
useRobertRipoll\State;
useRobertRipoll\StateStoreInterface;
useRobertRipoll\Transition;
// The subject on which to store the finite machine's current status
$order = new stdClass();
$order->id = 123;
$order->status = null;
// The possible finite machine statuses/nodes
$states = [
$initialState = new State(0, 'creation'),
$paidState = new State(1, 'paid'),
$preparingState = new State(2, 'preparing'),
$shippedState = new State(3, 'shipped'),
];
// The transitions between the possible finite machine nodes
$transitions = [
new Transition($initialState, $paidState, 'pay'),
new Transition($paidState, $preparingState, 'prepare', fn (object $order) => $paymentService->isPaid($order->id)),
new Transition($preparingState, $shippedState, 'ship'),
];
// The definition of the finite machine
$definition = new Definition($states, $initialState, $transitions);
// The logic behind the retrieval and storage of the state
$stateStore = newclass () implementsStateStoreInterface{
publicfunctiongetState(object $subject){
return $subject->status;
}
publicfunctionsetState(object $subject, $newState): void{
$subject->status = $newState;
}
};
$stateMachine = new FiniteStateMachine($definition, $order, $stateStore, null, 'Traditional finite state machine');
if ($stateMachine->can('prepare')) {
$stateMachine->apply('prepare');
}
useRobertRipoll\AutomaticFiniteStateMachine;
useRobertRipoll\Definition;
useRobertRipoll\Events\FiniteStateMachineEventInterface;
useRobertRipoll\Events\StateChangedEvent;
useRobertRipoll\State;
useRobertRipoll\StateStoreInterface;
useRobertRipoll\Transition;
// The subject on which to store the finite machine's current status
$order = new stdClass();
$order->id = 123;
$order->status = null;
// The possible finite machine statuses/nodes
$states = [
$initialState = new State(0, 'created'),
$paidState = new State(1, 'paid'),
$preparingState = new State(2, 'preparing'),
$shippedState = new State(3, 'shipped'),
];
// The transitions between the possible finite machine nodes
$transitions = [
new Transition($initialState, $paidState, 'pay', fn (object $order) => $paymentService->isPaid($order->id)),
new Transition($paidState, $preparingState, 'prepare'),
new Transition($preparingState, $shippedState, 'ship', fn (object $order) => $shipmentService->isShipped($order->id)),
];
// The definition of the finite machine
$definition = new Definition($states, $initialState, $transitions);
// The logic behind the retrieval and storage of the state
$stateStore = newclass () implementsStateStoreInterface{
publicfunctiongetState(object $subject){
return $subject->status;
}
publicfunctionsetState(object $subject, $newState): void{
$subject->status = $newState;
}
};
$eventDispatcher = newclass ($logService) implementsEventDispatcherInterface{
private LogService $logService;
publicfunction__construct(LogService $logService){
$this->logService = $logService;
}
publicfunctiondispatch(object $event){
/** @var FiniteStateMachineEventInterface $event */if ($event->getEventName() == StateChangedEvent::EVENT_NAME)
{
/** @var StateChangedEvent $event */
$oldState = $event->hasOldState() ? $event->getOldState()->getValue() : 'null';
$logService->log("Subject state changed from $oldState to {$event->getNewState()->getValue()}");
}
}
};
$stateMachine = new AutomaticFiniteStateMachine($definition, $order, $stateStore, $eventDispatcher, 'Automatic finite state machine');
$stateMachine->run();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.