[Workflow] Trigger entered event for subject entering in the Workflow for the first time

This commit is contained in:
Grégoire Pineau 2018-11-08 16:47:13 +01:00
parent 8d277ce3e5
commit 388840fd99
4 changed files with 15 additions and 4 deletions

View File

@ -1,6 +1,11 @@
CHANGELOG CHANGELOG
========= =========
4.3.0
-----
* Trigger `entered` event for subject entering in the Workflow for the first time
4.1.0 4.1.0
----- -----

View File

@ -35,7 +35,7 @@ class Event extends BaseEvent
* @param Transition $transition * @param Transition $transition
* @param WorkflowInterface $workflow * @param WorkflowInterface $workflow
*/ */
public function __construct($subject, Marking $marking, Transition $transition, $workflow = null) public function __construct($subject, Marking $marking, Transition $transition = null, $workflow = null)
{ {
$this->subject = $subject; $this->subject = $subject;
$this->marking = $marking; $this->marking = $marking;

View File

@ -373,6 +373,8 @@ class WorkflowTest extends TestCase
$workflow = new Workflow($definition, new MultipleStateMarkingStore(), $eventDispatcher, 'workflow_name'); $workflow = new Workflow($definition, new MultipleStateMarkingStore(), $eventDispatcher, 'workflow_name');
$eventNameExpected = array( $eventNameExpected = array(
'workflow.entered',
'workflow.workflow_name.entered',
'workflow.guard', 'workflow.guard',
'workflow.workflow_name.guard', 'workflow.workflow_name.guard',
'workflow.workflow_name.guard.t1', 'workflow.workflow_name.guard.t1',

View File

@ -61,6 +61,8 @@ class Workflow implements WorkflowInterface
// update the subject with the new marking // update the subject with the new marking
$this->markingStore->setMarking($subject, $marking); $this->markingStore->setMarking($subject, $marking);
$this->entered($subject, null, $marking);
} }
// check that the subject has a known place // check that the subject has a known place
@ -323,7 +325,7 @@ class Workflow implements WorkflowInterface
} }
} }
private function entered($subject, Transition $transition, Marking $marking): void private function entered($subject, Transition $transition = null, Marking $marking): void
{ {
if (null === $this->dispatcher) { if (null === $this->dispatcher) {
return; return;
@ -334,8 +336,10 @@ class Workflow implements WorkflowInterface
$this->dispatcher->dispatch('workflow.entered', $event); $this->dispatcher->dispatch('workflow.entered', $event);
$this->dispatcher->dispatch(sprintf('workflow.%s.entered', $this->name), $event); $this->dispatcher->dispatch(sprintf('workflow.%s.entered', $this->name), $event);
foreach ($transition->getTos() as $place) { if ($transition) {
$this->dispatcher->dispatch(sprintf('workflow.%s.entered.%s', $this->name, $place), $event); foreach ($transition->getTos() as $place) {
$this->dispatcher->dispatch(sprintf('workflow.%s.entered.%s', $this->name, $place), $event);
}
} }
} }