Skip to content

Commit

Permalink
[Workflow] Trigger entered event for subject entering in the Workfl…
Browse files Browse the repository at this point in the history
…ow for the first time
  • Loading branch information
lyrixx committed Nov 8, 2018
1 parent 8d277ce commit 388840f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/Symfony/Component/Workflow/CHANGELOG.md
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.3.0
-----

* Trigger `entered` event for subject entering in the Workflow for the first time

4.1.0
-----

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Workflow/Event/Event.php
Expand Up @@ -35,7 +35,7 @@ class Event extends BaseEvent
* @param Transition $transition
* @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->marking = $marking;
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Workflow/Tests/WorkflowTest.php
Expand Up @@ -373,6 +373,8 @@ public function testApplyWithEventDispatcher()
$workflow = new Workflow($definition, new MultipleStateMarkingStore(), $eventDispatcher, 'workflow_name');

$eventNameExpected = array(
'workflow.entered',
'workflow.workflow_name.entered',
'workflow.guard',
'workflow.workflow_name.guard',
'workflow.workflow_name.guard.t1',
Expand Down
10 changes: 7 additions & 3 deletions src/Symfony/Component/Workflow/Workflow.php
Expand Up @@ -61,6 +61,8 @@ public function getMarking($subject)

// update the subject with the new marking
$this->markingStore->setMarking($subject, $marking);

$this->entered($subject, null, $marking);
}

// check that the subject has a known place
Expand Down Expand Up @@ -323,7 +325,7 @@ private function enter($subject, Transition $transition, Marking $marking): void
}
}

private function entered($subject, Transition $transition, Marking $marking): void
private function entered($subject, Transition $transition = null, Marking $marking): void
{
if (null === $this->dispatcher) {
return;
Expand All @@ -334,8 +336,10 @@ private function entered($subject, Transition $transition, Marking $marking): vo
$this->dispatcher->dispatch('workflow.entered', $event);
$this->dispatcher->dispatch(sprintf('workflow.%s.entered', $this->name), $event);

foreach ($transition->getTos() as $place) {
$this->dispatcher->dispatch(sprintf('workflow.%s.entered.%s', $this->name, $place), $event);
if ($transition) {
foreach ($transition->getTos() as $place) {
$this->dispatcher->dispatch(sprintf('workflow.%s.entered.%s', $this->name, $place), $event);
}
}
}

Expand Down

0 comments on commit 388840f

Please sign in to comment.