chareice / laravel-eloquent-fsm
A finite state machine works with laravel eloquent model
dev-main
2023-07-03 19:05 UTC
Requires
- php: ^8.1
- illuminate/contracts: ^9.0
- spatie/laravel-package-tools: ^1.9.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.8
- nunomaduro/collision: ^6.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
- spatie/laravel-ray: ^1.26
This package is auto-updated.
Last update: 2024-11-09 21:32:31 UTC
README
English | 中文
A finite state machine works with laravel eloquent model
Installation
You can install the package via composer:
composer require chareice/laravel-eloquent-fsm
You can publish and run the migrations with:
php artisan vendor:publish --tag="eloquent-fsm-migrations"
php artisan migrate
Usage
use \Chareice\LaravelEloquentFSM\HasStateMachine; use \Chareice\LaravelEloquentFSM\StateMachineModelInterface; class Order extends Model implements StateMachineModelInterface { use HasStateMachine; public int $counter = 0; // define event public function events(): array { return [ Event::builder() ->setName(OrderStateEvent::PAY) ->setFrom(OrderState::PENDING) ->setTo(OrderState::PAID) ->setAfter(function () { $this->counter = 1; }) ->build(), Event::builder() ->setName(OrderStateEvent::SHIP) ->setFrom(OrderState::PAID) ->setTo(OrderState::SHIPPED) ->build(), ]; } } $order = Order::first(); // run event $order->getStateMachine()->runEvent(OrderStateEvent::PAY); // check new state $order->getStateMachine()->getCurrentState(); // equals OrderState::PAID // check event log $order->stateMachineLogs()->first();
Testing
composer test
Credits
License
The MIT License (MIT). Please see License File for more information.