From 7937442745637f104949b8ef541a4b9a01e0df2d Mon Sep 17 00:00:00 2001 From: Arkadius Stefanski Date: Wed, 31 Oct 2018 11:40:48 +0100 Subject: [PATCH 1/5] Improved the Messenger Handler Example by reusing the SendNotification-Example --- messenger.rst | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/messenger.rst b/messenger.rst index 22e23fbecee..6ba0d9beedc 100644 --- a/messenger.rst +++ b/messenger.rst @@ -46,14 +46,14 @@ Registering Handlers In order to do something when your message is dispatched, you need to create a message handler. It's a class with an ``__invoke`` method:: - // src/MessageHandler/MyMessageHandler.php + // src/MessageHandler/SendNotificationHandler.php namespace App\MessageHandler; use Symfony\Component\Messenger\Handler\MessageHandlerInterface; - class MyMessageHandler implements MessageHandlerInterface + class SendNotificationHandler implements MessageHandlerInterface { - public function __invoke(MyMessage $message) + public function __invoke(SendNotification $message) { // do something with it. } @@ -72,7 +72,7 @@ If you're not using service autoconfiguration, then you need to add this config: # config/services.yaml services: - App\MessageHandler\MyMessageHandler: + App\MessageHandler\SendNotificationHandler: tags: [messenger.message_handler] .. code-block:: xml @@ -85,7 +85,7 @@ If you're not using service autoconfiguration, then you need to add this config: http://symfony.com/schema/dic/services/services-1.0.xsd"> - + @@ -94,9 +94,9 @@ If you're not using service autoconfiguration, then you need to add this config: .. code-block:: php // config/services.php - use App\MessageHandler\MyMessageHandler; + use App\MessageHandler\SendNotificationHandler; - $container->register(MyMessageHandler::class) + $container->register(SendNotificationHandler::class) ->addTag('messenger.message_handler'); .. note:: From 5ee822317ce2cb34e3032e669e961631bbc5f871 Mon Sep 17 00:00:00 2001 From: Arkadius Stefanski Date: Wed, 31 Oct 2018 12:01:32 +0100 Subject: [PATCH 2/5] add missing use-statement --- messenger.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/messenger.rst b/messenger.rst index 6ba0d9beedc..7bed4d3ffb1 100644 --- a/messenger.rst +++ b/messenger.rst @@ -49,6 +49,7 @@ message handler. It's a class with an ``__invoke`` method:: // src/MessageHandler/SendNotificationHandler.php namespace App\MessageHandler; + use App\Message\SendNotification; use Symfony\Component\Messenger\Handler\MessageHandlerInterface; class SendNotificationHandler implements MessageHandlerInterface From 6b9d0439fd1a985646591fcad48135c5ee581d13 Mon Sep 17 00:00:00 2001 From: Arkadius Stefanski Date: Wed, 31 Oct 2018 12:32:30 +0100 Subject: [PATCH 3/5] add message example --- messenger.rst | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/messenger.rst b/messenger.rst index 7bed4d3ffb1..94e1569b3cc 100644 --- a/messenger.rst +++ b/messenger.rst @@ -19,6 +19,27 @@ install messenger before using it: $ composer require messenger +Message +------- + +Before you can send a message, you must create it first. There is no specific +requirement for a message:: + + // src/Message/SendNotification.php + namespace App\Message; + + class SendNotification + { + private $message; + + public function __construct(string $message) + { + $this->message = $message; + } + + // ...getters + } + Using the Messenger Service --------------------------- From 15c7228abbda4712c8aeddb6f1a2c446ff4ec3d9 Mon Sep 17 00:00:00 2001 From: Arkadius Stefanski Date: Wed, 7 Nov 2018 14:22:28 +0100 Subject: [PATCH 4/5] rename to --- messenger.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/messenger.rst b/messenger.rst index 94e1569b3cc..0954db000da 100644 --- a/messenger.rst +++ b/messenger.rst @@ -30,11 +30,11 @@ requirement for a message:: class SendNotification { - private $message; + private $content; - public function __construct(string $message) + public function __construct(string $content) { - $this->message = $message; + $this->content = $content; } // ...getters From 68af5151ae37928849d9a5048d0281eb3743379a Mon Sep 17 00:00:00 2001 From: Arkadius Stefanski Date: Sat, 10 Nov 2018 18:24:39 +0100 Subject: [PATCH 5/5] SendNotification changed to SmsNotification, added serializer requirement --- messenger.rst | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/messenger.rst b/messenger.rst index 0954db000da..002acd02e5e 100644 --- a/messenger.rst +++ b/messenger.rst @@ -23,12 +23,13 @@ Message ------- Before you can send a message, you must create it first. There is no specific -requirement for a message:: +requirement for a message, except it should be serializable and unserializable +by a Symfony Serializer instance:: - // src/Message/SendNotification.php + // src/Message/SmsNotification.php namespace App\Message; - class SendNotification + class SmsNotification { private $content; @@ -49,7 +50,7 @@ you need it, like in a controller:: // src/Controller/DefaultController.php namespace App\Controller; - use App\Message\SendNotification; + use App\Message\SmsNotification; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Messenger\MessageBusInterface; @@ -57,7 +58,7 @@ you need it, like in a controller:: { public function index(MessageBusInterface $bus) { - $bus->dispatch(new SendNotification('A string to be sent...')); + $bus->dispatch(new SmsNotification('A string to be sent...')); } } @@ -67,15 +68,15 @@ Registering Handlers In order to do something when your message is dispatched, you need to create a message handler. It's a class with an ``__invoke`` method:: - // src/MessageHandler/SendNotificationHandler.php + // src/MessageHandler/SmsNotificationHandler.php namespace App\MessageHandler; - use App\Message\SendNotification; + use App\Message\SmsNotification; use Symfony\Component\Messenger\Handler\MessageHandlerInterface; - class SendNotificationHandler implements MessageHandlerInterface + class SmsNotificationHandler implements MessageHandlerInterface { - public function __invoke(SendNotification $message) + public function __invoke(SmsNotification $message) { // do something with it. } @@ -94,7 +95,7 @@ If you're not using service autoconfiguration, then you need to add this config: # config/services.yaml services: - App\MessageHandler\SendNotificationHandler: + App\MessageHandler\SmsNotificationHandler: tags: [messenger.message_handler] .. code-block:: xml @@ -107,7 +108,7 @@ If you're not using service autoconfiguration, then you need to add this config: http://symfony.com/schema/dic/services/services-1.0.xsd"> - + @@ -116,9 +117,9 @@ If you're not using service autoconfiguration, then you need to add this config: .. code-block:: php // config/services.php - use App\MessageHandler\SendNotificationHandler; + use App\MessageHandler\SmsNotificationHandler; - $container->register(SendNotificationHandler::class) + $container->register(SmsNotificationHandler::class) ->addTag('messenger.message_handler'); .. note::