Download the PHP package bvtterfly/model-state-machine without Composer
On this page you can find all versions of the php package bvtterfly/model-state-machine. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bvtterfly/model-state-machine
More information about bvtterfly/model-state-machine
Files in bvtterfly/model-state-machine
Package model-state-machine
Short Description Laravel Model State Machine
License MIT
Homepage https://github.com/bvtterfly/model-state-machine
Informations about the package model-state-machine
🚨 THIS PACKAGE HAS BEEN ABANDONED 🚨
I no longer use Laravel and cannot justify the time needed to maintain this package. That's why I have chosen to abandon it. Feel free to fork my code and maintain your own copy.
Laravel Model State Machine
This package adds support for creating state machines for attributes on Laravel Eloquent Models.
Requirements
- PHP 8.1 or higher
- Laravel 9.x or higher
Installation
You can install the package via composer:
Usage
For example, we have a blog system, and our blog posts have three statuses: draft, pending, and published. When we are writing a post, it's in the draft status. Whenever we finish writing our blog posts, we schedule them for publication in the future, which changes the status of the post to pending. Once the post is published, the status changes to published.
The simplest backed enum that holds the states of a blog post status is:
Here's what the Blog post model would look like:
A model can have as many state machine fields as you want,
You need to add them to the list using the getStateMachineFields
method.
Since State machine loads state configuration from string backed enums, You need to cast state machine fields to correlated state enums in your model.
Now, You can get your state machine:
Get All states
You can use the getAllStates
method, which return collection of the all available states:
Get All allowed transitions
You can use the getStateTransitions
method, which return collection of available transitions for current/initial state
If the state field is null and the state configuration doesn't have a initial state (field in unknown state), It will throw an exception.
If you want to get available transitions for a state, You can pass it to the method:
Using transitions
To use transitions, call the transitionTo
method on the state field as follows:
You can pass array as a second argument to the
transitionTo
method for additional data that you'll need in your actions and transitions.
State Actions
You can add actions to run if a state changes to a state. In the above example, Maybe we want to send a tweet and send email to subscribers when the post is published.
We can do this using the #[Actions]
attribute. Here's how our PostState
would look like:
Actions are classes that implements Bvtterfly\ModelStateMachine\Contracts\StateMachineAction
:
Your actions may also type-hint any dependencies they need on their constructors. All actions are resolved via the Laravel service container, so dependencies will be injected automatically.
Transition Actions
In addition to state actions, maybe you want to run actions only when a specific state transit to another state.
You can pass array of actions as second argument to #[AllowTransitionTo]
.
In the above example, If we want to send a notification to the admin when the post status change to the pending, Our PostState
would look like this:
Transition Actions run before State Actions
Action With Validation
When using transitions, you can pass additional data as a second argument, and this data will pass to all actions. So, It's necessary to validate this data before running actions.
Validators are actions that implement Bvtterfly\ModelStateMachine\Contracts\StateMachineValidation
.
In above example, We want to send notification to admin when post status changes to pending:
Here's how our SendNotificationToAdmin
action would look like:
Custom transition Classes
This package comes with a default transition class that save new state after running State & Transition Actions. If you need to do more than just changing to the new state, you can use transition classes.
Custom transition are classes that implements Bvtterfly\ModelStateMachine\Contracts\StateTransition
.
For example, We want to store the user_id
of who changes the status of a post to pending
status in the post model:
You can pass this class as a third argument to the #[AllowTransitionTo]
.
Then, Our PostState
would look like this:
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Ari
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of model-state-machine with dependencies
spatie/laravel-package-tools Version ^1.9.2
illuminate/contracts Version ^9.0|^10.0