Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ibraheem-ghazi/laravel-stager

Laravel Stager State Machine, Its purpose is to add state machine functionality to models
https://github.com/ibraheem-ghazi/laravel-stager

generator laravel laravel-framework schedule stager-methods state-machine states statful trait transition

Last synced: about 1 month ago
JSON representation

Laravel Stager State Machine, Its purpose is to add state machine functionality to models

Awesome Lists containing this project

README

        

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
==============

```
composer require ibraheem-ghazi/stager
php artisan vendor:publish --provider="IbraheemGhazi\Stager\StagerServiceProvider"
```

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

```php
[
'ide-helper-path'=>'stager-methods-ide-helper.php',//where to save ide helper file (currently in root)
'fail-throw-exception'=>true, //useful for debug , for production its better to turn it off
'schedule-cronjob'=>'0 * * * *', //run cron job every hour once to handle schedule
'unauthorized-gaurd-exception'=>true,//should throw unauthorized exception if guard requesting this transition is not in guard array , if false then can method only return false without exception

//generator config
'constants-prefix'=>'STATE_', //
'shared-trait'=>[
// you can use this a trait to add shared functionality to all models that use stager
// or to override scopeStateChangeWithin , getStateChangedAt functions

// \App\Traits\SharedTrait::class,
],
],

////////////////////////////////////////////

\App\Payment::class => [

'state-column'=>'state', //(optional, default: state)

'init-state'=>'pending',//(optional, default: the first defined state

'states' => [
//state-name => numeric value
'pending' =>1,
'payment-accepted'=>2,
'in-progress'=>3,
'ended' => 4,
],

'schedules'=>[

//state-name
'pending'=>[

//if last state change time has passed trigger-delay then it will be included in transition run
// for example: run transition for all payments that has state 'pending' and changed from 2 days or more
'trigger-delay' =>[
'time-modifier'=>"DAY",
'interval'=>2
],

// the transition to be run on each row apply this schedule requirement (state , last state change time)
'transition'=>'payment-success',

// run commands after transition excuted
'commands'=>[
'command:subcommand'=>['param1'=>'val1'],
]
],
],
'transitions' => [
'payment-success' => [
'from' => 'pending',
'to' => 'payment-accepted',
'relation-state-condition'=>[
//relation class must be defined here in state-machine config
'some-realtion'=>'status'
],
'guard'=>['web'],//array of guards or string equal to '*' [default = '*']
//todo: affection class
'affect'=>[
//relation => transiojn_of_relation
'order' => 'waiting-seller'
],

],
'seller-accept' => [
'from' => 'payment-accept',
'to' => 'in-progress',
],
'finish' => [
'from' => 'in-progress',
'to' => 'ended',
],
'canceled' => [
'from' => 'pending',
'to' => 'ended',
],

],
],
];

```

* any time config file updated you must run

```
$ php artisan stager:generate
-C or --clean clean auto generated code from all registered models
-M or -model \App\MyModel clean auto generated code from sepcified model
```
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