Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ExtendsMapper.php
Go to the documentation of this file.
1 <?php
11 
16 class ExtendsMapper extends \Magento\Config\Model\Config\Structure\AbstractMapper
17 {
24 
30  protected $_extendedNodesList = [];
31 
37  protected $_pathConverter;
38 
42  public function __construct(
43  \Magento\Config\Model\Config\Structure\Mapper\Helper\RelativePathConverter $pathConverted
44  ) {
45  $this->_pathConverter = $pathConverted;
46  }
47 
54  public function map(array $data)
55  {
56  if (!isset($data['config']['system']['sections']) || !is_array($data['config']['system']['sections'])) {
57  return $data;
58  }
59 
60  $this->_systemConfiguration = & $data['config']['system']['sections'];
61 
62  foreach (array_keys($this->_systemConfiguration) as $nodeName) {
63  $this->_traverseAndExtend($nodeName);
64  }
65 
66  return $data;
67  }
68 
75  protected function _traverseAndExtend($path)
76  {
77  $node = $this->_getDataByPath($path);
78 
79  if (!is_array($node)) {
80  return;
81  }
82 
83  if (!empty($node['extends'])) {
84  $node = $this->_extendNode($path, $node['extends']);
85  }
86 
87  if (!empty($node['children'])) {
88  foreach (array_keys($node['children']) as $childName) {
89  $this->_traverseAndExtend($path . '/' . $childName);
90  }
91  }
92  }
93 
100  protected function _getDataByPath($path)
101  {
103  $pathParts = $this->_transformPathToKeysList($path);
104 
105  foreach ($pathParts as $part) {
106  $result = isset($result[$part]) ? $result[$part] : null;
107  if ($result === null) {
108  return $result;
109  }
110  }
111 
112  return $result;
113  }
114 
123  protected function _extendNode($path, $extendSourceNode)
124  {
125  $currentNodeData = $this->_getDataByPath($path);
126 
127  if (in_array($path, $this->_extendedNodesList)) {
128  return $currentNodeData;
129  }
130 
131  $extendSourcePath = $this->_pathConverter->convert($path, $extendSourceNode);
132  $data = $this->_getDataByPath($extendSourcePath);
133 
134  if (!$data) {
135  throw new \InvalidArgumentException(
136  sprintf('Invalid path in extends attribute of config/system/sections/%s node', $path)
137  );
138  }
139 
140  if (isset($data['extends'])) {
141  $data = $this->_extendNode($extendSourcePath, $data['extends']);
142  }
143 
144  $resultingData = $this->_mergeData($data, $currentNodeData);
145 
146  $this->_replaceData($path, $resultingData);
147  $this->_extendedNodesList[] = $path;
148 
149  return $resultingData;
150  }
151 
160  protected function _mergeData($arr1, $arr2)
161  {
162  foreach ($arr2 as $key => $value) {
163  if (isset($arr1[$key]) && is_array($arr1[$key]) && is_array($value)) {
164  $arr1[$key] = $this->_mergeData($arr1[$key], $value);
165  } else {
166  $arr1[$key] = $value;
167  }
168  }
169 
170  return $arr1;
171  }
172 
180  protected function _replaceData($path, $newData)
181  {
182  $pathParts = $this->_transformPathToKeysList($path);
184 
185  foreach ($pathParts as $part) {
186  if (!isset($result[$part])) {
187  return;
188  }
189  $result = & $result[$part];
190  }
191 
192  $result = $newData;
193  }
194 
201  protected function _transformPathToKeysList($path)
202  {
203  $path = str_replace('/', '/children/', $path);
204  return explode('/', $path);
205  }
206 }
__construct(\Magento\Config\Model\Config\Structure\Mapper\Helper\RelativePathConverter $pathConverted)
$value
Definition: gender.phtml:16