/** * {@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; }
/** * @test */ public function shouldLoadTransitionsFromJSONString() { $machine = new StateMachine(new Context(new Identifier('json-test', 'json-machine'))); $this->assertCount(0, $machine->getTransitions()); $json = $this->getJSON(); $loader = new JSON($json); $this->assertEquals($this->getJSON(), $loader->getJSON()); $count = $loader->load($machine); $this->assertCount(2, $machine->getTransitions()); $this->assertEquals(2, $count); $tbd = $machine->getTransition('b_to_done'); $b = $tbd->getStateFrom(); $d = $tbd->getStateTo(); $tab = $machine->getTransition('a_to_b'); $a = $tab->getStateFrom(); $this->assertEquals($b, $tab->getStateTo()); $this->assertSame($b, $tab->getStateTo()); $this->assertTrue($a->isInitial()); $this->assertTrue($b->isNormal()); $this->assertTrue($d->isFinal()); }
/** * {@inheritDoc} * Load the statemachine with data from a JSON string. * the JSON string is stored at the redis key '<prefix:>configuration' by default. * you can alter the configuration key by using Redis::setPrefix() and Redis::setConfigurationKey() * * First, the key '<prefix>:configuration:<machine-name>' is checked for existence. * If it exists, take the configuration from that key, else take the configuration form * the '<prefix>:configuration' key. * * This method can be overriden in a subclass to use another loader when * the data is stored in redis in YAML or XML form for example. * 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) $key = $this->getConfigurationKey(); $redis = $this->getRedis(); $specific_key = sprintf(self::KEY_CONFIGURATION_SPECIFIC, $key, $statemachine->getContext()->getMachine()); if ($redis->exists($specific_key)) { $key = $specific_key; } $loader = new JSON($this->getRedis()->get($key)); $count = $loader->load($statemachine); return $count; }