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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\MessageQueue\Topology\Config\Xml;
use Magento\Framework\Stdlib\BooleanUtils;
use Magento\Framework\Data\Argument\InterpreterInterface;
use Magento\Framework\Config\Converter\Dom\Flat as FlatConverter;
use Magento\Framework\Config\Dom\ArrayNodeConfig;
use Magento\Framework\Config\Dom\NodePathMatcher;
use Magento\Framework\MessageQueue\DefaultValueProvider;
/**
* Converts MessageQueue topology config from \DOMDocument to array
*/
class Converter implements \Magento\Framework\Config\ConverterInterface
{
/**
* @var FlatConverter
*/
private $converter;
/**
* Boolean value converter.
*
* @var BooleanUtils
*/
private $booleanUtils;
/**
* Argument interpreter.
*
* @var InterpreterInterface
*/
private $argumentInterpreter;
/**
* @var DefaultValueProvider
*/
private $defaultValue;
/**
* Initialize dependencies.
*
* @param BooleanUtils $booleanUtils
* @param InterpreterInterface $argumentInterpreter
* @param DefaultValueProvider $defaultValueProvider
*/
public function __construct(
BooleanUtils $booleanUtils,
InterpreterInterface $argumentInterpreter,
DefaultValueProvider $defaultValueProvider
) {
$this->booleanUtils = $booleanUtils;
$this->argumentInterpreter = $argumentInterpreter;
$this->defaultValue = $defaultValueProvider;
}
/**
* {@inheritdoc}
*/
public function convert($source)
{
$result = [];
/** @var $exchange \DOMElement */
foreach ($source->getElementsByTagName('exchange') as $exchange) {
$name = $this->getAttributeValue($exchange, 'name');
$connection = $this->getAttributeValue($exchange, 'connection');
$bindings = [];
$exchangeArguments = [];
/** @var \DOMNode $node */
foreach ($exchange->childNodes as $node) {
if (!in_array($node->nodeName, ['binding', 'arguments']) || $node->nodeType != XML_ELEMENT_NODE) {
continue;
}
switch ($node->nodeName) {
case 'binding':
$bindings = $this->processBindings($node, $bindings);
break;
case 'arguments':
$exchangeArguments = $this->processArguments($node);
break;
}
}
$autoDelete = $this->getAttributeValue($exchange, 'autoDelete', false);
$result[$name . '--' . $connection] = [
'name' => $name,
'type' => $this->getAttributeValue($exchange, 'type'),
'connection' => $connection,
'durable' => $this->booleanUtils->toBoolean($this->getAttributeValue($exchange, 'durable', true)),
'autoDelete' => $this->booleanUtils->toBoolean($autoDelete),
'internal' => $this->booleanUtils->toBoolean($this->getAttributeValue($exchange, 'internal', false)),
'bindings' => $bindings,
'arguments' => $exchangeArguments,
];
}
return $result;
}
/**
* Retrieve instance of XML converter
*
* @return FlatConverter
*/
private function getConverter()
{
if (!$this->converter) {
$arrayNodeConfig = new ArrayNodeConfig(new NodePathMatcher(), ['argument(/item)+' => 'name']);
$this->converter = new FlatConverter($arrayNodeConfig);
}
return $this->converter;
}
/**
* Process arguments.
*
* @param \DOMNode $node
* @return array
*/
private function processArguments(\DOMNode $node)
{
$output = [];
/** @var \DOMNode $argumentNode */
foreach ($node->childNodes as $argumentNode) {
if ($argumentNode->nodeType != XML_ELEMENT_NODE || $argumentNode->nodeName != 'argument') {
continue;
}
$argumentName = $argumentNode->attributes->getNamedItem('name')->nodeValue;
$argumentData = $this->getConverter()->convert($argumentNode, 'argument');
$output[$argumentName] = $this->argumentInterpreter->evaluate($argumentData);
}
return $output;
}
/**
* Get attribute value of the given node
*
* @param \DOMNode $node
* @param string $attributeName
* @param mixed $default
* @return string|null
*/
private function getAttributeValue(\DOMNode $node, $attributeName, $default = null)
{
$item = $node->attributes->getNamedItem($attributeName);
return $item ? $item->nodeValue : $default;
}
/**
* Parse bindings.
*
* @param \DOMNode $node
* @param array $bindings
* @return array
*/
private function processBindings($node, $bindings)
{
$bindingArguments = [];
$id = $this->getAttributeValue($node, 'id');
$isDisabled = $this->booleanUtils->toBoolean(
$this->getAttributeValue($node, 'disabled', false)
);
foreach ($node->childNodes as $arguments) {
if ($arguments->nodeName != 'arguments' || $arguments->nodeType != XML_ELEMENT_NODE) {
continue;
}
$bindingArguments = $this->processArguments($arguments);
}
$bindings[$id] = [
'id' => $id,
'destinationType' => $this->getAttributeValue($node, 'destinationType'),
'destination' => $this->getAttributeValue($node, 'destination'),
'disabled' => $isDisabled,
'topic' => $this->getAttributeValue($node, 'topic'),
'arguments' => $bindingArguments
];
return $bindings;
}
}