Skip to content
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
1 change: 1 addition & 0 deletions src/Symfony/Component/Workflow/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* Trigger `entered` event for subject entering in the Workflow for the first time.
* Added a context to `Workflow::apply()`. The `MethodMarkingStore` could be used to leverage this feature.
* The `TransitionEvent` is able to modify the context.
* Add style to transitions by declaring metadata:

use Symfony\Component\Workflow\Definition;
Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Component/Workflow/Event/TransitionEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,15 @@

class TransitionEvent extends Event
{
private $context;

public function setContext(array $context)
{
$this->context = $context;
}

public function getContext(): array
{
return $this->context;
}
}
10 changes: 9 additions & 1 deletion src/Symfony/Component/Workflow/Tests/Subject.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,27 @@
final class Subject
{
private $marking;
private $context;

public function __construct($marking = null)
{
$this->marking = $marking;
$this->context = [];
}

public function getMarking()
{
return $this->marking;
}

public function setMarking($marking)
public function setMarking($marking, array $context = [])
{
$this->marking = $marking;
$this->context = $context;
}

public function getContext(): array
{
return $this->context;
}
}
16 changes: 16 additions & 0 deletions src/Symfony/Component/Workflow/Tests/WorkflowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Symfony\Component\Workflow\Definition;
use Symfony\Component\Workflow\Event\Event;
use Symfony\Component\Workflow\Event\GuardEvent;
use Symfony\Component\Workflow\Event\TransitionEvent;
use Symfony\Component\Workflow\Exception\NotEnabledTransitionException;
use Symfony\Component\Workflow\Marking;
use Symfony\Component\Workflow\MarkingStore\MarkingStoreInterface;
Expand Down Expand Up @@ -418,6 +419,21 @@ public function testApplyWithEventDispatcher()
$this->assertSame($eventNameExpected, $eventDispatcher->dispatchedEvents);
}

public function testApplyWithContext()
{
$definition = $this->createComplexWorkflowDefinition();
$subject = new Subject();
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addListener('workflow.transition', function (TransitionEvent $event) {
$event->setContext(array_merge($event->getContext(), ['user' => 'admin']));
});
$workflow = new Workflow($definition, new MethodMarkingStore(), $eventDispatcher);

$workflow->apply($subject, 't1', ['foo' => 'bar']);

$this->assertSame(['foo' => 'bar', 'user' => 'admin'], $subject->getContext());
}

public function testEventName()
{
$definition = $this->createComplexWorkflowDefinition();
Expand Down
9 changes: 6 additions & 3 deletions src/Symfony/Component/Workflow/Workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public function apply($subject, $transitionName, array $context = [])

$this->leave($subject, $transition, $marking);

$this->transition($subject, $transition, $marking);
$context = $this->transition($subject, $transition, $marking, $context);

$this->enter($subject, $transition, $marking);

Expand Down Expand Up @@ -308,17 +308,20 @@ private function leave($subject, Transition $transition, Marking $marking): void
}
}

private function transition($subject, Transition $transition, Marking $marking): void
private function transition($subject, Transition $transition, Marking $marking, array $context): array
{
if (null === $this->dispatcher) {
return;
return $context;
}

$event = new TransitionEvent($subject, $marking, $transition, $this);
$event->setContext($context);

$this->dispatcher->dispatch($event, WorkflowEvents::TRANSITION);
$this->dispatcher->dispatch($event, sprintf('workflow.%s.transition', $this->name));
$this->dispatcher->dispatch($event, sprintf('workflow.%s.transition.%s', $this->name, $transition->getName()));

return $event->getContext();
}

private function enter($subject, Transition $transition, Marking $marking): void
Expand Down