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
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param array $configuration |
101
|
|
|
*/ |
102
|
|
|
private static function checkExchangesConfiguration(array $configuration) |
103
|
|
|
{ |
104
|
|
|
Assertion::keyIsset($configuration, 'exchanges', 'You must provide an `exchanges` configuration'); |
105
|
|
|
|
106
|
|
|
$exchanges = $configuration['exchanges']; |
107
|
|
|
Assertion::isArray($exchanges, 'The `exchanges` configuration must be an array'); |
108
|
|
|
|
109
|
|
|
foreach ($exchanges as $exchangeInformation) { |
110
|
|
|
Assertion::keyIsset($exchangeInformation, 'name', 'You must provide a name for the exchange'); |
111
|
|
|
Assertion::keyIsset($exchangeInformation, 'type', 'You must provide a type for the exchange'); |
112
|
|
|
|
113
|
|
|
$queues = $exchangeInformation['queues']; |
114
|
|
|
Assertion::keyIsset( |
115
|
|
|
$exchangeInformation, |
116
|
|
|
'queues', |
117
|
|
|
'You must provide a `queues` configuration for the exchange' |
118
|
|
|
); |
119
|
|
|
Assertion::isArray($queues, 'The `queues` configuration must be an array'); |
120
|
|
|
|
121
|
|
|
foreach ($queues as $queueInformation) { |
122
|
|
|
Assertion::keyIsset($queueInformation, 'name', 'You must provide a name for the queue'); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param array $configuration |
129
|
|
|
* @param OutputInterface $output |
130
|
|
|
*/ |
131
|
|
|
protected function declareQueues($configuration, OutputInterface $output) |
132
|
|
|
{ |
133
|
|
|
$queues = $configuration['queues']; |
134
|
|
|
foreach ($queues as $queue) { |
135
|
|
|
$this->driver->declareSimpleQueue($queue); |
136
|
|
|
$output->writeln(sprintf('<info>Declare queue <comment>%s</comment></info>', $queue)); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param array $configuration |
142
|
|
|
* @param OutputInterface $output |
143
|
|
|
*/ |
144
|
|
|
protected function bind($configuration, OutputInterface $output) |
145
|
|
|
{ |
146
|
|
|
$exchanges = $configuration['exchanges']; |
147
|
|
|
foreach ($exchanges as $exchangeInformation) { |
148
|
|
|
$exchangeName = $exchangeInformation['name']; |
149
|
|
|
$exchangeType = $exchangeInformation['type']; |
150
|
|
|
|
151
|
|
|
$this->driver->declareExchange($exchangeName, $exchangeType); |
152
|
|
|
|
153
|
|
|
$output->writeln( |
154
|
|
|
sprintf( |
155
|
|
|
'<info>Declare exchange <comment>%s</comment> [<comment>%s</comment>]</info>', |
156
|
|
|
$exchangeName, |
157
|
|
|
$exchangeType |
158
|
|
|
) |
159
|
|
|
); |
160
|
|
|
|
161
|
|
|
$queues = $exchangeInformation['queues']; |
162
|
|
|
foreach ($queues as $queueInformation) { |
163
|
|
|
$queueName = $queueInformation['name']; |
164
|
|
|
$routingKey = isset($queueInformation['routingKey']) ? $queueInformation['routingKey'] : ''; |
165
|
|
|
|
166
|
|
|
$this->driver->declareAndBindQueue($exchangeName, $queueName, $routingKey); |
167
|
|
|
|
168
|
|
|
$output->writeln(sprintf( |
169
|
|
|
'<info>Bind exchange <comment>%s</comment> to queue ' . |
170
|
|
|
'<comment>%s</comment> [<comment>%s</comment>]</info>', |
171
|
|
|
$exchangeName, |
172
|
|
|
$queueName, |
173
|
|
|
$routingKey |
174
|
|
|
)); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|