Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
Next Next commit
[Messenger] Autoconfigurable attributes
  • Loading branch information
alirezamirsepassi committed Oct 22, 2021
commit fdd8b868f820fc36120ad503e69a453dde07a557
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