1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Burrow\CLI; |
4
|
|
|
|
5
|
|
|
use Assert\Assertion; |
6
|
|
|
use Burrow\Driver; |
7
|
|
|
use Symfony\Component\Console\Command\Command; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @codeCoverageIgnore |
14
|
|
|
*/ |
15
|
|
|
class InitCommand extends Command |
16
|
|
|
{ |
17
|
|
|
/** @var Driver */ |
18
|
|
|
private $driver; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* DeclareQueueCommand constructor. |
22
|
|
|
* |
23
|
|
|
* @param Driver $driver |
24
|
|
|
*/ |
25
|
|
|
public function __construct(Driver $driver) |
26
|
|
|
{ |
27
|
|
|
parent::__construct(); |
28
|
|
|
|
29
|
|
|
$this->driver = $driver; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
protected function configure() |
33
|
|
|
{ |
34
|
|
|
$this->setName('admin:init') |
35
|
|
|
->setDescription('Init a RabbitMQ exchange / queue wiring.') |
36
|
|
|
->addArgument( |
37
|
|
|
'file', |
38
|
|
|
InputArgument::REQUIRED, |
39
|
|
|
'The path of the file to load wiring configuration from. It must contain a json declaration.' |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param InputInterface $input |
45
|
|
|
* @param OutputInterface $output |
46
|
|
|
* |
47
|
|
|
* @return int|null|void |
48
|
|
|
*/ |
49
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
50
|
|
|
{ |
51
|
|
|
$configuration = $this->getConfiguration($input); |
52
|
|
|
$this->declareQueues($configuration, $output); |
53
|
|
|
$this->bind($configuration, $output); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param InputInterface $input |
58
|
|
|
* |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
|
protected function getConfiguration(InputInterface $input) |
62
|
|
|
{ |
63
|
|
|
$file = $input->getArgument('file'); |
64
|
|
|
Assertion::file($file, 'You must provide a valid file name'); |
65
|
|
|
|
66
|
|
|
$configurationString = file_get_contents($file); |
67
|
|
|
Assertion::isJsonString($configurationString, 'The file must be a valid JSON'); |
68
|
|
|
|
69
|
|
|
$configuration = @json_decode($configurationString, true); |
70
|
|
|
if (json_last_error() !== JSON_ERROR_NONE) { |
71
|
|
|
throw new \InvalidArgumentException('Invalid json : ' . json_last_error_msg()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
self::checkConfiguration($configuration); |
75
|
|
|
|
76
|
|
|
return $configuration; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param array $configuration |
81
|
|
|
*/ |
82
|
|
|
private static function checkConfiguration(array $configuration) |
83
|
|
|
{ |
84
|
|
|
self::checkQueuesConfiguration($configuration); |
85
|
|
|
self::checkExchangesConfiguration($configuration); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param array $configuration |
90
|
|
|
*/ |
91
|
|
|
private static function checkQueuesConfiguration(array $configuration) |
92
|
|
|
{ |
93
|
|
|
Assertion::keyIsset($configuration, 'queues', 'You must provide a `queues` configuration'); |
94
|
|
|
|
95
|
|
|
$queues = $configuration['queues']; |
96
|
|
|
Assertion::isArray($queues, 'The `queues` configuration must be an array'); |
97
|
|
|
|
98
|
|
|
foreach ($queues as $queueInformation) { |
99
|
|
|
Assertion::keyIsset($queueInformation, 'name', 'You must provide a name for the queue'); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param array $configuration |
105
|
|
|
*/ |
106
|
|
|
private static function checkExchangesConfiguration(array $configuration) |
107
|
|
|
{ |
108
|
|
|
Assertion::keyIsset($configuration, 'exchanges', 'You must provide an `exchanges` configuration'); |
109
|
|
|
|
110
|
|
|
$exchanges = $configuration['exchanges']; |
111
|
|
|
Assertion::isArray($exchanges, 'The `exchanges` configuration must be an array'); |
112
|
|
|
|
113
|
|
|
foreach ($exchanges as $exchangeInformation) { |
114
|
|
|
self::checkExchangeConfiguration($exchangeInformation); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param array $exchangeInformation |
120
|
|
|
*/ |
121
|
|
|
private static function checkExchangeConfiguration(array $exchangeInformation) |
122
|
|
|
{ |
123
|
|
|
Assertion::keyIsset($exchangeInformation, 'name', 'You must provide a name for the exchange'); |
124
|
|
|
Assertion::keyIsset($exchangeInformation, 'type', 'You must provide a type for the exchange'); |
125
|
|
|
|
126
|
|
|
self::checkQueuesConfiguration($exchangeInformation); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param array $configuration |
131
|
|
|
* @param OutputInterface $output |
132
|
|
|
*/ |
133
|
|
|
private function declareQueues($configuration, OutputInterface $output) |
134
|
|
|
{ |
135
|
|
|
$queues = $configuration['queues']; |
136
|
|
|
foreach ($queues as $queue) { |
137
|
|
|
$this->driver->declareSimpleQueue($queue); |
138
|
|
|
$output->writeln(sprintf('<info>Declare queue <comment>%s</comment></info>', $queue)); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param array $configuration |
144
|
|
|
* @param OutputInterface $output |
145
|
|
|
*/ |
146
|
|
|
private function bind(array $configuration, OutputInterface $output) |
147
|
|
|
{ |
148
|
|
|
$exchanges = $configuration['exchanges']; |
149
|
|
|
foreach ($exchanges as $exchangeInformation) { |
150
|
|
|
$this->bindExchange($exchangeInformation, $output); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param array $exchangeInformation |
156
|
|
|
* @param OutputInterface $output |
157
|
|
|
*/ |
158
|
|
|
private function bindExchange(array $exchangeInformation, OutputInterface $output) |
159
|
|
|
{ |
160
|
|
|
$exchangeName = $exchangeInformation['name']; |
161
|
|
|
$exchangeType = $exchangeInformation['type']; |
162
|
|
|
|
163
|
|
|
$this->driver->declareExchange($exchangeName, $exchangeType); |
164
|
|
|
|
165
|
|
|
$output->writeln( |
166
|
|
|
sprintf( |
167
|
|
|
'<info>Declare exchange <comment>%s</comment> [<comment>%s</comment>]</info>', |
168
|
|
|
$exchangeName, |
169
|
|
|
$exchangeType |
170
|
|
|
) |
171
|
|
|
); |
172
|
|
|
|
173
|
|
|
$queues = $exchangeInformation['queues']; |
174
|
|
|
foreach ($queues as $queueInformation) { |
175
|
|
|
$this->bindQueue($output, $queueInformation, $exchangeName); |
|
|
|
|
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param string $exchangeName |
181
|
|
|
* @param string $queueInformation |
182
|
|
|
* @param OutputInterface $output |
183
|
|
|
*/ |
184
|
|
|
private function bindQueue( |
185
|
|
|
$exchangeName, |
186
|
|
|
$queueInformation, |
187
|
|
|
OutputInterface $output |
188
|
|
|
) { |
189
|
|
|
$queueName = $queueInformation['name']; |
190
|
|
|
$routingKey = isset($queueInformation['routingKey']) ? $queueInformation['routingKey'] : ''; |
191
|
|
|
|
192
|
|
|
$this->driver->declareAndBindQueue($exchangeName, $queueName, $routingKey); |
193
|
|
|
|
194
|
|
|
$output->writeln(sprintf( |
195
|
|
|
'<info>Bind exchange <comment>%s</comment> to queue ' . |
196
|
|
|
'<comment>%s</comment> [<comment>%s</comment>]</info>', |
197
|
|
|
$exchangeName, |
198
|
|
|
$queueName, |
199
|
|
|
$routingKey |
200
|
|
|
)); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: