yohang / finite
A simple PHP Finite State Machine
Installs: 3 059 360
Dependents: 21
Suggesters: 2
Security: 0
Stars: 1 309
Watchers: 58
Forks: 188
Open Issues: 61
Requires
- php: >=7.4
- symfony/event-dispatcher: ^4.0|^5.0|^6.0
- symfony/options-resolver: ^4.0|^5.0|^6.0
- symfony/property-access: ^4.0|^5.0|^6.0
Requires (Dev)
- phpunit/phpunit: ^8.5.25
- pimple/pimple: ^1.1.1
- symfony/dependency-injection: ^4.0|^5.0|^6.0
- symfony/framework-bundle: ^4.0|^5.0|^6.0
- symfony/security-bundle: ^4.0|^5.0|^6.0
- symfony/serializer: ^4.0|^5.0|^6.0
- twig/twig: ^2.0|^3.0
Suggests
- pimple/pimple: Needed for use with PimpleFactory
- symfony/security: Needed for using SecurityAwareStateMachine
- symfony/serializer: Needed for the use of the normalizer
- symfony/yaml: Yaml allows you to define your State graph in YAML
- dev-future / 2.0.x-dev
- dev-master / 1.3.x-dev
- 1.3.5
- 1.3.4
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.x-dev
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- 1.0.0-BETA2
- 1.0.0-BETA1
- dev-symfony-5-php-8
- dev-feature-new-docs
- dev-compatibility-symfony3
- dev-refactor-callback-handler
- dev-apply_additional_param
- dev-scrutinizer-patch-1
- dev-feature-context-graph-aware
This package is auto-updated.
Last update: 2024-11-25 12:41:17 UTC
README
Finite is a Simple State Machine, written in PHP. It can manage any Stateful object by defining states and transitions between these states.
Features
- Managing State/Transition graph for an object
- Defining and retrieving properties for states
- Event Listenable transitions
- Symfony2 integration
- Custom state graph loaders
- Twig Extension
Documentation
Documentation for master (1.1)
Getting started
Installation (via composer)
{ "require": { "yohang/finite": "~1.1" } }
Version note :
If your are using this library in a Symfony project, 1.1 version is only compatible with Symfony >=2.6
.
1.0 is compatible with Symfony >=2.3, <2.6
.
Define your Stateful Object
Your stateful object just need to implement the StatefulInterface
Interface.
use Finite\StatefulInterface; class Document implements StatefulInterface { private $state; public function setFiniteState($state) { $this->state = $state; } public function getFiniteState() { return $this->state; } }
Initializing a simple StateMachine
use Finite\StateMachine\StateMachine; use Finite\State\State; use Finite\State\StateInterface; // $document = retrieve your stateful object $sm = new StateMachine(); // Define states $sm->addState(new State('s1', StateInterface::TYPE_INITIAL)); $sm->addState('s2'); $sm->addState('s3'); $sm->addState(new State('s4', StateInterface::TYPE_FINAL)); // Define transitions $sm->addTransition('t12', 's1', 's2'); $sm->addTransition('t23', 's2', 's3'); $sm->addTransition('t34', 's3', 's4'); $sm->addTransition('t42', 's4', 's2'); // Initialize $sm->setObject($document); $sm->initialize(); // Retrieve current state $sm->getCurrentState(); // Can we process a transition ? $sm->can('t34');