1. Go to this page and download the library: Download eftec/statemachineone 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/ */
eftec / statemachineone example snippets
use eftec\statemachineone\StateMachineOne;
define("STATE_TRANSPORT",3);
define("STATE_ABORTTRANSPORT",4);
define("STATE_TODELIVER",5);
define("STATE_HELP",6);
define("STATE_DELIVERED",7);
define("STATE_ABORTED",8);
$smachine=new StateMachineOne();
$smachine->setDebug(true);
$smachine->tableJobs="chopsuey_jobs";
$smachine->tableJobLogs="chopsuey_logs";
$smachine->setDefaultInitState(STATE_PICK);
$smachine->setAutoGarbage(false); // we don't want to delete automatically a stopped job.
$smachine->setStates([STATE_PICK=>'Pick order'
,STATE_CANCEL=>'Cancel order'
,STATE_TRANSPORT=>'Transport order'
,STATE_ABORTTRANSPORT=>'Abort the delivery'
,STATE_TODELIVER=>'Pending to deliver'
,STATE_HELP=>'Request assistance'
,STATE_DELIVERED=>'Delivered'
,STATE_ABORTED=>'Aborted']);
$smachine->fieldDefault=[
'customerpresent'=>-1
,'addressnotfound'=>-1
,'signeddeliver'=>-1
,'abort'=>-1
,'instock'=>-1
,'picked'=>-1
,'message'=>''];
$smachine->setDB('mysql','localhost',"root","abc.123","statemachinedb");
$smachine->createDbTable(false); // you don't need to create this table every time.
$smachine->loadDBAllJob(); // we load all jobs, including finished ones.
// business rules
$smachine->addTransition(STATE_PICK,STATE_PICK
,'when instock = 0 set message="without stock"','stay'); // it stays in the same state
$smachine->addTransition(STATE_PICK,STATE_CANCEL
,'when instock = 0 set abort = 1','stop'); // ends the process
$smachine->addTransition(STATE_PICK,STATE_TRANSPORT
,'when instock = 1','change'); // changes transition
$smachine->addTransition(STATE_TRANSPORT,STATE_ABORTTRANSPORT
,'when abort = 1','stop'); // ends the process
$smachine->addTransition(STATE_TRANSPORT,STATE_DELIVERED
,'when addressnotfound = 0 and customerpresent = 1 and signeddeliver = 1 timeout 3600','stop'); // 1 hour max.
$smachine->addTransition(STATE_TRANSPORT,STATE_HELP
,'when addressnotfound = 1 or customerpresent = 0 timeout 3600','change'); // 1 hour max
$smachine->addTransition(STATE_HELP,STATE_ABORTED
,'when wait timeout 900','change'); // it waits 15 minutes max.
$smachine->addTransition(STATE_HELP,STATE_DELIVERED
,'when addressnotfound = 0 and customerpresent = 1 and signeddeliver = 1','change');
$msg=$smachine->fetchUI();
$smachine->checkAllJobs();
$smachine->viewUI(null,$msg); // null means it takes the current job
$smachine->addTransition(STATE_PICK,STATE_CANCEL
,'when instock = 0 set abort = 1','stop');
$smachine->addTransition(STATE_ONE,STATE_TWO,'when field = 0');
$smachine->addTransition([STATE_ONE,STATE_EXTRA],STATE_TWO,'when field = 0');
// its the same than to add multiples transitions:
// $smachine->addTransition(STATE_ONE,STATE_TWO,'when field = 0');
// $smachine->addTransition(STATE_EXTRA,STATE_TWO,'when field = 0');
"when field=0" // it happens when the field is zero.
"when $var='hi'" // it happens when the global variable is 'hi'
"when fn()=44" // the transition is triggered when the function fn() returns 44
"when always" // its always true. It is the same than "when 1=1". The transition is always executed
"when field contain 'text'"
"when field = field2" // when field (of the job) is equals to field2
"set id=_idjob"
"set ts=_time"
"set currentstate=_state0"
"set nextstate=_state1"
"set transition_happened=_result"
$v1=20;
$smachine->addTransition(S1,S2,"when $v1=1"); // WRONG: the variable v1 is evaluated when it is defined, i.e equals to write "when 20=1"
$smachine->addTransition(S1,S2,"when \$v1=1"); // RIGHT: the variable v1 is evaluated when the transition is checked
$smachine->addTransition(S1,S2,'when $v1=1'); // RIGHT:(') the variable v1 is evaluated when the transition is checked
"when field = 777" // when field is equals to 777
"when field = somefunc()" // function somefunc(Job $job) {...}
"when field = null() "
"when field = true()" // when field is equals to true
"when field = true()" // when field is equals to false
"set field=flip()" // it is only valid for set.
$smachine=new StateMachineOne();
function someFunction($job) {
// we could do something here we should return a value
return 0;
}
$var=222;
$smachine->fieldDefault=[
'field2'=>0
,'field3'=>0
,'field4'=>0];
$smachine->addTransition(STATE_ONE,STATE_TWO
,'when field2 = 2 and field3 > someFunction() and field4=$var');
$smachine->addTransition(STATE_ONE,STATE_TWO,'when field = 0 set field=1');
"set field = 0 , field2 = 3"
"set 0 = 20" // is invalid
"set myfunc() = 20"
"set field=20"
"set $variable=20"
"set field = 0"
"set field + 1"
"set field - 1"
$smachine->addTransition(STATE_ONE,STATE_TWO,'when field = 0 set field2="done" else field2="not done"');
$smachine->addTransition(STATE_ONE,STATE_TWO,'when field = 0 timeout 3600');
"timeout 3600" // 1 hour timeout
"timeout field" // timeout by field, it is calculated each time.
$smachine->addTransition(STATE_ONE,STATE_TWO,'when field = 0 fulltimeout 3600');
"fulltimeout 3600" // 1 hour timeout
"fulltimeout field" // timeout by field, the field is evaluated each time.
// $smachine is our state machine with transitions, events and such.
$job=$smachine->createJob(['value1'=>'hi','value2'=>'world']); // we create a job with the values value1 and value2
// or we could create the job the with the default values
$job=$smachine->createJob();
$smachine->checkJob($job); // then we executed the job.
var_dump($job->state); // the id of the current state.
var_dump($job->fields); // And we could see the result of the job
$smachine->checkJob($job); // then we executed the job.
$smachine->checkAllJob(); // We execute all jobs stored in our state machine.
$smachine->getLastJob(); // we load the last job (if we already has a job into a memory);
$smachine->getJob($idJob); // we get a job from the queue.
$smachine->getJob($idJob); // we get a job from the queue.
$smachine->getJobQueue(); // we get all the jobs from the queue
$smachine->loadDbJob($idJob); // we read a job with the id $idJob and we store into the queue
$smachine->loadDbAllJob(); // We load all jobs (including inactive jobs) from the database and we store into the queue
$smachine->loadDBActiveJobs(); // We load all active jobs from the database and we store into the queue.
$smachine->loadDbJob($idJob); // we read a job with the id $idJob
$smachine->loadDbAllJob(); // We load all jobs (including inactive jobs) from the database.
$smachine->loadDBActiveJobs(); // We load all active jobs from the database.
$smachine->saveDBAllJob(); // we save all the jobs in memory.
$smachine->saveDBJob($job); // we save a specific job.
$smachine->saveDBJobLog($job,$texto); // we save a log of a job.
$smachine->deleteJobDB($job); // we delete a specific job.
$flags=new Flags('flag1', true, $smachine); // the flags is called flag1, $smachine is a state machine
$flags->setParent($job); // it is also associate with a job.