16
16
use Symfony \Component \Console \Command \Command ;
17
17
use Symfony \Component \Console \Input \InputArgument ;
18
18
use Symfony \Component \Console \Input \InputInterface ;
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 \Exception \InvalidArgumentException ;
22
24
use Symfony \Component \Messenger \Transport \Receiver \MessageCountAwareInterface ;
23
25
24
26
/**
@@ -36,8 +38,10 @@ public function __construct(
36
38
37
39
protected function configure (): void
38
40
{
41
+ $ outputFormats = implode (', ' , $ this ->getAvailableFormatOptions ());
39
42
$ this
40
43
->addArgument ('transport_names ' , InputArgument::IS_ARRAY | InputArgument::OPTIONAL , 'List of transports \' names ' )
44
+ ->addOption ('format ' , '' , InputOption::VALUE_REQUIRED , 'The output format, e.g.: ' .$ outputFormats , 'text ' , $ this ->getAvailableFormatOptions ())
41
45
->setHelp (<<<EOF
42
46
The <info>%command.name%</info> command counts the messages for all the transports:
43
47
@@ -46,6 +50,11 @@ protected function configure(): void
46
50
Or specific transports only:
47
51
48
52
<info>php %command.full_name% <transportNames></info>
53
+
54
+ The <info>--format</info> option specifies the format of command output,
55
+ these are " {$ outputFormats }".
56
+
57
+ <info>php %command.full_name% --format=json</info>
49
58
EOF
50
59
)
51
60
;
@@ -55,6 +64,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
55
64
{
56
65
$ io = new SymfonyStyle ($ input , $ output instanceof ConsoleOutputInterface ? $ output ->getErrorOutput () : $ output );
57
66
67
+ $ format = $ input ->getOption ('format ' );
68
+ if (!\in_array ($ format , $ this ->getAvailableFormatOptions (), true )) {
69
+ throw new InvalidArgumentException ('Invalid output format. ' );
70
+ }
71
+
58
72
$ transportNames = $ this ->transportNames ;
59
73
if ($ input ->getArgument ('transport_names ' )) {
60
74
$ transportNames = $ input ->getArgument ('transport_names ' );
@@ -64,7 +78,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
64
78
$ uncountableTransports = [];
65
79
foreach ($ transportNames as $ transportName ) {
66
80
if (!$ this ->transportLocator ->has ($ transportName )) {
67
- $ io ->warning (\sprintf ('The "%s" transport does not exist. ' , $ transportName ));
81
+ if ($ this ->formatSupportsWarnings ($ format )) {
82
+ $ io ->warning (\sprintf ('The "%s" transport does not exist. ' , $ transportName ));
83
+ }
68
84
69
85
continue ;
70
86
}
@@ -77,12 +93,48 @@ protected function execute(InputInterface $input, OutputInterface $output): int
77
93
$ outputTable [] = [$ transportName , $ transport ->getMessageCount ()];
78
94
}
79
95
96
+ match ($ format ) {
97
+ 'text ' => $ this ->outputText ($ io , $ outputTable , $ uncountableTransports ),
98
+ 'json ' => $ this ->outputJson ($ io , $ outputTable , $ uncountableTransports ),
99
+ };
100
+
101
+ return 0 ;
102
+ }
103
+
104
+ private function outputText (SymfonyStyle $ io , array $ outputTable , array $ uncountableTransports ): void
105
+ {
80
106
$ io ->table (['Transport ' , 'Count ' ], $ outputTable );
81
107
82
108
if ($ uncountableTransports ) {
83
109
$ io ->note (\sprintf ('Unable to get message count for the following transports: "%s". ' , implode ('", " ' , $ uncountableTransports )));
84
110
}
111
+ }
85
112
86
- return 0 ;
113
+ private function outputJson (SymfonyStyle $ io , array $ outputTable , array $ uncountableTransports ): void
114
+ {
115
+ $ output = ['transports ' => []];
116
+ foreach ($ outputTable as [$ transportName , $ count ]) {
117
+ $ output ['transports ' ][$ transportName ] = ['count ' => $ count ];
118
+ }
119
+
120
+ if ($ uncountableTransports ) {
121
+ $ output ['uncountable_transports ' ] = $ uncountableTransports ;
122
+ }
123
+
124
+ $ io ->writeln (json_encode ($ output , \JSON_PRETTY_PRINT ));
125
+ }
126
+
127
+ private function formatSupportsWarnings (string $ format ): bool
128
+ {
129
+ return match ($ format ) {
130
+ 'text ' => true ,
131
+ 'json ' => false ,
132
+ };
133
+ }
134
+
135
+ /** @return string[] */
136
+ private function getAvailableFormatOptions (): array
137
+ {
138
+ return ['text ' , 'json ' ];
87
139
}
88
140
}
0 commit comments