/** * @param StateInterface $sourceState * @param TransitionInterface $sourceTransition * * @throws \InvalidArgumentException */ protected function addTransition(StateInterface $sourceState, TransitionInterface $sourceTransition) { if ($sourceState instanceof State) { $sourceState->addTransition($sourceTransition); } else { throw new \InvalidArgumentException('Overwrite this method to implement a different type!'); } }
/** * @param StateInterface $state */ protected function addState(StateInterface $state) { $name = $state->getName(); if ($this->states->hasState($name)) { if ($this->states->getState($name) !== $state) { throw new \Exception('There is already a different state with name "' . $name . '"'); } } else { $this->states->addState($state); /* @var $transition TransitionInterface */ foreach ($state->getTransitions() as $transition) { $targetState = $transition->getTargetState(); $this->addState($targetState); } } }
/** * @param DispatcherInterface $dispatcher * @param string $name * @param \ArrayAccess $context * * @throws \RuntimeException */ public function dispatchEvent(DispatcherInterface $dispatcher, $name, \ArrayAccess $context = null) { if ($this->dispatcher) { throw new \RuntimeException('Event dispatching is still running!'); } else { if ($this->currentState->hasEvent($name)) { $this->dispatcher = $dispatcher; if ($context) { $this->currentContext = $context; } else { $this->currentContext = new \ArrayIterator(array()); } $this->currentEvent = $this->currentState->getEvent($name); $dispatcher->dispatch($this->currentEvent, array($this->subject, $this->currentContext), new Callback(array($this, 'onDispatcherReady'))); } else { throw new \RuntimeException('Current state "' . $this->currentState->getName() . '" did not have event "' . $name . '"'); } } }
/** * @param DispatcherInterface $dispatcher * @param string $name * @param \ArrayAccess $context * * @throws \RuntimeException */ public function dispatchEvent(DispatcherInterface $dispatcher, $name, \ArrayAccess $context = null) { if ($this->dispatcher) { throw new \RuntimeException('Event dispatching is still running!'); } else { if ($this->currentState->hasEvent($name)) { $this->acquireLockOrThrowException(); $this->dispatcher = $dispatcher; if ($context) { $this->currentContext = $context; } else { $this->currentContext = new \ArrayIterator(array()); } $this->currentEvent = $this->currentState->getEvent($name); $dispatcher->dispatch($this->currentEvent, array($this->subject, $this->currentContext), new Callback(array($this, 'onDispatcherReady'))); } else { throw new WrongEventForStateException($this->currentState->getName(), $name); } } }
/** * @param StateInterface $sourceState * * @throws \InvalidArgumentException */ protected function mergeState(StateInterface $sourceState) { $name = $sourceState->getName(); $targetState = $this->findOrCreateState($name); $this->mergeMetadata($sourceState, $targetState); /* @var $transition TransitionInterface */ foreach ($sourceState->getTransitions() as $sourceTransition) { $targetTransition = $this->createTransition($sourceTransition); $this->addTransition($targetState, $targetTransition); } foreach ($sourceState->getEventNames() as $eventName) { $sourceEvent = $sourceState->getEvent($eventName); $targetEvent = $targetState->getEvent($eventName); $this->mergeMetadata($sourceEvent, $targetEvent); foreach ($sourceEvent->getObservers() as $observer) { $targetEvent->attach($observer); } } }
/** * @param TransitionInterface $transition * * @return string */ protected function getTransitionLabel(StateInterface $state, TransitionInterface $transition) { $labelParts = array(); $eventName = $transition->getEventName(); if ($eventName) { $labelParts[] = 'E: ' . $eventName; $event = $state->getEvent($eventName); $observerName = $this->convertObserverToString($event); if ($observerName) { $labelParts[] = 'C: ' . $observerName; } } $conditionName = $transition->getConditionName(); if ($conditionName) { $labelParts[] = 'IF: ' . $conditionName; } if ($transition instanceof WeightedInterface) { $labelParts[] = 'W: ' . $transition->getWeight(); } $label = implode(PHP_EOL, $labelParts); return $label; }
/** * @param StateInterface $sourceState * @param string $eventName * * @return EventInterface */ public function createEvent(StateInterface $sourceState, $eventName) { if ($sourceState instanceof State) { return $sourceState->getEvent($eventName); } else { throw new \InvalidArgumentException('Overwrite this method to implement a different type!'); } }
/** * @param TransitionInterface $transition * @return string */ protected function getTransitionLabel(StateInterface $state, TransitionInterface $transition) { $labelParts = array(); $eventName = $transition->getEventName(); if ($eventName) { $labelParts[] = 'E: ' . $eventName; $event = $state->getEvent($eventName); $observers = $event->getObservers(); $observerName = implode(', ', iterator_to_array($observers, false)); if ($observerName) { $labelParts[] = 'C: ' . $observerName; } } $conditionName = $transition->getConditionName(); if ($conditionName) { $labelParts[] = 'IF: ' . $conditionName; } $label = implode(PHP_EOL, $labelParts); return $label; }