Converter.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\MessageQueue\Topology\Config\Xml;
  7. use Magento\Framework\Stdlib\BooleanUtils;
  8. use Magento\Framework\Data\Argument\InterpreterInterface;
  9. use Magento\Framework\Config\Converter\Dom\Flat as FlatConverter;
  10. use Magento\Framework\Config\Dom\ArrayNodeConfig;
  11. use Magento\Framework\Config\Dom\NodePathMatcher;
  12. use Magento\Framework\MessageQueue\DefaultValueProvider;
  13. /**
  14. * Converts MessageQueue topology config from \DOMDocument to array
  15. */
  16. class Converter implements \Magento\Framework\Config\ConverterInterface
  17. {
  18. /**
  19. * @var FlatConverter
  20. */
  21. private $converter;
  22. /**
  23. * Boolean value converter.
  24. *
  25. * @var BooleanUtils
  26. */
  27. private $booleanUtils;
  28. /**
  29. * Argument interpreter.
  30. *
  31. * @var InterpreterInterface
  32. */
  33. private $argumentInterpreter;
  34. /**
  35. * @var DefaultValueProvider
  36. */
  37. private $defaultValue;
  38. /**
  39. * Initialize dependencies.
  40. *
  41. * @param BooleanUtils $booleanUtils
  42. * @param InterpreterInterface $argumentInterpreter
  43. * @param DefaultValueProvider $defaultValueProvider
  44. */
  45. public function __construct(
  46. BooleanUtils $booleanUtils,
  47. InterpreterInterface $argumentInterpreter,
  48. DefaultValueProvider $defaultValueProvider
  49. ) {
  50. $this->booleanUtils = $booleanUtils;
  51. $this->argumentInterpreter = $argumentInterpreter;
  52. $this->defaultValue = $defaultValueProvider;
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function convert($source)
  58. {
  59. $result = [];
  60. /** @var $exchange \DOMElement */
  61. foreach ($source->getElementsByTagName('exchange') as $exchange) {
  62. $name = $this->getAttributeValue($exchange, 'name');
  63. $connection = $this->getAttributeValue($exchange, 'connection');
  64. $bindings = [];
  65. $exchangeArguments = [];
  66. /** @var \DOMNode $node */
  67. foreach ($exchange->childNodes as $node) {
  68. if (!in_array($node->nodeName, ['binding', 'arguments']) || $node->nodeType != XML_ELEMENT_NODE) {
  69. continue;
  70. }
  71. switch ($node->nodeName) {
  72. case 'binding':
  73. $bindings = $this->processBindings($node, $bindings);
  74. break;
  75. case 'arguments':
  76. $exchangeArguments = $this->processArguments($node);
  77. break;
  78. }
  79. }
  80. $autoDelete = $this->getAttributeValue($exchange, 'autoDelete', false);
  81. $result[$name . '--' . $connection] = [
  82. 'name' => $name,
  83. 'type' => $this->getAttributeValue($exchange, 'type'),
  84. 'connection' => $connection,
  85. 'durable' => $this->booleanUtils->toBoolean($this->getAttributeValue($exchange, 'durable', true)),
  86. 'autoDelete' => $this->booleanUtils->toBoolean($autoDelete),
  87. 'internal' => $this->booleanUtils->toBoolean($this->getAttributeValue($exchange, 'internal', false)),
  88. 'bindings' => $bindings,
  89. 'arguments' => $exchangeArguments,
  90. ];
  91. }
  92. return $result;
  93. }
  94. /**
  95. * Retrieve instance of XML converter
  96. *
  97. * @return FlatConverter
  98. */
  99. private function getConverter()
  100. {
  101. if (!$this->converter) {
  102. $arrayNodeConfig = new ArrayNodeConfig(new NodePathMatcher(), ['argument(/item)+' => 'name']);
  103. $this->converter = new FlatConverter($arrayNodeConfig);
  104. }
  105. return $this->converter;
  106. }
  107. /**
  108. * Process arguments.
  109. *
  110. * @param \DOMNode $node
  111. * @return array
  112. */
  113. private function processArguments(\DOMNode $node)
  114. {
  115. $output = [];
  116. /** @var \DOMNode $argumentNode */
  117. foreach ($node->childNodes as $argumentNode) {
  118. if ($argumentNode->nodeType != XML_ELEMENT_NODE || $argumentNode->nodeName != 'argument') {
  119. continue;
  120. }
  121. $argumentName = $argumentNode->attributes->getNamedItem('name')->nodeValue;
  122. $argumentData = $this->getConverter()->convert($argumentNode, 'argument');
  123. $output[$argumentName] = $this->argumentInterpreter->evaluate($argumentData);
  124. }
  125. return $output;
  126. }
  127. /**
  128. * Get attribute value of the given node
  129. *
  130. * @param \DOMNode $node
  131. * @param string $attributeName
  132. * @param mixed $default
  133. * @return string|null
  134. */
  135. private function getAttributeValue(\DOMNode $node, $attributeName, $default = null)
  136. {
  137. $item = $node->attributes->getNamedItem($attributeName);
  138. return $item ? $item->nodeValue : $default;
  139. }
  140. /**
  141. * Parse bindings.
  142. *
  143. * @param \DOMNode $node
  144. * @param array $bindings
  145. * @return array
  146. */
  147. private function processBindings($node, $bindings)
  148. {
  149. $bindingArguments = [];
  150. $id = $this->getAttributeValue($node, 'id');
  151. $isDisabled = $this->booleanUtils->toBoolean(
  152. $this->getAttributeValue($node, 'disabled', false)
  153. );
  154. foreach ($node->childNodes as $arguments) {
  155. if ($arguments->nodeName != 'arguments' || $arguments->nodeType != XML_ELEMENT_NODE) {
  156. continue;
  157. }
  158. $bindingArguments = $this->processArguments($arguments);
  159. }
  160. $bindings[$id] = [
  161. 'id' => $id,
  162. 'destinationType' => $this->getAttributeValue($node, 'destinationType'),
  163. 'destination' => $this->getAttributeValue($node, 'destination'),
  164. 'disabled' => $isDisabled,
  165. 'topic' => $this->getAttributeValue($node, 'topic'),
  166. 'arguments' => $bindingArguments
  167. ];
  168. return $bindings;
  169. }
  170. }