Skip to content

Commit

Permalink
merge configuration blocks for AsMessageHandler attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
MrYamous authored Dec 10, 2023
1 parent 8ca8c9b commit b885083
Showing 1 changed file with 20 additions and 35 deletions.
55 changes: 20 additions & 35 deletions messenger.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1994,51 +1994,36 @@ wherever you need a query bus behavior instead of the ``MessageBusInterface``::
Customizing Handlers
--------------------

Configuring Handlers Using Attributes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can configure your handler 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): void
{
// ...
}
}

Possible options to configure with the attribute are:

============================== ====================================================================================================
Option Description
============================== ====================================================================================================
``bus`` Name of the bus from which the handler can receive messages, by default all buses.
``fromTransport`` Name of the transport from which the handler can receive messages, by default all transports.
``handles`` Type of messages (FQCN) that can be processed by the handler, only needed if can't be guessed by
type-hint.
``method`` Name of the method that will process the message, only if the target is a class.
``priority`` Priority of the handler when multiple handlers can process the same message.
============================== ====================================================================================================

.. _messenger-handler-config:

Manually Configuring Handlers
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Symfony will normally :ref:`find and register your handler automatically <messenger-handler>`.
But, you can also configure a handler manually - and pass it some extra config -
by tagging the handler service with ``messenger.message_handler``
while using ``#AsMessageHandler`` attribute or tagging the handler service
with ``messenger.message_handler``.

.. configuration-block::

.. code-block:: php-attributes
// 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): void
{
// ...
}
}
.. code-block:: yaml
# config/services.yaml
Expand Down

0 comments on commit b885083

Please sign in to comment.