Skip to content

[Workflow] Add support for getting updated context after a transition #41292

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
Jul 3, 2021
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
5 changes: 5 additions & 0 deletions src/Symfony/Component/Workflow/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

5.4
---

* Add support for getting updated context after a transition

5.3
---

Expand Down
17 changes: 17 additions & 0 deletions src/Symfony/Component/Workflow/Marking.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
class Marking
{
private $places = [];
private $context = null;

/**
* @param int[] $representation Keys are the place name and values should be 1
Expand Down Expand Up @@ -49,4 +50,20 @@ public function getPlaces()
{
return $this->places;
}

/**
* @internal
*/
public function setContext(array $context): void
{
$this->context = $context;
}

/**
* Returns the context after the subject has transitioned.
*/
public function getContext(): ?array
{
return $this->context;
}
}
23 changes: 22 additions & 1 deletion src/Symfony/Component/Workflow/Tests/WorkflowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,28 @@ public function testEventContext()
$dispatcher->addListener($eventName, $assertWorkflowContext);
}

$workflow->apply($subject, 't1', $context);
$marking = $workflow->apply($subject, 't1', $context);

$this->assertInstanceOf(Marking::class, $marking);
$this->assertSame($context, $marking->getContext());
}

public function testEventContextUpdated()
{
$definition = $this->createComplexWorkflowDefinition();
$subject = new Subject();
$dispatcher = new EventDispatcher();

$workflow = new Workflow($definition, new MethodMarkingStore(), $dispatcher);

$dispatcher->addListener('workflow.transition', function (TransitionEvent $event) {
$event->setContext(['foo' => 'bar']);
});

$marking = $workflow->apply($subject, 't1', ['initial']);

$this->assertInstanceOf(Marking::class, $marking);
$this->assertSame(['foo' => 'bar'], $marking->getContext());
}

public function testEventDefaultInitialContext()
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Workflow/Workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ public function apply(object $subject, string $transitionName, array $context =
$this->announce($subject, $transition, $marking, $context);
}

$marking->setContext($context);

return $marking;
}

Expand Down