8000 feature #30902 [Workflow] The TransitionEvent is able to modify the c… · symfony/symfony@e2e38de · GitHub
[go: up one dir, main page]

Skip to content

Commit e2e38de

Browse files
committed
feature #30902 [Workflow] The TransitionEvent is able to modify the context (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
2 parents e5f14b7 + 62ab775 commit e2e38de

File tree

5 files changed

+43
-4
lines changed

5 files changed

+43
-4
lines changed

src/Symfony/Component/Workflow/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

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

1112
use Symfony\Component\Workflow\Definition;

src/Symfony/Component/Workflow/Event/TransitionEvent.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,15 @@
1313

1414
class TransitionEvent extends Event
1515
{
16+
private $context;
17+
18+
public function setContext(array $context)
19+
{
20+
$this->context = $context;
21+
}
22+
23+
public function getContext(): array
24+
{
25+
return $this->context;
26+
}
1627
}

src/Symfony/Component/Workflow/Tests/Subject.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,27 @@
55
final class Subject
66
{
77
private $marking;
8+
private $context;
89

910
public function __construct($marking = null)
1011
{
1112
$this->marking = $marking;
13+
$this->context = [];
1214
}
1315

1416
public function getMarking()
1517
{
1618
return $this->marking;
1719
}
1820

19-
public function setMarking($marking)
21+
public function setMarking($marking, array $context = [])
2022
{
2123
$this->marking = $marking;
24+
$this->context = $context;
25+
}
26+
27+
public function getContext(): array
28+
{
29+
return $this->context;
2230
}
2331
}

src/Symfony/Component/Workflow/Tests/WorkflowTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Symfony\Component\Workflow\Definition;
88
use Symfony\Component\Workflow\Event\Event;
99
use Symfony\Component\Workflow\Event\GuardEvent;
10+
use Symfony\Component\Workflow\Event\TransitionEvent;
1011
use Symfony\Component\Workflow\Exception\NotEnabledTransitionException;
1112
use Symfony\Component\Workflow\Marking;
1213
use Symfony\Component\Workflow\MarkingStore\MarkingStoreInterface;
@@ -418,6 +419,21 @@ public function testApplyWithEventDispatcher()
418419
$this->assertSame($eventNameExpected, $eventDispatcher->dispatchedEvents);
419420
}
420421

422+
public function testApplyWithContext()
423+
{
424+
$definition = $this->createComplexWorkflowDefinition();
425+
$subject = new Subject();
426+
$eventDispatcher = new EventDispatcher();
427+
$eventDispatcher->addListener('workflow.transition', function (TransitionEvent $event) {
428+
$event->setContext(array_merge($event->getContext(), ['user' => 'admin']));
429+
});
430+
$workflow = new Workflow($definition, new MethodMarkingStore(), $eventDispatcher);
431+
432+
$workflow->apply($subject, 't1', ['foo' => 'bar']);
433+
434+
$this->assertSame(['foo' => 'bar', 'user' => 'admin'], $subject->getContext());
435+
}
436+
421437
public function testEventName()
422438
{
423439
$definition = $this->createComplexWorkflowDefinition();

src/Symfony/Component/Workflow/Workflow.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public function apply($subject, $transitionName, array $context = [])
176176

177177
$this->leave($subject, $transition, $marking);
178178

179-
$this->transition($subject, $transition, $marking);
179+
$context = $this->transition($subject, $transition, $marking, $context);
180180

181181
$this->enter($subject, $transition, $marking);
182182

@@ -308,17 +308,20 @@ private function leave($subject, Transition $transition, Marking $marking): void
308308
}
309309
}
310310

311-
private function transition($subject, Transition $transition, Marking $marking): void
311+
private function transition($subject, Transition $transition, Marking $marking, array $context): array
312312
{
313313
if (null === $this->dispatcher) {
314-
return;
314+
return $context;
315315
}
316316

317317
$event = new TransitionEvent($subject, $marking, $transition, $this);
318+
$event->setContext($context);
318319

319320
$this->dispatcher->dispatch($event, WorkflowEvents::TRANSITION);
320321
$this->dispatcher->dispatch($event, sprintf('workflow.%s.transition', $this->name));
321322
$this->dispatcher->dispatch($event, sprintf('workflow.%s.transition.%s', $this->name, $transition->getName()));
323+
324+
return $event->getContext();
322325
}
323326

324327
private function enter($subject, Transition $transition, Marking $marking): void

0 commit comments

Comments
 (0)
0