Skip to content

[Messenger] Autoconfigurable attributes #15990

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

Merged
merged 2 commits into from
Jan 19, 2022
Merged
Changes from 1 commit
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
47 changes: 40 additions & 7 deletions messenger.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd remove the mention of the interface

: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)
{
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down