izzum\statemachine\loader\JSON::load PHP Method

load() public method

{@inheritDoc}
public load ( StateMachine $stateMachine )
$stateMachine izzum\statemachine\StateMachine
    public function load(StateMachine $stateMachine)
    {
        //decode the json in a php object structure
        $decoded = json_decode($this->getJSON(), false);
        if (!$decoded) {
            //could not decode (make sure that fully qualified names are escaped with
            //2 backslashes: \\izzum\\commands\\NullCommand and that only double quotes are used.
            throw new Exception(sprintf('could not decode json data. did you only use double quotes? check the json format against %s', 'http://jsonlint.com/'), Exception::BAD_LOADERDATA);
        }
        $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 json. seems like a wrong configuration.', $name), Exception::BAD_LOADERDATA);
        }
        //accessing json as an object with an @ error suppresion operator ('shut the fuck 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 json 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);
    }

Usage Example

 /**
  * {@inheritDoc}
  * Load the statemachine via a document in a mongodb collection.
  * 
  * The document, originally loaded as a json string (see JSON::getJSONSchema)
  * is stored at the mongodb collection 'configuration' by default.
  * multiple machine definitions can be provided in a single document, or in multiple documents in the collection.
  * The first document containing the 'machines.name' key with the value matching 
  * the name of the $statemachine is used.
  *
  * You could use the ReaderWriterDelegator to use another source to load the configuration from.
  */
 public function load(StateMachine $statemachine)
 {
     //use the JSON loader to load the configuration (see the json schema we expect in JSON::getJSONSchema)
     //mongodb does not store JSON but documents (converts the json structure) and the mongodb
     //php library returns these documents as php objects.
     //therefore, we json_encode it again, so it can be json_decoded in the JSON class :-(
     //alternatively, we could write a PHP Loader, but the assumption is that the speed gain is not worth it.
     $loader = new JSON(json_encode($this->getClient()->izzum->configuration->findOne(array("machines.name" => $statemachine->getContext()->getMachine()))));
     $count = $loader->load($statemachine);
     return $count;
 }
All Usage Examples Of izzum\statemachine\loader\JSON::load