/** * @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 StateInterface $state * * @return \Fhaculty\Graph\Vertex */ public function createStatusVertex(StateInterface $state) { $stateName = $state->getName(); $vertex = $this->graph->createVertex($stateName, true); if ($state instanceof \ArrayAccess) { $layout = $this->getLayoutOptions($state, $this->stateLayout); if (method_exists($vertex, 'setLayout')) { $vertex->setLayout($layout); } else { foreach ($layout as $name => $value) { $vertex->setAttribute('graphviz.' . $name, $value); } } } return $vertex; }
/** * @param StateInterface $state * @return \Fhaculty\Graph\Vertex */ public function createStatusVertex(StateInterface $state) { $stateName = $state->getName(); $vertex = $this->createVertex($stateName, true); if ($state instanceof \ArrayAccess) { $layout = $this->getLayoutOptions($state, $this->stateLayout); $vertex->setLayout($layout); } return $vertex; }