Download the PHP package sebdesign/laravel-state-machine without Composer
On this page you can find all versions of the php package sebdesign/laravel-state-machine. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sebdesign/laravel-state-machine
More information about sebdesign/laravel-state-machine
Files in sebdesign/laravel-state-machine
Package laravel-state-machine
Short Description Winzou State Machine service provider for Laravel
License MIT
Homepage https://github.com/sebdesign/laravel-state-machine
Informations about the package laravel-state-machine
Winzou State Machine service provider for Laravel
This is a Laravel service provider for winzou/state-machine. It provides dependency injection for the StateMachineFactory
. You can also use Laravel's service container to resolve class methods for the callbacks. A facade is also available for convenience.
Installation
You can install the package via composer. This package requires Laravel 7.0 or higher.
For previous Laravel versions, please check the compatibility table.
Then require the package using the command-line interface:
Versions
If you need to install this package in older Laravel installations, use the table below to find a compatible version.
Package | Laravel | PHP |
---|---|---|
^3.0 | ^7.0 - ^9.0 |
^7.2.5 |
^2.0 | 5.5.* - ^6.0 |
^7.0 |
^1.0 | 5.1.* - 5.8.* |
^5.5.9 | ^7.0 |
Since version 5.5, Laravel uses package auto-discovery, so you don't need to manually add the ServiceProvider and the facade. If you don't use auto-discovery or you are using an older version, add the service provider and the facade in config/app.php.
Configuration
Publish the config file in config/state-machine.php
.
Please see the documentation of the StateMachineBundle for all the available options.
Usage
Now you can use the $stateMachine
to interact with the state of the $article
.
Callbacks
Callbacks are used to guard transitions or execute some code before or after applying transitions. This package adds the ability to use Laravel's service container to resolve callbacks.
E.g.:
You want to call the handle
method on the MyService
class to determine if the state machine can apply the submit_changes
transition.
You can specify callbacks in array format, e.g. ['Class', 'method']
, or in @ delimited string format, e.g. Class@method
.
Using Gates and Policies
If you don't want to use custom classes/methods for guarding, you can use Laravel's authorization gates or policies for determining if a transition can be applied.
Instead of specifying a do
key, you must define a can
key with the name of the ability you want to check. Since Laravel 5.5, you can also specify an array of abilities, and every one will be checked.
By default, the object instance will be passed as an argument to the gate. You can also override the arguments by specifying them in the args
key.
Example using a gate:
In this example, we have defined a gate which will accept the $article
as and argument. You are not required to define this parameter in your gate if you don't need it.
Example using a policy:
Say you have created an ArticlePolicy
policy for your Article
model, which has an approve
method.
You should define approve
in the can
index. This will be the equivalent of calling $user->can('approve', $article)
.
You can also override the arguments that will be passed in the approved
method, by specifying the args
array. You must use object
as the first argument in order for the policy class to be resolved. E.g.: 'args' => ['object', '"final_approval"']
would be the equivalent of calling $user->can('approve', [$article, 'final_approval'])
.
Events
When checking if a transition can be applied, the SM\Event\SMEvents::TEST_TRANSITION
event is fired.
Before and after a transition is being applied, the SM\Event\SMEvents::PRE_TRANSITION
and SM\Event\SMEvents::POST_TRANSITION
events are fired respectively.
All the events receive a Sebdesign\SM\Event\TransitionEvent
instance.
If you wish to listen to all the events with the same listener, you can use the winzou.state_machine.*
wildcard parameter.
You can define your own listeners in your app's EventServiceProvider
. E.g.:
Context
You can also pass additional data as an array when checking or applying transitions.
This array will be passed to the Sebdesign\SM\Event\TransitionEvent
.
You can access the array using $event->getContext()
in your event listeners or callbacks.
Example using an event listener:
Example using a callback:
Metadata
You can optionally store metadata in graphs, states and transitions. The metadata are stored in associative arrays, and can be anything you want.
The state machine object offers many flexible ways to fetch metadata, either as associative arrays, either specific values by keys. You can also pass default values or closures in case the specified key doesn't exist.
Get metadata from the graph
Get metadata from a state
Get metadata from the current state
Get metadata from a transition
Debug command
An artisan command for debugging graphs is included. It accepts the name of the graph as an argument. If no arguments are passed, the graph name will be asked interactively.
Visualize command
An artisan command for generating an image of a given graph is included. It accepts the name of the graph as an argument. It's taken from the corresponding bundle for Symfony: https://github.com/MadMind/StateMachineVisualizationBundle, so all credits goes to the original author.
If you want to run this command, you need to have installed dot - Part of graphviz package (http://www.graphviz.org/). In your mac, this is equal to having run
Statable trait for Eloquent models
If you want to interact with the state machine directly within your models, you can install the laravel-statable package by iben12.
This package allows you to get the graph from the model, checking/applying transitions, as well as recording the state history in the database.
Changelog
Please see CHANGELOG for more information what has changed recently.
Testing
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-state-machine with dependencies
illuminate/support Version ^7.0 | ^8.0 | ^9.0 | ^10.0 | ^11.0
winzou/state-machine Version ^0.4.2|^0.4