12
12
namespace Symfony \Component \Messenger \Command ;
13
13
14
14
use Symfony \Component \Console \Attribute \AsCommand ;
15
+ use Symfony \Component \Console \Command \Command ;
15
16
use Symfony \Component \Console \Exception \RuntimeException ;
16
17
use Symfony \Component \Console \Input \InputArgument ;
17
18
use Symfony \Component \Console \Input \InputInterface ;
18
19
use Symfony \Component \Console \Input \InputOption ;
19
20
use Symfony \Component \Console \Output \ConsoleOutputInterface ;
20
21
use Symfony \Component \Console \Output \OutputInterface ;
21
22
use Symfony \Component \Console \Style \SymfonyStyle ;
23
+ use Symfony \Component \Messenger \Envelope ;
22
24
use Symfony \Component \Messenger \Transport \Receiver \ListableReceiverInterface ;
23
25
use Symfony \Component \Messenger \Transport \Receiver \ReceiverInterface ;
24
26
@@ -32,7 +34,8 @@ protected function configure(): void
32
34
{
33
35
$ this
34
36
->setDefinition ([
35
- new InputArgument ('id ' , InputArgument::REQUIRED | InputArgument::IS_ARRAY , 'Specific message id(s) to remove ' ),
37
+ new InputArgument ('id ' , InputArgument::OPTIONAL | InputArgument::IS_ARRAY , 'Specific message id(s) to remove ' ),
38
+ new InputOption ('all ' , null , InputOption::VALUE_NONE , 'Remove all failed messages from the transport ' ),
36
39
new InputOption ('force ' , null , InputOption::VALUE_NONE , 'Force the operation without confirmation ' ),
37
40
new InputOption ('transport ' , null , InputOption::VALUE_OPTIONAL , 'Use a specific failure transport ' , self ::DEFAULT_TRANSPORT_OPTION ),
38
41
new InputOption ('show-messages ' , null , InputOption::VALUE_NONE , 'Display messages before removing it (if multiple ids are given) ' ),
@@ -43,6 +46,10 @@ protected function configure(): void
43
46
<info>php %command.full_name% {id1} [{id2} ...]</info>
44
47
45
48
The specific ids can be found via the messenger:failed:show command.
49
+
50
+ You can remove all messages from the failure transport by using the "--all" option (note that you also need the "--force" option):
51
+
52
+ <info>php %command.full_name% --all --force</info>
46
53
EOF
47
54
)
48
55
;
@@ -61,18 +68,38 @@ protected function execute(InputInterface $input, OutputInterface $output): int
61
68
62
69
$ shouldForce = $ input ->getOption ('force ' );
63
70
$ ids = (array ) $ input ->getArgument ('id ' );
64
- $ shouldDisplayMessages = $ input ->getOption ('show-messages ' ) || 1 === \count ($ ids );
65
- $ this ->removeMessages ($ failureTransportName , $ ids , $ receiver , $ io , $ shouldForce , $ shouldDisplayMessages );
71
+ $ shouldDeleteAllMessages = $ input ->getOption ('all ' );
66
72
67
- return 0 ;
68
- }
73
+ $ idsCount = \count ($ ids );
74
+ if (!$ shouldDeleteAllMessages && !$ idsCount ) {
75
+ throw new RuntimeException ('Please specify at least one message id. If you want to remove all messages, use the "--all" option. ' );
76
+ } elseif ($ shouldDeleteAllMessages && $ idsCount ) {
77
+ throw new RuntimeException ('You cannot specify message ids when using the "--all" option. ' );
78
+ }
79
+
80
+ if ($ shouldDeleteAllMessages && !$ shouldForce ) {
81
+ $ io ->error ('Please provide the "--force" option when using "--all" to confirm the removal of all failed messages in the transport. ' );
82
+
83
+ return Command::FAILURE ;
84
+ }
85
+
86
+ $ shouldDisplayMessages = $ input ->getOption ('show-messages ' ) || 1 === $ idsCount ;
69
87
70
- private function removeMessages (string $ failureTransportName , array $ ids , ReceiverInterface $ receiver , SymfonyStyle $ io , bool $ shouldForce , bool $ shouldDisplayMessages ): void
71
- {
72
88
if (!$ receiver instanceof ListableReceiverInterface) {
73
89
throw new RuntimeException (sprintf ('The "%s" receiver does not support removing specific messages. ' , $ failureTransportName ));
74
90
}
75
91
92
+ if ($ shouldDeleteAllMessages ) {
93
+ $ this ->removeAllMessages ($ receiver , $ io , $ shouldDisplayMessages );
94
+ } else {
95
+ $ this ->removeMessagesById ($ ids , $ receiver , $ io , $ shouldForce , $ shouldDisplayMessages );
96
+ }
97
+
98
+ return 0 ;
99
+ }
100
+
101
+ private function removeMessagesById (array $ ids , ListableReceiverInterface $ receiver , SymfonyStyle $ io , bool $ shouldForce , bool $ shouldDisplayMessages ): void
102
+ {
76
103
foreach ($ ids as $ id ) {
77
104
$ this ->phpSerializer ?->acceptPhpIncompleteClass();
78
105
try {
@@ -99,4 +126,19 @@ private function removeMessages(string $failureTransportName, array $ids, Receiv
99
126
}
100
127
}
101
128
}
129
+
130
+ private function removeAllMessages (ListableReceiverInterface $ receiver , SymfonyStyle $ io , bool $ shouldDisplayMessages ): void
131
+ {
132
+ $ count = 0 ;
133
+ foreach ($ receiver ->all () as $ envelope ) {
134
+ if ($ shouldDisplayMessages ) {
135
+ $ this ->displaySingleMessage ($ envelope , $ io );
136
+ }
137
+
138
+ $ receiver ->reject ($ envelope );
139
+ ++$ count ;
140
+ }
141
+
142
+ $ io ->note (sprintf ('%d messages were removed. ' , $ count ));
143
+ }
102
144
}
0 commit comments