저자: Yohan Giarelli (yohan@frequence-web.fr)
예제 #1
0
 /**
  * {@inheritdoc}
  *
  * @param StateMachineInterface $stateMachine
  */
 public function load(StateMachineInterface $stateMachine)
 {
     $stateMachine->setStateAccessor(new PropertyPathStateAccessor($this->config['property_path']));
     $stateMachine->setGraph($this->config['graph']);
     array_walk($this->config['states'], array($stateMachine, 'addState'));
     array_walk($this->config['transitions'], array($stateMachine, 'addTransition'));
 }
예제 #2
0
 private function addTransitions(Digraph $graph, StateMachine $stateMachine)
 {
     foreach ($stateMachine->getStates() as $stateName) {
         $state = $stateMachine->getState($stateName);
         foreach ($state->getTransitions() as $transitionName) {
             $transition = $stateMachine->getTransition($transitionName);
             $graph->beginEdge(array($stateName, $transition->getState()), array('label' => $transitionName))->end();
         }
     }
 }
예제 #3
0
 /**
  * @param StateMachineInterface $stateMachine
  */
 private function loadTransitions(StateMachineInterface $stateMachine)
 {
     $resolver = new OptionsResolver();
     $resolver->setRequired(array('from', 'to'));
     $resolver->setOptional(array('guard'));
     $resolver->setNormalizers(array('from' => function (Options $options, $v) {
         return (array) $v;
     }, 'guard' => function (Options $options, $v) {
         return !isset($v) ? null : $v;
     }));
     foreach ($this->config['transitions'] as $transition => $config) {
         $config = $resolver->resolve($config);
         $stateMachine->addTransition(new Transition($transition, $config['from'], $config['to'], $config['guard']));
     }
 }
예제 #4
0
 /**
  * @param StateMachineInterface $stateMachine
  */
 private function loadTransitions(StateMachineInterface $stateMachine)
 {
     $resolver = new OptionsResolver();
     $resolver->setRequired(array('from', 'to'));
     $resolver->setDefaults(array('guard' => null, 'configure_properties' => null, 'properties' => array()));
     $resolver->setAllowedTypes('configure_properties', array('null', 'callable'));
     $resolver->setNormalizer('from', function (Options $options, $v) {
         return (array) $v;
     });
     $resolver->setNormalizer('guard', function (Options $options, $v) {
         return !isset($v) ? null : $v;
     });
     $resolver->setNormalizer('configure_properties', function (Options $options, $v) {
         $resolver = new OptionsResolver();
         $resolver->setDefaults($options['properties']);
         if (is_callable($v)) {
             $v($resolver);
         }
         return $resolver;
     });
     foreach ($this->config['transitions'] as $transition => $config) {
         $config = $resolver->resolve($config);
         $stateMachine->addTransition(new Transition($transition, $config['from'], $config['to'], $config['guard'], $config['configure_properties']));
     }
 }
예제 #5
0
 /**
  * @param StateMachineInterface $sm
  * @param string                $event
  * @param callable              $callback
  * @param array                 $specs
  *
  * @return $this
  */
 protected function add(StateMachineInterface $sm, $event, $callback, array $specs)
 {
     $specs = $this->processSpecs($specs);
     $listener = function (TransitionEvent $e) use($sm, $callback, $specs) {
         if ($sm !== $e->getStateMachine()) {
             return;
         }
         if (!(in_array(CallbackHandler::ALL, $specs['to']) || in_array($e->getTransition()->getState(), $specs['to']))) {
             return;
         }
         if (!(in_array(CallbackHandler::ALL, $specs['from']) || in_array($e->getInitialState()->getName(), $specs['from']))) {
             return;
         }
         if (in_array($e->getTransition()->getState(), $specs['exclude_to'])) {
             return;
         }
         if (in_array($e->getInitialState()->getName(), $specs['exclude_from'])) {
             return;
         }
         call_user_func($callback, $sm->getObject(), $e);
     };
     $events = array($event);
     if (count($specs['on']) > 0 && !in_array(self::ALL, $specs['on'])) {
         $events = array_map(function ($v) use($event) {
             return $event . '.' . $v;
         }, $specs['on']);
     }
     foreach ($events as $event) {
         $this->dispatcher->addListener($event, $listener);
     }
     return $this;
 }
예제 #6
0
 /**
  * Adds all transitions as edges.
  *
  * @param \Finite\StateMachine\StateMachineInterface $stateMachine
  */
 private function addEdges(StateMachineInterface $stateMachine)
 {
     $states = $stateMachine->getStates();
     foreach ($states as $sName) {
         $state = $stateMachine->getState($sName);
         $transitions = $state->getTransitions();
         foreach ($transitions as $tName) {
             $trans = $stateMachine->getTransition($tName);
             $attributes = $this->getEdgeDefaultAttributes($trans);
             if ($this->visitors) {
                 try {
                     foreach ($this->visitors as $visitor) {
                         $attributes = $visitor->getEdgeAttributes($attributes, $trans, $state, $stateMachine);
                     }
                 } catch (SkipElementException $ex) {
                     continue;
                 }
             }
             $this->graph->beginEdge([$state->getName(), $trans->getState()], $attributes)->end();
         }
     }
 }
예제 #7
0
 /**
  * Adds all transitions as edges.
  *
  * @param \Finite\StateMachine\StateMachineInterface $stateMachine
  */
 private function addEdges(StateMachineInterface $stateMachine)
 {
     $states = $stateMachine->getStates();
     foreach ($states as $name) {
         $state = $stateMachine->getState($name);
         /* @var $state \Finite\State\StateInterface */
         $transitions = $state->getTransitions();
         foreach ($transitions as $name) {
             $trans = $stateMachine->getTransition($name);
             /* @var $trans Finite\Transition\TransitionInterface */
             $this->graph->beginEdge(array($state->getName(), $trans->getState()), array('label' => $trans->getName()))->end();
         }
     }
 }