Skip to content

Commit 6c0e48d

Browse files
committed
feature symfony#22587 [Workflow] Add transition completed event (izzyp)
This PR was merged into the 3.4 branch. Discussion ---------- [Workflow] Add transition completed event | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | symfony/symfony-docs#8213 --- Because the "entered" event is the only event dispatched after the new marking is applied, and publish's an event upon entering into a "Place" (as opposed to completing a transition), it is not sufficient for a lot of use cases and is causing bugs. Example: Enabled Transitions: 1. A -> B 2. B -> C 3. C -> B Transition 1 and transition 3, will dispatch an "entered" event on Place B, forcing post transition behaviour to be the same for both transition 1 and 3. A user might need different behaviour depending on the transition, rather the the destination. A concrete use case would be when applying an "undo" transition to a subject. One may or may not want to re-trigger all the events associated with the original transition to that Place. I propose adding a "completed" event (ie. Transition completed) in addition to the entered event. Commits ------- c254cac [Workflow] Added an transition completed event
2 parents 266d9d3 + c254cac commit 6c0e48d

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

src/Symfony/Component/Workflow/CHANGELOG.md

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

77
* Added support for `Event::getWorkflowName()` for "announce" events.
8+
* Added `workflow.completed` events which are fired after a transition is completed.
89

910
3.3.0
1011
-----

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ public function testApplyWithEventDispatcher()
287287
'workflow.workflow_name.entered',
288288
'workflow.workflow_name.entered.b',
289289
'workflow.workflow_name.entered.c',
290+
'workflow.completed',
291+
'workflow.workflow_name.completed',
292+
'workflow.workflow_name.completed.t1',
290293
// Following events are fired because of announce() method
291294
'workflow.announce',
292295
'workflow.workflow_name.announce',

src/Symfony/Component/Workflow/Workflow.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ public function apply($subject, $transitionName)
151151

152152
$this->entered($subject, $transition, $marking);
153153

154+
$this->completed($subject, $transition, $marking);
155+
154156
$this->announce($subject, $transition, $marking);
155157
}
156158

@@ -309,6 +311,19 @@ private function entered($subject, Transition $transition, Marking $marking)
309311
}
310312
}
311313

314+
private function completed($subject, Transition $transition, Marking $marking)
315+
{
316+
if (null === $this->dispatcher) {
317+
return;
318+
}
319+
320+
$event = new Event($subject, $marking, $transition, $this->name);
321+
322+
$this->dispatcher->dispatch('workflow.completed', $event);
323+
$this->dispatcher->dispatch(sprintf('workflow.%s.completed', $this->name), $event);
324+
$this->dispatcher->dispatch(sprintf('workflow.%s.completed.%s', $this->name, $transition->getName()), $event);
325+
}
326+
312327
private function announce($subject, Transition $initialTransition, Marking $marking)
313328
{
314329
if (null === $this->dispatcher) {

0 commit comments

Comments
 (0)