Skip to content
Permalink
Browse files

feature #30902 [Workflow] The TransitionEvent is able to modify the c…

…ontext (lyrixx)

This PR was merged into the 4.3-dev branch.

Discussion
----------

[Workflow] The TransitionEvent is able to modify the context

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

EUFOSSA

ping @HeahDude

Commits
-------

62ab775 [Workflow] The TransitionEvent is able to modify the context
  • Loading branch information...
fabpot committed Apr 6, 2019
2 parents e5f14b7 + 62ab775 commit e2e38dea9ae3c7d80d267c8c448863876409e2ce
@@ -6,6 +6,7 @@ CHANGELOG

* Trigger `entered` event for subject entering in the Workflow for the first time.
* Added a context to `Workflow::apply()`. The `MethodMarkingStore` could be used to leverage this feature.
* The `TransitionEvent` is able to modify the context.
* Add style to transitions by declaring metadata:

use Symfony\Component\Workflow\Definition;
@@ -13,4 +13,15 @@
class TransitionEvent extends Event
{
private $context;
public function setContext(array $context)
{
$this->context = $context;
}
public function getContext(): array
{
return $this->context;
}
}
@@ -5,19 +5,27 @@
final class Subject
{
private $marking;
private $context;
public function __construct($marking = null)
{
$this->marking = $marking;
$this->context = [];
}
public function getMarking()
{
return $this->marking;
}
public function setMarking($marking)
public function setMarking($marking, array $context = [])
{
$this->marking = $marking;
$this->context = $context;
}
public function getContext(): array
{
return $this->context;
}
}
@@ -7,6 +7,7 @@
use Symfony\Component\Workflow\Definition;
use Symfony\Component\Workflow\Event\Event;
use Symfony\Component\Workflow\Event\GuardEvent;
use Symfony\Component\Workflow\Event\TransitionEvent;
use Symfony\Component\Workflow\Exception\NotEnabledTransitionException;
use Symfony\Component\Workflow\Marking;
use Symfony\Component\Workflow\MarkingStore\MarkingStoreInterface;
@@ -418,6 +419,21 @@ public function testApplyWithEventDispatcher()
$this->assertSame($eventNameExpected, $eventDispatcher->dispatchedEvents);
}
public function testApplyWithContext()
{
$definition = $this->createComplexWorkflowDefinition();
$subject = new Subject();
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addListener('workflow.transition', function (TransitionEvent $event) {
$event->setContext(array_merge($event->getContext(), ['user' => 'admin']));
});
$workflow = new Workflow($definition, new MethodMarkingStore(), $eventDispatcher);
$workflow->apply($subject, 't1', ['foo' => 'bar']);
$this->assertSame(['foo' => 'bar', 'user' => 'admin'], $subject->getContext());
}
public function testEventName()
{
$definition = $this->createComplexWorkflowDefinition();
@@ -176,7 +176,7 @@ public function apply($subject, $transitionName, array $context = [])
$this->leave($subject, $transition, $marking);
$this->transition($subject, $transition, $marking);
$context = $this->transition($subject, $transition, $marking, $context);
$this->enter($subject, $transition, $marking);
@@ -308,17 +308,20 @@ private function leave($subject, Transition $transition, Marking $marking): void
}
}
private function transition($subject, Transition $transition, Marking $marking): void
private function transition($subject, Transition $transition, Marking $marking, array $context): array
{
if (null === $this->dispatcher) {
return;
return $context;
}
$event = new TransitionEvent($subject, $marking, $transition, $this);
$event->setContext($context);
$this->dispatcher->dispatch($event, WorkflowEvents::TRANSITION);
$this->dispatcher->dispatch($event, sprintf('workflow.%s.transition', $this->name));
$this->dispatcher->dispatch($event, sprintf('workflow.%s.transition.%s', $this->name, $transition->getName()));
return $event->getContext();
}
private function enter($subject, Transition $transition, Marking $marking): void

0 comments on commit e2e38de

Please sign in to comment.
You can’t perform that action at this time.