Download the PHP package ibraheem-ghazi/stager without Composer
On this page you can find all versions of the php package ibraheem-ghazi/stager. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ibraheem-ghazi/stager
More information about ibraheem-ghazi/stager
Files in ibraheem-ghazi/stager
Package stager
Short Description Laravel state machine for eloquent
License MIT
Informations about the package stager
Laravel Stager State Machine
This is a Laravel 5.6 package for eloquent models. Its purpose is to add state machine functionalty to models
Features
- all state-machines in one config file
- states defined as name,value
- transitions from multiple status
- transition affect related model
- schedules run every X times you defined
- schedules transitions depending on state and last time state changed
- schedules support run command depending on state
- support shared trait for shared props and attribute or override {some} of stager methods
- support events before and after transitions
- support additional executions or extra conditions before access transition
- auto generate ide helper file for magic functions
- [new] guarded transitions
- [new] pre transition realtion status checker
- [new] added support for collection transitions
Installation
Configuration
- in order to add state machine you have to define it in
config/state-machine.php
Notes:
- each state define a constant by generator
- each state must have unique value for current model so it does not overlap others when get current state name.
- state-column and init-state attributes are optional which by default are state-column = state and init-state is by default first state
-
each attribute key must be in kebab-case
- any time config file updated you must run
this command will auto modify actual model file and add needed use
statements and write needed constants from states
//an example of auto-generated data to model
<?php
namespace App;
use \IbraheemGhazi\Stager\Traits\Stager;
use Illuminate\Database\Eloquent\Model;
class Payment extends Model
{
/**** AUTO-GENERATED STAGER DATA ****/
use Stager;
const STATE_PENDING = 1;
const STATE_PAYMENT_ACCEPTED = 2;
const STATE_IN_PROGRESS = 3;
const STATE_ENDED = 4;
/**** END OF AUTO-GENERATED STAGER DATA ****/
}
Note: you must not edit anything between these comments (or comments it self):
/**** AUTO-GENERATED STAGER DATA ****/
...
/**** END OF AUTO-GENERATED STAGER DATA ****/
the generator will read model file if it has these comments it will replace it's content with new auto generated code otherwise if it can not find these comment it will assume the class does not contains that data
Shared Trait
shared trait used to define a shared properties for all models that use stager, for example if you want to add a relation , it's mainly defined so you can easily override 2 Stager methods for last state changed time :
getStateChangedAt() get last time state has changed
scopeStateChangeWithin($query, $modifier, $interval) a scope to get only rows that changed Within X Days for example
preTransitionNameTransition Function
if a function defined in the model with studly case of transition has prefix pre
and suffix Transition
it will be executed before transition action
if it return TRUE
it will complete the transition otherwise it will abort it
example: assume you have a transition named : cancel-order then
where ...$args
is array of parameters passed from doCancelOrder
Functions Available
func | params | return | description |
---|---|---|---|
getCurrentStateValue | - | mixed | return value of current state/ |
getCurrentStateName | - | string | return name of current state/ |
hasValidStateMachine | - | bool | check if this model has valid state machine or not/ |
Magic Functions Available
each magic function has prefix [is, can, do] with studly case of target either state or transition
Transition Life Cycle
- check if can transition from current state
- if model define preTransitionNameTransition then it will execute , if it return true the transition continue otherwise it will abort (fail)
- trigger beforeTransition event
- update the state
- execute affection if exists
- trigger afterTransition event