Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Xml.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Config.php';
26 
28 #require_once 'Zend/Xml/Security.php';
29 
31 #require_once 'Zend/Xml/Exception.php';
32 
42 {
46  const XML_NAMESPACE = 'http://framework.zend.com/xml/zend-config-xml/1.0/';
47 
53  protected $_skipExtends = false;
54 
84  public function __construct($xml, $section = null, $options = false)
85  {
86  if (empty($xml)) {
87  #require_once 'Zend/Config/Exception.php';
88  throw new Zend_Config_Exception('Filename is not set');
89  }
90 
91  $allowModifications = false;
92  if (is_bool($options)) {
93  $allowModifications = $options;
94  } elseif (is_array($options)) {
95  if (isset($options['allowModifications'])) {
96  $allowModifications = (bool) $options['allowModifications'];
97  }
98  if (isset($options['skipExtends'])) {
99  $this->_skipExtends = (bool) $options['skipExtends'];
100  }
101  }
102 
103  set_error_handler(array($this, '_loadFileErrorHandler')); // Warnings and errors are suppressed
104  if (strstr($xml, '<?xml')) {
106  } else {
107  try {
108  if (!$config = Zend_Xml_Security::scanFile($xml)) {
109  #require_once 'Zend/Config/Exception.php';
110  throw new Zend_Config_Exception(
111  "Error failed to load $xml file"
112  );
113  }
114  } catch (Zend_Xml_Exception $e) {
115  #require_once 'Zend/Config/Exception.php';
116  throw new Zend_Config_Exception(
117  $e->getMessage()
118  );
119  }
120  }
121 
122  restore_error_handler();
123  // Check if there was a error while loading file
124  if ($this->_loadFileErrorStr !== null) {
125  #require_once 'Zend/Config/Exception.php';
126  throw new Zend_Config_Exception($this->_loadFileErrorStr);
127  }
128 
129  if ($section === null) {
130  $dataArray = array();
131  foreach ($config as $sectionName => $sectionData) {
132  $dataArray[$sectionName] = $this->_processExtends($config, $sectionName);
133  }
134 
135  parent::__construct($dataArray, $allowModifications);
136  } else if (is_array($section)) {
137  $dataArray = array();
138  foreach ($section as $sectionName) {
139  if (!isset($config->$sectionName)) {
140  #require_once 'Zend/Config/Exception.php';
141  throw new Zend_Config_Exception("Section '$sectionName' cannot be found in $xml");
142  }
143 
144  $dataArray = array_merge($this->_processExtends($config, $sectionName), $dataArray);
145  }
146 
147  parent::__construct($dataArray, $allowModifications);
148  } else {
149  if (!isset($config->$section)) {
150  #require_once 'Zend/Config/Exception.php';
151  throw new Zend_Config_Exception("Section '$section' cannot be found in $xml");
152  }
153 
154  $dataArray = $this->_processExtends($config, $section);
155  if (!is_array($dataArray)) {
156  // Section in the XML file contains just one top level string
157  $dataArray = array($section => $dataArray);
158  }
159 
160  parent::__construct($dataArray, $allowModifications);
161  }
162 
163  $this->_loadedSection = $section;
164  }
165 
176  protected function _processExtends(SimpleXMLElement $element, $section, array $config = array())
177  {
178  if (!isset($element->$section)) {
179  #require_once 'Zend/Config/Exception.php';
180  throw new Zend_Config_Exception("Section '$section' cannot be found");
181  }
182 
183  $thisSection = $element->$section;
184  $nsAttributes = $thisSection->attributes(self::XML_NAMESPACE);
185 
186  if (isset($thisSection['extends']) || isset($nsAttributes['extends'])) {
187  $extendedSection = (string) (isset($nsAttributes['extends']) ? $nsAttributes['extends'] : $thisSection['extends']);
188  $this->_assertValidExtend($section, $extendedSection);
189 
190  if (!$this->_skipExtends) {
191  $config = $this->_processExtends($element, $extendedSection, $config);
192  }
193  }
194 
195  $config = $this->_arrayMergeRecursive($config, $this->_toArray($thisSection));
196 
197  return $config;
198  }
199 
207  protected function _toArray(SimpleXMLElement $xmlObject)
208  {
209  $config = array();
210  $nsAttributes = $xmlObject->attributes(self::XML_NAMESPACE);
211 
212  // Search for parent node values
213  if (count($xmlObject->attributes()) > 0) {
214  foreach ($xmlObject->attributes() as $key => $value) {
215  if ($key === 'extends') {
216  continue;
217  }
218 
219  $value = (string) $value;
220 
221  if (array_key_exists($key, $config)) {
222  if (!is_array($config[$key])) {
223  $config[$key] = array($config[$key]);
224  }
225 
226  $config[$key][] = $value;
227  } else {
228  $config[$key] = $value;
229  }
230  }
231  }
232 
233  // Search for local 'const' nodes and replace them
234  if (count($xmlObject->children(self::XML_NAMESPACE)) > 0) {
235  if (count($xmlObject->children()) > 0) {
236  #require_once 'Zend/Config/Exception.php';
237  throw new Zend_Config_Exception("A node with a 'const' childnode may not have any other children");
238  }
239 
240  $dom = dom_import_simplexml($xmlObject);
241  $namespaceChildNodes = array();
242 
243  // We have to store them in an array, as replacing nodes will
244  // confuse the DOMNodeList later
245  foreach ($dom->childNodes as $node) {
246  if ($node instanceof DOMElement && $node->namespaceURI === self::XML_NAMESPACE) {
247  $namespaceChildNodes[] = $node;
248  }
249  }
250 
251  foreach ($namespaceChildNodes as $node) {
252  switch ($node->localName) {
253  case 'const':
254  if (!$node->hasAttributeNS(self::XML_NAMESPACE, 'name')) {
255  #require_once 'Zend/Config/Exception.php';
256  throw new Zend_Config_Exception("Misssing 'name' attribute in 'const' node");
257  }
258 
259  $constantName = $node->getAttributeNS(self::XML_NAMESPACE, 'name');
260 
261  if (!defined($constantName)) {
262  #require_once 'Zend/Config/Exception.php';
263  throw new Zend_Config_Exception("Constant with name '$constantName' was not defined");
264  }
265 
266  $constantValue = constant($constantName);
267 
268  $dom->replaceChild($dom->ownerDocument->createTextNode($constantValue), $node);
269  break;
270 
271  default:
272  #require_once 'Zend/Config/Exception.php';
273  throw new Zend_Config_Exception("Unknown node with name '$node->localName' found");
274  }
275  }
276 
277  return (string) simplexml_import_dom($dom);
278  }
279 
280  // Search for children
281  if (count($xmlObject->children()) > 0) {
282  foreach ($xmlObject->children() as $key => $value) {
283  if (count($value->children()) > 0 || count($value->children(self::XML_NAMESPACE)) > 0) {
284  $value = $this->_toArray($value);
285  } else if (count($value->attributes()) > 0) {
286  $attributes = $value->attributes();
287  if (isset($attributes['value'])) {
288  $value = (string) $attributes['value'];
289  } else {
290  $value = $this->_toArray($value);
291  }
292  } else {
293  $value = (string) $value;
294  }
295 
296  if (array_key_exists($key, $config)) {
297  if (!is_array($config[$key]) || !array_key_exists(0, $config[$key])) {
298  $config[$key] = array($config[$key]);
299  }
300 
301  $config[$key][] = $value;
302  } else {
303  $config[$key] = $value;
304  }
305  }
306  } else if (!isset($xmlObject['extends']) && !isset($nsAttributes['extends']) && (count($config) === 0)) {
307  // Object has no children nor attributes and doesn't use the extends
308  // attribute: it's a string
309  $config = (string) $xmlObject;
310  }
311 
312  return $config;
313  }
314 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
static scanFile($file, DOMDocument $dom=null)
Definition: Security.php:142
static scan($xml, DOMDocument $dom=null)
Definition: Security.php:71
$config
Definition: fraud_order.php:17
const XML_NAMESPACE
Definition: Xml.php:46
__construct($xml, $section=null, $options=false)
Definition: Xml.php:84
$value
Definition: gender.phtml:16
$attributes
Definition: matrix.phtml:13
_processExtends(SimpleXMLElement $element, $section, array $config=array())
Definition: Xml.php:176
_assertValidExtend($extendingSection, $extendedSection)
Definition: Config.php:423
_toArray(SimpleXMLElement $xmlObject)
Definition: Xml.php:207
_arrayMergeRecursive($firstArray, $secondArray)
Definition: Config.php:464
$element
Definition: element.phtml:12