-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Workflow] Added Context to Workflow Event #37539
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,10 +30,12 @@ | |
* @author Fabien Potencier <fabien@symfony.com> | ||
* @author Grégoire Pineau <lyrixx@lyrixx.info> | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
* @author Carlos Pereira De Amorim <carlos@shauri.fr> | ||
*/ | ||
class Workflow implements WorkflowInterface | ||
{ | ||
public const DISABLE_ANNOUNCE_EVENT = 'workflow_disable_announce_event'; | ||
public const DEFAULT_INITIAL_CONTEXT = ['initial' => true]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason you made this an associative array and not an indexed one? I see in one of your tests you did use an indexed array as $context. Are yuo There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this way, you can do : |
||
|
||
private $definition; | ||
private $markingStore; | ||
|
@@ -51,7 +53,7 @@ public function __construct(Definition $definition, MarkingStoreInterface $marki | |
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getMarking(object $subject) | ||
public function getMarking(object $subject, array $context = []) | ||
{ | ||
$marking = $this->markingStore->getMarking($subject); | ||
|
||
|
@@ -71,7 +73,11 @@ public function getMarking(object $subject) | |
// update the subject with the new marking | ||
$this->markingStore->setMarking($subject, $marking); | ||
|
||
$this->entered($subject, null, $marking); | ||
if (!$context) { | ||
$context = self::DEFAULT_INITIAL_CONTEXT; | ||
} | ||
|
||
$this->entered($subject, null, $marking, $context); | ||
} | ||
|
||
// check that the subject has a known place | ||
|
@@ -154,7 +160,7 @@ public function buildTransitionBlockerList(object $subject, string $transitionNa | |
*/ | ||
public function apply(object $subject, string $transitionName, array $context = []) | ||
{ | ||
$marking = $this->getMarking($subject); | ||
$marking = $this->getMarking($subject, $context); | ||
|
||
$transitionExist = false; | ||
$approvedTransitions = []; | ||
|
@@ -197,20 +203,20 @@ public function apply(object $subject, string $transitionName, array $context = | |
} | ||
|
||
foreach ($approvedTransitions as $transition) { | ||
$this->leave($subject, $transition, $marking); | ||
$this->leave($subject, $transition, $marking, $context); | ||
|
||
$context = $this->transition($subject, $transition, $marking, $context); | ||
|
||
$this->enter($subject, $transition, $marking); | ||
$this->enter($subject, $transition, $marking, $context); | ||
|
||
$this->markingStore->setMarking($subject, $marking, $context); | ||
|
||
$this->entered($subject, $transition, $marking); | ||
$this->entered($subject, $transition, $marking, $context); | ||
|
||
$this->completed($subject, $transition, $marking); | ||
$this->completed($subject, $transition, $marking, $context); | ||
|
||
if (!($context[self::DISABLE_ANNOUNCE_EVENT] ?? false)) { | ||
$this->announce($subject, $transition, $marking); | ||
$this->announce($subject, $transition, $marking, $context); | ||
} | ||
} | ||
|
||
|
@@ -324,12 +330,12 @@ private function guardTransition(object $subject, Marking $marking, Transition $ | |
return $event; | ||
} | ||
|
||
private function leave(object $subject, Transition $transition, Marking $marking): void | ||
private function leave(object $subject, Transition $transition, Marking $marking, array $context = []): void | ||
{ | ||
$places = $transition->getFroms(); | ||
|
||
if (null !== $this->dispatcher) { | ||
$event = new LeaveEvent($subject, $marking, $transition, $this); | ||
$event = new LeaveEvent($subject, $marking, $transition, $this, $context); | ||
|
||
$this->dispatcher->dispatch($event, WorkflowEvents::LEAVE); | ||
$this->dispatcher->dispatch($event, sprintf('workflow.%s.leave', $this->name)); | ||
|
@@ -350,8 +356,7 @@ private function transition(object $subject, Transition $transition, Marking $ma | |
return $context; | ||
} | ||
|
||
$event = new TransitionEvent($subject, $marking, $transition, $this); | ||
$event->setContext($context); | ||
$event = new TransitionEvent($subject, $marking, $transition, $this, $context); | ||
|
||
$this->dispatcher->dispatch($event, WorkflowEvents::TRANSITION); | ||
$this->dispatcher->dispatch($event, sprintf('workflow.%s.transition', $this->name)); | ||
|
@@ -360,12 +365,12 @@ private function transition(object $subject, Transition $transition, Marking $ma | |
return $event->getContext(); | ||
} | ||
|
||
private function enter(object $subject, Transition $transition, Marking $marking): void | ||
private function enter(object $subject, Transition $transition, Marking $marking, array $context): void | ||
{ | ||
$places = $transition->getTos(); | ||
|
||
if (null !== $this->dispatcher) { | ||
$event = new EnterEvent($subject, $marking, $transition, $this); | ||
$event = new EnterEvent($subject, $marking, $transition, $this, $context); | ||
|
||
$this->dispatcher->dispatch($event, WorkflowEvents::ENTER); | ||
$this->dispatcher->dispatch($event, sprintf('workflow.%s.enter', $this->name)); | ||
|
@@ -380,13 +385,13 @@ private function enter(object $subject, Transition $transition, Marking $marking | |
} | ||
} | ||
|
||
private function entered(object $subject, ?Transition $transition, Marking $marking): void | ||
private function entered(object $subject, ?Transition $transition, Marking $marking, array $context): void | ||
{ | ||
if (null === $this->dispatcher) { | ||
return; | ||
} | ||
|
||
$event = new EnteredEvent($subject, $marking, $transition, $this); | ||
$event = new EnteredEvent($subject, $marking, $transition, $this, $context); | ||
|
||
$this->dispatcher->dispatch($event, WorkflowEvents::ENTERED); | ||
$this->dispatcher->dispatch($event, sprintf('workflow.%s.entered', $this->name)); | ||
|
@@ -395,29 +400,33 @@ private function entered(object $subject, ?Transition $transition, Marking $mark | |
foreach ($transition->getTos() as $place) { | ||
$this->dispatcher->dispatch($event, sprintf('workflow.%s.entered.%s', $this->name, $place)); | ||
} | ||
} elseif (!empty($this->definition->getInitialPlaces())) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not related to the current PR. Could you revert it? thanks There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. without that, the DEFAULT_INITIAL_CONTEXT makes no sense. it's a little far fetched, but I believe adding this context enhancement is a good moment to add this default context Unless you don't want a DEFAULT_INITIAL_CONTEXT ? |
||
foreach ($this->definition->getInitialPlaces() as $place) { | ||
$this->dispatcher->dispatch($event, sprintf('workflow.%s.entered.%s', $this->name, $place)); | ||
} | ||
} | ||
} | ||
|
||
private function completed(object $subject, Transition $transition, Marking $marking): void | ||
private function completed(object $subject, Transition $transition, Marking $marking, array $context): void | ||
{ | ||
if (null === $this->dispatcher) { | ||
return; | ||
} | ||
|
||
$event = new CompletedEvent($subject, $marking, $transition, $this); | ||
$event = new CompletedEvent($subject, $marking, $transition, $this, $context); | ||
|
||
$this->dispatcher->dispatch($event, WorkflowEvents::COMPLETED); | ||
$this->dispatcher->dispatch($event, sprintf('workflow.%s.completed', $this->name)); | ||
$this->dispatcher->dispatch($event, sprintf('workflow.%s.completed.%s', $this->name, $transition->getName())); | ||
} | ||
|
||
private function announce(object $subject, Transition $initialTransition, Marking $marking): void | ||
private function announce(object $subject, Transition $initialTransition, Marking $marking, array $context): void | ||
{ | ||
if (null === $this->dispatcher) { | ||
return; | ||
} | ||
|
||
$event = new AnnounceEvent($subject, $marking, $initialTransition, $this); | ||
$event = new AnnounceEvent($subject, $marking, $initialTransition, $this, $context); | ||
|
||
$this->dispatcher->dispatch($event, WorkflowEvents::ANNOUNCE); | ||
$this->dispatcher->dispatch($event, sprintf('workflow.%s.announce', $this->name)); | ||
|
Uh oh!
There was an error while loading. Please reload this page.