I'm wondering if Laravel has some built-in state machine mechanism? And if not, what's the best way to use this excellent library called Finite (https://github.com/yohang/Finite).
Here's what I have (use case : a job board) :
To start, I made my model "stateful":
use Finite\StatefulInterface;
class Offer extends Eloquent implements StatefulInterface {
Then in my offers controller's store action:
$stateMachine = new StateMachine();
$stateMachine->addState(new State('created', StateInterface::TYPE_INITIAL));
$stateMachine->addState('draft');
$stateMachine->addState(new State('published', StateInterface::TYPE_FINAL));
$stateMachine->addTransition('preview', 'created', 'draft');
$stateMachine->addTransition('publish', 'draft', 'published');
$stateMachine->setObject($offer);
$stateMachine->initialize();
From what I understand, when a user previews an offer (for example), I should be calling:
$stateMachine->apply('preview').
My question is:
How do I keep track of all the states and transitions across my app? Do I store states in my Offer model? Do I create additional tables?
A system where particular inputs cause particular changes in state can be represented using finite state machines. This example describes the various states of a turnstile. Inserting a coin into a turnstile will unlock it, and after the turnstile has been pushed, it locks again.
Introduction. A Finite State Machine, or FSM, is a computation model that can be used to simulate sequential logic, or, in other words, to represent and control execution flow. Finite State Machines can be used to model problems in many fields, including mathematics, artificial intelligence, games or linguistics.
Finite state machine (FSM) is a term used by programmers, mathematicians, engineers and other professionals to describe a mathematical model for any system that has a limited number of conditional states of being.
A state machine reads a set of inputs and changes to a different state based on those inputs. A state is a description of the status of a system waiting to execute a transition. A transition is a set of actions to execute when a condition is fulfilled or an event received.
Please head to this gist
: FiniteAuditTrail Trait: Such a good starting point for your request!
PHP Files of interest:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With