8000 minor #23781 [Workflow] do not emit not needed guard events (xabbuh) · symfony/symfony@457d57b · GitHub
[go: up one dir, main page]

Skip to content

Commit 457d57b

Browse files
committed
minor #23781 [Workflow] do not emit not needed guard events (xabbuh)
This PR was merged into the 3.4 branch. Discussion ---------- [Workflow] do not emit not needed guard events | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #23677 | License | MIT | Doc PR | Commits ------- 47c68e1 [Workflow] do not emit not needed guard events
2 parents a12ebf7 + 47c68e1 commit 457d57b

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,31 @@ public function testCanWithGuard()
137137
$this->assertFalse($workflow->can($subject, 't1'));
138138
}
139139

140+
public function testCanDoesNotTriggerGuardEventsForNotEnabledTransitions()
141+
{
142+
$definition = $this->createComplexWorkflowDefinition();
143+
$subject = new \stdClass();
144+
$subject->marking = null;
145+
146+
$dispatchedEvents = array();
147+
$eventDispatcher = new EventDispatcher();
148+
149+
$workflow = new Workflow($definition, new MultipleStateMarkingStore(), $eventDispatcher, 'workflow_name');
150+
$workflow->apply($subject, 't1');
151+
$workflow->apply($subject, 't2');
152+
153+
$eventDispatcher->addListener('workflow.workflow_name.guard.t3', function () use (&$dispatchedEvents) {
154+
$dispatchedEvents[] = 'workflow_name.guard.t3';
155+
});
156+
$eventDispatcher->addListener('workflow.workflow_name.guard.t4', function () use (&$dispatchedEvents) {
157+
$dispatchedEvents[] = 'workflow_name.guard.t4';
158+
});
159+
160+
$workflow->can($subject, 't3');
161+
162+
$this->assertSame(array('workflow_name.guard.t3'), $dispatchedEvents);
163+
}
164+
140165
/**
141166
* @expectedException \Symfony\Component\Workflow\Exception\LogicException
142167
* @expectedExceptionMessage Unable to apply transition "t2" for workflow "unnamed".

src/Symfony/Component/Workflow/Workflow.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,19 @@ public function getMarking($subject)
9292
*/
9393
public function can($subject, $transitionName)
9494
{
95-
$transitions = $this->getEnabledTransitions($subject);
95+
$transitions = $this->definition->getTransitions();
96+
$marking = $this->getMarking($subject);
9697

9798
foreach ($transitions as $transition) {
98-
if ($transitionName === $transition->getName()) {
99+
foreach ($transition->getFroms() as $place) {
100+
if (!$marking->has($place)) {
101+
// do not emit guard events for transitions where the marking does not contain
102+
// all "from places" (thus the transition couldn't be applied anyway)
103+
continue 2;
104+
}
105+
}
106+
107+
if ($transitionName === $transition->getName() && $this->doCan($subject, $marking, $transition)) {
99108
return true;
100109
}
101110
}

0 commit comments

Comments
 (0)
0