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

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d0eb9df

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

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
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 & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,17 @@ public function can($subject, $transitionName)
125125
*/
126126
public function apply($subject, $transitionName)
127127
{
128-
$transitions = $this->getEnabledTransitions($subject);
128+
$marking = $this->getMarking($subject);
129+
$transitions = [];
129130

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);
131+
foreach ($this->definition->getTransitions() as $transition) {
132+
if ($transition->getName() !== $transitionName) {
133+
continue;
134+
}
135+
if ($this->doCan($subject, $marking, $transition)) {
136+
$transitions[] = $transition;
137+
}
138+
}
134139

135140
$applied = false;
136141

0 commit comments

Comments
 (0)
0