Code Monkey home page Code Monkey logo

kohana-statemachine's Introduction

Kohana StateMachine Module

This is a fairly simple state machine module for Kohana 3. It can be used standalone or with ORM.

Example Usage

Standalone StateMachine

Initialize a StateMachine

// Array of valid states, and the list of valid transitions from that state.
$transitions = array(
	'pending'  => array('active', 'rejected'),
	'active'   => array('deleted'),
	'rejected' => array('deleted'),
	'deleted'  => array(),
);

// Array of callbacks to be executed when transitioning to a state.
$transition_callbacks = array(
	'rejected' => array($this, 'transition_to_rejected_callback'),
);

// The intial state
$initial_state = 'pending';

// Initialize the StateMachine
$state_machine = StateMachine::factory($initial_state, array(
	'transitions'          => $transitions,
	'transition_callbacks' => $transition_callbacks,
));

Get the current state

$state_machine->state();

Check if we can transition to a supplied state, from the current state

$state_machine->can_transition('deleted');

Check if we can transition to a supplied state, from a supplied state

$state_machine->can_transition('deleted', 'pending');

Transition to the supplied state

$state_machine->can_transition('active');

Generate and return a PNG image documenting the statemachine

$state_machine->generate_diagram('png');

ORM_StateMachine Extension

Initialize a StateMachine

class Model_User extends ORM_StateMachine {

	/**
	 * @var  string  Initial State
	 */
	protected $_initial_state = 'pending';

	/**
	 * @var  string  Column to use for state management
	 */
	protected $_state_column = 'state';

	/**
	 * Returns the transitions array
	 * 
	 * @return  array
	 */
	public function transitions()
	{
		return array(
			'pending'  => array('active', 'rejected'),
			'active'   => array('deleted'),
			'rejected' => array('deleted'),
			'deleted'  => array(),
		);
	}

	/**
	 * Returns the transition callbacks array
	 * 
	 * @return  array
	 */
	public function transition_callbacks()
	{
		return array(
			'rejected' => array($this, 'transition_to_rejected_callback'),
		);
	}

	/**
	 * Callback triggered when a user is rejected
	 * 
	 * @return  array
	 */
	public function transition_to_rejected_callback($state_to, $state_from)
	{
		// Do something... Maybe email a rejection letter?

		// Remember that this is called regardless of if the model has been
		// saved or not!
	}
}

Get the current state

$model->state();

Check if we can transition to a supplied state, from the current state

$model->can_transition('deleted');

Check if we can transition to a supplied state, from a supplied state

$model->can_transition('deleted', 'pending');

Transition to the supplied state and save.

$model->transition('active')->save();

kohana-statemachine's People

Contributors

kiall avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.