Skip to content

Add output of debug:messenger information on Messenger doc #13390

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
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
70 changes: 63 additions & 7 deletions components/messenger.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,34 @@ Concepts
to use for transport, markers identifying a received message or any sort of
metadata your middleware or transport layer may use.

**Message**
There is no specific requirement for a message, it can be any kind of object except it
should be serializable and unserializable by a Symfony Serializer instance.
Copy link
Contributor

Choose a reason for hiding this comment

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

Actually not. Native serialization mechanism is used by default


Message
-------
Message is a serializable object that holds the data that will be dispatched. It does not need
to extend a class or implement an interface.
Copy link
Contributor

Choose a reason for hiding this comment

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

interface::

And remove the next Example line please


Example::

namespace App\Message;

class MyMessage {
Copy link
Contributor

Choose a reason for hiding this comment

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

Opening bracket should be on the next line


protected $name = 'name';
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add string typehint


public function __construct(string $name)
Copy link
Contributor

Choose a reason for hiding this comment

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

If Name property has a default value, why is it needed to provide a name in this example?

Can we remove the default from the property?

{
$this->name = $name;
}

public function getName(): string
{
return $this->name;
}
}

Bus
---

Expand Down Expand Up @@ -112,8 +140,9 @@ that will do the required processing for your message::
namespace App\MessageHandler;

use App\Message\MyMessage;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;

class MyMessageHandler
class MyMessageHandler implements MessageHandlerInterface
{
public function __invoke(MyMessage $message)
{
Expand Down Expand Up @@ -209,7 +238,7 @@ transport will be responsible for communicating with your message broker or 3rd
Your own Sender
~~~~~~~~~~~~~~~

Imagine that you already have an ``ImportantAction`` message going through the
You already have the ``MyMessage`` message going through the
message bus and being handled by a handler. Now, you also want to send this
message as an email (using the :doc:`Mime </components/mime>` and
:doc:`Mailer </components/mailer>` components).
Expand All @@ -219,13 +248,13 @@ you can create your own message sender::

namespace App\MessageSender;

use App\Message\ImportantAction;
use App\Message\MyMessage;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Transport\Sender\SenderInterface;
use Symfony\Component\Mime\Email;

class ImportantActionToEmailSender implements SenderInterface
class MyMessageToEmailSender implements SenderInterface
Copy link
Contributor

Choose a reason for hiding this comment

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

I would keep the old name

{
private $mailer;
private $toEmail;
Expand All @@ -240,15 +269,15 @@ you can create your own message sender::
{
$message = $envelope->getMessage();

if (!$message instanceof ImportantAction) {
throw new \InvalidArgumentException(sprintf('This transport only supports "%s" messages.', ImportantAction::class));
if (!$message instanceof MyMessage) {
throw new \InvalidArgumentException(sprintf('This transport only supports "%s" messages.', MyMessage::class));
}

$this->mailer->send(
(new Email())
->to($this->toEmail)
->subject('Important action made')
->html('<h1>Important action</h1><p>Made by '.$message->getUsername().'</p>')
->html('<h1>Important action</h1><p>Made by '.$message->getName().'</p>')
);

return $envelope;
Expand Down Expand Up @@ -329,6 +358,33 @@ loop, the message bus will add a :class:`Symfony\\Component\\Messenger\\Stamp\\R
stamp to the message envelopes and the :class:`Symfony\\Component\\Messenger\\Middleware\\SendMessageMiddleware`
middleware will know it should not route these messages again to a transport.

Debugging the Messenger
-----------------------

The ``debug:messenger`` command lists available messages & handlers per bus.
You can also restrict the list to a specific bus by providing its name as argument.

.. code-block:: terminal

$ php bin/console debug:messenger

Messenger
=========

messenger.bus.default
---------------------

The following messages can be dispatched:

-----------------------------------------------------
App\Message\MyMessage
handled by App\MessageHandler\MyMessageHandler

Symfony\Component\Mailer\Messenger\SendEmailMessage
handled by mailer.messenger.message_handler

-----------------------------------------------------

Learn more
----------

Expand Down