From acf60125ffda2c3cadf924c34059187de619acaf Mon Sep 17 00:00:00 2001 From: Alexandre Daubois Date: Thu, 3 Aug 2023 16:26:51 +0200 Subject: [PATCH] [Workflow] Add PHP attributes to register listeners and guards --- reference/attributes.rst | 11 +++++++++++ workflow.rst | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/reference/attributes.rst b/reference/attributes.rst index 217c73346f0..761099a7356 100644 --- a/reference/attributes.rst +++ b/reference/attributes.rst @@ -117,6 +117,17 @@ Each validation constraint comes with a PHP attribute. See * :doc:`HasNamedArgument ` +Workflow +~~~~~~~~ + +* :ref:`AsAnnounceListener ` +* :ref:`AsCompletedListener ` +* :ref:`AsEnterListener ` +* :ref:`AsEnteredListener ` +* :ref:`AsGuardListener ` +* :ref:`AsLeaveListener ` +* :ref:`AsTransitionListener ` + .. _`AsEntityAutocompleteField`: https://symfony.com/bundles/ux-autocomplete/current/index.html#usage-in-a-form-with-ajax .. _`AsLiveComponent`: https://symfony.com/bundles/ux-live-component/current/index.html .. _`AsTwigComponent`: https://symfony.com/bundles/ux-twig-component/current/index.html diff --git a/workflow.rst b/workflow.rst index dad77a90386..bca1f129863 100644 --- a/workflow.rst +++ b/workflow.rst @@ -361,6 +361,8 @@ name. You can find the list of available workflow services with the ``php bin/console debug:autowiring workflow`` command. +.. _workflow_using-events: + Using Events ------------ @@ -519,6 +521,40 @@ it via the marking:: // contains the new value $marking->getContext(); +It is also possible to listen to these events by declaring event listeners +with the following attributes: + +* :class:`Symfony\\Component\\Workflow\\Attribute\\AsAnnounceListener` +* :class:`Symfony\\Component\\Workflow\\Attribute\\AsCompletedListener` +* :class:`Symfony\\Component\\Workflow\\Attribute\\AsEnterListener` +* :class:`Symfony\\Component\\Workflow\\Attribute\\AsEnteredListener` +* :class:`Symfony\\Component\\Workflow\\Attribute\\AsGuardListener` +* :class:`Symfony\\Component\\Workflow\\Attribute\\AsLeaveListener` +* :class:`Symfony\\Component\\Workflow\\Attribute\\AsTransitionListener` + +These attributes do work like the +:class:`Symfony\\Component\\EventDispatcher\\Attribute\\AsEventListener` +attributes:: + + class ArticleWorkflowEventListener + { + #[AsTransitionListener(workflow: 'my-workflow', transition: 'published')] + public function onPublishedTransition(TransitionEvent $event): void + { + // ... + } + + // ... + } + +You may refer to the documentation about +:ref:`defining event listeners with PHP attributes ` +for further use. + +.. versionadded:: 6.4 + + The workflow event attributes were introduced in Symfony 6.4. + .. _workflow-usage-guard-events: Guard Events