Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b350c80

Browse files
committed
feature #35322 [Workflow] Added a way to not fire the announce event (lyrixx)
This PR was merged into the 5.1-dev branch. Discussion ---------- [Workflow] Added a way to not fire the announce event | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #35286 | License | MIT | Doc PR | Commits ------- d31939d [Workflow] Added a way to not fire the annonce event
2 parents 0607691 + d31939d commit b350c80

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
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
* Added context to `TransitionException` and its child classes whenever they are thrown in `Workflow::apply()`
88
* Added `Registry::has()` to check if a workflow exists
9+
* Added support for `$context[Workflow::DISABLE_ANNOUNCE_EVENT] = true` when calling `workflow->apply()` to not fire the announce event
910

1011
5.0.0
1112
-----

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,30 @@ public function testApplyWithEventDispatcher()
426426
$this->assertSame($eventNameExpected, $eventDispatcher->dispatchedEvents);
427427
}
428428

429+
public function provideApplyWithEventDispatcherForAnnounceTests()
430+
{
431+
yield [false, [Workflow::DISABLE_ANNOUNCE_EVENT => true]];
432+
yield [true, [Workflow::DISABLE_ANNOUNCE_EVENT => false]];
433+
yield [true, []];
434+
}
435+
436+
/** @dataProvider provideApplyWithEventDispatcherForAnnounceTests */
437+
public function testApplyWithEventDispatcherForAnnounce(bool $fired, array $context)
438+
{
439+
$definition = $this->createComplexWorkflowDefinition();
440+
$subject = new Subject();
441+
$eventDispatcher = new EventDispatcherMock();
442+
$workflow = new Workflow($definition, new MethodMarkingStore(), $eventDispatcher, 'workflow_name');
443+
444+
$workflow->apply($subject, 't1', $context);
445+
446+
if ($fired) {
447+
$this->assertContains('workflow.workflow_name.announce', $eventDispatcher->dispatchedEvents);
448+
} else {
449+
$this->assertNotContains('workflow.workflow_name.announce', $eventDispatcher->dispatchedEvents);
450+
}
451+
}
452+
429453
public function testApplyDoesNotTriggerExtraGuardWithEventDispatcher()
430454
{
431455
$transitions[] = new Transition('a-b', 'a', 'b');

src/Symfony/Component/Workflow/Workflow.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
*/
3434
class Workflow implements WorkflowInterface
3535
{
36+
public const DISABLE_ANNOUNCE_EVENT = 'workflow_disable_announce_event';
37+
3638
private $definition;
3739
private $markingStore;
3840
private $dispatcher;
@@ -207,7 +209,9 @@ public function apply(object $subject, string $transitionName, array $context =
207209

208210
$this->completed($subject, $transition, $marking);
209211

210-
$this->announce($subject, $transition, $marking);
212+
if (!($context[self::DISABLE_ANNOUNCE_EVENT] ?? false)) {
213+
$this->announce($subject, $transition, $marking);
214+
}
211215
}
212216

213217
return $marking;

0 commit comments

Comments
 (0)