Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/robertripoll/automaticfinitestatemachine
A basic PHP Finite State Machine that allows the creation and usage of automatic finite state machines.
https://github.com/robertripoll/automaticfinitestatemachine
automatic-finite-state-machine automatic-state-machine composer-package finite-state-machine php php-library php7 php74 state-machine
Last synced: 26 days ago
JSON representation
A basic PHP Finite State Machine that allows the creation and usage of automatic finite state machines.
- Host: GitHub
- URL: https://github.com/robertripoll/automaticfinitestatemachine
- Owner: robertripoll
- License: mit
- Created: 2022-05-07T22:22:11.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2022-05-08T21:19:57.000Z (over 2 years ago)
- Last Synced: 2024-09-29T20:01:18.807Z (about 1 month ago)
- Topics: automatic-finite-state-machine, automatic-state-machine, composer-package, finite-state-machine, php, php-library, php7, php74, state-machine
- Language: PHP
- Homepage:
- Size: 29.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Automatic Finite State Machine
A basic PHP Finite State Machine, based on [Symfony Workflows](https://github.com/symfony/workflow).
It allows the creation and usage of automatic finite state machines, that is, they apply transitions based on the current
state and the possible transitions available.## Installation
```bash
$ composer require robertripoll/automatic-finite-state-machine
```## Usage
### Traditional Finite State Machine
```php
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 = new class () implements StateStoreInterface {
public function getState(object $subject) {
return $subject->status;
}public function setState(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');
}
```### Automatic Finite State Machine
```php
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 = new class () implements StateStoreInterface {
public function getState(object $subject) {
return $subject->status;
}public function setState(object $subject, $newState): void {
$subject->status = $newState;
}
};$eventDispatcher = new class ($logService) implements EventDispatcherInterface {
private LogService $logService;public function __construct(LogService $logService) {
$this->logService = $logService;
}public function dispatch(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();
```