Reader.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * Backend System Configuration reader.
  4. * Retrieves system configuration form layout from system.xml files. Merges configuration and caches it.
  5. *
  6. * Copyright © Magento, Inc. All rights reserved.
  7. * See COPYING.txt for license details.
  8. */
  9. namespace Magento\Config\Model\Config\Structure;
  10. use Magento\Framework\DataObject;
  11. use Magento\Framework\Exception\LocalizedException;
  12. use Magento\Framework\View\TemplateEngine\Xhtml\CompilerInterface;
  13. /**
  14. * Class Reader
  15. * @api
  16. * @since 100.0.2
  17. */
  18. class Reader extends \Magento\Framework\Config\Reader\Filesystem
  19. {
  20. /**
  21. * List of identifier attributes for merging
  22. *
  23. * @var array
  24. */
  25. protected $_idAttributes = [
  26. '/config/system/tab' => 'id',
  27. '/config/system/section' => 'id',
  28. '/config/system/section(/group)+' => 'id',
  29. '/config/system/section(/group)+/field' => 'id',
  30. '/config/system/section(/group)+/field/depends/field' => 'id',
  31. '/config/system/section(/group)+/field/options/option' => 'label',
  32. ];
  33. /**
  34. * @var CompilerInterface
  35. */
  36. protected $compiler;
  37. /**
  38. * Constructor
  39. *
  40. * @param \Magento\Framework\Config\FileResolverInterface $fileResolver
  41. * @param Converter $converter
  42. * @param \Magento\Config\Model\Config\SchemaLocator $schemaLocator
  43. * @param \Magento\Framework\Config\ValidationStateInterface $validationState
  44. * @param CompilerInterface $compiler
  45. * @param string $fileName
  46. * @param array $idAttributes
  47. * @param string $domDocumentClass
  48. * @param string $defaultScope
  49. */
  50. public function __construct(
  51. \Magento\Framework\Config\FileResolverInterface $fileResolver,
  52. Converter $converter,
  53. \Magento\Config\Model\Config\SchemaLocator $schemaLocator,
  54. \Magento\Framework\Config\ValidationStateInterface $validationState,
  55. CompilerInterface $compiler,
  56. $fileName = 'system.xml',
  57. $idAttributes = [],
  58. $domDocumentClass = \Magento\Framework\Config\Dom::class,
  59. $defaultScope = 'global'
  60. ) {
  61. $this->compiler = $compiler;
  62. parent::__construct(
  63. $fileResolver,
  64. $converter,
  65. $schemaLocator,
  66. $validationState,
  67. $fileName,
  68. $idAttributes,
  69. $domDocumentClass,
  70. $defaultScope
  71. );
  72. }
  73. /**
  74. * Read configuration files
  75. *
  76. * @param array $fileList
  77. * @return array
  78. * @throws LocalizedException
  79. */
  80. protected function _readFiles($fileList)
  81. {
  82. /** @var \Magento\Framework\Config\Dom $configMerger */
  83. $configMerger = null;
  84. foreach ($fileList as $key => $content) {
  85. try {
  86. $content = $this->processingDocument($content);
  87. if (!$configMerger) {
  88. $configMerger = $this->_createConfigMerger($this->_domDocumentClass, $content);
  89. } else {
  90. $configMerger->merge($content);
  91. }
  92. } catch (\Magento\Framework\Config\Dom\ValidationException $e) {
  93. throw new LocalizedException(
  94. new \Magento\Framework\Phrase(
  95. 'The XML in file "%1" is invalid:' . "\n%2\nVerify the XML and try again.",
  96. [$key, $e->getMessage()]
  97. )
  98. );
  99. }
  100. }
  101. if ($this->validationState->isValidationRequired()) {
  102. $errors = [];
  103. if ($configMerger && !$configMerger->validate($this->_schemaFile, $errors)) {
  104. $message = "Invalid Document \n";
  105. throw new LocalizedException(
  106. new \Magento\Framework\Phrase($message . implode("\n", $errors))
  107. );
  108. }
  109. }
  110. $output = [];
  111. if ($configMerger) {
  112. $output = $this->_converter->convert($configMerger->getDom());
  113. }
  114. return $output;
  115. }
  116. /**
  117. * Processing nodes of the document before merging
  118. *
  119. * @param string $content
  120. * @throws \Magento\Framework\Config\Dom\ValidationException
  121. * @return string
  122. */
  123. protected function processingDocument($content)
  124. {
  125. $object = new DataObject();
  126. $document = new \DOMDocument();
  127. try {
  128. $document->loadXML($content);
  129. } catch (\Exception $e) {
  130. throw new \Magento\Framework\Config\Dom\ValidationException($e->getMessage());
  131. }
  132. $this->compiler->compile($document->documentElement, $object, $object);
  133. return $document->saveXML();
  134. }
  135. }