Skip to content

Commit bcfae23

Browse files
committed
Finish the documentation about the custom adapter
1 parent 2493c90 commit bcfae23

File tree

2 files changed

+71
-11
lines changed

2 files changed

+71
-11
lines changed

components/messenger.rst

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,16 @@ First, create your sender::
136136
Your own receiver
137137
-----------------
138138

139-
A consumer is responsible for receiving messages from a source and dispatching
139+
A receiver is responsible for receiving messages from a source and dispatching
140140
them to the application.
141141

142-
Let's say you already proceed some "orders" on your application using a
142+
Let's say you already processed some "orders" in your application using a
143143
``NewOrder`` message. Now you want to integrate with a 3rd party or a legacy
144144
application but you can't use an API and need to use a shared CSV file with new
145145
orders.
146146

147147
You will read this CSV file and dispatch a ``NewOrder`` message. All you need to
148-
do is your custom CSV consumer and Symfony will do the rest.
148+
do is to write your custom CSV receiver and Symfony will do the rest.
149149

150150
First, create your receiver::
151151

@@ -179,9 +179,8 @@ First, create your receiver::
179179
Same bus received and sender
180180
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
181181

182-
To allow us to receive and send messages on the same bus and prevent a loop, the
183-
message bus is equipped with the ``WrapIntoReceivedMessage`` received. It will
184-
wrap the received messages into ``ReceivedMessage`` objects and the
185-
``SendMessageMiddleware`` middleware will know it should not send these messages.
186-
187-
.. _`PHP Enqueue bridge`: https://github.com/sroze/enqueue-bridge
182+
To allow us to receive and send messages on the same bus and prevent an infinite
183+
loop, the message bus is equipped with the :code:`WrapIntoReceivedMessage` middleware.
184+
It will wrap the received messages into :code:`ReceivedMessage` objects and the
185+
:code:`SendMessageMiddleware` middleware will know it should not route these
186+
messages again to an adapter.

messenger.rst

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Once you've created your handler, you need to register it:
6161
.. code-block:: xml
6262
6363
<service id="App\MessageHandler\MyMessageHandler">
64-
<tag name="message_handler" />
64+
<tag name="messenger.message_handler" />
6565
</service>
6666
6767
.. note::
@@ -78,7 +78,7 @@ most of the AMQP brokers such as RabbitMQ.
7878

7979
.. note::
8080

81-
If you need more message brokers, you should have a look to Enqueue's adapter
81+
If you need more message brokers, you should have a look to `Enqueue's adapter`_
8282
which supports things like Kafka, Amazon SQS or Google Pub/Sub.
8383

8484
An adapter is registered using a "DSN", which is a string that represents the
@@ -176,7 +176,47 @@ Learn how to build your own adapters within the Component's documentation. Once
176176
you have built your classes, you can register your adapter factory to be able to
177177
use it via a DSN in the Symfony application.
178178

179+
Create your adapter Factory
180+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
179181

182+
You need to give FrameworkBundle the opportunity to create your adapter from a
183+
DSN. You will need an adapter factory::
184+
185+
use Symfony\Component\Messenger\Adapter\Factory\AdapterInterface;
186+
use Symfony\Component\Messenger\Adapter\Factory\AdapterFactoryInterface;
187+
188+
class YourAdapterFactory implements AdapterFactoryInterface
189+
{
190+
public function create(string $dsn): AdapterInterface
191+
{
192+
return new YourAdapter(/* ... */);
193+
}
194+
195+
public function supports(string $dsn): bool
196+
{
197+
return 0 === strpos($dsn, 'my-adapter://');
198+
}
199+
}
200+
201+
The :code:`YourAdaper` class need to implements the :code:`AdapterInterface`. It
202+
will like the following example::
203+
204+
use Symfony\Component\Messenger\Adapter\Factory\AdapterInterface;
205+
use Symfony\Component\Messenger\Transport\ReceiverInterface;
206+
use Symfony\Component\Messenger\Transport\SenderInterface;
207+
208+
class YourAdapter implements AdapterInterface
209+
{
210+
public function receiver(): ReceiverInterface
211+
{
212+
return new YourReceiver(/* ... */);
213+
}
214+
215+
public function sender(): SenderInterface
216+
{
217+
return new YourSender(/* ... */);
218+
}
219+
}
180220

181221
Register your factory
182222
~~~~~~~~~~~~~~~~~~~~~
@@ -186,3 +226,24 @@ Register your factory
186226
<service id="Your\Adapter\Factory">
187227
<tag name="messenger.adapter_factory" />
188228
</service>
229+
230+
Use your adapter
231+
~~~~~~~~~~~~~~~~
232+
233+
Within the :code:`framework.messenger.adapters.*` configuration, create your
234+
named adapter using your own DSN:
235+
236+
.. code-block:: yaml
237+
238+
framework:
239+
messenger:
240+
adapters:
241+
yours: 'my-adapter://...'
242+
243+
This will give you access to the following services:
244+
245+
1. :code:`messenger.yours_adapter`: the instance of your adapter.
246+
2. :code:`messenger.yours_receiver` and :code:`messenger.yours_sender`, the
247+
receiver and sender created by the adapter.
248+
249+
.. _`PHP Enqueue bridge`: https://github.com/sroze/enqueue-bridge

0 commit comments

Comments
 (0)