Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Config.php
Go to the documentation of this file.
1 <?php
29 class Zend_Config implements Countable, Iterator
30 {
37 
43  protected $_index;
44 
50  protected $_count;
51 
57  protected $_data;
58 
66 
74  protected $_loadedSection;
75 
82  protected $_extends = array();
83 
91  protected $_loadFileErrorStr = null;
92 
105  public function __construct(array $array, $allowModifications = false)
106  {
107  $this->_allowModifications = (boolean) $allowModifications;
108  $this->_loadedSection = null;
109  $this->_index = 0;
110  $this->_data = array();
111  foreach ($array as $key => $value) {
112  if (is_array($value)) {
113  $this->_data[$key] = new self($value, $this->_allowModifications);
114  } else {
115  $this->_data[$key] = $value;
116  }
117  }
118  $this->_count = count($this->_data);
119  }
120 
128  public function get($name, $default = null)
129  {
130  $result = $default;
131  if (array_key_exists($name, $this->_data)) {
132  $result = $this->_data[$name];
133  }
134  return $result;
135  }
136 
143  public function __get($name)
144  {
145  return $this->get($name);
146  }
147 
157  public function __set($name, $value)
158  {
159  if ($this->_allowModifications) {
160  if (is_array($value)) {
161  $this->_data[$name] = new self($value, true);
162  } else {
163  $this->_data[$name] = $value;
164  }
165  $this->_count = count($this->_data);
166  } else {
168  #require_once 'Zend/Config/Exception.php';
169  throw new Zend_Config_Exception('Zend_Config is read only');
170  }
171  }
172 
179  public function __clone()
180  {
181  $array = array();
182  foreach ($this->_data as $key => $value) {
183  if ($value instanceof Zend_Config) {
184  $array[$key] = clone $value;
185  } else {
186  $array[$key] = $value;
187  }
188  }
189  $this->_data = $array;
190  }
191 
197  public function toArray()
198  {
199  $array = array();
201  foreach ($data as $key => $value) {
202  if ($value instanceof Zend_Config) {
203  $array[$key] = $value->toArray();
204  } else {
205  $array[$key] = $value;
206  }
207  }
208  return $array;
209  }
210 
217  public function __isset($name)
218  {
219  return isset($this->_data[$name]);
220  }
221 
229  public function __unset($name)
230  {
231  if ($this->_allowModifications) {
232  unset($this->_data[$name]);
233  $this->_count = count($this->_data);
234  $this->_skipNextIteration = true;
235  } else {
237  #require_once 'Zend/Config/Exception.php';
238  throw new Zend_Config_Exception('Zend_Config is read only');
239  }
240 
241  }
242 
248  public function count()
249  {
250  return $this->_count;
251  }
252 
258  public function current()
259  {
260  $this->_skipNextIteration = false;
261  return current($this->_data);
262  }
263 
269  public function key()
270  {
271  return key($this->_data);
272  }
273 
278  public function next()
279  {
280  if ($this->_skipNextIteration) {
281  $this->_skipNextIteration = false;
282  return;
283  }
284  next($this->_data);
285  $this->_index++;
286  }
287 
292  public function rewind()
293  {
294  $this->_skipNextIteration = false;
295  reset($this->_data);
296  $this->_index = 0;
297  }
298 
304  public function valid()
305  {
306  return $this->_index < $this->_count;
307  }
308 
314  public function getSectionName()
315  {
316  if(is_array($this->_loadedSection) && count($this->_loadedSection) == 1) {
317  $this->_loadedSection = $this->_loadedSection[0];
318  }
319  return $this->_loadedSection;
320  }
321 
327  public function areAllSectionsLoaded()
328  {
329  return $this->_loadedSection === null;
330  }
331 
332 
341  public function merge(Zend_Config $merge)
342  {
343  foreach($merge as $key => $item) {
344  if(array_key_exists($key, $this->_data)) {
345  if($item instanceof Zend_Config && $this->$key instanceof Zend_Config) {
346  $this->$key = $this->$key->merge(new Zend_Config($item->toArray(), !$this->readOnly()));
347  } else {
348  $this->$key = $item;
349  }
350  } else {
351  if($item instanceof Zend_Config) {
352  $this->$key = new Zend_Config($item->toArray(), !$this->readOnly());
353  } else {
354  $this->$key = $item;
355  }
356  }
357  }
358 
359  return $this;
360  }
361 
368  public function setReadOnly()
369  {
370  $this->_allowModifications = false;
371  foreach ($this->_data as $key => $value) {
372  if ($value instanceof Zend_Config) {
373  $value->setReadOnly();
374  }
375  }
376  }
377 
383  public function readOnly()
384  {
386  }
387 
393  public function getExtends()
394  {
395  return $this->_extends;
396  }
397 
405  public function setExtend($extendingSection, $extendedSection = null)
406  {
407  if ($extendedSection === null && isset($this->_extends[$extendingSection])) {
408  unset($this->_extends[$extendingSection]);
409  } else if ($extendedSection !== null) {
410  $this->_extends[$extendingSection] = $extendedSection;
411  }
412  }
413 
423  protected function _assertValidExtend($extendingSection, $extendedSection)
424  {
425  // detect circular section inheritance
426  $extendedSectionCurrent = $extendedSection;
427  while (array_key_exists($extendedSectionCurrent, $this->_extends)) {
428  if ($this->_extends[$extendedSectionCurrent] == $extendingSection) {
430  #require_once 'Zend/Config/Exception.php';
431  throw new Zend_Config_Exception('Illegal circular inheritance detected');
432  }
433  $extendedSectionCurrent = $this->_extends[$extendedSectionCurrent];
434  }
435  // remember that this section extends another section
436  $this->_extends[$extendingSection] = $extendedSection;
437  }
438 
447  public function _loadFileErrorHandler($errno, $errstr, $errfile, $errline)
448  {
449  if ($this->_loadFileErrorStr === null) {
450  $this->_loadFileErrorStr = $errstr;
451  } else {
452  $this->_loadFileErrorStr .= (PHP_EOL . $errstr);
453  }
454  }
455 
464  protected function _arrayMergeRecursive($firstArray, $secondArray)
465  {
466  if (is_array($firstArray) && is_array($secondArray)) {
467  foreach ($secondArray as $key => $value) {
468  if (isset($firstArray[$key])) {
469  $firstArray[$key] = $this->_arrayMergeRecursive($firstArray[$key], $value);
470  } else {
471  if($key === 0) {
472  $firstArray= array(0=>$this->_arrayMergeRecursive($firstArray, $value));
473  } else {
474  $firstArray[$key] = $value;
475  }
476  }
477  }
478  } else {
479  $firstArray = $secondArray;
480  }
481 
482  return $firstArray;
483  }
484 }
setExtend($extendingSection, $extendedSection=null)
Definition: Config.php:405
setReadOnly()
Definition: Config.php:368
merge(Zend_Config $merge)
Definition: Config.php:341
__set($name, $value)
Definition: Config.php:157
$value
Definition: gender.phtml:16
getSectionName()
Definition: Config.php:314
__get($name)
Definition: Config.php:143
__isset($name)
Definition: Config.php:217
_loadFileErrorHandler($errno, $errstr, $errfile, $errline)
Definition: Config.php:447
__unset($name)
Definition: Config.php:229
$_loadedSection
Definition: Config.php:74
$_allowModifications
Definition: Config.php:36
$_loadFileErrorStr
Definition: Config.php:91
_assertValidExtend($extendingSection, $extendedSection)
Definition: Config.php:423
areAllSectionsLoaded()
Definition: Config.php:327
$_skipNextIteration
Definition: Config.php:65
__construct(array $array, $allowModifications=false)
Definition: Config.php:105
getExtends()
Definition: Config.php:393
_arrayMergeRecursive($firstArray, $secondArray)
Definition: Config.php:464
if(!isset($_GET['name'])) $name
Definition: log.php:14