11
11
12
12
namespace Symfony \Component \Messenger \DependencyInjection ;
13
13
14
+ use Symfony \Component \DependencyInjection \ChildDefinition ;
14
15
use Symfony \Component \DependencyInjection \Compiler \CompilerPassInterface ;
15
16
use Symfony \Component \DependencyInjection \Compiler \PriorityTaggedServiceTrait ;
16
17
use Symfony \Component \DependencyInjection \Compiler \ServiceLocatorTagPass ;
21
22
use Symfony \Component \Messenger \Handler \ChainHandler ;
22
23
use Symfony \Component \Messenger \Handler \MessageHandlerInterface ;
23
24
use Symfony \Component \Messenger \Handler \MessageSubscriberInterface ;
25
+ use Symfony \Component \Messenger \TraceableMessageBus ;
24
26
25
27
/**
26
28
* @author Samuel Roze <[email protected] >
@@ -29,28 +31,38 @@ class MessengerPass implements CompilerPassInterface
29
31
{
30
32
use PriorityTaggedServiceTrait;
31
33
32
- private $ messageBusService ;
33
- private $ messageHandlerResolverService ;
34
34
private $ handlerTag ;
35
+ private $ busTag ;
36
+ private $ senderTag ;
37
+ private $ receiverTag ;
35
38
36
- public function __construct (string $ messageBusService = 'message_bus ' , string $ messageHandlerResolverService = 'messenger.handler_resolver ' , string $ handlerTag = 'messenger.message_handler ' )
39
+ public function __construct (string $ handlerTag = 'messenger.message_handler ' , string $ busTag = 'messenger.bus ' , string $ senderTag = 'messenger.sender ' , string $ receiverTag = ' messenger.receiver ' )
37
40
{
38
- $ this ->messageBusService = $ messageBusService ;
39
- $ this ->messageHandlerResolverService = $ messageHandlerResolverService ;
40
41
$ this ->handlerTag = $ handlerTag ;
42
+ $ this ->busTag = $ busTag ;
43
+ $ this ->senderTag = $ senderTag ;
44
+ $ this ->receiverTag = $ receiverTag ;
41
45
}
42
46
43
47
/**
44
48
* {@inheritdoc}
45
49
*/
46
50
public function process (ContainerBuilder $ container )
47
51
{
48
- if (!$ container ->hasDefinition ($ this -> messageBusService )) {
52
+ if (!$ container ->hasDefinition (' messenger.handler_resolver ' )) {
49
53
return ;
50
54
}
51
55
52
- if (!$ container ->getParameter ('kernel.debug ' ) || !$ container ->hasAlias ('logger ' )) {
53
- $ container ->removeDefinition ('messenger.middleware.debug.logging ' );
56
+ foreach ($ container ->findTaggedServiceIds ($ this ->busTag ) as $ busId => $ tags ) {
57
+ if ($ container ->hasParameter ($ busMiddlewaresParameter = $ busId .'.middlewares ' )) {
58
+ $ this ->registerBusMiddlewares ($ container , $ busId , $ container ->getParameter ($ busMiddlewaresParameter ));
59
+
60
+ $ container ->getParameterBag ()->remove ($ busMiddlewaresParameter );
61
+ }
62
+
63
+ if ($ container ->hasDefinition ('messenger.data_collector ' )) {
64
+ $ this ->registerBusToCollector ($ container , $ busId , $ tags [0 ]);
65
+ }
54
66
}
55
67
56
68
$ this ->registerReceivers ($ container );
@@ -110,7 +122,7 @@ private function registerHandlers(ContainerBuilder $container)
110
122
$ handlersLocatorMapping ['handler. ' .$ message ] = $ handler ;
111
123
}
112
124
113
- $ handlerResolver = $ container ->getDefinition ($ this -> messageHandlerResolverService );
125
+ $ handlerResolver = $ container ->getDefinition (' messenger.handler_resolver ' );
114
126
$ handlerResolver ->replaceArgument (0 , ServiceLocatorTagPass::register ($ container , $ handlersLocatorMapping ));
115
127
}
116
128
@@ -149,7 +161,7 @@ private function guessHandledClasses(\ReflectionClass $handlerClass, string $ser
149
161
private function registerReceivers (ContainerBuilder $ container )
150
162
{
151
163
$ receiverMapping = array ();
152
- foreach ($ container ->findTaggedServiceIds (' messenger.receiver ' ) as $ id => $ tags ) {
164
+ foreach ($ container ->findTaggedServiceIds ($ this -> receiverTag ) as $ id => $ tags ) {
153
165
foreach ($ tags as $ tag ) {
154
166
$ receiverMapping [$ id ] = new Reference ($ id );
155
167
@@ -165,7 +177,7 @@ private function registerReceivers(ContainerBuilder $container)
165
177
private function registerSenders (ContainerBuilder $ container )
166
178
{
167
179
$ senderLocatorMapping = array ();
168
- foreach ($ container ->findTaggedServiceIds (' messenger.sender ' ) as $ id => $ tags ) {
180
+ foreach ($ container ->findTaggedServiceIds ($ this -> senderTag ) as $ id => $ tags ) {
169
181
foreach ($ tags as $ tag ) {
170
182
$ senderLocatorMapping [$ id ] = new Reference ($ id );
171
183
@@ -177,4 +189,35 @@ private function registerSenders(ContainerBuilder $container)
177
189
178
190
$ container ->getDefinition ('messenger.sender_locator ' )->replaceArgument (0 , $ senderLocatorMapping );
179
191
}
192
+
193
+ private function registerBusToCollector (ContainerBuilder $ container , string $ busId , array $ tag )
194
+ {
195
+ $ container ->setDefinition (
196
+ $ tracedBusId = 'debug.traced. ' .$ busId ,
197
+ (new Definition (TraceableMessageBus::class, array (new Reference ($ tracedBusId .'.inner ' ))))->setDecoratedService ($ busId )
198
+ );
199
+
200
+ $ container ->getDefinition ('messenger.data_collector ' )->addMethodCall ('registerBus ' , array ($ tag ['name ' ] ?? $ busId , new Reference ($ tracedBusId )));
201
+ }
202
+
203
+ private function registerBusMiddlewares (ContainerBuilder $ container , string $ busId , array $ middlewares )
204
+ {
205
+ $ container ->getDefinition ($ busId )->replaceArgument (0 , array_map (function (string $ name ) use ($ container , $ busId ) {
206
+ if (!$ container ->has ($ messengerMiddlewareId = 'messenger.middleware. ' .$ name )) {
207
+ $ messengerMiddlewareId = $ name ;
208
+ }
209
+
210
+ if (!$ container ->has ($ messengerMiddlewareId )) {
211
+ throw new RuntimeException (sprintf ('Invalid middleware "%s": define such service to be able to use it. ' , $ name ));
212
+ }
213
+
214
+ if ($ container ->getDefinition ($ messengerMiddlewareId )->isAbstract ()) {
215
+ $ childDefinition = new ChildDefinition ($ messengerMiddlewareId );
216
+
217
+ $ container ->setDefinition ($ messengerMiddlewareId = $ busId .'.middleware. ' .$ name , $ childDefinition );
218
+ }
219
+
220
+ return new Reference ($ messengerMiddlewareId );
221
+ }, $ middlewares ));
222
+ }
180
223
}
0 commit comments