From fdd8b868f820fc36120ad503e69a453dde07a557 Mon Sep 17 00:00:00 2001 From: Alireza Mirsepassi Date: Fri, 22 Oct 2021 20:11:42 +0330 Subject: [PATCH 1/2] [Messenger] Autoconfigurable attributes --- messenger.rst | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/messenger.rst b/messenger.rst index cbbd7e6b31f..7e8d1aa640f 100644 --- a/messenger.rst +++ b/messenger.rst @@ -51,17 +51,19 @@ serialized:: .. _messenger-handler: A message handler is a PHP callable, the recommended way to create it is to -create a class that implements :class:`Symfony\\Component\\Messenger\\Handler\\MessageHandlerInterface` -and has an ``__invoke()`` method that's type-hinted with the message class (or a -message interface):: +create a class using :class:`Symfony\\Component\\Messenger\\Attribute\\AsMessageHandler` +attribute which has an ``__invoke()`` method that's type-hinted with the +message class (or a message interface) or you can create a class without the attribute, by implementing +:class:`Symfony\\Component\\Messenger\\Handler\\MessageHandlerInterface`:: // src/MessageHandler/SmsNotificationHandler.php namespace App\MessageHandler; use App\Message\SmsNotification; - use Symfony\Component\Messenger\Handler\MessageHandlerInterface; + use Symfony\Component\Messenger\Attribute\AsMessageHandler; - class SmsNotificationHandler implements MessageHandlerInterface + #[AsMessageHandler] + class SmsNotificationHandler { public function __invoke(SmsNotification $message) { @@ -349,9 +351,10 @@ Then, in your handler, you can query for a fresh object:: use App\Message\NewUserWelcomeEmail; use App\Repository\UserRepository; - use Symfony\Component\Messenger\Handler\MessageHandlerInterface; + use Symfony\Component\Messenger\Attribute\AsMessageHandler; - class NewUserWelcomeEmailHandler implements MessageHandlerInterface + #[AsMessageHandler] + class NewUserWelcomeEmailHandler { private $userRepository; @@ -1769,6 +1772,36 @@ Customizing Handlers .. _messenger-handler-config: +Configuring Handlers Using Attribute +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +You can configure your handler easily by passing options to the attribute:: + + // src/MessageHandler/SmsNotificationHandler.php + namespace App\MessageHandler; + + use App\Message\OtherSmsNotification; + use App\Message\SmsNotification; + use Symfony\Component\Messenger\Attribute\AsMessageHandler; + + #[AsMessageHandler(fromTransport: 'async', priority: 10)] + class SmsNotificationHandler + { + public function __invoke(SmsNotification $message) + { + // ... + } + } + + +Possible options to configure with the attribute are: + +* ``bus`` +* ``fromTransport`` +* ``handles`` +* ``method`` +* ``priority`` + Manually Configuring Handlers ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ From 70690757bcf51a3bf0c9e764fd5d69225a4a361b Mon Sep 17 00:00:00 2001 From: Wouter de Jong Date: Wed, 19 Jan 2022 17:14:26 +0100 Subject: [PATCH 2/2] [#15990] Minor changes --- messenger.rst | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/messenger.rst b/messenger.rst index 7e8d1aa640f..2d40e64c6c3 100644 --- a/messenger.rst +++ b/messenger.rst @@ -50,11 +50,15 @@ serialized:: .. _messenger-handler: +.. versionadded:: 5.4 + + The ``#[AsMessageHandler]`` PHP attribute was introduced in Symfony + 5.4. PHP attributes require at least PHP 8.0. + A message handler is a PHP callable, the recommended way to create it is to -create a class using :class:`Symfony\\Component\\Messenger\\Attribute\\AsMessageHandler` -attribute which has an ``__invoke()`` method that's type-hinted with the -message class (or a message interface) or you can create a class without the attribute, by implementing -:class:`Symfony\\Component\\Messenger\\Handler\\MessageHandlerInterface`:: +create a class that has the :class:`Symfony\\Component\\Messenger\\Attribute\\AsMessageHandler` +attribute and has an ``__invoke()`` method that's type-hinted with the +message class (or a message interface):: // src/MessageHandler/SmsNotificationHandler.php namespace App\MessageHandler; @@ -71,6 +75,12 @@ message class (or a message interface) or you can create a class without the att } } +.. note:: + + You can also create a class without the attribute (e.g. if you're + using PHP 7.4), by implementing :class:`Symfony\\Component\\Messenger\\Handler\\MessageHandlerInterface` + instead. + Thanks to :ref:`autoconfiguration ` and the ``SmsNotification`` type-hint, Symfony knows that this handler should be called when an ``SmsNotification`` message is dispatched. Most of the time, this is all you need to do. But you can @@ -1770,12 +1780,15 @@ on a case-by-case basis via the :class:`Symfony\\Component\\Messenger\\Stamp\\Se Customizing Handlers -------------------- -.. _messenger-handler-config: +Configuring Handlers Using Attributes +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. versionadded:: 5.4 -Configuring Handlers Using Attribute -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + The ``#[AsMessageHandler]`` PHP attribute was introduced in Symfony + 5.4. PHP attributes require at least PHP 8.0. -You can configure your handler easily by passing options to the attribute:: +You can configure your handler by passing options to the attribute:: // src/MessageHandler/SmsNotificationHandler.php namespace App\MessageHandler; @@ -1793,7 +1806,6 @@ You can configure your handler easily by passing options to the attribute:: } } - Possible options to configure with the attribute are: * ``bus`` @@ -1802,6 +1814,8 @@ Possible options to configure with the attribute are: * ``method`` * ``priority`` +.. _messenger-handler-config: + Manually Configuring Handlers ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~