Skip to content

[Worfklow] Add 'initiated' event which is dispatched upon entering inital place #37481

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

Closed
wants to merge 10 commits into from
Closed
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 @@ -5,6 +5,7 @@ CHANGELOG
-----

* Added function `getEnabledTransition` to easily retrieve a specific transition object
* Added and trigger `initiated` event for subject entering in the Workflow for the first time.

5.1.0
-----
Expand Down
16 changes: 16 additions & 0 deletions src/Symfony/Component/Workflow/Event/InitiatedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Workflow\Event;

final class InitiatedEvent extends Event
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ public function __construct(LoggerInterface $logger)
$this->logger = $logger;
}

public function onInitiate(Event $event)
{
foreach ($event->getMarking()->getPlaces() as $place => $nbToken) {
$this->logger->info(sprintf('Initiated "%s" for subject of class "%s" in workflow "%s".', $place, \get_class($event->getSubject()), $event->getWorkflowName()));
}
}

public function onLeave(Event $event)
{
foreach ($event->getTransition()->getFroms() as $place) {
Expand All @@ -49,6 +56,7 @@ public function onEnter(Event $event)
public static function getSubscribedEvents()
{
return [
'workflow.initiate' => ['onInitiate'],
'workflow.leave' => ['onLeave'],
'workflow.transition' => ['onTransition'],
'workflow.enter' => ['onEnter'],
Expand Down
21 changes: 21 additions & 0 deletions src/Symfony/Component/Workflow/Workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Workflow\Event\EnteredEvent;
use Symfony\Component\Workflow\Event\EnterEvent;
use Symfony\Component\Workflow\Event\GuardEvent;
use Symfony\Component\Workflow\Event\InitiatedEvent;
use Symfony\Component\Workflow\Event\LeaveEvent;
use Symfony\Component\Workflow\Event\TransitionEvent;
use Symfony\Component\Workflow\Exception\LogicException;
Expand All @@ -30,6 +31,7 @@
* @author Fabien Potencier <fabien@symfony.com>
* @author Grégoire Pineau <lyrixx@lyrixx.info>
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
* @author Marcel Berteler <pluk77@gmail.com>
*/
class Workflow implements WorkflowInterface
{
Expand Down Expand Up @@ -71,6 +73,7 @@ public function getMarking(object $subject)
// update the subject with the new marking
$this->markingStore->setMarking($subject, $marking);

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

Expand Down Expand Up @@ -398,6 +401,24 @@ private function entered(object $subject, ?Transition $transition, Marking $mark
}
}

private function initiated(object $subject, Marking $marking): void
{
if (null === $this->dispatcher) {
return;
}

$event = new InitiatedEvent($subject, $marking, null, $this);

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

if (!empty($this->definition->getInitialPlaces())) {
foreach ($this->definition->getInitialPlaces() as $place) {
$this->dispatcher->dispatch($event, sprintf('workflow.%s.initiated.%s', $this->name, $place));
}
}
}

private function completed(object $subject, Transition $transition, Marking $marking): void
{
if (null === $this->dispatcher) {
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/Workflow/WorkflowEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Workflow\Event\EnteredEvent;
use Symfony\Component\Workflow\Event\EnterEvent;
use Symfony\Component\Workflow\Event\GuardEvent;
use Symfony\Component\Workflow\Event\InitiatedEvent;
use Symfony\Component\Workflow\Event\LeaveEvent;
use Symfony\Component\Workflow\Event\TransitionEvent;

Expand Down Expand Up @@ -50,6 +51,11 @@ final class WorkflowEvents
*/
const ENTERED = 'workflow.entered';

/**
* @Event("Symfony\Component\Workflow\Event\InitiatedEvent")
*/
const INITIATED = 'workflow.initiated';

/**
* @Event("Symfony\Component\Workflow\Event\LeaveEvent")
*/
Expand Down