View.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Config;
  7. /**
  8. * View configuration files handler
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class View extends \Magento\Framework\Config\Reader\Filesystem
  14. {
  15. /**
  16. * @var array
  17. */
  18. protected $xpath;
  19. /**
  20. * View config data
  21. *
  22. * @var array
  23. */
  24. protected $data;
  25. /**
  26. * @param FileResolverInterface $fileResolver
  27. * @param ConverterInterface $converter
  28. * @param SchemaLocatorInterface $schemaLocator
  29. * @param ValidationStateInterface $validationState
  30. * @param string $fileName
  31. * @param array $idAttributes
  32. * @param string $domDocumentClass
  33. * @param string $defaultScope
  34. * @param array $xpath
  35. */
  36. public function __construct(
  37. FileResolverInterface $fileResolver,
  38. ConverterInterface $converter,
  39. SchemaLocatorInterface $schemaLocator,
  40. ValidationStateInterface $validationState,
  41. $fileName,
  42. $idAttributes = [],
  43. $domDocumentClass = \Magento\Framework\Config\Dom::class,
  44. $defaultScope = 'global',
  45. $xpath = []
  46. ) {
  47. $this->xpath = $xpath;
  48. $idAttributes = $this->getIdAttributes();
  49. parent::__construct(
  50. $fileResolver,
  51. $converter,
  52. $schemaLocator,
  53. $validationState,
  54. $fileName,
  55. $idAttributes,
  56. $domDocumentClass,
  57. $defaultScope
  58. );
  59. }
  60. /**
  61. * Get a list of variables in scope of specified module
  62. *
  63. * Returns array(<var_name> => <var_value>)
  64. *
  65. * @param string $module
  66. * @return array
  67. */
  68. public function getVars($module)
  69. {
  70. $this->initData();
  71. return $this->data['vars'][$module] ?? [];
  72. }
  73. /**
  74. * Get value of a configuration option variable
  75. *
  76. * @param string $module
  77. * @param string $var
  78. * @return string|false|array
  79. */
  80. public function getVarValue($module, $var)
  81. {
  82. $this->initData();
  83. if (!isset($this->data['vars'][$module])) {
  84. return false;
  85. }
  86. $value = $this->data['vars'][$module];
  87. foreach (explode('/', $var) as $node) {
  88. if (is_array($value) && isset($value[$node])) {
  89. $value = $value[$node];
  90. } else {
  91. return false;
  92. }
  93. }
  94. return $value;
  95. }
  96. /**
  97. * Retrieve a list media attributes in scope of specified module
  98. *
  99. * @param string $module
  100. * @param string $mediaType
  101. * @return array
  102. */
  103. public function getMediaEntities($module, $mediaType)
  104. {
  105. $this->initData();
  106. return $this->data['media'][$module][$mediaType] ?? [];
  107. }
  108. /**
  109. * Retrieve array of media attributes
  110. *
  111. * @param string $module
  112. * @param string $mediaType
  113. * @param string $mediaId
  114. * @return array
  115. */
  116. public function getMediaAttributes($module, $mediaType, $mediaId)
  117. {
  118. $this->initData();
  119. return $this->data['media'][$module][$mediaType][$mediaId] ?? [];
  120. }
  121. /**
  122. * Variables are identified by module and name
  123. *
  124. * @return array
  125. */
  126. protected function getIdAttributes()
  127. {
  128. $idAttributes = [
  129. '/view/vars' => 'module',
  130. '/view/vars/(var/)*var' => 'name',
  131. '/view/exclude/item' => ['type', 'item'],
  132. ];
  133. foreach ($this->xpath as $attribute) {
  134. if (is_array($attribute)) {
  135. foreach ($attribute as $key => $id) {
  136. if (count($id) > 1) {
  137. $idAttributes[$key] = array_values($id);
  138. } else {
  139. $idAttributes[$key] = array_shift($id);
  140. }
  141. }
  142. }
  143. }
  144. return $idAttributes;
  145. }
  146. /**
  147. * Get excluded file list
  148. *
  149. * @return array
  150. */
  151. public function getExcludedFiles()
  152. {
  153. $items = $this->getItems();
  154. return $items['file'] ?? [];
  155. }
  156. /**
  157. * Get excluded directory list
  158. *
  159. * @return array
  160. */
  161. public function getExcludedDir()
  162. {
  163. $items = $this->getItems();
  164. return $items['directory'] ?? [];
  165. }
  166. /**
  167. * Get a list of excludes
  168. *
  169. * @return array
  170. */
  171. protected function getItems()
  172. {
  173. $this->initData();
  174. return $this->data['exclude'] ?? [];
  175. }
  176. /**
  177. * Initialize data array
  178. *
  179. * @return void
  180. */
  181. protected function initData()
  182. {
  183. if ($this->data === null) {
  184. $this->data = $this->read();
  185. }
  186. }
  187. /**
  188. * {@inheritdoc}
  189. * @since 100.1.0
  190. */
  191. public function read($scope = null)
  192. {
  193. $scope = $scope ?: $this->_defaultScope;
  194. $result = [];
  195. $parents = (array)$this->_fileResolver->getParents($this->_fileName, $scope);
  196. // Sort parents desc
  197. krsort($parents);
  198. foreach ($parents as $parent) {
  199. $result = array_replace_recursive($result, $this->_readFiles([$parent]));
  200. }
  201. return array_replace_recursive($result, parent::read($scope));
  202. }
  203. }