Skip to content

[Workflow] WIP Create MarkingHistoryStore to allow logging of an entity's states #28265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,12 @@ private function addWorkflowSection(ArrayNodeDefinition $rootNode)
->arrayNode('audit_trail')
->canBeEnabled()
->end()
->arrayNode('marking_history_store')
->children()
->scalarNode('history_property')->isRequired()->end()
->scalarNode('memo_property')->end()
->end()
->end()
->enumNode('type')
->values(array('workflow', 'state_machine'))
->defaultValue('state_machine')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,17 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
$markingStoreDefinition = new Reference($workflow['marking_store']['service']);
}

// create Marking History Store for optionally logging state history
if (isset($workflow['marking_history_store'])) {
$historyProperty = $workflow['marking_history_store']['history_property'];
$memoProperty = $workflow['marking_history_store']['memo_property'];

$markingHistoryDefinition = new Definition(Workflow\MarkingHistoryStore::class);
$markingHistoryDefinition->setPublic(false);
$markingHistoryDefinition->addArgument($historyProperty);
$markingHistoryDefinition->addArgument($memoProperty);
}

// Create Workflow
$workflowId = sprintf('%s.%s', $type, $name);
$workflowDefinition = new ChildDefinition(sprintf('%s.abstract', $type));
Expand All @@ -580,6 +591,9 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
$workflowDefinition->replaceArgument(1, $markingStoreDefinition);
}
$workflowDefinition->replaceArgument(3, $name);
if (isset($markingHistoryDefinition)) {
$workflowDefinition->replaceArgument(4, $markingHistoryDefinition);
}

