- Notifications
You must be signed in to change notification settings - Fork9
Statemachine in PHP 5.6 / PHP 7
License
Metabor/Statemachine
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Statemachine in PHP 5.6 / PHP 7
Onceinstalled, let's use a sample statemachine:
<?phprequire_once'vendor/autoload.php';useMetabor\Statemachine\Process;useMetabor\Statemachine\State;useMetabor\Statemachine\Statemachine;useMetabor\Statemachine\Transition;$closed =newState('closed');$opened =newState('opened');$eventOpen ='open';$eventClose ='close';$closed->addTransition(newTransition($opened,$eventOpen));$closed->addTransition(newTransition($closed,$eventClose));$opened->addTransition(newTransition($opened,$eventOpen));$opened->addTransition(newTransition($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 =newstdClass();// start process with closed status;$initialState =$closed;$process =newProcess('process name',$initialState);$statemachine =newStatemachine($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 motorecho'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;
This library implements afinite-state machine in PHP 5.3.
It was first developed for a talk at a conference. The example from my talk is available on Github and Packagist asmetabor/statemachine-example.
In the namespace MetaborStd are abstract types defined that are exemplified implemented in this project.If you have to implement or use a statemachine in your project, feel free to either use this libary at all or replace the parts that didn't fit your needs by using the MetaborStd Interfaces.
The library supports visualizing of the process graph by using clue/graph andGraphViz "Graph Visualization Software".
The recommended way to install this library isthrough composer.New to composer?
{"require": {"metabor/statemachine":"~1.2" }}Optional recommendation:In order to be able to use theprocess graph drawing feature you'll have toinstall GraphViz (dot executable). Users of Debian/Ubuntu-based distributions may simplyinvokesudo apt-get install graphviz, Windows users have todownload GraphViZ for Windows and remainingusers should install fromGraphViz homepage.To use this feature you also have to add this to your composer.json:
{"require": {"graphp/graphviz":"*","clue/graph":"*","metabor/statemachine":"~1.2" }}Anexample how to draw and display the graph, can be found inmetabor/statemachine-example.
This library uses phpunit for its extensive testsuite.You can either use a global installation or rely on the one composer installswhen you first run$ composer install.This sets up the developer environment, so that youcan now run it from the project root directory:
$ php vendor/bin/phpunit
If you encounter any issues, please don't hesitate to drop us a line, file a bug report or even best provide us with a patch / pull request and/or unit test to reproduce your problem.
Besides directly working with the code, any additional documentation, additions to our readme or even fixing simple typos are appreciated just as well.
Any feedback and/or contribution is welcome!
Released under the terms of the permissiveMIT license.
About
Statemachine in PHP 5.6 / PHP 7
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors9
Uh oh!
There was an error while loading.Please reload this page.


