Store to MongoDB

In the previous example we showed how to store a process to file.
In this example we show how to store it to MongoDB using makasim/yadm library. Whenever the engine changes the state of a process or token it is persisted using InMemoryDAL (Data access layer).

Setup


mkdir ~/pvm-store-to-mongodb
cd ~/pvm-store-to-mongodb
composer req formapro/pvm:0.4.x-dev makasim/values:0.5.x-dev makasim/yadm:0.5.x-dev

# create such file with the code below
php store-to-mongodb.php

Result

store-to-mongodb.php

Connected to mongodb://mongo/pvm_demo
a_task
Found the process in DB

store-to-mongodb.php


<?php
use Formapro\Pvm\DefaultBehaviorRegistry;
use 
Formapro\Pvm\Process;
use 
Formapro\Pvm\ProcessEngine;
use 
Formapro\Pvm\Token;
use 
Formapro\Pvm\ProcessBuilder;
use 
Formapro\Pvm\Yadm\InProcessDAL;
use 
Makasim\Yadm\CollectionFactory;
use 
Makasim\Yadm\Hydrator;
use 
Makasim\Yadm\Storage;

require_once 
__DIR__.'/vendor/autoload.php';

$mongoDsn getenv('MONGO_DSN');
$mongoClient = new \MongoDB\Client($mongoDsn);
$mongoCollection = (new CollectionFactory($mongoClient$mongoDsn))->create('pvm_process');
$processStorage = new Storage($mongoCollection, new Hydrator(Process::class));
echo 
'Connected to '.$mongoDsn.PHP_EOL;

$yadmDal = new InProcessDAL($processStorage);

$process = (new ProcessBuilder())
    ->
createNode('a_task''print_label')->end()
    ->
createStartTransition('a_task')->end()

    ->
getProcess()
;

$processId $process->getId();

$registry = new DefaultBehaviorRegistry([
    
'print_label' => function(Token $token) {
        echo 
$token->getTo()->getId().PHP_EOL;
    },
]);

$engine = new ProcessEngine($registry$yadmDal);

$token $engine->createTokenFor($process->getStartTransition());
$engine->proceed($token);

if (
$process $processStorage->findOne(['id' => $process->getId()])) {
    echo 
'Found the process in DB'.PHP_EOL;
} else {
    echo 
'the process was not found in DB'.PHP_EOL;
}
{
    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Process.json",
    "id": "0b7e5022-2bfc-4edd-a673-308b98924767",
    "nodes": {
        "a_task": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Node.json",
            "id": "a_task",
            "behavior": "print_label"
        }
    },
    "transitions": {
        "cda5f1f5-e2b4-4ce5-af5a-e9339493d68f": {
            "id": "cda5f1f5-e2b4-4ce5-af5a-e9339493d68f",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "a_task"
        }
    },
    "inTransitions": {
        "a_task": [
            "cda5f1f5-e2b4-4ce5-af5a-e9339493d68f"
        ]
    },
    "tokens": {
        "b2efb950-12e5-4a7b-86ee-ae2594abfe99": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "b2efb950-12e5-4a7b-86ee-ae2594abfe99",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "6c16d074-c81d-4761-8834-2886bb37cabc",
                    "transitionId": "cda5f1f5-e2b4-4ce5-af5a-e9339493d68f",
                    "weight": 1,
                    "state": "opened",
                    "time": 15968741906134
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "0e272d0a-2f9c-45bc-9249-84e04daf5297",
                    "transitionId": "cda5f1f5-e2b4-4ce5-af5a-e9339493d68f",
                    "weight": 1,
                    "state": "passed",
                    "time": 15968741906137
                }
            ]
        }
    }
}