You are here

class TestMachine in State Machine 7.3

Same name and namespace in other branches
  1. 7 state_machine.api.php \TestMachine
  2. 7.2 state_machine.api.php \TestMachine

The State Machine module is an API only. You must extend the StateMachine class to use this module. Extend the StateMachine class to get started

Hierarchy

Expanded class hierarchy of TestMachine

See also

state_flow_node.inc for a complete example.

File

./state_machine.api.php, line 14
State Machine Developer Documentation

View source
class TestMachine extends StateMachine {

  /**
   * Called from StateMachine::__construct to initialize the states and events.
   *
   * Defines States Draft, Published and Unpublished.
   * Define Events Publish, Unpublish and To Draft
   */
  public function init() {

    // Initialize states
    // @see StateMachine::create_state
    // Add Draft State.
    $this
      ->create_state('draft');

    // Add Published State
    // We Use the the "on_enter and on_exit functionality."
    $this
      ->create_state('published', array(
      // When the state is set to "Published" run the mehtod
      // "on_enter_published" on this object.
      'on_enter' => array(
        $this,
        'on_enter_published',
      ),
      // When leaving the state "Published" then run the mehtod
      // "on_exit_published" on this object.
      'on_exit' => array(
        $this,
        'on_exit_published',
      ),
    ));

    // Add the state "unpublished"
    $this
      ->create_state('unpublished');

    // Initialize events.
    // @see StateMachine::create_event
    // Add the pusblish event to  move the state from "draft" to "published".
    $this
      ->create_event('publish', array(
      'origin' => 'draft',
      'target' => 'published',
    ));

    // Add the unpusblish event to  move the state from "published" to
    // "unpublished".
    $this
      ->create_event('unpublish', array(
      'origin' => 'published',
      'target' => 'unpublished',
    ));

    // Add the "to draft" event to move the state from unpublished to draft.
    $this
      ->create_event('to draft', array(
      'origin' => 'unpublished',
      'target' => 'draft',
    ));
  }

  /**
   * Actions when entering the state.
   */
  public function on_enter_published() {

    // Do some stuff.
  }

  /**
   * Actions when leaving the state.
   */
  public function on_exit_published() {

    // Do some stuff.
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StateMachine::$current protected property 1
StateMachine::$events protected property
StateMachine::$initial protected property 1
StateMachine::$object protected property 1
StateMachine::$states protected property
StateMachine::create_event protected function Create a new event.
StateMachine::create_state protected function Create a new state.
StateMachine::fire_event public function Trigger an event to process a transition. 1
StateMachine::get_all_events public function Get all of the events.
StateMachine::get_available_events public function Returns an array of events that are valid for the current state.
StateMachine::get_current_state public function Returns the current state.
StateMachine::get_event public function Return an event instance by key, lazy-loading the instance if necessary. 1
StateMachine::get_state public function Return a state instance by key, lazy-loading the instance if necessary.
StateMachine::ignore public function Whether State Machine to be ignored.
StateMachine::load protected function Load the current state from the given state storage. 1
StateMachine::on_event_fail protected function Method to be called when firing an event fails for any reason.
StateMachine::persist protected function Persist the current state to the object storage.
StateMachine::set_current_state protected function Set the current state to the state identified by the specified key.
StateMachine::set_initial_state protected function Set the initial state for this machine.
StateMachine::__construct public function Create instance of StateMachine. 1
TestMachine::init public function Called from StateMachine::__construct to initialize the states and events. Overrides StateMachine::init
TestMachine::on_enter_published public function Actions when entering the state.
TestMachine::on_exit_published public function Actions when leaving the state.