Example #1
0
 /**
  * @param EventInterface $event
  * @param mixed callback
  * @param StateMachine $stateMachine
  *
  * @since Method available since Release 2.0.0
  */
 public function logActionCall(EventInterface $event, $payload, StateMachine $stateMachine)
 {
     foreach (debug_backtrace() as $stackFrame) {
         if ($stackFrame['function'] == 'runAction' || $stackFrame['function'] == 'evaluateGuard') {
             $calledBy = $stackFrame['function'];
         }
     }
     $this->actionCalls[] = array('state' => $stateMachine->getCurrentState()->getStateId(), 'event' => $event->getEventId(), 'calledBy' => @$calledBy);
 }
 /**
  * Sets the exit action to the state.
  *
  * @param string   $stateId
  * @param callback $action
  *
  * @throws ActionNotCallableException
  * @throws StateNotFoundException
  */
 public function setExitAction($stateId, $action)
 {
     $state = $this->stateMachine->getState($stateId);
     if ($state === null) {
         throw new StateNotFoundException(sprintf('The state "%s" is not found in the state machine "%s".', $stateId, $this->stateMachine->getStateMachineId()));
     }
     if (!is_callable($action)) {
         throw new ActionNotCallableException(sprintf('The action for the event "%s" in the state "%s" is not callable.', EventInterface::EVENT_EXIT, $stateId));
     }
     $state->getEvent(EventInterface::EVENT_EXIT)->setAction($action);
 }
 /**
  * Adds an state transition to the state machine.
  *
  * @param string $stateId
  * @param string $eventId
  * @param string $nextStateId
  *
  * @throws StateNotFoundException
  */
 public function addTransition($stateId, $eventId, $nextStateId)
 {
     $state = $this->stateMachine->getState($stateId);
     if ($state === null) {
         throw new StateNotFoundException(sprintf('The state "%s" is not found.', $stateId));
     }
     $event = $state->getEvent($eventId);
     if ($event === null) {
         $event = new TransitionEvent($eventId);
     }
     $nextState = $this->stateMachine->getState($nextStateId);
     if ($nextState === null) {
         throw new StateNotFoundException(sprintf('The state "%s" is not found.', $nextStateId));
     }
     $this->stateMachine->addTransition(new Transition($nextState, $state, $event));
 }
Example #4
0
 /**
  * Triggers an event.
  *
  * @param  string                                             $eventID
  * @return \Stagehand\FSM\State
  * @throws \Piece\Flow\PageFlow\PageFlowNotActivatedException
  */
 public function triggerEvent($eventID)
 {
     if (is_null($this->getCurrentState())) {
         throw new PageFlowNotActivatedException('The page flow must be activated to trigger any event.');
     }
     parent::triggerEvent($eventID);
     if ($this->getCurrentState()->isEndState()) {
         $this->triggerEvent(PageFlowInterface::EVENT_END);
     }
     return $this->getCurrentState();
 }