Skip to content

Commit

Permalink
Factored Serializer support into DoctrineExecution and DefinitionStor…
Browse files Browse the repository at this point in the history
…age implementations - Removed JsonSerializer because it cannot serialize/deserialize object instances
  • Loading branch information
beberlei committed Jul 11, 2010
1 parent 74d7f02 commit 85fb1ea
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 97 deletions.
9 changes: 2 additions & 7 deletions lib/DoctrineExtensions/Workflow/DefinitionStorage.php
Expand Up @@ -133,17 +133,12 @@ protected function loadWorkflow($workflowId, $workflowName, $workflowVersion)
foreach ( $result as $node ) {
$node = array_change_key_case($node, \CASE_LOWER);

$configuration = \ezcWorkflowDatabaseUtil::unserialize(
$node['node_configuration'], null
);
$configuration = $this->options->getSerializer()->unserialize($node['node_configuration'], null);

if ( is_null( $configuration ) ) {
$configuration = \ezcWorkflowUtil::getDefaultConfiguration( $node['node_class'] );
}

/*$nodes[$node['node_id']] = new $node['node_class'](
$configuration
);*/
$nodes[$node['node_id']] = $this->options->getNodeFactory()->createNode($node['node_class'], $configuration);

if ($nodes[$node['node_id']] instanceof \ezcWorkflowNodeFinally &&
Expand Down Expand Up @@ -264,7 +259,7 @@ public function save( \ezcWorkflow $workflow )
$this->conn->insert($this->options->nodeTable(), array(
'workflow_id' => (int)$workflow->id,
'node_class' => get_class($node),
'node_configuration' => \ezcWorkflowDatabaseUtil::serialize( $node->getConfiguration() ),
'node_configuration' => $this->options->getSerializer()->serialize( $node->getConfiguration() ),
));

$nodeId = $this->conn->lastInsertId();
Expand Down
69 changes: 32 additions & 37 deletions lib/DoctrineExtensions/Workflow/DoctrineExecution.php
Expand Up @@ -50,15 +50,6 @@ protected function doStart($parentId)
$platform = $this->conn->getDatabasePlatform();

$variables = $this->variables;
$executionEntityName = null;
$executionEntityId = null;

if (isset($variables['entityName'])) {
$executionEntityName = $variables['entityName'];
}
if (isset($variables['entityId'])) {
$executionEntityId = $variables['entityId'];
}

$executionNextPollDate = null;
if (isset($variables['batchWaitInterval'])) {
Expand All @@ -67,22 +58,22 @@ protected function doStart($parentId)
}

$executionNextPollDate = new \DateTime("now");
$executionNextPollDate->add($variables['waitInterval']);
$executionNextPollDate->add($variables['batchWaitInterval']);
$executionNextPollDate = $executionNextPollDate->format($platform->getDateTimeFormatString());
}

$serializer = $this->options->getSerializer();

$now = new \DateTime("now");
$data = array(
'workflow_id' => (int)$this->workflow->id,
'execution_parent' => $parentId,
'execution_started' => $now->format($platform->getDateTimeFormatString()),
'execution_variables' => \ezcWorkflowDatabaseUtil::serialize($variables),
'execution_waiting_for' => \ezcWorkflowDatabaseUtil::serialize($this->waitingFor),
'execution_threads' => \ezcWorkflowDatabaseUtil::serialize($this->threads),
'execution_next_thread_id' => (int)$this->nextThreadId,
'execution_entity_name' => $executionEntityName,
'execution_entity_id' => $executionEntityId,
'execution_next_poll_date' => $executionNextPollDate,
'workflow_id' => (int)$this->workflow->id,
'execution_parent' => $parentId,
'execution_started' => $now->format($platform->getDateTimeFormatString()),
'execution_variables' => $serializer->serialize($variables),
'execution_waiting_for' => $serializer->serialize($this->waitingFor),
'execution_threads' => $serializer->serialize($this->threads),
'execution_next_thread_id' => (int)$this->nextThreadId,
'execution_next_poll_date' => $executionNextPollDate,
);
$this->conn->insert($this->options->executionTable(), $data);

Expand Down Expand Up @@ -149,26 +140,28 @@ protected function doSuspend()
$executionNextPollDate = $executionNextPollDate->format($platform->getDateTimeFormatString());
}

$serializer = $this->options->getSerializer();

