/**
* {@inheritDoc}
*/
public function load(StateMachine $stateMachine)
{
//decode the json in a php object structure
$decoded = yaml_parse($this->getYaml(), false);
//yaml decoding returns a php array.
$name = $stateMachine->getContext()->getMachine();
$found = false;
$data = null;
if (is_array(@$decoded['machines'])) {
foreach ($decoded['machines'] as $data) {
if ($data['name'] === $name) {
$found = true;
break;
}
}
}
if (!$found) {
//no name match found
throw new Exception(sprintf('no machine data found for "%s" in yaml. seems like a wrong configuration.', $name), Exception::BAD_LOADERDATA);
}
//accessing an array with an @ error suppresion operator ('shut the f**k up' operator),
//allows you to get properties, even if they do not exist, without notices.
//this lets us be a little lazy in mapping the array properties to the state and transition properties
$states = array();
foreach ($data['states'] as $state) {
$tmp = new State($state['name'], $state['type'], @$state['entry_command'], @$state['exit_command'], @$state['entry_callable'], @$state['exit_callable']);
$tmp->setDescription(@$state['description']);
$states[$tmp->getName()] = $tmp;
}
$transitions = array();
foreach ($data['transitions'] as $transition) {
$tmp = new Transition($states[$transition['state_from']], $states[$transition['state_to']], @$transition['event'], @$transition['rule'], @$transition['command'], @$transition['guard_callable'], @$transition['transition_callable']);
$tmp->setDescription(@$transition['description']);
$transitions[] = $tmp;
}
//delegate to loader
$loader = new LoaderArray($transitions);
return $loader->load($stateMachine);
}