izzum\statemachine\persistence\MongoDB::load PHP Метод

load() публичный Метод

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 load ( StateMachine $statemachine )
$statemachine izzum\statemachine\StateMachine
    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;
    }

Usage Example

 /**
  * @test
  */
 public function shouldBeAbleToStoreAndRetrieveData()
 {
     $adapter = new MongoDB("mongodb://localhost:27017");
     //fixture
     $adapter->getClient()->izzum->states->drop();
     $adapter->getClient()->izzum->configuration->drop();
     $adapter->getClient()->izzum->history->drop();
     $configuration = file_get_contents(__DIR__ . '/../loader/fixture-example.json');
     //via the mongo shell, you could directly enter the json.
     //via php, we first need to decode the json to input it via the php mongo driver as an array
     $configuration = json_decode($configuration, true);
     //var_dump( $configuration);
     $adapter->getClient()->izzum->configuration->insert($configuration);
     //end fixture
     $machine = new StateMachine(new Context(new Identifier('mongo', 'test-machine'), null, $adapter));
     $adapter->load($machine);
     $machine->add("adding for " . __FUNCTION__);
     $machine->runToCompletion("testing 213");
     $this->assertEquals(array("mongo"), $adapter->getEntityIds('test-machine'));
     $this->assertEquals(array("mongo"), $adapter->getEntityIds('test-machine', 'done'));
     $this->assertEquals(array(), $adapter->getEntityIds('test-machine', 'a'));
     $this->assertEquals(array(), $adapter->getEntityIds('test-machine', 'b'));
     $this->assertEquals(array(), $adapter->getEntityIds('test-machine', 'c'));
     $machine = new StateMachine(new Context(new Identifier('another-mongo', 'test-machine'), null, $adapter));
     $adapter->load($machine);
     $machine->add("adding for " . __FUNCTION__);
     $machine->runToCompletion("testing 213");
     $machine = new StateMachine(new Context(new Identifier('foobar', 'non-used-machine'), null, $adapter));
     $adapter->load($machine);
     $machine->add("adding for " . __FUNCTION__);
     $machine->runToCompletion("testing 213");
     $index = array("entity_id" => 1, "machine" => 1);
     $options = array("background" => true);
     $adapter->getClient()->izzum->history->createIndex($index, $options);
     //getting the state for an entity_id/machine should be fast
     //db.states.createIndex({entity_id: 1, machine: 1}, {background: true});
     $index = array("entity_id" => 1, "machine" => 1);
     $options = array("background" => true);
     $adapter->getClient()->izzum->states->createIndex($index, $options);
     //recreate the existing statemachine
     $machine = new StateMachine(new Context(new Identifier('another-mongo', 'test-machine'), null, $adapter));
     $adapter->load($machine);
     $this->assertFalse($machine->add(), 'already added');
     $this->assertEquals('done', $machine->getCurrentState()->getName(), 'state persisted');
     $this->assertEquals(0, $machine->runToCompletion(), 'alread in a final state, no transitions');
     $this->markTestIncomplete("needs more tests to check the database and some scenarios");
 }