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": "2bdeb53c-c629-4f64-aca4-c79946933784",
    "nodes": {
        "a_task": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Node.json",
            "id": "a_task",
            "behavior": "print_label"
        }
    },
    "transitions": {
        "d003fc98-1697-4888-af88-9dd65ae7a9df": {
            "id": "d003fc98-1697-4888-af88-9dd65ae7a9df",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "a_task"
        }
    },
    "inTransitions": {
        "a_task": [
            "d003fc98-1697-4888-af88-9dd65ae7a9df"
        ]
    },
    "tokens": {
        "0ad256ad-4dbc-4d11-ac74-61466e56a05f": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "0ad256ad-4dbc-4d11-ac74-61466e56a05f",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "f2847774-2870-433c-9fde-b613da1fa1a2",
                    "transitionId": "d003fc98-1697-4888-af88-9dd65ae7a9df",
                    "weight": 1,
                    "state": "opened",
                    "time": 15421461957021
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "72db3e14-da33-4b22-8e7b-7c049be37d19",
                    "transitionId": "d003fc98-1697-4888-af88-9dd65ae7a9df",
                    "weight": 1,
                    "state": "passed",
                    "time": 15421461957026
                }
            ]
        }
    }
}