Reader.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Ui\Config;
  7. use Magento\Framework\Config\ConverterInterface as ConfigConverter;
  8. use Magento\Framework\Config\Dom\ValidationException;
  9. use Magento\Framework\Config\FileResolverInterface;
  10. use Magento\Framework\Config\ReaderInterface;
  11. use Magento\Framework\Exception\LocalizedException;
  12. use Magento\Framework\Phrase;
  13. /**
  14. * UI Component configuration reader
  15. */
  16. class Reader implements ReaderInterface
  17. {
  18. /**
  19. * List of identifier attributes for merging
  20. *
  21. * @var array
  22. */
  23. private $idAttributes = ['/' => 'name'];
  24. /**
  25. * @var Reader\Definition
  26. */
  27. private $definitionReader;
  28. /**
  29. * @var ReaderFactory
  30. */
  31. private $readerFactory;
  32. /**
  33. * @var FileResolverInterface
  34. */
  35. private $fileResolver;
  36. /**
  37. * @var ConfigConverter
  38. */
  39. private $converter;
  40. /**
  41. * @var Reader\DomFactory
  42. */
  43. private $readerDomFactory;
  44. /**
  45. * The name of file that stores Ui configuration
  46. *
  47. * @var string
  48. */
  49. private $fileName;
  50. /**
  51. * Reader constructor.
  52. *
  53. * @param string $fileName
  54. * @param FileResolverInterface $fileResolver
  55. * @param ConfigConverter $converter
  56. * @param Reader\Definition $definitionReader
  57. * @param ReaderFactory $readerFactory
  58. * @param Reader\DomFactory $readerDomFactory
  59. * @param array $idAttributes
  60. */
  61. public function __construct(
  62. $fileName,
  63. FileResolverInterface $fileResolver,
  64. ConfigConverter $converter,
  65. Reader\Definition $definitionReader,
  66. ReaderFactory $readerFactory,
  67. Reader\DomFactory $readerDomFactory,
  68. array $idAttributes = []
  69. ) {
  70. $this->fileName = $fileName;
  71. $this->fileResolver = $fileResolver;
  72. $this->converter = $converter;
  73. $this->definitionReader = $definitionReader;
  74. $this->readerFactory = $readerFactory;
  75. $this->readerDomFactory = $readerDomFactory;
  76. $this->idAttributes = array_replace($this->idAttributes, $idAttributes);
  77. }
  78. /**
  79. * Load configuration scope
  80. *
  81. * @param string|null $scope
  82. * @return array
  83. */
  84. public function read($scope = null)
  85. {
  86. $scope = $scope ?: 'global';
  87. $fileList = $this->fileResolver->get($this->fileName, $scope);
  88. if (!count($fileList)) {
  89. return [];
  90. }
  91. $output = $this->readFiles($fileList);
  92. return $output;
  93. }
  94. /**
  95. * Read, merge configuration files and validate resulted configuration
  96. *
  97. * @param array $fileList
  98. * @return array
  99. * @throws LocalizedException
  100. */
  101. private function readFiles($fileList)
  102. {
  103. /** @var \Magento\Ui\Config\Reader\Dom $configMerger */
  104. $configMerger = null;
  105. $output = [];
  106. foreach ($fileList as $key => $content) {
  107. try {
  108. $configMerger = $this->readerDomFactory->create(
  109. [
  110. 'xml' => $content,
  111. 'idAttributes' => $this->idAttributes,
  112. ]
  113. );
  114. $output = array_replace_recursive($output, $this->converter->convert($configMerger->getDom()));
  115. } catch (ValidationException $e) {
  116. throw new LocalizedException(
  117. new Phrase(
  118. 'The XML in file "%1" is invalid:' . "\n%2\nVerify the XML and try again.",
  119. [$key, $e->getMessage()]
  120. )
  121. );
  122. }
  123. }
  124. $definitionData = $this->definitionReader->read();
  125. if (isset($output['attributes']['extends'])) {
  126. $extendsReader = $this->readerFactory->create(
  127. [
  128. 'fileName' => sprintf(
  129. Data::SEARCH_PATTERN,
  130. $output['attributes']['extends']
  131. )
  132. ]
  133. );
  134. $extendsData = $extendsReader->read();
  135. $output = array_replace_recursive($extendsData, $output);
  136. }
  137. $output = $this->mergeDefinition($output, $definitionData);
  138. return $output;
  139. }
  140. /**
  141. * Merge definition to ui component configuration
  142. *
  143. * @param array $component
  144. * @param array $definitions
  145. * @return array
  146. */
  147. private function mergeDefinition(array $component, array $definitions)
  148. {
  149. foreach ($component['children'] as $name => $child) {
  150. $component['children'][$name] = $this->mergeDefinition($child, $definitions);
  151. }
  152. if (isset($component['uiComponentType'])) {
  153. $definition = isset($definitions[$component['uiComponentType']])
  154. ? $definitions[$component['uiComponentType']]
  155. : [];
  156. $component = array_replace_recursive($definition, $component);
  157. unset($component['uiComponentType']);
  158. }
  159. return $component;
  160. }
  161. }