// Store to container
$container->setDefinition($workflowId, $workflowDefinition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,21 @@
<argument type="constant">null</argument> <!-- marking store -->
<argument type="service" id="event_dispatcher" on-invalid="ignore" />
<argument /> <!-- name -->
<argument type="constant">null</argument> <!-- marking history -->
</service>
<service id="state_machine.abstract" class="Symfony\Component\Workflow\StateMachine" abstract="true" public="true">
<argument /> <!-- workflow definition -->
<argument type="constant">null</argument> <!-- marking store -->
<argument type="service" id="event_dispatcher" on-invalid="ignore" />
<argument /> <!-- name -->
<argument type="constant">null</argument> <!-- marking history -->
</service>

<service id="workflow.marking_store.multiple_state" class="Symfony\Component\Workflow\MarkingStore\MultipleStateMarkingStore" abstract="true" />
<service id="workflow.marking_store.single_state" class="Symfony\Component\Workflow\MarkingStore\SingleStateMarkingStore" abstract="true" />

<service id="workflow.marking_history_store" class="Symfony\Component\Workflow\MarkingHistoryStore" abstract="true" />

<service id="workflow.registry" class="Symfony\Component\Workflow\Registry" />
<service id="Symfony\Component\Workflow\Registry" alias="workflow.registry" />

Expand Down
68 changes: 68 additions & 0 deletions src/Symfony/Component/Workflow/MarkingHistoryStore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Workflow;

use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;

final class MarkingHistoryStore
{
private $historyProperty;
private $memoProperty;

private $historyPropertyAccessor;
private $memoPropertyAccessor;

public function __construct(string $historyProperty, string $memoProperty = null, PropertyAccessorInterface $historyPropertyAccessor = null, PropertyAccessorInterface $memoPropertyAccessor = null)
{
$this->historyProperty = $historyProperty;
$this->memoProperty = $memoProperty;

$this->historyPropertyAccessor = $historyPropertyAccessor ?? PropertyAccess::createPropertyAccessor();
$this->memoPropertyAccessor = $memoPropertyAccessor ?? PropertyAccess::createPropertyAccessor();
}

/**
* @param $subject
* @param $transition Transition
* @param $marking Marking
* @param $workflowName string
*/
public function updateMarkingHistory($subject, Transition $transition, Marking $marking, string $workflowName)
{
// get existing state history for this object
$existingHistory = $this->historyPropertyAccessor->getValue($subject, $this->historyProperty) ?? array();

// build the array to append to the log, using the workflow name as the log's key
$arr = array();
$arr['timestamp'] = date('Y-m-d H:i:s');
$arr['marking'] = $marking->getPlaces();
$arr['transition'] = $transition->getName();

if (null !== $this->memoProperty) {
$arr['memo'] = $this->memoPropertyAccessor->getValue($subject, $this->memoProperty) ?? ''; // an optional memo
}

// the key is used to allow logging the history of multiple workflows
$key = $workflowName;
if (!array_key_exists($key, $existingHistory)) {
$existingHistory[$key] = array();
}
$existingHistory[$key][] = $arr;

// set the history property value
$this->historyPropertyAccessor->setValue($subject, $this->historyProperty, $existingHistory);

// finally, clear out the memo field now that it's been logged
$this->memoPropertyAccessor->setValue($subject, $this->memoProperty, null);
}
}
13 changes: 11 additions & 2 deletions src/Symfony/Component/Workflow/StateMachine.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Workflow;

use Symfony\Component\EventDispatcher\EventDispatcherInterface;
Expand All @@ -11,8 +20,8 @@
*/
class StateMachine extends Workflow
{
public function __construct(Definition $definition, MarkingStoreInterface $markingStore = null, EventDispatcherInterface $dispatcher = null, string $name = 'unnamed')
public function __construct(Definition $definition, MarkingStoreInterface $markingStore = null, EventDispatcherInterface $dispatcher = null, string $name = 'unnamed', MarkingHistoryStore $markingHistoryStore = null)
{
parent::__construct($definition, $markingStore ?: new SingleStateMarkingStore(), $dispatcher, $name);
parent::__construct($definition, $markingStore ?: new SingleStateMarkingStore(), $dispatcher, $name, $markingHistoryStore);
}
}
13 changes: 12 additions & 1 deletion src/Symfony/Component/Workflow/Workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ class Workflow implements WorkflowInterface
private $markingStore;
private $dispatcher;
private $name;
private $markingHistoryStore;

public function __construct(Definition $definition, MarkingStoreInterface $markingStore = null, EventDispatcherInterface $dispatcher = null, string $name = 'unnamed')
public function __construct(Definition $definition, MarkingStoreInterface $markingStore = null, EventDispatcherInterface $dispatcher = null, string $name = 'unnamed', MarkingHistoryStore $markingHistoryStore = null)
{
$this->definition = $definition;
$this->markingStore = $markingStore ?: new MultipleStateMarkingStore();
$this->dispatcher = $dispatcher;
$this->name = $name;
$this->markingHistoryStore = $markingHistoryStore;
}

/**
Expand Down Expand Up @@ -160,6 +162,10 @@ public function apply($subject, $transitionName)

$this->markingStore->setMarking($subject, $marking);

if (null !== $this->markingHistoryStore) {
$this->markingHistoryStore->updateMarkingHistory($subject, $transition, $marking, $this->getName());
}

$this->entered($subject, $transition, $marking);

$this->completed($subject, $transition, $marking);
Expand Down Expand Up @@ -363,4 +369,9 @@ private function announce($subject, Transition $initialTransition, Marking $mark
$this->dispatcher->dispatch(sprintf('workflow.%s.announce.%s', $this->name, $transition->getName()), $event);
}
}

public function getMarkingHistoryStore(): ?MarkingHistoryStore
{
return $this->markingHistoryStore;
}
}
7 changes: 7 additions & 0 deletions src/Symfony/Component/Workflow/WorkflowInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,11 @@ public function getDefinition();
public function getMarkingStore();

public function getMetadataStore(): MetadataStoreInterface;

/**
* Returns the optional Marking History Store for logging the state history on the entity.
*
* @return null|MarkingHistoryStore
*/
public function getMarkingHistoryStore(): ?MarkingHistoryStore;
}