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
}
@@ -56,13 +57,15 @@ public function __construct(MessageBusInterface $bus, ContainerInterface $receiv
56
57
protected function configure (): void
57
58
{
58
59
$ defaultReceiverName = 1 === \count ($ this ->receiverNames ) ? current ($ this ->receiverNames ) : null ;
60
+ $ defaultBusName = 1 === \count ($ this ->busNames ) ? current ($ this ->busNames ) : null ;
59
61
60
62
$ this
61
63
->setDefinition (array (
62
64
new InputArgument ('receiver ' , $ defaultReceiverName ? InputArgument::OPTIONAL : InputArgument::REQUIRED , 'Name of the receiver ' , $ defaultReceiverName ),
63
65
new InputOption ('limit ' , 'l ' , InputOption::VALUE_REQUIRED , 'Limit the number of received messages ' ),
64
66
new InputOption ('memory-limit ' , 'm ' , InputOption::VALUE_REQUIRED , 'The memory limit the worker can consume ' ),
65
67
new InputOption ('time-limit ' , 't ' , InputOption::VALUE_REQUIRED , 'The time limit in seconds the worker can run ' ),
68
+ new InputOption ('bus ' , 'b ' , InputOption::VALUE_REQUIRED , 'Name of the bus to which received messages should be dispatched ' , $ defaultBusName ),
66
69
))
67
70
->setDescription ('Consumes messages ' )
68
71
->setHelp (<<<'EOF'
@@ -91,18 +94,35 @@ protected function configure(): void
91
94
*/
92
95
protected function interact (InputInterface $ input , OutputInterface $ output )
93
96
{
94
- if (!$ this ->receiverNames || $ this ->receiverLocator ->has ($ receiverName = $ input ->getArgument ('receiver ' ))) {
95
- return ;
97
+ $ style = new SymfonyStyle ($ input , $ output );
98
+
99
+ if ($ this ->receiverNames && !$ this ->receiverLocator ->has ($ receiverName = $ input ->getArgument ('receiver ' ))) {
100
+ if (null === $ receiverName ) {
101
+ $ style ->block ('Missing receiver argument. ' , null , 'error ' , ' ' , true );
102
+ $ input ->setArgument ('receiver ' , $ style ->choice ('Select one of the available receivers ' , $ this ->receiverNames ));
103
+ } elseif ($ alternatives = $ this ->findAlternatives ($ receiverName , $ this ->receiverNames )) {
104
+ $ style ->block (sprintf ('Receiver "%s" is not defined. ' , $ receiverName ), null , 'error ' , ' ' , true );
105
+ if ($ style ->confirm (sprintf ('Do you want to receive from "%s" instead? ' , $ alternatives [0 ]), false )) {
106
+ $ input ->setArgument ('receiver ' , $ alternatives [0 ]);
107
+ }
108
+ }
96
109
}
97
110
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 ]);
111
+ $ busName = $ input ->getOption ('bus ' );
112
+ if ($ this ->busNames && !$ this ->busLocator ->has ($ busName )) {
113
+ if (null === $ busName ) {
114
+ $ style ->block ('Missing bus argument. ' , null , 'error ' , ' ' , true );
115
+ $ input ->setOption ('bus ' , $ style ->choice ('Select one of the available buses ' , $ this ->busNames ));
116
+ } elseif ($ alternatives = $ this ->findAlternatives ($ busName , $ this ->busNames )) {
117
+ $ style ->block (sprintf ('Bus "%s" is not defined. ' , $ busName ), null , 'error ' , ' ' , true );
118
+
119
+ if (1 === \count ($ alternatives )) {
120
+ if ($ style ->confirm (sprintf ('Do you want to dispatch to "%s" instead? ' , $ alternatives [0 ]), true )) {
121
+ $ input ->setOption ('bus ' , $ alternatives [0 ]);
122
+ }
123
+ } else {
124
+ $ input ->setOption ('bus ' , $ style ->choice ('Did you mean one of the following buses instead? ' , $ alternatives , $ alternatives [0 ]));
125
+ }
106
126
}
107
127
}
108
128
}
@@ -116,7 +136,12 @@ protected function execute(InputInterface $input, OutputInterface $output): void
116
136
throw new RuntimeException (sprintf ('Receiver "%s" does not exist. ' , $ receiverName ));
117
137
}
118
138
139
+ if (!$ this ->busLocator ->has ($ busName = $ input ->getOption ('bus ' ))) {
140
+ throw new RuntimeException (sprintf ('Bus "%s" does not exist. ' , $ busName ));
141
+ }
142
+
119
143
$ receiver = $ this ->receiverLocator ->get ($ receiverName );
144
+ $ bus = $ this ->busLocator ->get ($ busName );
120
145
121
146
if ($ limit = $ input ->getOption ('limit ' )) {
122
147
$ receiver = new StopWhenMessageCountIsExceededReceiver ($ receiver , $ limit , $ this ->logger );
@@ -130,7 +155,7 @@ protected function execute(InputInterface $input, OutputInterface $output): void
130
155
$ receiver = new StopWhenTimeLimitIsReachedReceiver ($ receiver , $ timeLimit , $ this ->logger );
131
156
}
132
157
133
- $ worker = new Worker ($ receiver , $ this -> bus );
158
+ $ worker = new Worker ($ receiver , $ bus );
134
159
$ worker ->run ();
135
160
}
136
161
0 commit comments