[Workflow] The TransitionEvent is able to modify the context
This commit is contained in:
parent
09dee1737d
commit
62ab775154
@ -6,6 +6,7 @@ CHANGELOG
|
|||||||
|
|
||||||
* Trigger `entered` event for subject entering in the Workflow for the first time.
|
* 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.
|
* 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:
|
* Add style to transitions by declaring metadata:
|
||||||
|
|
||||||
use Symfony\Component\Workflow\Definition;
|
use Symfony\Component\Workflow\Definition;
|
||||||
|
@ -13,4 +13,15 @@ namespace Symfony\Component\Workflow\Event;
|
|||||||
|
|
||||||
class TransitionEvent extends Event
|
class TransitionEvent extends Event
|
||||||
{
|
{
|
||||||
|
private $context;
|
||||||
|
|
||||||
|
public function setContext(array $context)
|
||||||
|
{
|
||||||
|
$this->context = $context;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getContext(): array
|
||||||
|
{
|
||||||
|
return $this->context;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,10 +5,12 @@ namespace Symfony\Component\Workflow\Tests;
|
|||||||
final class Subject
|
final class Subject
|
||||||
{
|
{
|
||||||
private $marking;
|
private $marking;
|
||||||
|
private $context;
|
||||||
|
|
||||||
public function __construct($marking = null)
|
public function __construct($marking = null)
|
||||||
{
|
{
|
||||||
$this->marking = $marking;
|
$this->marking = $marking;
|
||||||
|
$this->context = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getMarking()
|
public function getMarking()
|
||||||
@ -16,8 +18,14 @@ final class Subject
|
|||||||
return $this->marking;
|
return $this->marking;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setMarking($marking)
|
public function setMarking($marking, array $context = [])
|
||||||
{
|
{
|
||||||
$this->marking = $marking;
|
$this->marking = $marking;
|
||||||
|
$this->context = $context;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getContext(): array
|
||||||
|
{
|
||||||
|
return $this->context;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@ use Symfony\Component\EventDispatcher\EventDispatcher;
|
|||||||
use Symfony\Component\Workflow\Definition;
|
use Symfony\Component\Workflow\Definition;
|
||||||
use Symfony\Component\Workflow\Event\Event;
|
use Symfony\Component\Workflow\Event\Event;
|
||||||
use Symfony\Component\Workflow\Event\GuardEvent;
|
use Symfony\Component\Workflow\Event\GuardEvent;
|
||||||
|
use Symfony\Component\Workflow\Event\TransitionEvent;
|
||||||
use Symfony\Component\Workflow\Exception\NotEnabledTransitionException;
|
use Symfony\Component\Workflow\Exception\NotEnabledTransitionException;
|
||||||
use Symfony\Component\Workflow\Marking;
|
use Symfony\Component\Workflow\Marking;
|
||||||
use Symfony\Component\Workflow\MarkingStore\MarkingStoreInterface;
|
use Symfony\Component\Workflow\MarkingStore\MarkingStoreInterface;
|
||||||
@ -418,6 +419,21 @@ class WorkflowTest extends TestCase
|
|||||||
$this->assertSame($eventNameExpected, $eventDispatcher->dispatchedEvents);
|
$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()
|
public function testEventName()
|
||||||
{
|
{
|
||||||
$definition = $this->createComplexWorkflowDefinition();
|
$definition = $this->createComplexWorkflowDefinition();
|
||||||
|
@ -176,7 +176,7 @@ class Workflow implements WorkflowInterface
|
|||||||
|
|
||||||
$this->leave($subject, $transition, $marking);
|
$this->leave($subject, $transition, $marking);
|
||||||
|
|
||||||
$this->transition($subject, $transition, $marking);
|
$context = $this->transition($subject, $transition, $marking, $context);
|
||||||
|
|
||||||
$this->enter($subject, $transition, $marking);
|
$this->enter($subject, $transition, $marking);
|
||||||
|
|
||||||
@ -308,17 +308,20 @@ class Workflow implements WorkflowInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function transition($subject, Transition $transition, Marking $marking): void
|
private function transition($subject, Transition $transition, Marking $marking, array $context): array
|
||||||
{
|
{
|
||||||
if (null === $this->dispatcher) {
|
if (null === $this->dispatcher) {
|
||||||
return;
|
return $context;
|
||||||
}
|
}
|
||||||
|
|
||||||
$event = new TransitionEvent($subject, $marking, $transition, $this);
|
$event = new TransitionEvent($subject, $marking, $transition, $this);
|
||||||
|
$event->setContext($context);
|
||||||
|
|
||||||
$this->dispatcher->dispatch($event, WorkflowEvents::TRANSITION);
|
$this->dispatcher->dispatch($event, WorkflowEvents::TRANSITION);
|
||||||
$this->dispatcher->dispatch($event, sprintf('workflow.%s.transition', $this->name));
|
$this->dispatcher->dispatch($event, sprintf('workflow.%s.transition', $this->name));
|
||||||
$this->dispatcher->dispatch($event, sprintf('workflow.%s.transition.%s', $this->name, $transition->getName()));
|
$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
|
private function enter($subject, Transition $transition, Marking $marking): void
|
||||||
|
Reference in New Issue
Block a user