8000 [Workflow] Do not trigger extra guard · symfony/symfony@ad06197 · GitHub
[go: up one dir, main page]

Skip to content

Commit ad06197

Browse files
committed
[Workflow] Do not trigger extra guard
With this patch, guard are executed only on wanted transitions
1 parent b647f74 commit ad06197

File tree

2 files changed

+49
-16
lines changed

2 files changed

+49
-16
lines changed

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,45 @@ public function testApplyWithEventDispatcher()
304304
$this->assertSame($eventNameExpected, $eventDispatcher->dispatchedEvents);
305305
}
306306

307+
public function testApplyDoesNotTriggerExtraGuardWithEventDispatcher()
308+
{
309+
$transitions[] = new Transition('a-b', 'a', 'b');
310+
$transitions[] = new Transition('a-c', 'a', 'c');
311+
$definition = new Definition(['a', 'b', 'c'], $transitions);
312+
313+
$subject = new \stdClass();
314+
$subject->marking = null;
315+
$eventDispatcher = new EventDispatcherMock();
316+
$workflow = new Workflow($definition, new MultipleStateMarkingStore(), $eventDispatcher, 'workflow_name');
317+
318+
$eventNameExpected = [
319+
'workflow.guard',
320+
'workflow.workflow_name.guard',
321+
'workflow.workflow_name.guard.a-b',
322+
'workflow.leave',
323+
'workflow.workflow_name.leave',
324+
'workflow.workflow_name.leave.a',
325+
'workflow.transition',
326+
'workflow.workflow_name.transition',
327+
'workflow.workflow_name.transition.a-b',
328+
'workflow.enter',
329+
'workflow.workflow_name.enter',
330+
'workflow.workflow_name.enter.b',
331+
'workflow.entered',
332+
'workflow.workflow_name.entered',
333+
'workflow.workflow_name.entered.b',
334+
'workflow.completed',
335+
'workflow.workflow_name.completed',
336+
'workflow.workflow_name.completed.a-b',
337+
'workflow.announce',
338+
'workflow.workflow_name.announce',
339+
];
340+
341+
$marking = $workflow->apply($subject, 'a-b');
342+
343+
$this->assertSame($eventNameExpected, $eventDispatcher->dispatchedEvents);
344+
}
345+
307346
public function testEventName()
308347
{
309348
$definition = $this->createComplexWorkflowDefinition();

src/Symfony/Component/Workflow/Workflow.php

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -125,22 +125,20 @@ public function can($subject, $transitionName)
125125
*/
126126
public function apply($subject, $transitionName)
127127
{
128-
$transitions = $this->getEnabledTransitions($subject);
129-
130-
// We can shortcut the getMarking method in order to boost performance,
131-
// since the "getEnabledTransitions" method already checks the Marking
132-
// state
133-
$marking = $this->markingStore->getMarking($subject);
134-
135-
$applied = false;
128+
$marking = $this->getMarking($subject);
129+
$transitions = [];
136130

137-
foreach ($transitions as $transition) {
138-
if ($transitionName !== $transition->getName()) {
139-
continue;
131+
foreach ($this->definition->getTransitions() as $transition) {
132+
if ($transitionName === $transition->getName() && $this->doCan($subject, $marking, $transition)) {
133+
$transitions[] = $transition;
140134
}
135+
}
141136

142-
$applied = true;
137+
if (!$transitions) {
138+
throw new LogicException(sprintf('Unable to apply transition "%s" for workflow "%s".', $transitionName, $this->name));
139+
}
143140

141+
foreach ($transitions as $transition) {
144142
$this->leave($subject, $transition, $marking);
145143

146144
$this->transition($subject, $transition, $marking);
@@ -156,10 +154,6 @@ public function apply($subject, $transitionName)
156154
$this->announce($subject, $transition, $marking);
157155
}
158156

159-
if (!$applied) {
160-
throw new LogicException(sprintf('Unable to apply transition "%s" for workflow "%s".', $transitionName, $this->name));
161-
}
162-
163157
return $marking;
164158
}
165159

0 commit comments

Comments
 (0)
0