Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Ini.php
Go to the documentation of this file.
1 <?php
26 #require_once 'Zend/Config.php';
27 
28 
36 {
42  protected $_nestSeparator = '.';
43 
49  protected $_sectionSeparator = ':';
50 
56  protected $_skipExtends = false;
57 
101  public function __construct($filename, $section = null, $options = false)
102  {
103  if (empty($filename)) {
107  #require_once 'Zend/Config/Exception.php';
108  throw new Zend_Config_Exception('Filename is not set');
109  }
110 
111  $allowModifications = false;
112  if (is_bool($options)) {
113  $allowModifications = $options;
114  } elseif (is_array($options)) {
115  if (isset($options['allowModifications'])) {
116  $allowModifications = (bool) $options['allowModifications'];
117  }
118  if (isset($options['nestSeparator'])) {
119  $this->_nestSeparator = (string) $options['nestSeparator'];
120  }
121  if (isset($options['skipExtends'])) {
122  $this->_skipExtends = (bool) $options['skipExtends'];
123  }
124  }
125 
126  $iniArray = $this->_loadIniFile($filename);
127 
128  if (null === $section) {
129  // Load entire file
130  $dataArray = array();
131  foreach ($iniArray as $sectionName => $sectionData) {
132  if(!is_array($sectionData)) {
133  $dataArray = $this->_arrayMergeRecursive($dataArray, $this->_processKey(array(), $sectionName, $sectionData));
134  } else {
135  $dataArray[$sectionName] = $this->_processSection($iniArray, $sectionName);
136  }
137  }
138  parent::__construct($dataArray, $allowModifications);
139  } else {
140  // Load one or more sections
141  if (!is_array($section)) {
142  $section = array($section);
143  }
144  $dataArray = array();
145  foreach ($section as $sectionName) {
146  if (!isset($iniArray[$sectionName])) {
150  #require_once 'Zend/Config/Exception.php';
151  throw new Zend_Config_Exception("Section '$sectionName' cannot be found in $filename");
152  }
153  $dataArray = $this->_arrayMergeRecursive($this->_processSection($iniArray, $sectionName), $dataArray);
154 
155  }
156  parent::__construct($dataArray, $allowModifications);
157  }
158 
159  $this->_loadedSection = $section;
160  }
161 
170  protected function _parseIniFile($filename)
171  {
172  set_error_handler(array($this, '_loadFileErrorHandler'));
173  $iniArray = parse_ini_file($filename, true); // Warnings and errors are suppressed
174  restore_error_handler();
175 
176  // Check if there was a error while loading file
177  if ($this->_loadFileErrorStr !== null) {
181  #require_once 'Zend/Config/Exception.php';
182  throw new Zend_Config_Exception($this->_loadFileErrorStr);
183  }
184 
185  return $iniArray;
186  }
187 
200  protected function _loadIniFile($filename)
201  {
202  $loaded = $this->_parseIniFile($filename);
203  $iniArray = array();
204  foreach ($loaded as $key => $data)
205  {
206  $pieces = explode($this->_sectionSeparator, $key);
207  $thisSection = trim($pieces[0]);
208  switch (count($pieces)) {
209  case 1:
210  $iniArray[$thisSection] = $data;
211  break;
212 
213  case 2:
214  $extendedSection = trim($pieces[1]);
215  $iniArray[$thisSection] = array_merge(array(';extends'=>$extendedSection), $data);
216  break;
217 
218  default:
222  #require_once 'Zend/Config/Exception.php';
223  throw new Zend_Config_Exception("Section '$thisSection' may not extend multiple sections in $filename");
224  }
225  }
226 
227  return $iniArray;
228  }
229 
241  protected function _processSection($iniArray, $section, $config = array())
242  {
243  $thisSection = $iniArray[$section];
244 
245  foreach ($thisSection as $key => $value) {
246  if (strtolower($key) == ';extends') {
247  if (isset($iniArray[$value])) {
248  $this->_assertValidExtend($section, $value);
249 
250  if (!$this->_skipExtends) {
251  $config = $this->_processSection($iniArray, $value, $config);
252  }
253  } else {
257  #require_once 'Zend/Config/Exception.php';
258  throw new Zend_Config_Exception("Parent section '$section' cannot be found");
259  }
260  } else {
261  $config = $this->_processKey($config, $key, $value);
262  }
263  }
264  return $config;
265  }
266 
277  protected function _processKey($config, $key, $value)
278  {
279  if (strpos($key, $this->_nestSeparator) !== false) {
280  $pieces = explode($this->_nestSeparator, $key, 2);
281  if (strlen($pieces[0]) && strlen($pieces[1])) {
282  if (!isset($config[$pieces[0]])) {
283  if ($pieces[0] === '0' && !empty($config)) {
284  // convert the current values in $config into an array
285  $config = array($pieces[0] => $config);
286  } else {
287  $config[$pieces[0]] = array();
288  }
289  } elseif (!is_array($config[$pieces[0]])) {
293  #require_once 'Zend/Config/Exception.php';
294  throw new Zend_Config_Exception("Cannot create sub-key for '{$pieces[0]}' as key already exists");
295  }
296  $config[$pieces[0]] = $this->_processKey($config[$pieces[0]], $pieces[1], $value);
297  } else {
301  #require_once 'Zend/Config/Exception.php';
302  throw new Zend_Config_Exception("Invalid key '$key'");
303  }
304  } else {
305  $config[$key] = $value;
306  }
307  return $config;
308  }
309 }
$_sectionSeparator
Definition: Ini.php:49
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__construct($filename, $section=null, $options=false)
Definition: Ini.php:101
$config
Definition: fraud_order.php:17
_loadIniFile($filename)
Definition: Ini.php:200
_parseIniFile($filename)
Definition: Ini.php:170
$value
Definition: gender.phtml:16
_processSection($iniArray, $section, $config=array())
Definition: Ini.php:241
_processKey($config, $key, $value)
Definition: Ini.php:277
_assertValidExtend($extendingSection, $extendedSection)
Definition: Config.php:423
_arrayMergeRecursive($firstArray, $secondArray)
Definition: Config.php:464