Navigation Menu

Skip to content

Commit

Permalink
feature #30902 [Workflow] The TransitionEvent is able to modify the c…
Browse files Browse the repository at this point in the history
…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 e2e38de
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Component/Workflow/CHANGELOG.md
Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Component/Workflow/Event/TransitionEvent.php
Expand Up @@ -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;
}
}
10 changes: 9 additions & 1 deletion src/Symfony/Component/Workflow/Tests/Subject.php
Expand Up @@ -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;
}
}
16 changes: 16 additions & 0 deletions src/Symfony/Component/Workflow/Tests/WorkflowTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
9 changes: 6 additions & 3 deletions src/Symfony/Component/Workflow/Workflow.php
Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e2e38de

Please sign in to comment.