$now = new \DateTime("now");
$data = array(
'execution_suspended' => $now->format($platform->getDateTimeFormatString()),
'execution_variables' => \ezcWorkflowDatabaseUtil::serialize($variables),
'execution_waiting_for' => \ezcWorkflowDatabaseUtil::serialize($this->waitingFor),
'execution_threads' => \ezcWorkflowDatabaseUtil::serialize($this->threads),
'execution_next_thread_id' => (int)$this->nextThreadId,
'execution_next_poll_date' => $executionNextPollDate,
'execution_suspended' => $now->format($platform->getDateTimeFormatString()),
'execution_variables' => $serializer->serialize($variables),
'execution_waiting_for' => $serializer->serialize($this->waitingFor),
'execution_threads' => $serializer->serialize($this->threads),
'execution_next_thread_id' => (int)$this->nextThreadId,
'execution_next_poll_date' => $executionNextPollDate,
);

$this->cleanUpExecutionStateTable();
$this->conn->update($this->options->executionTable(), $data, array('execution_id' => (int)$this->id));

foreach ($this->activatedNodes AS $node) {
$data = array(
'execution_id' => (int)$this->id,
'node_id' => (int)$node->getId(),
'node_state' => \ezcWorkflowDatabaseUtil::serialize( $node->getState() ),
'node_activated_from' => \ezcWorkflowDatabaseUtil::serialize( $node->getActivatedFrom() ),
'node_thread_id' => $node->getThreadId(),
'execution_id' => (int)$this->id,
'node_id' => (int)$node->getId(),
'node_state' => $serializer->serialize( $node->getState() ),
'node_activated_from' => $serializer->serialize( $node->getActivatedFrom() ),
'node_thread_id' => $node->getThreadId(),
);
$this->conn->insert($this->options->executionStateTable(), $data);
}
Expand Down Expand Up @@ -198,9 +191,11 @@ protected function loadExecution($executionId)
$this->id = (int)$execution['execution_id'];
$this->nextThreadId = $execution['execution_next_thread_id'];

$this->variables = \ezcWorkflowDatabaseUtil::unserialize($execution['execution_variables']);
$this->waitingFor = \ezcWorkflowDatabaseUtil::unserialize($execution['execution_waiting_for']);
$this->threads = \ezcWorkflowDatabaseUtil::unserialize($execution['execution_threads']);
$serializer = $this->options->getSerializer();

$this->variables = $serializer->unserialize($execution['execution_variables']);
$this->waitingFor = $serializer->unserialize($execution['execution_waiting_for']);
$this->threads = $serializer->unserialize($execution['execution_threads']);

$this->workflow = $this->definitionStorage->loadById($execution['workflow_id']);

Expand All @@ -216,9 +211,9 @@ protected function loadExecution($executionId)
$row = array_change_key_case($row, \CASE_LOWER);

$active[$row['node_id']] = array(
'activated_from' => \ezcWorkflowDatabaseUtil::unserialize($row['node_activated_from']),
'state' => \ezcWorkflowDatabaseUtil::unserialize($row['node_state'], null),
'thread_id' => $row['node_thread_id'],
'activated_from' => $serializer->unserialize($row['node_activated_from']),
'state' => $serializer->unserialize($row['node_state'], null),
'thread_id' => $row['node_thread_id'],
);
}

Expand Down
2 changes: 0 additions & 2 deletions lib/DoctrineExtensions/Workflow/SchemaBuilder.php
Expand Up @@ -100,8 +100,6 @@ public function getWorkflowSchema(WorkflowOptions $options)
$executionTable->addColumn('execution_threads', 'text', array('notnull' => false, "length" => null));
$executionTable->addColumn('execution_next_thread_id', 'integer');
$executionTable->addColumn('execution_next_poll_date', 'datetime', array('notnull' => false));
$executionTable->addColumn('execution_entity_name', 'string', array('notnull' => false));
$executionTable->addColumn('execution_entity_id', 'integer', array('notnull' => false));

$executionTable->setPrimaryKey(array('execution_id'));
$executionTable->addIndex(array('execution_parent'));
Expand Down
33 changes: 0 additions & 33 deletions lib/DoctrineExtensions/Workflow/Util/Serialize/JsonSerializer.php

This file was deleted.

10 changes: 9 additions & 1 deletion tests/DoctrineExtensions/Workflow/DefinitionStorageTest.php
Expand Up @@ -7,10 +7,18 @@ class DefinitionStorageTest extends \PHPUnit_Framework_TestCase
private $conn;
private $options;

