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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

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

Latest Version on Packagist run-tests Check & fix styling Total Downloads

This package adds support for creating state machines for attributes on Laravel Eloquent Models.

Requirements

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

License

The MIT License (MIT). Please see License File for more information.


All versions of model-state-machine with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
spatie/laravel-package-tools Version ^1.9.2
illuminate/contracts Version ^9.0|^10.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package bvtterfly/model-state-machine contains the following files

Loading the files please wait ....