Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Converter.php
Go to the documentation of this file.
1 <?php
7 
14 
19 {
23  private $converter;
24 
30  private $booleanUtils;
31 
37  private $argumentInterpreter;
38 
42  private $defaultValue;
43 
51  public function __construct(
52  BooleanUtils $booleanUtils,
53  InterpreterInterface $argumentInterpreter,
54  DefaultValueProvider $defaultValueProvider
55  ) {
56  $this->booleanUtils = $booleanUtils;
57  $this->argumentInterpreter = $argumentInterpreter;
58  $this->defaultValue = $defaultValueProvider;
59  }
60 
64  public function convert($source)
65  {
66  $result = [];
68  foreach ($source->getElementsByTagName('exchange') as $exchange) {
69  $name = $this->getAttributeValue($exchange, 'name');
70  $connection = $this->getAttributeValue($exchange, 'connection');
71 
72  $bindings = [];
73  $exchangeArguments = [];
75  foreach ($exchange->childNodes as $node) {
76  if (!in_array($node->nodeName, ['binding', 'arguments']) || $node->nodeType != XML_ELEMENT_NODE) {
77  continue;
78  }
79  switch ($node->nodeName) {
80  case 'binding':
81  $bindings = $this->processBindings($node, $bindings);
82  break;
83 
84  case 'arguments':
85  $exchangeArguments = $this->processArguments($node);
86  break;
87  }
88  }
89 
90  $autoDelete = $this->getAttributeValue($exchange, 'autoDelete', false);
91  $result[$name . '--' . $connection] = [
92  'name' => $name,
93  'type' => $this->getAttributeValue($exchange, 'type'),
94  'connection' => $connection,
95  'durable' => $this->booleanUtils->toBoolean($this->getAttributeValue($exchange, 'durable', true)),
96  'autoDelete' => $this->booleanUtils->toBoolean($autoDelete),
97  'internal' => $this->booleanUtils->toBoolean($this->getAttributeValue($exchange, 'internal', false)),
98  'bindings' => $bindings,
99  'arguments' => $exchangeArguments,
100  ];
101  }
102  return $result;
103  }
104 
110  private function getConverter()
111  {
112  if (!$this->converter) {
113  $arrayNodeConfig = new ArrayNodeConfig(new NodePathMatcher(), ['argument(/item)+' => 'name']);
114  $this->converter = new FlatConverter($arrayNodeConfig);
115  }
116  return $this->converter;
117  }
118 
125  private function processArguments(\DOMNode $node)
126  {
127  $output = [];
129  foreach ($node->childNodes as $argumentNode) {
130  if ($argumentNode->nodeType != XML_ELEMENT_NODE || $argumentNode->nodeName != 'argument') {
131  continue;
132  }
133  $argumentName = $argumentNode->attributes->getNamedItem('name')->nodeValue;
134  $argumentData = $this->getConverter()->convert($argumentNode, 'argument');
135  $output[$argumentName] = $this->argumentInterpreter->evaluate($argumentData);
136  }
137  return $output;
138  }
139 
148  private function getAttributeValue(\DOMNode $node, $attributeName, $default = null)
149  {
150  $item = $node->attributes->getNamedItem($attributeName);
151  return $item ? $item->nodeValue : $default;
152  }
153 
161  private function processBindings($node, $bindings)
162  {
163  $bindingArguments = [];
164  $id = $this->getAttributeValue($node, 'id');
165  $isDisabled = $this->booleanUtils->toBoolean(
166  $this->getAttributeValue($node, 'disabled', false)
167  );
168  foreach ($node->childNodes as $arguments) {
169  if ($arguments->nodeName != 'arguments' || $arguments->nodeType != XML_ELEMENT_NODE) {
170  continue;
171  }
172  $bindingArguments = $this->processArguments($arguments);
173  }
174  $bindings[$id] = [
175  'id' => $id,
176  'destinationType' => $this->getAttributeValue($node, 'destinationType'),
177  'destination' => $this->getAttributeValue($node, 'destination'),
178  'disabled' => $isDisabled,
179  'topic' => $this->getAttributeValue($node, 'topic'),
180  'arguments' => $bindingArguments
181  ];
182  return $bindings;
183  }
184 }
__construct(BooleanUtils $booleanUtils, InterpreterInterface $argumentInterpreter, DefaultValueProvider $defaultValueProvider)
Definition: Converter.php:51
$id
Definition: fieldset.phtml:14
$source
Definition: source.php:23
$arguments
$connection
Definition: bulk.php:13
if(!isset($_GET['name'])) $name
Definition: log.php:14