You are here

public function StateMachine::fire_event in State Machine 6

Same name and namespace in other branches
  1. 7.3 inc/base.inc \StateMachine::fire_event()
  2. 7 inc/base.inc \StateMachine::fire_event()
  3. 7.2 inc/base.inc \StateMachine::fire_event()

Trigger an event to process a transition. Callbacks and guard conditions will be processed in the following order:

  • event:before_transition
  • event:guard, exits if guard condition return FALSE
  • oldstate:on_exit
  • update the current state
  • newstate:on_enter
  • event:after_transition

File

inc/base.inc, line 163

Class

StateMachine

Code

public function fire_event($key) {
  $event = $this
    ->get_event($key);
  if ($event && ($new_state = $event
    ->execute())) {

    // Allow the previous state to run its 'on_exit' callbacks.
    $this
      ->get_state($this
      ->get_current_state())
      ->on_exit();

    // Set and save the new state.
    $this
      ->set_current_state($new_state);
    $this
      ->persist();

    // Allow the new state to run its 'on_enter' callbacks.
    $this
      ->get_state($this
      ->get_current_state())
      ->on_enter();

    // Allow the event to "finish"
    $event
      ->finish();
  }
  else {
    $this
      ->on_event_fail($event);
    return FALSE;
  }
}