1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\MessageQueue\Config\Reader\Xml\Converter;
use Magento\Framework\MessageQueue\ConfigInterface;
use Magento\Framework\MessageQueue\Config\Validator;
use Magento\Framework\Reflection\MethodsMap;
use Magento\Framework\Communication\ConfigInterface as CommunicationConfig;
use Magento\Framework\MessageQueue\ConfigInterface as QueueConfig;
use Magento\Framework\MessageQueue\ConsumerInterface;
/**
* Converts MessageQueue config from \DOMDocument to array
*
* @deprecated 102.0.1
*/
class TopicConfig implements \Magento\Framework\Config\ConverterInterface
{
const DEFAULT_TYPE = 'amqp';
const DEFAULT_EXCHANGE = 'magento';
const DEFAULT_INSTANCE = ConsumerInterface::class;
/**
* @var Validator
*/
private $xmlValidator;
/**
* @var MethodsMap
*/
private $methodsMap;
/**
* @var CommunicationConfig
*/
private $communicationConfig;
/**
* Initialize dependencies.
*
* @param MethodsMap $methodsMap
* @param Validator $xmlValidator
* @param CommunicationConfig $communicationConfig
*/
public function __construct(
MethodsMap $methodsMap,
Validator $xmlValidator,
CommunicationConfig $communicationConfig
) {
$this->methodsMap = $methodsMap;
$this->xmlValidator = $xmlValidator;
$this->communicationConfig = $communicationConfig;
}
/**
* {@inheritDoc}
*/
public function convert($source)
{
$topics = $this->extractTopics($source);
$topics = $this->processWildcard($topics);
$publishers = $this->buildPublishers($topics);
$binds = $this->buildBinds($topics);
$map = $this->buildExchangeTopicToQueue($topics);
$consumers = $this->buildConsumers($topics);
return [
ConfigInterface::TOPICS => $this->buildTopicsConfiguration($topics),
ConfigInterface::PUBLISHERS => $publishers,
ConfigInterface::BINDS => $binds,
ConfigInterface::CONSUMERS => $consumers,
ConfigInterface::EXCHANGE_TOPIC_TO_QUEUES_MAP => $map,
];
}
/**
* Generate list of topics.
*
* @param array $topics
* @return array
*/
private function buildTopicsConfiguration($topics)
{
$output = [];
foreach ($topics as $topicName => $topicConfig) {
$topicDefinition = $this->communicationConfig->getTopic($topicName);
$schemaType =
$topicDefinition['request_type'] == CommunicationConfig::TOPIC_REQUEST_TYPE_CLASS
? QueueConfig::TOPIC_SCHEMA_TYPE_OBJECT
: QueueConfig::TOPIC_SCHEMA_TYPE_METHOD;
$schemaValue = $topicDefinition[CommunicationConfig::TOPIC_REQUEST];
$output[$topicName] = [
'name' => $topicName,
'schema' => [
'schema_type' => $schemaType,
'schema_value' => $schemaValue
],
'response_schema' => [
'schema_type' => isset($topicDefinition['response']) ? QueueConfig::TOPIC_SCHEMA_TYPE_OBJECT : null,
'schema_value' => $topicDefinition['response']
],
'is_synchronous' => $topicDefinition[CommunicationConfig::TOPIC_IS_SYNCHRONOUS],
'publisher' => $topicConfig['type'] . '-' . $topicConfig['exchange']
];
}
return $output;
}
/**
* Generate consumers.
*
* @param array $topics
* @return array
*/
private function buildConsumers($topics)
{
$output = [];
foreach ($topics as $topicName => $topicConfig) {
$topic = $this->communicationConfig->getTopic($topicName);
foreach ($topicConfig['queues'] as $queueName => $queueConfig) {
$handlers = [];
foreach ($queueConfig['handlers'] as $handler) {
if (!isset($handler[QueueConfig::CONSUMER_CLASS])) {
$handlerExploded = explode('::', $handler);
unset($handler);
$handler[QueueConfig::CONSUMER_CLASS] = $handlerExploded[0];
$handler[QueueConfig::CONSUMER_METHOD] = $handlerExploded[1];
}
$handlers[] = $handler;
}
$queueConfig['handlers'] = $handlers;
$output[$queueConfig['consumer']] = [
'name' => $queueConfig['consumer'],
'queue' => $queueName,
'handlers' => [$topicName => $queueConfig['handlers']],
'instance_type' => $queueConfig['consumerInstance'] != null
? $queueConfig['consumerInstance'] : self::DEFAULT_INSTANCE,
'consumer_type' => $topic[CommunicationConfig::TOPIC_IS_SYNCHRONOUS] ? 'sync' : 'async',
'max_messages' => $queueConfig['maxMessages'],
'connection' => $topicConfig['type']
];
}
}
return $output;
}
/**
* Generate topics list based on wildcards.
*
* @param array $topics
* @return array
*/
private function processWildcard($topics)
{
$topicDefinitions = $this->communicationConfig->getTopics();
$wildcardKeys = [];
$topicNames = array_keys($topics);
foreach ($topicNames as $topicName) {
if (strpos($topicName, '*') !== false || strpos($topicName, '#') !== false) {
$wildcardKeys[] = $topicName;
}
}
foreach (array_unique($wildcardKeys) as $wildcardKey) {
$pattern = $this->xmlValidator->buildWildcardPattern($wildcardKey);
foreach (array_keys($topicDefinitions) as $topicName) {
if (preg_match($pattern, $topicName)) {
if (isset($topics[$topicName])) {
$topics[$topicName] = array_merge($topics[$topicName], $topics[$wildcardKey]);
} else {
$topics[$topicName] = $topics[$wildcardKey];
}
}
}
unset($topics[$wildcardKey]);
}
return $topics;
}
/**
* Generate publishers
*
* @param array $topics
* @return array
*/
private function buildPublishers($topics)
{
$output = [];
foreach ($topics as $topicConfig) {
$publisherName = $topicConfig['type'] . '-' . $topicConfig['exchange'];
$output[$publisherName] = [
'name' => $publisherName,
'connection' => $topicConfig['type'],
'exchange' => $topicConfig['exchange']
];
}
return $output;
}
/**
* Generate binds
*
* @param array $topics
* @return array
*/
private function buildBinds($topics)
{
$output = [];
foreach ($topics as $topicName => $topicConfig) {
$queueNames = array_keys($topicConfig['queues']);
foreach ($queueNames as $queueName) {
$name = $topicName . '--' . $topicConfig['exchange']. '--' .$queueName;
$output[$name] = [
'queue' => $queueName,
'exchange' => $topicConfig['exchange'],
'topic' => $topicName,
];
}
}
return $output;
}
/**
* Generate topic-to-queues map.
*
* @param array $topics
* @return array
*/
private function buildExchangeTopicToQueue($topics)
{
$output = [];
foreach ($topics as $topicName => $topicConfig) {
$key = $topicConfig['type'] . '-' . $topicConfig['exchange'] . '--' . $topicName;
$queueNames = array_keys($topicConfig['queues']);
foreach ($queueNames as $queueName) {
$output[$key][] = $queueName;
$output[$key] = array_unique($output[$key]);
}
}
return $output;
}
/**
* Extract topics configuration.
*
* @param \DOMDocument $config
* @return array
*/
private function extractTopics($config)
{
$output = [];
/** @var $brokerNode \DOMElement */
foreach ($config->getElementsByTagName('broker') as $brokerNode) {
$topicName = $this->getAttributeValue($brokerNode, 'topic');
$output[$topicName] = [
ConfigInterface::TOPIC_NAME => $topicName,
'type' => $this->getAttributeValue($brokerNode, 'type', self::DEFAULT_TYPE),
'exchange' => $this->getAttributeValue($brokerNode, 'exchange', self::DEFAULT_EXCHANGE),
'consumerInstance' => $this->getAttributeValue($brokerNode, 'consumerInstance'),
'maxMessages' => $this->getAttributeValue($brokerNode, 'maxMessages'),
'queues' => $this->extractQueuesFromBroker($brokerNode, $topicName)
];
}
return $output;
}
/**
* Extract queues configuration from the topic node.
*
* @param \DOMElement $brokerNode
* @param string $topicName
* @return array
*/
protected function extractQueuesFromBroker(\DOMElement $brokerNode, $topicName)
{
$queues = [];
$topicConfig = $this->communicationConfig->getTopic($topicName);
/** @var $queueNode \DOMElement */
foreach ($brokerNode->getElementsByTagName('queue') as $queueNode) {
$handler = $this->getAttributeValue($queueNode, 'handler');
$queueName = $this->getAttributeValue($queueNode, 'name');
$queue = [
'name'=> $queueName,
'handlerName' => $this->getAttributeValue($queueNode, 'handlerName'),
'handlers' => $handler ? ['default' => $handler] : $topicConfig['handlers'],
'exchange' => $this->getAttributeValue($queueNode, 'exchange'),
'consumer' => $this->getAttributeValue($queueNode, 'consumer'),
'consumerInstance' => $this->getAttributeValue($queueNode, 'consumerInstance'),
'maxMessages' => $this->getAttributeValue($queueNode, 'maxMessages', null),
'type' => $this->getAttributeValue($queueNode, 'type')
];
$queues[$queueName] = $queue;
}
return $queues;
}
/**
* Get attribute value of the given node
*
* @param \DOMNode $node
* @param string $attributeName
* @param mixed $default
* @return string|null
*/
protected function getAttributeValue(\DOMNode $node, $attributeName, $default = null)
{
$item = $node->attributes->getNamedItem($attributeName);
return $item ? $item->nodeValue : $default;
}
}