1. Go to this page and download the library: Download metabor/statemachine library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
metabor / statemachine example snippets
Metabor\Statemachine\Process;
use Metabor\Statemachine\State;
use Metabor\Statemachine\Statemachine;
use Metabor\Statemachine\Transition;
$closed = new State('closed');
$opened = new State('opened');
$eventOpen = 'open';
$eventClose = 'close';
$closed->addTransition(new Transition($opened, $eventOpen));
$closed->addTransition(new Transition($closed, $eventClose));
$opened->addTransition(new Transition($opened, $eventOpen));
$opened->addTransition(new Transition($closed, $eventClose));
// adding some action to events
// the parts of observing the event and executing the command are separated in this example
// normaly it could be put all together into your own command (base)class
$openCommand = new \Metabor\Callback\Callback(
function ()
{
echo 'motor is opening door' . PHP_EOL;
});
$observerForOpenEvent = new \Metabor\Observer\Callback($openCommand);
$closed->getEvent($eventOpen)->attach($observerForOpenEvent);
$closeCommand = new \Metabor\Callback\Callback(
function ()
{
echo 'motor is closing door' . PHP_EOL;
});
$observerForCloseEvent = new \Metabor\Observer\Callback($closeCommand);
$opened->getEvent($eventClose)->attach($observerForCloseEvent);
// stateful subject that belongs to this statemachine
$subject = new stdClass();
// start process with closed status;
$initialState = $closed;
$process = new Process('process name', $initialState);
$statemachine = new Statemachine($subject, $process);
echo 'Status:' . $statemachine->getCurrentState()->getName() . PHP_EOL;
echo 'Event:' . $eventOpen . PHP_EOL;
$statemachine->triggerEvent($eventOpen);
echo 'Status:' . $statemachine->getCurrentState()->getName() . PHP_EOL;
// opening an open door would not activate the motor
echo 'Event:' . $eventOpen . PHP_EOL;
$statemachine->triggerEvent($eventOpen);
echo 'Status:' . $statemachine->getCurrentState()->getName() . PHP_EOL;
echo 'Event:' . $eventClose . PHP_EOL;
$statemachine->triggerEvent($eventClose);
echo 'Status:' . $statemachine->getCurrentState()->getName() . PHP_EOL;
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.