Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zinovyev/php-fsm
PHP Finite-state Machine
https://github.com/zinovyev/php-fsm
Last synced: about 2 months ago
JSON representation
PHP Finite-state Machine
- Host: GitHub
- URL: https://github.com/zinovyev/php-fsm
- Owner: zinovyev
- License: gpl-2.0
- Created: 2014-05-13T14:48:43.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2017-06-29T07:29:08.000Z (over 7 years ago)
- Last Synced: 2024-09-19T08:48:30.333Z (4 months ago)
- Language: PHP
- Homepage:
- Size: 114 KB
- Stars: 9
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
PHP Finite-state Machine
=======Finite-state machine allows you to create an object, containing different states and transitions between them,
that can change its behaivour according to the current state.## Install:
For installing the FSM machine use Composer. Simply add following to your composer.json file:
```json
"require": {
"zinovyev/php-fsm": "0.6.5"
}
```## Creating a State machine steps:
+ Create State classes and define states:
```php
class StateA extends State
{
public function foo($name)
{
printf("Hello, %s!", $name);
}
}
```+ Create a new FSM\Client instance (StateMachine).
+ Bind your States to the Client instance, apply transactions and set initial state:```php
class StateMachine extends Client
{
public function __construct()
{
parent::__construct();
/* ... */
$this
->addState($stateA)
/* ... */
->createTransition('fourth', 'stateA', 'stateB')
/* ... */
```
or
```php
$stateMachine = new Client;
$stateMachine
->addState($stateA)
->addState($stateB)
->createTransition('fourth', 'stateA', 'stateB')
;
```+ Use Memento if you want do store current State and parameters and restore them later:
```php
$memento = $stateMachine->createMemento();
```
and restore:
```php
$stateMachine = new StateMachine();
$stateMachine->applyMemento($memento);
```+ That's all =) Your simple State machine is now configured and ready for use!
## Example code:
```php
setName('stateA');
$stateA->setType(StateInterface::TYPE_INITIAL);
// Create StateB finite type instance of class StateB
$stateB = new StateB();
$stateB->setName('stateB');
$stateB->setType(StateInterface::TYPE_FINITE);
// Create StateC regular type instance of class StateC
$stateC = new StateC();
$stateC->setName('stateC');
$stateC->setType(StateInterface::TYPE_REGULAR);
// Create StateD regular type instance of class StateB
$stateD = new StateB();
$stateD->setName('stateD');
$stateD->setType(StateInterface::TYPE_REGULAR);
// Attach states and transitions
$this
->addState($stateA)
->addState($stateB)
->addState($stateC)
->addState($stateD)
->setInitialState($stateA)
->createTransition('initial', 'stateA', 'stateA')
->createTransition('second', 'stateA', 'stateC')
->createTransition('secondAlternative', 'stateA', 'stateD')
->createTransition('third', 'stateC', 'stateD')
->createTransition('thirdAlternative', 'stateD', 'stateC')
->createTransition('fourth', 'stateD', 'stateB')
->createTransition('fourthAlternative', 'stateC', 'stateB')
->createTransition('shortWay', 'stateA', 'stateB')
;
}
}// Create new StateMachine instance
$stateMachine = new StateMachine();
$stateMachine->foo = 'bar'; // Add public property// Test StateA state
printf("%d) State machine is at state: '%s'. Test function call result is: '%s'\n", 1,
$stateMachine->getCurrentState()->getName(), // Get State Name
$stateMachine->callAction('foo', $properties = [100]) // Call State function
);// Accept transition "initial"
$stateMachine->acceptTransitionByName('initial');
printf("%d) State machine is at SAME state: '%s'. Test function call result: '%s'\n", 2,
$stateMachine->getCurrentState()->getName(), // Get State Name
$stateMachine->callAction('foo', $properties = [200]) // Call State function
);// Accept transition "second"
$stateMachine->acceptTransitionByName('second');
printf("%d) State machine is at state: '%s'. Test function call result: '%s'\n", 3,
$stateMachine->getCurrentState()->getName(), // Get State Name
$stateMachine->fooBar('foo bar') // Call State function
);// Create a memento (snapshot) of the current state
$memento = $stateMachine->createMemento();// Unset StateMachine
$stateMachine = null;
unset($stateMachine);// Restore StateMachine from a snapshot (Memento)
$stateMachine = new StateMachine();
$stateMachine->applyMemento($memento);printf("=*= Check property value after restore: \$foo='%s' =*=\n", $stateMachine->foo);
// Accept transition "third"
$stateMachine->acceptTransitionByName('third');
printf("%d) State machine is at state '%s'. Test function call result: '%s'\n", 4,
$stateMachine->getCurrentState()->getName(), // Get State Name
$stateMachine->bar(1) // Call State function
);// Accept transition "fourth"
$stateMachine->acceptTransitionByName('fourth');
printf("%d) State machine is at state '%s'. Test function call result: '%s'\n", 5,
$stateMachine->getCurrentState()->getName(), // Get State Name
$stateMachine->bar(2) // Call State function
);
```## Example code execution result:
*If you run the code in your console, you'll see the following output*:
```
1) State machine is at state: 'stateA'. Test function call result is: '100'
2) State machine is at SAME state: 'stateA'. Test function call result: '200'
3) State machine is at state: 'stateC'. Test function call result: 'rab oof'
=*= Check property value after restore: $foo='bar' =*=
4) State machine is at state 'stateD'. Test function call result: '2'
5) State machine is at state 'stateB'. Test function call result: '4'
```