Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Zend_Config_Xml Class Reference
Inheritance diagram for Zend_Config_Xml:
Zend_Config

Public Member Functions

 __construct ($xml, $section=null, $options=false)
 
- Public Member Functions inherited from Zend_Config
 __construct (array $array, $allowModifications=false)
 
 get ($name, $default=null)
 
 __get ($name)
 
 __set ($name, $value)
 
 __clone ()
 
 toArray ()
 
 __isset ($name)
 
 __unset ($name)
 
 count ()
 
 current ()
 
 key ()
 
 next ()
 
 rewind ()
 
 valid ()
 
 getSectionName ()
 
 areAllSectionsLoaded ()
 
 merge (Zend_Config $merge)
 
 setReadOnly ()
 
 readOnly ()
 
 getExtends ()
 
 setExtend ($extendingSection, $extendedSection=null)
 
 _loadFileErrorHandler ($errno, $errstr, $errfile, $errline)
 

Data Fields

const XML_NAMESPACE = 'http://framework.zend.com/xml/zend-config-xml/1.0/'
 

Protected Member Functions

 _processExtends (SimpleXMLElement $element, $section, array $config=array())
 
 _toArray (SimpleXMLElement $xmlObject)
 
- Protected Member Functions inherited from Zend_Config
 _assertValidExtend ($extendingSection, $extendedSection)
 
 _arrayMergeRecursive ($firstArray, $secondArray)
 

Protected Attributes

 $_skipExtends = false
 
- Protected Attributes inherited from Zend_Config
 $_allowModifications
 
 $_index
 
 $_count
 
 $_data
 
 $_skipNextIteration
 
 $_loadedSection
 
 $_extends = array()
 
 $_loadFileErrorStr = null
 

Detailed Description

Definition at line 41 of file Xml.php.

Constructor & Destructor Documentation

◆ __construct()

__construct (   $xml,
  $section = null,
  $options = false 
)

Loads the section $section from the config file (or string $xml for access facilitated by nested object properties.

Sections are defined in the XML as children of the root element.

In order to extend another section, a section defines the "extends" attribute having a value of the section name from which the extending section inherits values.

Note that the keys in $section will override any keys of the same name in the sections that have been included via "extends".

The $options parameter may be provided as either a boolean or an array. If provided as a boolean, this sets the $allowModifications option of Zend_Config. If provided as an array, there are two configuration directives that may be set. For example:

$options = array( 'allowModifications' => false, 'skipExtends' => false );

Parameters
string$xmlXML file or string to process
mixed$sectionSection to process
array | boolean$options
Exceptions
Zend_Config_ExceptionWhen xml is not set or cannot be loaded
Zend_Config_ExceptionWhen section $sectionName cannot be found in $xml

Definition at line 84 of file Xml.php.

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  }
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
_processExtends(SimpleXMLElement $element, $section, array $config=array())
Definition: Xml.php:176

Member Function Documentation

◆ _processExtends()

_processExtends ( SimpleXMLElement  $element,
  $section,
array  $config = array() 
)
protected

Helper function to process each element in the section and handle the "extends" inheritance attribute.

Parameters
SimpleXMLElement$elementXML Element to process
string$sectionSection to process
array$configConfiguration which was parsed yet
Exceptions
Zend_Config_ExceptionWhen $section cannot be found
Returns
array

Definition at line 176 of file Xml.php.

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  }
$config
Definition: fraud_order.php:17
_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

◆ _toArray()

_toArray ( SimpleXMLElement  $xmlObject)
protected

Returns a string or an associative and possibly multidimensional array from a SimpleXMLElement.

Parameters
SimpleXMLElement$xmlObjectConvert a SimpleXMLElement into an array
Returns
array|string

Definition at line 207 of file Xml.php.

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  }
$config
Definition: fraud_order.php:17
$value
Definition: gender.phtml:16
$attributes
Definition: matrix.phtml:13
_toArray(SimpleXMLElement $xmlObject)
Definition: Xml.php:207

Field Documentation

◆ $_skipExtends

$_skipExtends = false
protected

Definition at line 53 of file Xml.php.

◆ XML_NAMESPACE

const XML_NAMESPACE = 'http://framework.zend.com/xml/zend-config-xml/1.0/'

XML namespace for ZF-related tags and attributes

Definition at line 46 of file Xml.php.


The documentation for this class was generated from the following file: