8000 [Workflown] Add support for getting updated context after a transition · symfony/symfony@a5052f8 · GitHub
[go: up one dir, main page]

Skip to content

Commit a5052f8

Browse files
committed
[Workflown] Add support for getting updated context after a transition
1 parent e32d330 commit a5052f8

File tree

4 files changed

+76
-31
lines changed

4 files changed

+76
-31
lines changed
Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,37 @@
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

712
* Deprecate `InvalidTokenConfigurationException`
8-
* Added `MermaidDumper` to dump Workflow graphs in the Mermaid.js flowchart format
13+
* Add `MermaidDumper` to dump Workflow graphs in the Mermaid.js flowchart format
914

1015
5.2.0
1116
-----
1217

13-
* Added `Workflow::getEnabledTransition()` to easily retrieve a specific transition object
14-
* Added context to the event dispatched
18+
* Add `Workflow::getEnabledTransition()` to easily retrieve a specific transition object
19+
* Add context to the event dispatched
1520
* Dispatch an event when the subject enters in the workflow for the very first time
16-
* Added a default context to the previous event
17-
* Added support for specifying which events should be dispatched when calling `workflow->apply()`
21+
* Add a default context to the previous event
22+
* Add support for specifying which events should be dispatched when calling `workflow->apply()`
1823

1924
5.1.0
2025
-----
2126

22-
* Added context to `TransitionException` and its child classes whenever they are thrown in `Workflow::apply()`
23-
* Added `Registry::has()` to check if a workflow exists
24-
* Added support for `$context[Workflow::DISABLE_ANNOUNCE_EVENT] = true` when calling `workflow->apply()` to not fire the announce event
27+
* Add context to `TransitionException` and its child classes whenever they are thrown in `Workflow::apply()`
28+
* Add `Registry::has()` to check if a workflow exists
29+
* Add support for `$context[Workflow::DISABLE_ANNOUNCE_EVENT] = true` when calling `workflow->apply()` to not fire the announce event
2530

2631
5.0.0
2732
-----
2833

29-
* Added argument `$context` to `MarkingStoreInterface::setMarking()`
34+
* Add argument `$context` to `MarkingStoreInterface::setMarking()`
3035

3136
4.4.0
3237
-----
@@ -37,7 +42,7 @@ CHANGELOG
3742
-----
3843

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

@@ -59,23 +64,23 @@ CHANGELOG
5964
* Dispatch `EnteredEvent` on `workflow.entered`
6065
* Dispatch `CompletedEvent` on `workflow.completed`
6166
* Dispatch `AnnounceEvent` on `workflow.announce`
62-
* Added support for many `initialPlaces`
63-
* Deprecated `DefinitionBuilder::setInitialPlace()` method, use `DefinitionBuilder::setInitialPlaces()` instead.
64-
* Deprecated the `MultipleStateMarkingStore` class, use the `MethodMarkingStore` instead.
65-
* Deprecated the `SingleStateMarkingStore` class, use the `MethodMarkingStore` instead.
67+
* Add support for many `initialPlaces`
68+
* Deprecate `DefinitionBuilder::setInitialPlace()` method, use `DefinitionBuilder::setInitialPlaces()` instead.
69+
* Deprecate the `MultipleStateMarkingStore` class, use the `MethodMarkingStore` instead.
70+
* Deprecate the `SingleStateMarkingStore` class, use the `MethodMarkingStore` instead.
6671

6772
4.1.0
6873
-----
6974

70-
* Deprecated the `DefinitionBuilder::reset()` method, use the `clear()` one instead.
71-
* Deprecated the usage of `add(Workflow $workflow, $supportStrategy)` in `Workflow/Registry`, use `addWorkflow(WorkflowInterface, $supportStrategy)` instead.
72-
* Deprecated the usage of `SupportStrategyInterface`, use `WorkflowSupportStrategyInterface` instead.
75+
* Deprecate the `DefinitionBuilder::reset()` method, use the `clear()` one instead.
76+
* Deprecate the usage of `add(Workflow $workflow, $supportStrategy)` in `Workflow/Registry`, use `addWorkflow(WorkflowInterface, $supportStrategy)` instead.
77+
* Deprecate the usage of `SupportStrategyInterface`, use `WorkflowSupportStrategyInterface` instead.
7378
* The `Workflow` class now implements `WorkflowInterface`.
74-
* Deprecated the class `ClassInstanceSupportStrategy` in favor of the class `InstanceOfSupportStrategy`.
75-
* Added TransitionBlockers as a way to pass around reasons why exactly
79+
* Deprecate the class `ClassInstanceSupportStrategy` in favor of the class `InstanceOfSupportStrategy`.
80+
* Add TransitionBlockers as a way to pass around reasons why exactly
7681
transitions can't be made.
77-
* Added a `MetadataStore`.
78-
* Added `Registry::all` to return all the workflows associated with the
82+
* Add a `MetadataStore`.
83+
* Add `Registry::all` to return all the workflows associated with the
7984
specific subject.
8085

8186
4.0.0
@@ -86,21 +91,21 @@ CHANGELOG
8691
3.4.0
8792
-----
8893

89-
* Added guard `is_valid()` method support.
90-
* Added support for `Event::getWorkflowName()` for "announce" events.
91-
* Added `workflow.completed` events which are fired after a transition is completed.
94+
* Add guard `is_valid()` method support.
95+
* Add support for `Event::getWorkflowName()` for "announce" events.
96+
* Add `workflow.completed` events which are fired after a transition is completed.
9297

9398
3.3.0
9499
-----
95100

96-
* Added support for expressions to guard transitions and added an `is_granted()`
101+
* Add support for expressions to guard transitions and added an `is_granted()`
97102
function that can be used in these expressions to use the authorization checker.
98103
* The `DefinitionBuilder` class now provides a fluent interface.
99104
* The `AuditTrailListener` now includes the workflow name in its log entries.
100-
* Added `workflow.entered` events which is fired after the marking has been set.
101-
* Deprecated class name support in `WorkflowRegistry::add()` as second parameter.
105+
* Add `workflow.entered` events which is fired after the marking has been set.
106+
* Deprecate class name support in `WorkflowRegistry::add()` as second parameter.
102107
Wrap the class name in an instance of ClassInstanceSupportStrategy instead.
103-
* Added support for `Event::getWorkflowName()`.
104-
* Added `SupportStrategyInterface` to allow custom strategies to decide whether
108+
* Add support for `Event::getWorkflowName()`.
109+
* Add `SupportStrategyInterface` to allow custom strategies to decide whether
105110
or not a workflow supports a subject.
106-
* Added `ValidateWorkflowPass`.
111+
* Add `ValidateWorkflowPass`.

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 transitionend.
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)
0