Dom.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\ObjectManager\Config\Mapper;
  7. use Magento\Framework\Data\Argument\InterpreterInterface;
  8. use Magento\Framework\Stdlib\BooleanUtils;
  9. class Dom implements \Magento\Framework\Config\ConverterInterface
  10. {
  11. /**
  12. * @var BooleanUtils
  13. */
  14. private $booleanUtils;
  15. /**
  16. * @var ArgumentParser
  17. */
  18. private $argumentParser;
  19. /**
  20. * @var InterpreterInterface
  21. */
  22. private $argumentInterpreter;
  23. /**
  24. * @param BooleanUtils $booleanUtils
  25. * @param ArgumentParser $argumentParser
  26. * @param InterpreterInterface $argumentInterpreter
  27. */
  28. public function __construct(
  29. InterpreterInterface $argumentInterpreter,
  30. BooleanUtils $booleanUtils = null,
  31. ArgumentParser $argumentParser = null
  32. ) {
  33. $this->argumentInterpreter = $argumentInterpreter;
  34. $this->booleanUtils = $booleanUtils ?: new BooleanUtils();
  35. $this->argumentParser = $argumentParser ?: new ArgumentParser();
  36. }
  37. /**
  38. * Convert configuration in DOM format to assoc array that can be used by object manager
  39. *
  40. * @param \DOMDocument $config
  41. * @return array
  42. * @throws \Exception
  43. * @todo this method has high cyclomatic complexity in order to avoid performance issues
  44. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  45. * @SuppressWarnings(PHPMD.NPathComplexity)
  46. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  47. */
  48. public function convert($config)
  49. {
  50. $output = [];
  51. /** @var \DOMNode $node */
  52. foreach ($config->documentElement->childNodes as $node) {
  53. if ($node->nodeType != XML_ELEMENT_NODE) {
  54. continue;
  55. }
  56. switch ($node->nodeName) {
  57. case 'preference':
  58. $output['preferences'][$node->attributes->getNamedItem(
  59. 'for'
  60. )->nodeValue] = $node->attributes->getNamedItem(
  61. 'type'
  62. )->nodeValue;
  63. break;
  64. case 'type':
  65. case 'virtualType':
  66. $typeData = [];
  67. $typeNodeAttributes = $node->attributes;
  68. $typeNodeShared = $typeNodeAttributes->getNamedItem('shared');
  69. if ($typeNodeShared) {
  70. $typeData['shared'] = $this->booleanUtils->toBoolean($typeNodeShared->nodeValue);
  71. }
  72. if ($node->nodeName == 'virtualType') {
  73. $attributeType = $typeNodeAttributes->getNamedItem('type');
  74. // attribute type is required for virtual type only in merged configuration
  75. if ($attributeType) {
  76. $typeData['type'] = $attributeType->nodeValue;
  77. }
  78. }
  79. $typeArguments = [];
  80. $typePlugins = [];
  81. /** @var \DOMNode $typeChildNode */
  82. foreach ($node->childNodes as $typeChildNode) {
  83. if ($typeChildNode->nodeType != XML_ELEMENT_NODE) {
  84. continue;
  85. }
  86. switch ($typeChildNode->nodeName) {
  87. case 'arguments':
  88. /** @var \DOMNode $argumentNode */
  89. foreach ($typeChildNode->childNodes as $argumentNode) {
  90. if ($argumentNode->nodeType != XML_ELEMENT_NODE) {
  91. continue;
  92. }
  93. $argumentName = $argumentNode->attributes->getNamedItem('name')->nodeValue;
  94. $argumentData = $this->argumentParser->parse($argumentNode);
  95. $typeArguments[$argumentName] = $this->argumentInterpreter->evaluate(
  96. $argumentData
  97. );
  98. }
  99. break;
  100. case 'plugin':
  101. $pluginAttributes = $typeChildNode->attributes;
  102. $pluginDisabledNode = $pluginAttributes->getNamedItem('disabled');
  103. $pluginSortOrderNode = $pluginAttributes->getNamedItem('sortOrder');
  104. $pluginTypeNode = $pluginAttributes->getNamedItem('type');
  105. $pluginData = [
  106. 'sortOrder' => $pluginSortOrderNode ? (int)$pluginSortOrderNode->nodeValue : 0,
  107. ];
  108. if ($pluginDisabledNode) {
  109. $pluginData['disabled'] = $this->booleanUtils->toBoolean(
  110. $pluginDisabledNode->nodeValue
  111. );
  112. }
  113. if ($pluginTypeNode) {
  114. $pluginData['instance'] = $pluginTypeNode->nodeValue;
  115. }
  116. $typePlugins[$pluginAttributes->getNamedItem('name')->nodeValue] = $pluginData;
  117. break;
  118. default:
  119. throw new \Exception(
  120. "Invalid application config. Unknown node: {$typeChildNode->nodeName}."
  121. );
  122. }
  123. }
  124. $typeData['arguments'] = $typeArguments;
  125. if (!empty($typePlugins)) {
  126. $typeData['plugins'] = $typePlugins;
  127. }
  128. $output[$typeNodeAttributes->getNamedItem('name')->nodeValue] = $typeData;
  129. break;
  130. default:
  131. throw new \Exception("Invalid application config. Unknown node: {$node->nodeName}.");
  132. }
  133. }
  134. return $output;
  135. }
  136. }