acacha/stateful-eloquent
Composer 安装命令:
composer require acacha/stateful-eloquent
包简介
State machines for Eloquent classes
README 文档
README
If you're familiar with my AASM, then this is a similar take – just implemented in Laravel 5 for Eloquent classes.
References
Forked from https://github.com/mikerice/stateful-eloquent
Installation
Step 1: Install Through Composer
composer require acacha/stateful-eloquent
Step 2: Add the Service Provider
Acacha\Stateful\Providers\StatefulServiceProvider::class,
Step 3: Update your Eloquent Model
Your models should use the Stateful trait and interface
use Acacha\Stateful\Traits\StatefulTrait; use Acacha\Stateful\Contracts\Stateful; class Transaction extends Model implements Stateful { use StatefulTrait; }
Step 4: Create your Model States
Your models should have an array name $states that define your model states.
/** * Transaction States * * @var array */ protected $states = [ 'draft' => ['initial' => true], 'processing', 'errored', 'active', 'closed' => ['final' => true] ];
Step 5: Create your Model State Transitions
/** * Transaction State Transitions * * @var array */ protected $transitions = [ 'process' => [ 'from' => ['draft', 'errored'], 'to' => 'processing' ], 'activate' => [ 'from' => 'processing', 'to' => 'active' ], 'fail' => [ 'from' => 'processing', 'to' => 'errored' ], 'close' => [ 'from' => 'active', 'to' => 'close' ] ]; /** * @return bool */ protected function validateProcess() { $validate = true; if (!$validate) { $this->addValidateProcessMessage(); } return $validate; } /** * @return bool */ protected function validateActivate() { //dd("validateActivate"); return true; } /** * @return bool */ protected function validateFail() { //dd("validateFail"); return true; } /** * @return bool */ protected function validateClose() { //dd("validateClose"); return true; } protected function beforeProcess() { //dd("doing something before entering processing state"); } protected function afterProcess() { //dd("doing something after leaving processing state"); }
Database configuration
You model migration should have a state field like:
class CreateModelTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('model', function (Blueprint $table) { $table->increments('id'); ... $table->string('state'); ... $table->timestamps(); }); }
Usage
$transaction = new Transaction(); //Execute transition $transaction->process(); //Check if a state is active (return true or false) $transaction->active();
统计信息
- 总下载量: 7.17k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 11
- 点击次数: 4
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-11-20