Skip to content

Commit 5b973df

Browse files
Nyholmxabbuh
authored andcommitted
Properly show all events and describe guard events
1 parent a360e3c commit 5b973df

File tree

1 file changed

+57
-8
lines changed

1 file changed

+57
-8
lines changed

workflow/usage.rst

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,63 @@ object with an ``EventDispatcher``. You can now create event listeners to
182182
block transitions (i.e. depending on the data in the blog post). The following
183183
events are dispatched:
184184

185+
* ``workflow.leave``
186+
* ``workflow.[workflow name].leave``
187+
* ``workflow.[workflow name].leave.[transition name]``
188+
189+
* ``workflow.transition``
190+
* ``workflow.[workflow name].transition``
191+
* ``workflow.[workflow name].transition.[transition name]``
192+
193+
* ``workflow.enter``
194+
* ``workflow.[workflow name].enter``
195+
* ``workflow.[workflow name].enter.[transition name]``
196+
197+
* ``workflow.announce``
198+
* ``workflow.[workflow name].announce``
199+
* ``workflow.[workflow name].announce.[transition name]``
200+
201+
Here is an example how to enable logging for every time a the "blog_publishing" workflow leaves a place::
202+
203+
use Psr\Log\LoggerInterface;
204+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
205+
use Symfony\Component\Workflow\Event\Event;
206+
207+
class WorkflowLogger implements EventSubscriberInterface
208+
{
209+
public function __construct(LoggerInterface $logger)
210+
{
211+
$this->logger = $logger;
212+
}
213+
214+
public function onLeave(Event $event)
215+
{
216+
$this->logger->alert(sprintf(
217+
'Blog post (id: "%s") preformed transaction "%s" form "%s" to "%s"',
218+
$event->getSubject()->getId(),
219+
$event->getTransition()->getName(),
220+
implode(', ', array_keys($event->getMarking()->getPlaces())),
221+
implode(', ', $event->getTransition()->getTos())
222+
));
223+
}
224+
225+
public static function getSubscribedEvents()
226+
{
227+
return array(
228+
'workflow.blog_publishing.leave' => 'onLeave',
229+
);
230+
}
231+
}
232+
233+
Guard events
234+
~~~~~~~~~~~~
235+
236+
There are a special kind of events called "Guard events". Their event listeners
237+
are invoked every time a call to ``Workflow::can``, ``Workflow::apply`` or
238+
``Workflow::getEnabledTransitions`` is executed. With the guard events you may
239+
add custom logic to decide what transitions that are valid or not. Here is a list
240+
of the guard event names.
241+
185242
* ``workflow.guard``
186243
* ``workflow.[workflow name].guard``
187244
* ``workflow.[workflow name].guard.[transition name]``
@@ -213,14 +270,6 @@ See example to make sure no blog post without title is moved to "review"::
213270
}
214271
}
215272

216-
With help from the ``EventDispatcher`` and the ``AuditTrailListener`` you
217-
could easily enable logging::
218-
219-
use Symfony\Component\Workflow\EventListener\AuditTrailListener;
220-
221-
$logger = new AnyPsr3Logger();
222-
$subscriber = new AuditTrailListener($logger);
223-
$dispatcher->addSubscriber($subscriber);
224273

225274
Event Methods
226275
~~~~~~~~~~~~~

0 commit comments

Comments
 (0)