PHPackages                             eftec/statemachineone - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. eftec/statemachineone

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

eftec/statemachineone
=====================

A state Machine library for business processes

2.28(1y ago)1144.0k13[1 issues](https://github.com/EFTEC/StateMachineOne/issues)LGPL-3.0PHPPHP &gt;=7.4CI failing

Since Dec 9Pushed 1y ago3 watchersCompare

[ Source](https://github.com/EFTEC/StateMachineOne)[ Packagist](https://packagist.org/packages/eftec/statemachineone)[ Docs](https://github.com/EFTEC/StateMachineOne)[ RSS](/packages/eftec-statemachineone/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (10)Dependencies (4)Versions (45)Used By (0)

StateMachineOne
===============

[](#statemachineone)

It is State Machine library written on PHP aimed to business process.
This library has only a simple external dependency, it is a minimalist (yet complete) library with only 3 classes.

Since this library is PHP native, then it could run in Laravel, Symfony and any other frameworks.

[![Packagist](https://camo.githubusercontent.com/98083c945fd5dfa5311b7913c808e82e4a9aedad71bc2cb5189b114a546e9ff9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f65667465632f73746174656d616368696e656f6e652e737667)](https://packagist.org/packages/eftec/statemachineone)[![Total Downloads](https://camo.githubusercontent.com/b03a4925f56a757e810145177a5d4a7f52d7c48e5f878345a67cc595bd68e76d/68747470733a2f2f706f7365722e707567782e6f72672f65667465632f73746174656d616368696e656f6e652f646f776e6c6f616473)](https://packagist.org/packages/eftec/statemachineone)![Maintenance](https://camo.githubusercontent.com/0c8f829897840ac35cb3daf181a719612c0f64c0ed5fca3c7b90ed7591169162/68747470733a2f2f696d672e736869656c64732e696f2f6d61696e74656e616e63652f7965732f323032352e737667)![composer](https://camo.githubusercontent.com/7a6cce75e3353cd615b111f2f4ff50dec30cf814dddb88b2613f656cec298330/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f636f6d706f7365722d253345322e302d626c75652e737667)![php](https://camo.githubusercontent.com/59558613d05bebac3748d4f75f0c94435dec5fb11d059b448c2d172e25d82120/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d372e342d677265656e2e737667)![php](https://camo.githubusercontent.com/5cd91a78fb469ca20b235b6951fb6dd77bda78ac4633eb432e93699bcb141589/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d382e342d677265656e2e737667)![CocoaPods](https://camo.githubusercontent.com/347353606ed8f26b45bcf9da083db0063fa1dadd1baef36a5f3bf9ce1d127548/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f646f63732d37302532352d79656c6c6f772e737667)

What is a state machine?.
-------------------------

[](#what-is-a-state-machine)

A State Machine (also called **Automata**) is a procedural execution of a **job** based in **states**. Every job must have a single state at the same time, such as "INITIATED","PENDING","IN PROCESS" and so on, and the job changes of state (**transition**) according to some logic or condition. Such conditions could be a field, a time or a custom function.

The target of this library is to ease the process to create a state machine for business.

Table of Contents
-----------------

[](#table-of-contents)

- [StateMachineOne](#statemachineone)
    - [What is a state machine?.](#what-is-a-state-machine)
    - [Table of Contents](#table-of-contents)
    - [Notes](#notes)
    - [Example, ChopSuey Chinese Delivery Food.](#example-chopsuey-chinese-delivery-food)
        - [Fields (ChopSuey's exercise)](#fields-chopsueys-exercise)
        - [States (ChopSuey's exercise)](#states-chopsueys-exercise)
        - [Transitions (ChopSuey's exercise)](#transitions-chopsueys-exercise)
        - [Final Code (ChopSuey's example)](#final-code-chopsueys-example)
    - [Other examples](#other-examples)
    - [Transition language](#transition-language)
    - [The transition language is written with the next syntax.](#the-transition-language-is-written-with-the-next-syntax)
        - [Transition when](#transition-when)
            - [For example](#for-example-)
        - [Transition set](#transition-set)
        - [Transition else](#transition-else)
        - [Transition timeout (in seconds)](#transition-timeout-in-seconds)
        - [Transition fulltimeout (in seconds)](#transition-fulltimeout-in-seconds)
    - [JOB](#job)
        - [What is a job?](#what-is-a-job)
        - [Creating a job](#creating-a-job)
        - [Running the state machine in a job](#running-the-state-machine-in-a-job)
        - [Getting a job](#getting-a-job)
        - [Database and jobs.](#database-and-jobs)
        - [Fields used in Job](#fields-used-in-job)
    - [Flags](#flags)
    - [GUI](#gui)
    - [Classes](#classes)
    - [Cache Configuration](#cache-configuration)
        - [Saving configuration](#saving-configuration)
        - [Loading configuration:](#loading-configuration)
    - [Log Format](#log-format)
        - [\[info\]](#info)
        - [\[error\]](#error)
    - [License](#license)
    - [Version](#version)
    - [What is missing](#what-is-missing)

Notes
-----

[](#notes)

- **Job:** it's the process to run. A job could have a single state at the same time.
- **State:** it's the current condition of the job.
- **Transition:** it's the change from one **state** to another. The transition is conditioned to a set of values, time or a function.
    Also, every transition could have a timeout. If the timeout is reached then the transition is done, no matter the values or the conditions (even if it has the **active** state paused). The transition could have 3 outcomes:
    - **change** The transition changes of state and the job is keep active. It is only possible to do the transition if the job has the **active state** = active.
    - **pause** The transition changes of state and the job is paused. It is only possible to do the transition if the job has the **active state** = active.
    - **continue** The transition changes of state and the job resumes of the pause. It is only possible to do the transition if the job has the **active state** = pause or active
    - **stop** The transition changes of state and the job is stopped. It is only possible to do the transition if the job has the **active state** = active or pause.
    - **stay** The transition does not change of state, but it does the operations defined by the set.
    - **stayonce** It's similar than stay, but it does the operation once.
    - **Event**: (Optional). Events are special operation that changes one or more states.
- **Active:** Every job has an **active state**. There are 4: none,stop,active,inactive,pause. It is different from the states. So, for example, a job could have the **state**: INPROGRESS and the **active state**: PAUSE.
    - **none** = the job doesn't exist. It can't change of state, neither it is loaded (from the database) by default
    - **stop** = the job has stopped (finished), it could be a successful, aborted or canceled. It can't change of state neither it is loaded by default.
    - **pause** = the job is on hold, it will not change of state (unless it is forced) but it could be continued.
    - **active** = the job is running normally, it could change of state.
    - **inactive** = the job is scheduled to run at some date. it couldn't change of state unless it is activated (by schedule)
- **Refs:** Every job has some related fields. For example, if the job is about an invoice, then refs must be the number of the invoice. The State Machine doesn't use any refs, but it keeps the values for integration with other systems.
- **Fields:** Every job has some fields or values, it could trigger a transition.

Example, ChopSuey Chinese Delivery Food.
----------------------------------------

[](#example-chopsuey-chinese-delivery-food)

We need to create a process to deliver Chinese food at home. Is it easy?. Well, let's find it out.

[![ChopSuey](Docs/ChopSuey.jpg)](Docs/ChopSuey.jpg)

### Fields (ChopSuey's exercise)

[](#fields-chopsueys-exercise)

**Fields** are values used for out State Machine. In this example, I am not including other values that it could be useful (such as money, customer name, address and such) because they are not part or used by the state machine.

- **customerpresent** =1 if the customer is at home, 0=if not, =null not defined yet
- **addressnotfound** =1 if the address is not found by the delivery boy, =0 if found, =null if it's not yet defined.
- **signeddeliver** =1 if the customer signed the delivery, =0 if not, =null if it's not defined.
- **abort** =1 if the delivery must be aborted (for example, an accident), =0 if not.
- **instock** =1 if the product is in stock, =0 if it's not, =null if it is not defined.
- **picked** =1 if the delivery boy picked and packed the product, =0 if not yet.

### States (ChopSuey's exercise)

[](#states-chopsueys-exercise)

It must include all the possible situation. The real world is not as easy as: sell and money.

- **STATE\_PICK** The delivery boy will pick the food if any. For example, what if a customer asks for Pekin's duck (with orange sauce) and the restaurant doesn't have?.

[![cooker](Docs/cooker.jpg)](Docs/cooker.jpg)

- **STATE\_CANCEL** The order is canceled.
- **STATE\_TRANSPORT** The delivery boy is on route to deliver the food.

[![cooker](Docs/deliveryboy.jpg)](Docs/deliveryboy.jpg)

- **STATE\_ABORTTRANSPORT** Something happened, the delivery must be aborted.
- **STATE\_HELP** The delivery boy is ready to deliver, but he is not able to find the address or maybe there is nobody, so he calls for help.
- **STATE\_DELIVERED** The food is delivered. Our hero returns to base (Chinese restaurant).

[![cooker](Docs/food.jpg)](Docs/food.jpg)

- **STATE\_ABORTED** The transaction is aborted, nobody at home or the address is wrong.

### Transitions (ChopSuey's exercise)

[](#transitions-chopsueys-exercise)

- **STATE\_PICK** -&gt; **STATE\_CANCEL** (END) When?. instock=0 (end of the job)
- **STATE\_PICK** -&gt; **STATE\_TRANSPORT** When?. instock=1 and picked=1
- **STATE\_TRANSPORT** -&gt; **STATE\_ABORTTRANSPORT** (END) When?. abort=1 (for some reason, our boy abort the transport, is it raining?)
- **STATE\_TRANSPORT** -&gt; **STATE\_DELIVERED** (END) When?. addressnotfound=0,customerpresent=1 and signeddeliver=1. It is delivered, the customer is present, and it signed the delivery (plus a tip, I hope it)
- **STATE\_TRANSPORT** -&gt; **STATE\_HELP** When?. addressnotfound=1,customerpresent=0 and signeddeliver&lt;&gt;1. Our delivery calls to home and ask for new instructions. Is it Fake Street #1234 the right address?.
- **STATE\_HELP** -&gt; **STATE\_ABORTED** (END) When?. (15 minutes deadline) or if abort=1.
    Our delivery called home and yes, the address is fake (it's a shocking surprise)
- **STATE\_HELP** -&gt; **STATE\_DELIVERED** (END) When?. addressnotfound=0,customerpresent=1 and signeddeliver=1. It is delivered, the customer is present, and it signed the delivery (plus a tip, I hope it)

### Final Code (ChopSuey's example)

[](#final-code-chopsueys-example)

[Example/ChopSuey.php](example/ChopSuey.php)

Other examples
--------------

[](#other-examples)

[Example/BuyMilk.php](example/BuyMilk.php) (buy milk)

[Example/Car.php](example/Car.php) (car parking)

```
