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

[Workflow] Do not trigger extra guards #31584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Workflow] Do not trigger extra guard
With this patch, guard are executed only on wanted transitions
  • Loading branch information
lyrixx committed May 26, 2019
commit ad0619748ee54109c144d16eb99bf14de026c7bf
39 changes: 39 additions & 0 deletions 39 src/Symfony/Component/Workflow/Tests/WorkflowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,45 @@ public function testApplyWithEventDispatcher()
$this->assertSame($eventNameExpected, $eventDispatcher->dispatchedEvents);
}

public function testApplyDoesNotTriggerExtraGuardWithEventDispatcher()
{
$transitions[] = new Transition('a-b', 'a', 'b');
$transitions[] = new Transition('a-c', 'a', 'c');
$definition = new Definition(['a', 'b', 'c'], $transitions);

$subject = new \stdClass();
$subject->marking = null;
$eventDispatcher = new EventDispatcherMock();
$workflow = new Workflow($definition, new MultipleStateMarkingStore(), $eventDispatcher, 'workflow_name');

$eventNameExpected = [
'workflow.guard',
'workflow.workflow_name.guard',
'workflow.workflow_name.guard.a-b',
'workflow.leave',
'workflow.workflow_name.leave',
'workflow.workflow_name.leave.a',
'workflow.transition',
'workflow.workflow_name.transition',
'workflow.workflow_name.transition.a-b',
'workflow.enter',
'workflow.workflow_name.enter',
'workflow.workflow_name.enter.b',
'workflow.entered',
'workflow.workflow_name.entered',
'workflow.workflow_name.entered.b',
'workflow.completed',
'workflow.workflow_name.completed',
'workflow.workflow_name.completed.a-b',
'workflow.announce',
'workflow.workflow_name.announce',
];

$marking = $workflow->apply($subject, 'a-b');

$this->assertSame($eventNameExpected, $eventDispatcher->dispatchedEvents);
}

public function testEventName()
{
$definition = $this->createComplexWorkflowDefinition();
Expand Down
26 changes: 10 additions & 16 deletions 26 src/Symfony/Component/Workflow/Workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,22 +125,20 @@ public function can($subject, $transitionName)
*/
public function apply($subject, $transitionName)
{
$transitions = $this->getEnabledTransitions($subject);

// We can shortcut the getMarking method in order to boost performance,
// since the "getEnabledTransitions" method already checks the Marking
// state
$marking = $this->markingStore->getMarking($subject);

$applied = false;
$marking = $this->getMarking($subject);
$transitions = [];

foreach ($transitions as $transition) {
if ($transitionName !== $transition->getName()) {
continue;
foreach ($this->definition->getTransitions() as $transition) {
if ($transitionName === $transition->getName() && $this->doCan($subject, $marking, $transition)) {
$transitions[] = $transition;
}
}

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

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

$this->transition($subject, $transition, $marking);
Expand All @@ -156,10 +154,6 @@ public function apply($subject, $transitionName)
$this->announce($subject, $transition, $marking);
}

if (!$applied) {
throw new LogicException(sprintf('Unable to apply transition "%s" for workflow "%s".', $transitionName, $this->name));
}

return $marking;
}

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.