Filesystem.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /**
  3. * Filesystem configuration loader. Loads configuration from XML files, split by scopes
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. *
  8. */
  9. namespace Magento\Framework\Config\Reader;
  10. /**
  11. * @SuppressWarnings(PHPMD.NumberOfChildren)
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class Filesystem implements \Magento\Framework\Config\ReaderInterface
  16. {
  17. /**
  18. * File locator
  19. *
  20. * @var \Magento\Framework\Config\FileResolverInterface
  21. */
  22. protected $_fileResolver;
  23. /**
  24. * Config converter
  25. *
  26. * @var \Magento\Framework\Config\ConverterInterface
  27. */
  28. protected $_converter;
  29. /**
  30. * The name of file that stores configuration
  31. *
  32. * @var string
  33. */
  34. protected $_fileName;
  35. /**
  36. * Path to corresponding XSD file with validation rules for merged config
  37. *
  38. * @var string
  39. */
  40. protected $_schema;
  41. /**
  42. * Path to corresponding XSD file with validation rules for separate config files
  43. *
  44. * @var string
  45. */
  46. protected $_perFileSchema;
  47. /**
  48. * List of id attributes for merge
  49. *
  50. * @var array
  51. */
  52. protected $_idAttributes = [];
  53. /**
  54. * Class of dom configuration document used for merge
  55. *
  56. * @var string
  57. */
  58. protected $_domDocumentClass;
  59. /**
  60. * @var \Magento\Framework\Config\ValidationStateInterface
  61. */
  62. protected $validationState;
  63. /**
  64. * @var string
  65. * @since 100.0.3
  66. */
  67. protected $_defaultScope;
  68. /**
  69. * @var string
  70. * @since 100.0.3
  71. */
  72. protected $_schemaFile;
  73. /**
  74. * Constructor
  75. *
  76. * @param \Magento\Framework\Config\FileResolverInterface $fileResolver
  77. * @param \Magento\Framework\Config\ConverterInterface $converter
  78. * @param \Magento\Framework\Config\SchemaLocatorInterface $schemaLocator
  79. * @param \Magento\Framework\Config\ValidationStateInterface $validationState
  80. * @param string $fileName
  81. * @param array $idAttributes
  82. * @param string $domDocumentClass
  83. * @param string $defaultScope
  84. */
  85. public function __construct(
  86. \Magento\Framework\Config\FileResolverInterface $fileResolver,
  87. \Magento\Framework\Config\ConverterInterface $converter,
  88. \Magento\Framework\Config\SchemaLocatorInterface $schemaLocator,
  89. \Magento\Framework\Config\ValidationStateInterface $validationState,
  90. $fileName,
  91. $idAttributes = [],
  92. $domDocumentClass = \Magento\Framework\Config\Dom::class,
  93. $defaultScope = 'global'
  94. ) {
  95. $this->_fileResolver = $fileResolver;
  96. $this->_converter = $converter;
  97. $this->_fileName = $fileName;
  98. $this->_idAttributes = array_replace($this->_idAttributes, $idAttributes);
  99. $this->validationState = $validationState;
  100. $this->_schemaFile = $schemaLocator->getSchema();
  101. $this->_perFileSchema = $schemaLocator->getPerFileSchema() && $validationState->isValidationRequired()
  102. ? $schemaLocator->getPerFileSchema() : null;
  103. $this->_domDocumentClass = $domDocumentClass;
  104. $this->_defaultScope = $defaultScope;
  105. }
  106. /**
  107. * Load configuration scope
  108. *
  109. * @param string|null $scope
  110. * @return array
  111. */
  112. public function read($scope = null)
  113. {
  114. $scope = $scope ?: $this->_defaultScope;
  115. $fileList = $this->_fileResolver->get($this->_fileName, $scope);
  116. if (!count($fileList)) {
  117. return [];
  118. }
  119. $output = $this->_readFiles($fileList);
  120. return $output;
  121. }
  122. /**
  123. * Read configuration files
  124. *
  125. * @param array $fileList
  126. * @return array
  127. * @throws \Magento\Framework\Exception\LocalizedException
  128. */
  129. protected function _readFiles($fileList)
  130. {
  131. /** @var \Magento\Framework\Config\Dom $configMerger */
  132. $configMerger = null;
  133. foreach ($fileList as $key => $content) {
  134. try {
  135. if (!$configMerger) {
  136. $configMerger = $this->_createConfigMerger($this->_domDocumentClass, $content);
  137. } else {
  138. $configMerger->merge($content);
  139. }
  140. } catch (\Magento\Framework\Config\Dom\ValidationException $e) {
  141. throw new \Magento\Framework\Exception\LocalizedException(
  142. new \Magento\Framework\Phrase(
  143. 'The XML in file "%1" is invalid:' . "\n%2\nVerify the XML and try again.",
  144. [$key, $e->getMessage()]
  145. )
  146. );
  147. }
  148. }
  149. if ($this->validationState->isValidationRequired()) {
  150. $errors = [];
  151. if ($configMerger && !$configMerger->validate($this->_schemaFile, $errors)) {
  152. $message = "Invalid Document \n";
  153. throw new \Magento\Framework\Exception\LocalizedException(
  154. new \Magento\Framework\Phrase($message . implode("\n", $errors))
  155. );
  156. }
  157. }
  158. $output = [];
  159. if ($configMerger) {
  160. $output = $this->_converter->convert($configMerger->getDom());
  161. }
  162. return $output;
  163. }
  164. /**
  165. * Return newly created instance of a config merger
  166. *
  167. * @param string $mergerClass
  168. * @param string $initialContents
  169. * @return \Magento\Framework\Config\Dom
  170. * @throws \UnexpectedValueException
  171. */
  172. protected function _createConfigMerger($mergerClass, $initialContents)
  173. {
  174. $result = new $mergerClass(
  175. $initialContents,
  176. $this->validationState,
  177. $this->_idAttributes,
  178. null,
  179. $this->_perFileSchema
  180. );
  181. if (!$result instanceof \Magento\Framework\Config\Dom) {
  182. throw new \UnexpectedValueException(
  183. "Instance of the DOM config merger is expected, got {$mergerClass} instead."
  184. );
  185. }
  186. return $result;
  187. }
  188. }