Skip to content

Commit

Permalink
Merge pull request #6 from formapro/pvm-class-map
Browse files Browse the repository at this point in the history
Intr schema, enchance object builder. fix doc.
  • Loading branch information
makasim authored Sep 28, 2017
2 parents 43bc303 + eda01f5 commit a6e6952
Show file tree
Hide file tree
Showing 16 changed files with 102 additions and 51 deletions.
5 changes: 4 additions & 1 deletion docs/conditions-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ use Formapro\Pvm\CallbackBehavior;
use Formapro\Pvm\ProcessEngine;
use Formapro\Pvm\Process;
use Formapro\Pvm\Token;
use Formapro\Pvm\ObjectBuilderHook;

(new ObjectBuilderHook())->register();

$registry = new DefaultBehaviorRegistry();
$registry->register('print_label', new CallbackBehavior(function(Token $token) {
Expand All @@ -20,7 +23,7 @@ $registry->register('condition', new CallbackBehavior(function(Token $token) {
return ['first'];
}));

$process = new Process();
$process = Process::create();
$foo = $process->createNode();
$foo->setLabel('foo');
$foo->setBehavior('condition');
Expand Down
5 changes: 4 additions & 1 deletion docs/cycle-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ use Formapro\Pvm\Process;
use Formapro\Pvm\Token;
use function Makasim\Values\get_value;
use function Makasim\Values\set_value;
use Formapro\Pvm\ObjectBuilderHook;

(new ObjectBuilderHook())->register();

$registry = new DefaultBehaviorRegistry();
$registry->register('print_label', new CallbackBehavior(function(Token $token) {
Expand All @@ -27,7 +30,7 @@ $registry->register('go_back_first_three_times', new CallbackBehavior(function(T
return $counter < 3 ? ['back'] : ['forth'];
}));

$process = new Process();
$process = Process::create();
$foo = $process->createNode();
$foo->setLabel('foo');
$foo->setBehavior('print_label');
Expand Down
5 changes: 4 additions & 1 deletion docs/fork-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ use Formapro\Pvm\CallbackBehavior;
use Formapro\Pvm\ProcessEngine;
use Formapro\Pvm\Process;
use Formapro\Pvm\Token;
use Formapro\Pvm\ObjectBuilderHook;

(new ObjectBuilderHook())->register();

$registry = new DefaultBehaviorRegistry();
$registry->register('print_label', new CallbackBehavior(function(Token $token) {
echo $token->getTransition()->getTo()->getLabel().' ';
}));

$process = new Process();
$process = Process::create();
$foo = $process->createNode();
$foo->setLabel('foo');
$foo->setBehavior('print_label');
Expand Down
5 changes: 4 additions & 1 deletion docs/mongo-storage-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ namespace Acme;

use Formapro\Pvm\Process;
use Formapro\Pvm\Yadm\MongoProcessStorage;
use Formapro\Pvm\ObjectBuilderHook;

$process = new Process();
(new ObjectBuilderHook())->register();

$process = Process::create();
$fooNode = $process->createNode();
$fooNode->setLabel('foo');
$fooNode->setBehavior('print_label');
Expand Down
5 changes: 4 additions & 1 deletion docs/parallel-execution-with-enqueue.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ use Formapro\Pvm\ProcessStorage;
use Formapro\Pvm\Process;
use Formapro\Pvm\Token;
use Formapro\Pvm\Enqueue\AsyncTransition;
use Formapro\Pvm\ObjectBuilderHook;

(new ObjectBuilderHook())->register();

$client = new SimpleClient('amqp://');
$asyncTransition = new AsyncTransition($client->getProducer());
Expand All @@ -36,7 +39,7 @@ $registry->register('print_label', new CallbackBehavior(function(Token $token) {
echo $token->getTransition()->getTo()->getLabel().' ';
}));

$process = new Process();
$process = Process::create();
$foo = $process->createNode();
$foo->setLabel('foo');
$foo->setBehavior('print_label');
Expand Down
5 changes: 4 additions & 1 deletion docs/pause-and-continue-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ use Formapro\Pvm\Token;
use Formapro\Pvm\Exception\WaitExecutionException;
use function Makasim\Values\set_value;
use function Makasim\Values\get_value;
use Formapro\Pvm\ObjectBuilderHook;

(new ObjectBuilderHook())->register();

$registry = new DefaultBehaviorRegistry();
$registry->register('pause_and_continue', new CallbackBehavior(function(Token $token) {
Expand All @@ -25,7 +28,7 @@ $registry->register('pause_and_continue', new CallbackBehavior(function(Token $t
echo 'purchased. ';
}));

$process = new Process();
$process = Process::create();
$fooNode = $process->createNode();
$fooNode->setLabel('foo');
$fooNode->setBehavior('pause_and_continue');
Expand Down
5 changes: 4 additions & 1 deletion docs/sequence-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ use Formapro\Pvm\CallbackBehavior;
use Formapro\Pvm\ProcessEngine;
use Formapro\Pvm\Process;
use Formapro\Pvm\Token;
use Formapro\Pvm\ObjectBuilderHook;

(new ObjectBuilderHook())->register();

$registry = new DefaultBehaviorRegistry();
$registry->register('print_label', new CallbackBehavior(function(Token $token) {
echo $token->getTransition()->getTo()->getLabel().' ';
}));

$process = new Process();
$process = Process::create();
$fooNode = $process->createNode();
$fooNode->setLabel('foo');
$fooNode->setBehavior('print_label');
Expand Down
5 changes: 4 additions & 1 deletion docs/synchronization-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ use Formapro\Pvm\CallbackBehavior;
use Formapro\Pvm\ProcessEngine;
use Formapro\Pvm\Process;
use Formapro\Pvm\Token;
use Formapro\Pvm\ObjectBuilderHook;

(new ObjectBuilderHook())->register();

$registry = new DefaultBehaviorRegistry();
$registry->register('print_label', new CallbackBehavior(function(Token $token) {
Expand All @@ -38,7 +41,7 @@ $registry->register('join', new CallbackBehavior(function (Token $token) {
throw new InterruptExecutionException();
}));

$process = new Process();
$process = Process::create();

$fork = $process->createNode();
$fork->setLabel('fork');
Expand Down
2 changes: 1 addition & 1 deletion src/Yadm/CreateTrait.php → src/CreateTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ trait CreateTrait
public static function create(array $data = [])
{
return build_object(null, array_replace([
'class' => static::class,
'schema' => static::SCHEMA,
], $data));
}
}
2 changes: 2 additions & 0 deletions src/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

class Node
{
const SCHEMA = 'http://pvm.forma-pro.com/schemas/Node.json';

use ValuesTrait {
setValue as public;
getValue as public;
Expand Down
33 changes: 33 additions & 0 deletions src/ObjectBuilderHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
namespace Formapro\Pvm;

use function Makasim\Values\register_global_hook;

class ObjectBuilderHook
{
/**
* @var PvmClassMap
*/
private $classMap;

/**
* @param string[] $classMap
*/
public function __construct(array $classMap = [])
{
$this->classMap = new PvmClassMap($classMap);
}

public function register()
{
register_global_hook('get_object_class', function(array $values) {
if (isset($values['schema'])) {
if (false == array_key_exists($values['schema'], $this->classMap->get())) {
throw new \LogicException(sprintf('An object has class set "%s" but there is no class for it', $values['class']));
}

return $this->classMap[$values['schema']];
}
});
}
}
2 changes: 2 additions & 0 deletions src/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

class Process
{
const SCHEMA = 'http://pvm.forma-pro.com/schemas/Process.json';

use ValuesTrait {
getValue as public;
setValue as public;
Expand Down
28 changes: 28 additions & 0 deletions src/PvmClassMap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
namespace Formapro\Pvm;

class PvmClassMap
{
/**
* @var string[]
*/
private $classMap;

/**
* @param string[] $classMap
*/
public function __construct(array $classMap = [])
{
$this->classMap = array_replace([
Process::SCHEMA => Process::class,
Node::SCHEMA => Node::class,
Token::SCHEMA => Token::class,
Transition::SCHEMA => Transition::class,
], $classMap);
}

public function get(): array
{
return $this->classMap;
}
}
2 changes: 2 additions & 0 deletions src/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

class Token
{
const SCHEMA = 'http://pvm.forma-pro.com/schemas/Token.json';

use ValuesTrait;
use CreateTrait;

Expand Down
2 changes: 2 additions & 0 deletions src/Transition.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

class Transition
{
const SCHEMA = 'http://pvm.forma-pro.com/schemas/Transition.json';

use ValuesTrait {
setValue as public;
getValue as public;
Expand Down
42 changes: 0 additions & 42 deletions src/Yadm/ObjectBuilderHook.php

This file was deleted.

0 comments on commit a6e6952

Please sign in to comment.