Definition.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Ui\Config\Reader;
  7. use Magento\Framework\Config\Dom\ValidationException;
  8. use Magento\Framework\Config\ReaderInterface;
  9. use Magento\Framework\Exception\LocalizedException;
  10. use Magento\Framework\Phrase;
  11. /**
  12. * UI Component definition config reader
  13. */
  14. class Definition extends \Magento\Framework\Config\Reader\Filesystem implements ReaderInterface
  15. {
  16. /**
  17. * Load configuration scope
  18. *
  19. * @param string|null $scope
  20. * @return array
  21. */
  22. public function read($scope = null)
  23. {
  24. $scope = $scope ?: $this->_defaultScope;
  25. $fileList = $this->_fileResolver->get($this->_fileName, $scope);
  26. if (!count($fileList)) {
  27. return [];
  28. }
  29. $output = $this->readFiles($fileList);
  30. return $output;
  31. }
  32. /**
  33. * Read, merge configuration files and validate resulted XML
  34. *
  35. * @param array $fileList
  36. * @return array
  37. * @throws LocalizedException if XML file is invalid
  38. */
  39. private function readFiles($fileList)
  40. {
  41. /** @var \Magento\Framework\Config\Dom $configMerger */
  42. $configMerger = null;
  43. foreach ($fileList as $key => $content) {
  44. try {
  45. if (!$configMerger) {
  46. $configMerger = $this->_createConfigMerger($this->_domDocumentClass, $content);
  47. } else {
  48. $configMerger->merge($content);
  49. }
  50. } catch (ValidationException $e) {
  51. throw new LocalizedException(
  52. new Phrase(
  53. 'The XML in file "%1" is invalid.' . "\n%2\nVerify the XML and try again.",
  54. [$key, $e->getMessage()]
  55. )
  56. );
  57. }
  58. }
  59. if ($this->validationState->isValidationRequired()) {
  60. $errors = [];
  61. if ($configMerger && !$configMerger->validate($this->_schemaFile, $errors)) {
  62. $message = "Invalid Document \n";
  63. throw new LocalizedException(
  64. new Phrase($message . implode("\n", $errors))
  65. );
  66. }
  67. }
  68. $output = [];
  69. if ($configMerger) {
  70. $output = $this->_converter->convert($configMerger->getDom()->documentElement);
  71. }
  72. return $output;
  73. }
  74. }