20
20
use Symfony \Component \Console \Input \InputOption ;
21
21
use Symfony \Component \Console \Output \OutputInterface ;
22
22
use Symfony \Component \Console \Style \SymfonyStyle ;
23
- use Symfony \Component \Messenger \MessageBusInterface ;
24
23
use Symfony \Component \Messenger \Transport \Enhancers \StopWhenMemoryUsageIsExceededReceiver ;
25
24
use Symfony \Component \Messenger \Transport \Enhancers \StopWhenMessageCountIsExceededReceiver ;
26
25
use Symfony \Component \Messenger \Transport \Enhancers \StopWhenTimeLimitIsReachedReceiver ;
@@ -35,17 +34,19 @@ class ConsumeMessagesCommand extends Command
35
34
{
36
35
protected static $ defaultName = 'messenger:consume-messages ' ;
37
36
38
- private $ bus ;
37
+ private $ busLocator ;
39
38
private $ receiverLocator ;
40
39
private $ logger ;
41
40
private $ receiverNames ;
41
+ private $ busNames ;
42
42
43
- public function __construct (MessageBusInterface $ bus , ContainerInterface $ receiverLocator , LoggerInterface $ logger = null , array $ receiverNames = array ())
43
+ public function __construct (ContainerInterface $ busLocator , ContainerInterface $ receiverLocator , LoggerInterface $ logger = null , array $ receiverNames = array (), array $ busNames = array ())
44
44
{
45
- $ this ->bus = $ bus ;
45
+ $ this ->busLocator = $ busLocator ;
46
46
$ this ->receiverLocator = $ receiverLocator ;
47
47
$ this ->logger = $ logger ;
48
48
$ this ->receiverNames = $ receiverNames ;
49
+ $ this ->busNames = $ busNames ;
49
50
50
51
parent ::__construct ();
51
52
}
@@ -63,6 +64,7 @@ protected function configure(): void
63
64
new InputOption ('limit ' , 'l ' , InputOption::VALUE_REQUIRED , 'Limit the number of received messages ' ),
64
65
new InputOption ('memory-limit ' , 'm ' , InputOption::VALUE_REQUIRED , 'The memory limit the worker can consume ' ),
65
66
new InputOption ('time-limit ' , 't ' , InputOption::VALUE_REQUIRED , 'The time limit in seconds the worker can run ' ),
67
+ new InputOption ('bus ' , 'b ' , InputOption::VALUE_REQUIRED , 'Name of the bus to which received messages should be dispatched ' , 'message_bus ' ),
66
68
))
67
69
->setDescription ('Consumes messages ' )
68
70
->setHelp (<<<'EOF'
@@ -91,18 +93,34 @@ protected function configure(): void
91
93
*/
92
94
protected function interact (InputInterface $ input , OutputInterface $ output )
93
95
{
94
- if (!$ this ->receiverNames || $ this ->receiverLocator ->has ($ receiverName = $ input ->getArgument ('receiver ' ))) {
95
- return ;
96
+ $ style = new SymfonyStyle ($ input , $ output );
97
+
98
+ if ($ this ->receiverNames && !$ this ->receiverLocator ->has ($ receiverName = $ input ->getArgument ('receiver ' ))) {
99
+ if (null === $ receiverName ) {
100
+ $ style ->block ('Missing receiver argument. ' , null , 'error ' , ' ' , true );
101
+ $ input ->setArgument ('receiver ' , $ style ->choice ('Select one of the available receivers ' , $ this ->receiverNames ));
102
+ } elseif ($ alternatives = $ this ->findAlternatives ($ receiverName , $ this ->receiverNames )) {
103
+ $ style ->block (sprintf ('Receiver "%s" is not defined. ' , $ receiverName ), null , 'error ' , ' ' , true );
104
+ if ($ style ->confirm (sprintf ('Do you want to receive from "%s" instead? ' , $ alternatives [0 ]), false )) {
105
+ $ input ->setArgument ('receiver ' , $ alternatives [0 ]);
106
+ }
107
+ }
96
108
}
97
109
98
- $ style = new SymfonyStyle ($ input , $ output );
99
- if (null === $ receiverName ) {
100
- $ style ->block ('Missing receiver argument. ' , null , 'error ' , ' ' , true );
101
- $ input ->setArgument ('receiver ' , $ style ->choice ('Select one of the available receivers ' , $ this ->receiverNames ));
102
- } elseif ($ alternatives = $ this ->findAlternatives ($ receiverName , $ this ->receiverNames )) {
103
- $ style ->block (sprintf ('Receiver "%s" is not defined. ' , $ receiverName ), null , 'error ' , ' ' , true );
104
- if ($ style ->confirm (sprintf ('Do you want to receive from "%s" instead? ' , $ alternatives [0 ]), false )) {
105
- $ input ->setArgument ('receiver ' , $ alternatives [0 ]);
110
+
111
+ $ busName = $ input ->getOption ('bus ' );
112
+
113
+ if ($ busName && $ this ->busNames && !$ this ->busLocator ->has ($ busName ) && $ alternatives = $ this ->findAlternatives ($ busName , $ this ->busNames )) {
114
+ if ($ alternatives = $ this ->findAlternatives ($ busName , $ this ->busNames )) {
115
+ $ style ->block (sprintf ('Bus "%s" is not defined. ' , $ busName ), null , 'error ' , ' ' , true );
116
+
117
+ if (1 === \count ($ alternatives )) {
118
+ if ($ style ->confirm (sprintf ('Do you want to dispatch to "%s" instead? ' , $ alternatives [0 ]), true )) {
119
+ $ input ->setOption ('bus ' , $ alternatives [0 ]);
120
+ }
121
+ } else {
122
+ $ input ->setOption ('bus ' , $ style ->choice ('Did you mean one of the following buses instead? ' , $ alternatives , $ alternatives [0 ]));
123
+ }
106
124
}
107
125
}
108
126
}
@@ -116,7 +134,12 @@ protected function execute(InputInterface $input, OutputInterface $output): void
116
134
throw new RuntimeException (sprintf ('Receiver "%s" does not exist. ' , $ receiverName ));
117
135
}
118
136
137
+ if (!$ this ->busLocator ->has ($ busName = $ input ->getOption ('bus ' ))) {
138
+ throw new RuntimeException (sprintf ('Bus "%s" does not exist. ' , $ receiverName ));
139
+ }
140
+
119
141
$ receiver = $ this ->receiverLocator ->get ($ receiverName );
142
+ $ bus = $ this ->busLocator ->get ($ busName );
120
143
121
144
if ($ limit = $ input ->getOption ('limit ' )) {
122
145
$ receiver = new StopWhenMessageCountIsExceededReceiver ($ receiver , $ limit , $ this ->logger );
@@ -130,7 +153,7 @@ protected function execute(InputInterface $input, OutputInterface $output): void
130
153
$ receiver = new StopWhenTimeLimitIsReachedReceiver ($ receiver , $ timeLimit , $ this ->logger );
131
154
}
132
155
133
- $ worker = new Worker ($ receiver , $ this -> bus );
156
+ $ worker = new Worker ($ receiver , $ bus );
134
157
$ worker ->run ();
135
158
}
136
159
0 commit comments