PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/magento/module-config/Model/Config/Structure.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 227 lines | 126 code | 15 blank | 86 comment | 14 complexity | dbccca5004e77d4e739beb3a6af9c643 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * System configuration structure
  8. */
  9. namespace Magento\Config\Model\Config;
  10. class Structure implements \Magento\Config\Model\Config\Structure\SearchInterface
  11. {
  12. /**
  13. * Key that contains field type in structure array
  14. */
  15. const TYPE_KEY = '_elementType';
  16. /**
  17. * Configuration structure represented as tree
  18. *
  19. * @var array
  20. */
  21. protected $_data;
  22. /**
  23. * Config tab iterator
  24. *
  25. * @var \Magento\Config\Model\Config\Structure\Element\Iterator\Tab
  26. */
  27. protected $_tabIterator;
  28. /**
  29. * Pool of config element flyweight objects
  30. *
  31. * @var \Magento\Config\Model\Config\Structure\Element\FlyweightFactory
  32. */
  33. protected $_flyweightFactory;
  34. /**
  35. * Provider of current config scope
  36. *
  37. * @var ScopeDefiner
  38. */
  39. protected $_scopeDefiner;
  40. /**
  41. * List of cached elements
  42. *
  43. * @var \Magento\Config\Model\Config\Structure\ElementInterface[]
  44. */
  45. protected $_elements;
  46. /**
  47. * @param \Magento\Config\Model\Config\Structure\Data $structureData
  48. * @param \Magento\Config\Model\Config\Structure\Element\Iterator\Tab $tabIterator
  49. * @param \Magento\Config\Model\Config\Structure\Element\FlyweightFactory $flyweightFactory
  50. * @param ScopeDefiner $scopeDefiner
  51. */
  52. public function __construct(
  53. \Magento\Config\Model\Config\Structure\Data $structureData,
  54. \Magento\Config\Model\Config\Structure\Element\Iterator\Tab $tabIterator,
  55. \Magento\Config\Model\Config\Structure\Element\FlyweightFactory $flyweightFactory,
  56. ScopeDefiner $scopeDefiner
  57. ) {
  58. $this->_data = $structureData->get();
  59. $this->_tabIterator = $tabIterator;
  60. $this->_flyweightFactory = $flyweightFactory;
  61. $this->_scopeDefiner = $scopeDefiner;
  62. }
  63. /**
  64. * Retrieve tab iterator
  65. *
  66. * @return \Magento\Config\Model\Config\Structure\Element\Iterator
  67. */
  68. public function getTabs()
  69. {
  70. if (isset($this->_data['sections'])) {
  71. foreach ($this->_data['sections'] as $sectionId => $section) {
  72. if (isset($section['tab']) && $section['tab']) {
  73. $this->_data['tabs'][$section['tab']]['children'][$sectionId] = $section;
  74. }
  75. }
  76. $this->_tabIterator->setElements($this->_data['tabs'], $this->_scopeDefiner->getScope());
  77. }
  78. return $this->_tabIterator;
  79. }
  80. /**
  81. * Find element by path
  82. *
  83. * @param string $path
  84. * @return \Magento\Config\Model\Config\Structure\ElementInterface|null
  85. */
  86. public function getElement($path)
  87. {
  88. return $this->getElementByPathParts(explode('/', $path));
  89. }
  90. /**
  91. * Retrieve first available section in config structure
  92. *
  93. * @return \Magento\Config\Model\Config\Structure\ElementInterface
  94. */
  95. public function getFirstSection()
  96. {
  97. $tabs = $this->getTabs();
  98. $tabs->rewind();
  99. /** @var $tab \Magento\Config\Model\Config\Structure\Element\Tab */
  100. $tab = $tabs->current();
  101. $tab->getChildren()->rewind();
  102. return $tab->getChildren()->current();
  103. }
  104. /**
  105. * Find element by path parts
  106. *
  107. * @param string[] $pathParts
  108. * @return \Magento\Config\Model\Config\Structure\ElementInterface|null
  109. */
  110. public function getElementByPathParts(array $pathParts)
  111. {
  112. $path = implode('_', $pathParts);
  113. if (isset($this->_elements[$path])) {
  114. return $this->_elements[$path];
  115. }
  116. $children = [];
  117. if ($this->_data) {
  118. $children = $this->_data['sections'];
  119. }
  120. $child = [];
  121. foreach ($pathParts as $pathPart) {
  122. if ($children && (array_key_exists($pathPart, $children))) {
  123. $child = $children[$pathPart];
  124. $children = array_key_exists('children', $child) ? $child['children'] : [];
  125. } else {
  126. $child = $this->_createEmptyElement($pathParts);
  127. break;
  128. }
  129. }
  130. $this->_elements[$path] = $this->_flyweightFactory->create($child['_elementType']);
  131. $this->_elements[$path]->setData($child, $this->_scopeDefiner->getScope());
  132. return $this->_elements[$path];
  133. }
  134. /**
  135. * Create empty element data
  136. *
  137. * @param string[] $pathParts
  138. * @return array
  139. */
  140. protected function _createEmptyElement(array $pathParts)
  141. {
  142. switch (count($pathParts)) {
  143. case 1:
  144. $elementType = 'section';
  145. break;
  146. case 2:
  147. $elementType = 'group';
  148. break;
  149. default:
  150. $elementType = 'field';
  151. }
  152. $elementId = array_pop($pathParts);
  153. return ['id' => $elementId, 'path' => implode('/', $pathParts), '_elementType' => $elementType];
  154. }
  155. /**
  156. * Retrieve paths of fields that have provided attributes with provided values
  157. *
  158. * @param string $attributeName
  159. * @param mixed $attributeValue
  160. * @return array
  161. */
  162. public function getFieldPathsByAttribute($attributeName, $attributeValue)
  163. {
  164. $result = [];
  165. foreach ($this->_data['sections'] as $section) {
  166. if (!isset($section['children'])) {
  167. continue;
  168. }
  169. foreach ($section['children'] as $group) {
  170. if (isset($group['children'])) {
  171. $path = $section['id'] . '/' . $group['id'];
  172. $result = array_merge(
  173. $result,
  174. $this->_getGroupFieldPathsByAttribute(
  175. $group['children'],
  176. $path,
  177. $attributeName,
  178. $attributeValue
  179. )
  180. );
  181. }
  182. }
  183. }
  184. return $result;
  185. }
  186. /**
  187. * Find group fields with specified attribute and attribute value
  188. *
  189. * @param array $fields
  190. * @param string $parentPath
  191. * @param string $attributeName
  192. * @param mixed $attributeValue
  193. * @return array
  194. */
  195. protected function _getGroupFieldPathsByAttribute(array $fields, $parentPath, $attributeName, $attributeValue)
  196. {
  197. $result = [];
  198. foreach ($fields as $field) {
  199. if (isset($field['children'])) {
  200. $result += $this->_getGroupFieldPathsByAttribute(
  201. $field['children'],
  202. $parentPath . '/' . $field['id'],
  203. $attributeName,
  204. $attributeValue
  205. );
  206. } elseif (isset($field[$attributeName]) && $field[$attributeName] == $attributeValue) {
  207. $result[] = $parentPath . '/' . $field['id'];
  208. }
  209. }
  210. return $result;
  211. }
  212. }