Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfc3ead2

Browse files
committed
Merge branch '5.4' into 6.0
* 5.4: [#15990] Minor changes [Messenger] Autoconfigurable attributes
2 parents8e1b619 +01be48d commitfc3ead2

File tree

1 file changed

+54
-7
lines changed

1 file changed

+54
-7
lines changed

‎messenger.rst‎

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,37 @@ serialized::
5050

5151
.. _messenger-handler:
5252

53+
..versionadded::5.4
54+
55+
The ``#[AsMessageHandler]`` PHP attribute was introduced in Symfony
56+
5.4. PHP attributes require at least PHP 8.0.
57+
5358
A message handler is a PHP callable, the recommended way to create it is to
54-
create a class thatimplements:class:`Symfony\\Component\\Messenger\\Handler\\MessageHandlerInterface`
55-
and has an ``__invoke()`` method that's type-hinted with the message class (or a
56-
message interface)::
59+
create a class thathas the:class:`Symfony\\Component\\Messenger\\Attribute\\AsMessageHandler`
60+
attributeand has an ``__invoke()`` method that's type-hinted with the
61+
messageclass (or a messageinterface)::
5762

5863
// src/MessageHandler/SmsNotificationHandler.php
5964
namespace App\MessageHandler;
6065

6166
use App\Message\SmsNotification;
62-
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
67+
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
6368

64-
class SmsNotificationHandler implements MessageHandlerInterface
69+
#[AsMessageHandler]
70+
class SmsNotificationHandler
6571
{
6672
public function __invoke(SmsNotification $message)
6773
{
6874
// ... do some work - like sending an SMS message!
6975
}
7076
}
7177

78+
..note::
79+
80+
You can also create a class without the attribute (e.g. if you're
81+
using PHP 7.4), by implementing:class:`Symfony\\Component\\Messenger\\Handler\\MessageHandlerInterface`
82+
instead.
83+
7284
Thanks to:ref:`autoconfiguration<services-autoconfigure>` and the ``SmsNotification``
7385
type-hint, Symfony knows that this handler should be called when an ``SmsNotification``
7486
message is dispatched. Most of the time, this is all you need to do. But you can
@@ -349,9 +361,10 @@ Then, in your handler, you can query for a fresh object::
349361

350362
use App\Message\NewUserWelcomeEmail;
351363
use App\Repository\UserRepository;
352-
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
364+
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
353365

354-
class NewUserWelcomeEmailHandler implements MessageHandlerInterface
366+
#[AsMessageHandler]
367+
class NewUserWelcomeEmailHandler
355368
{
356369
private $userRepository;
357370

@@ -1673,6 +1686,40 @@ on a case-by-case basis via the :class:`Symfony\\Component\\Messenger\\Stamp\\Se
16731686
Customizing Handlers
16741687
--------------------
16751688

1689+
Configuring Handlers Using Attributes
1690+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1691+
1692+
..versionadded::5.4
1693+
1694+
The ``#[AsMessageHandler]`` PHP attribute was introduced in Symfony
1695+
5.4. PHP attributes require at least PHP 8.0.
1696+
1697+
You can configure your handler by passing options to the attribute::
1698+
1699+
// src/MessageHandler/SmsNotificationHandler.php
1700+
namespace App\MessageHandler;
1701+
1702+
use App\Message\OtherSmsNotification;
1703+
use App\Message\SmsNotification;
1704+
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
1705+
1706+
#[AsMessageHandler(fromTransport: 'async', priority: 10)]
1707+
class SmsNotificationHandler
1708+
{
1709+
public function __invoke(SmsNotification $message)
1710+
{
1711+
// ...
1712+
}
1713+
}
1714+
1715+
Possible options to configure with the attribute are:
1716+
1717+
* ``bus``
1718+
* ``fromTransport``
1719+
* ``handles``
1720+
* ``method``
1721+
* ``priority``
1722+
16761723
.. _messenger-handler-config:
16771724

16781725
Manually Configuring Handlers

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp