19
19
use Symfony \Component \Console \Input \InputInterface ;
20
20
use Symfony \Component \Console \Input \InputOption ;
21
21
use Symfony \Component \Console \Output \OutputInterface ;
22
+ use Symfony \Component \Console \Style \SymfonyStyle ;
22
23
use Symfony \Component \Messenger \MessageBusInterface ;
23
24
use Symfony \Component \Messenger \Transport \Enhancers \StopWhenMemoryUsageIsExceededReceiver ;
24
25
use Symfony \Component \Messenger \Transport \Enhancers \StopWhenMessageCountIsExceededReceiver ;
@@ -37,14 +38,14 @@ class ConsumeMessagesCommand extends Command
37
38
private $ bus ;
38
39
private $ receiverLocator ;
39
40
private $ logger ;
40
- private $ defaultReceiverName ;
41
+ private $ receiverNames ;
41
42
42
- public function __construct (MessageBusInterface $ bus , ContainerInterface $ receiverLocator , LoggerInterface $ logger = null , string $ defaultReceiverName = null )
43
+ public function __construct (MessageBusInterface $ bus , ContainerInterface $ receiverLocator , LoggerInterface $ logger = null , array $ receiverNames = array () )
43
44
{
44
45
$ this ->bus = $ bus ;
45
46
$ this ->receiverLocator = $ receiverLocator ;
46
47
$ this ->logger = $ logger ;
47
- $ this ->defaultReceiverName = $ defaultReceiverName ;
48
+ $ this ->receiverNames = $ receiverNames ;
48
49
49
50
parent ::__construct ();
50
51
}
@@ -54,9 +55,11 @@ public function __construct(MessageBusInterface $bus, ContainerInterface $receiv
54
55
*/
55
56
protected function configure (): void
56
57
{
58
+ $ defaultReceiverName = 1 === \count ($ this ->receiverNames ) ? current ($ this ->receiverNames ) : null ;
59
+
57
60
$ this
58
61
->setDefinition (array (
59
- new InputArgument ('receiver ' , $ this -> defaultReceiverName ? InputArgument::OPTIONAL : InputArgument::REQUIRED , 'Name of the receiver ' , $ this -> defaultReceiverName ),
62
+ new InputArgument ('receiver ' , $ defaultReceiverName ? InputArgument::OPTIONAL : InputArgument::REQUIRED , 'Name of the receiver ' , $ defaultReceiverName ),
60
63
new InputOption ('limit ' , 'l ' , InputOption::VALUE_REQUIRED , 'Limit the number of received messages ' ),
61
64
new InputOption ('memory-limit ' , 'm ' , InputOption::VALUE_REQUIRED , 'The memory limit the worker can consume ' ),
62
65
new InputOption ('time-limit ' , 't ' , InputOption::VALUE_REQUIRED , 'The time limit in seconds the worker can run ' ),
@@ -83,6 +86,27 @@ protected function configure(): void
83
86
;
84
87
}
85
88
89
+ /**
90
+ * {@inheritdoc}
91
+ */
92
+ protected function interact (InputInterface $ input , OutputInterface $ output )
93
+ {
94
+ if (!$ this ->receiverNames || $ this ->receiverLocator ->has ($ receiverName = $ input ->getArgument ('receiver ' ))) {
95
+ return ;
96
+ }
97
+
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 ]);
106
+ }
107
+ }
108
+ }
109
+
86
110
/**
87
111
* {@inheritdoc}
88
112
*/
@@ -134,4 +158,21 @@ private function convertToBytes(string $memoryLimit): int
134
158
135
159
return $ max ;
136
160
}
161
+
162
+ private function findAlternatives ($ name , array $ collection )
163
+ {
164
+ $ alternatives = array ();
165
+ foreach ($ collection as $ item ) {
166
+ $ lev = levenshtein ($ name , $ item );
167
+ if ($ lev <= \strlen ($ name ) / 3 || false !== strpos ($ item , $ name )) {
168
+ $ alternatives [$ item ] = isset ($ alternatives [$ item ]) ? $ alternatives [$ item ] - $ lev : $ lev ;
169
+ }
170
+ }
171
+
172
+ $ threshold = 1e3 ;
173
+ $ alternatives = array_filter ($ alternatives , function ($ lev ) use ($ threshold ) { return $ lev < 2 * $ threshold ; });
174
+ ksort ($ alternatives , SORT_NATURAL | SORT_FLAG_CASE );
175
+
176
+ return array_keys ($ alternatives );
177
+ }
137
178
}
0 commit comments