Download the PHP package troojaan/laravel-state-machine without Composer
On this page you can find all versions of the php package troojaan/laravel-state-machine. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download troojaan/laravel-state-machine
More information about troojaan/laravel-state-machine
Files in troojaan/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.
Features
-
It provides dependency injection for the
StateMachineFactory
. -
You can use Laravel's service container to resolve class methods for the callbacks, along with their dependencies.
-
It allows you to listen for transition events using Laravel's event dispatcher.
- A facade is also available for convenience.
Installation
You can install the package via composer. This package requires Laravel 5.1 or higher.
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 resolve callbacks and inject their dependencies from Laravel's Service Container.
Defining a callback
Under the callbacks.guard
array of your configuration, add an associative array to define the callback. The key of the array can be anything you want (e.g. guard_on_submitting
). This array must have a clause, a callback, and may have some arguments.
Clause
First, you need to specify a clause that will determine when the callback will be invoked. A clause has a key (from
, to
, on
, excluded_from
, excluded_to
, excluded_on
) and its value is the state or the transition that should satisfy the clause.
E.g. 'on' => 'submit_changes'
will be triggered when the transition submit changes
is being checked or applied.
Callback
Second, you need to specify the callback that will be invoked under the do
key. The callback must be a callable
, and can be one of the following:
A closure:
A built-in or user-defined function as a string:
A class method as array:
Classes are resolved through the service container with their dependencies.
A class method as string:
Arguments
Using dependency injection
By default, if you don't specify the args
key in your array, all the callback method parameters will be injected automatically using Laravel's service container, like Route/Controller methods.
Type-hints
All type-hinted parameters are resolved from the container, e.g. App $app
.
Object in the state machine
If a parameter has the same type-hint as the object that is associated to the state machine, e.g. App\Article $article
, the instance will be injected.
State machine event
If a parameter has the state machine event type-hint, e.g. SM\Event\TransitionEvent $e
, the event that was triggered in the state machine will be injected. Alternatively you can define a parameter named $event
without a type-hint.
Using ExpressionLanguage notation
Otherwise, you can define one or multiple arguments that will be passed explicitly to your callbacks in the given order, by using the args
key. This package is using Symfony's ExpressionLanguage notation to evaluate expressions.
Here are some examples:
Example
You want to call the handle
method on the MyService
class to determine if the state machine can apply the submit_changes
transition. The handle method will receive the object of the state machine as the first argument, and the transition event as the second argument.
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 an 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.:
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.
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 Sébastien at [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 5.1.* || 5.2.* || 5.3.* || 5.4.* || 5.5.* || 5.6.*
winzou/state-machine Version ^0.3.2