Skip to content

Commit 6402572

Browse files
committed
feature #41292 [Workflow] Add support for getting updated context after a transition (lyrixx)
This PR was merged into the 5.4 branch. Discussion ---------- [Workflow] Add support for getting updated context after a transition | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | | License | MIT | Doc PR | I have a listener that lock the transition. Then, I need to give this lock to my consumer. ATM, there is no easy way to pass a new resource from a listener to the caller. I could add a new `tmpLock` property on my subject (this is what I did temporary), but I'm not confortable with this hack. By adding the final context (it could be updated, that the point of listener) to the marking, I could easily get data back. Finally, the PHP doc of WorkflowInterface::apply() tells us: ```php * `@return` Marking The new Marking ``` So I think it's legit to add also the new context Commits ------- 571c75e [Workflown] Add support for getting updated context after a transition
2 parents dde9835 + 571c75e commit 6402572

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

src/Symfony/Component/Workflow/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.4
5+
---
6+
7+
* Add support for getting updated context after a transition
8+
49
5.3
510
---
611

src/Symfony/Component/Workflow/Marking.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
class Marking
2020
{
2121
private $places = [];
22+
private $context = null;
2223

2324
/**
2425
* @param int[] $representation Keys are the place name and values should be 1
@@ -49,4 +50,20 @@ public function getPlaces()
4950
{
5051
return $this->places;
5152
}
53+
54+
/**
55+
* @internal
56+
*/
57+
public function setContext(array $context): void
58+
{
59+
$this->context = $context;
60+
}
61+
62+
/**
63+
* Returns the context after the subject has transitioned.
64+
*/
65+
public function getContext(): ?array
66+
{
67+
return $this->context;
68+
}
5269
}

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,28 @@ public function testEventContext()
637637
$dispatcher->addListener($eventName, $assertWorkflowContext);
638638
}
639639

640-
$workflow->apply($subject, 't1', $context);
640+
$marking = $workflow->apply($subject, 't1', $context);
641+
642+
$this->assertInstanceOf(Marking::class, $marking);
643+
$this->assertSame($context, $marking->getContext());
644+
}
645+
646+
public function testEventContextUpdated()
647+
{
648+
$definition = $this->createComplexWorkflowDefinition();
649+
$subject = new Subject();
650+
$dispatcher = new EventDispatcher();
651+
652+
$workflow = new Workflow($definition, new MethodMarkingStore(), $dispatcher);
653+
654+
$dispatcher->addListener('workflow.transition', function (TransitionEvent $event) {
655+
$event->setContext(['foo' => 'bar']);
656+
});
657+
658+
$marking = $workflow->apply($subject, 't1', ['initial']);
659+
660+
$this->assertInstanceOf(Marking::class, $marking);
661+
$this->assertSame(['foo' => 'bar'], $marking->getContext());
641662
}
642663

643664
public function testEventDefaultInitialContext()

src/Symfony/Component/Workflow/Workflow.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ public function apply(object $subject, string $transitionName, array $context =
244244
$this->announce($subject, $transition, $marking, $context);
245245
}
246246

247+
$marking->setContext($context);
248+
247249
return $marking;
248250
}
249251

0 commit comments

Comments
 (0)