public function createSerializer()
{
if (isset($GLOBALS['DOCTRINE_WORKFLOW_SERIALIZER_IMPL'])) {
return new $GLOBALS['DOCTRINE_WORKFLOW_SERIALIZER_IMPL']();
}
return null;
}

public function setUp()
{
$this->conn = \DoctrineExtensions\Workflow\TestHelper::getConnection();
$this->options = new WorkflowOptions('test_');
$this->options = new WorkflowOptions('test_', null, null, $this->createSerializer());
TestHelper::createSchema($this->options);
}

Expand Down
10 changes: 9 additions & 1 deletion tests/DoctrineExtensions/Workflow/DoctrineExecutionTest.php
Expand Up @@ -26,10 +26,18 @@ class DoctrineExecutionTest extends \PHPUnit_Framework_TestCase
*/
private $storage;

public function createSerializer()
{
if (isset($GLOBALS['DOCTRINE_WORKFLOW_SERIALIZER_IMPL'])) {
return new $GLOBALS['DOCTRINE_WORKFLOW_SERIALIZER_IMPL']();
}
return null;
}

public function setUp()
{
$this->conn = \DoctrineExtensions\Workflow\TestHelper::getConnection();
$this->options = new WorkflowOptions('test_');
$this->options = new WorkflowOptions('test_', null, null, $this->createSerializer());
TestHelper::createSchema($this->options);
$this->storage = new DefinitionStorage($this->conn, $this->options);
}
Expand Down
29 changes: 22 additions & 7 deletions tests/DoctrineExtensions/Workflow/TestHelper.php
Expand Up @@ -16,15 +16,30 @@ static public function createSchema(WorkflowOptions $options)
{
$conn = self::getConnection();

$schemaBuilder = new SchemaBuilder($conn);
try {
$schemaBuilder->dropWorkflowSchema($options);
} catch(\PDOException $e) {

if (!isset(self::$schema[$options->getTablePrefix()])) {
$schemaBuilder = new SchemaBuilder($conn);
try {
$schemaBuilder->dropWorkflowSchema($options);
} catch(\PDOException $e) {

}
$schemaBuilder->createWorkflowSchema($options);

self::$schema[$options->getTablePrefix()] = true;
}
$schemaBuilder->createWorkflowSchema($options);

self::$schema[$options->getTablePrefix()] = true;
$platform = $conn->getDatabasePlatform();
$tables = array(
$options->executionStateTable(),
$options->executionTable(),
$options->variableHandlerTable(),
$options->nodeConnectionTable(),
$options->nodeTable(),
$options->workflowTable(),
);
foreach ($tables AS $table) {
$conn->executeUpdate($platform->getTruncateTableSQL($table));
}
}

static public function getConnection()
Expand Down
11 changes: 2 additions & 9 deletions tests/DoctrineExtensions/Workflow/Util/SerializerTest.php
Expand Up @@ -17,6 +17,8 @@ public static function dataSerialize()
array(array('foo' => 'bar'), array('foo' => 'bar'), array()),
array(array('foo' => 'bar'), array('foo' => 'bar'), null),
array(array('c' => "\xc9\x80"), array('c' => "\xc9\x80"), null),
// empty object instances exist in ezcWorkflow!!
array(array('c' => new \stdClass()), array('c' => new \stdClass()), null),
);
}

Expand All @@ -41,13 +43,4 @@ public function testWbbxSerializer($value, $expectedValue, $defaultValue)
$z = new WddxSerializer();
$this->assertEquals($expectedValue, $z->unserialize($z->serialize($value), $defaultValue));
}

/**
* @dataProvider dataSerialize
*/
public function testJsonSerializer($value, $expectedValue, $defaultValue)
{
$z = new JsonSerializer();
$this->assertEquals($expectedValue, $z->unserialize($z->serialize($value), $defaultValue));
}
}
1 change: 1 addition & 0 deletions tests/phpunit.dist.xml
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./DoctrineExtensions/Workflow/TestHelper.php">
<php>
<var name="DOCTRINE_WORKFLOW_SERIALIZER_IMPL" value="DoctrineExtensions\Workflow\Util\Serialize\ZetaSerializer" />
<var name="doctrine2-common-path" value="" />
<var name="doctrine2-dbal-path" value="" />
<var name="ezc-base-file" value="" />
Expand Down

0 comments on commit 85fb1ea

Please sign in to comment.