Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Yaml.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Config.php';
26 
36 {
40  const EXTENDS_NAME = "_extends";
41 
47  protected $_skipExtends = false;
48 
54  protected $_yamlDecoder = array(__CLASS__, 'decode');
55 
60  protected static $_ignoreConstants = false;
61 
68  public static function setIgnoreConstants($flag)
69  {
70  self::$_ignoreConstants = (bool) $flag;
71  }
72 
78  public static function ignoreConstants()
79  {
81  }
82 
88  public function getYamlDecoder()
89  {
90  return $this->_yamlDecoder;
91  }
92 
99  public function setYamlDecoder($yamlDecoder)
100  {
101  if (!is_callable($yamlDecoder)) {
102  #require_once 'Zend/Config/Exception.php';
103  throw new Zend_Config_Exception('Invalid parameter to setYamlDecoder() - must be callable');
104  }
105 
106  $this->_yamlDecoder = $yamlDecoder;
107  return $this;
108  }
109 
131  public function __construct($yaml, $section = null, $options = false)
132  {
133  if (empty($yaml)) {
134  #require_once 'Zend/Config/Exception.php';
135  throw new Zend_Config_Exception('Filename is not set');
136  }
137 
138  $ignoreConstants = $staticIgnoreConstants = self::ignoreConstants();
139  $allowModifications = false;
140  if (is_bool($options)) {
141  $allowModifications = $options;
142  } elseif (is_array($options)) {
143  foreach ($options as $key => $value) {
144  switch (strtolower($key)) {
145  case 'allow_modifications':
146  case 'allowmodifications':
147  $allowModifications = (bool) $value;
148  break;
149  case 'skip_extends':
150  case 'skipextends':
151  $this->_skipExtends = (bool) $value;
152  break;
153  case 'ignore_constants':
154  case 'ignoreconstants':
155  $ignoreConstants = (bool) $value;
156  break;
157  case 'yaml_decoder':
158  case 'yamldecoder':
159  $this->setYamlDecoder($value);
160  break;
161  default:
162  break;
163  }
164  }
165  }
166 
167  // Suppress warnings and errors while loading file
168  set_error_handler(array($this, '_loadFileErrorHandler'));
169  $yaml = file_get_contents($yaml);
170  restore_error_handler();
171 
172  // Check if there was a error while loading file
173  if ($this->_loadFileErrorStr !== null) {
174  #require_once 'Zend/Config/Exception.php';
175  throw new Zend_Config_Exception($this->_loadFileErrorStr);
176  }
177 
178  // Override static value for ignore_constants if provided in $options
179  self::setIgnoreConstants($ignoreConstants);
180 
181  // Parse YAML
182  $config = call_user_func($this->getYamlDecoder(), $yaml);
183 
184  // Reset original static state of ignore_constants
185  self::setIgnoreConstants($staticIgnoreConstants);
186 
187  if (null === $config) {
188  // decode failed
189  #require_once 'Zend/Config/Exception.php';
190  throw new Zend_Config_Exception("Error parsing YAML data");
191  }
192 
193  if (null === $section) {
194  $dataArray = array();
195  foreach ($config as $sectionName => $sectionData) {
196  $dataArray[$sectionName] = $this->_processExtends($config, $sectionName);
197  }
198  parent::__construct($dataArray, $allowModifications);
199  } elseif (is_array($section)) {
200  $dataArray = array();
201  foreach ($section as $sectionName) {
202  if (!isset($config[$sectionName])) {
203  #require_once 'Zend/Config/Exception.php';
204  throw new Zend_Config_Exception(sprintf(
205  'Section "%s" cannot be found',
206  implode(' ', (array)$section)
207  ));
208  }
209 
210  $dataArray = array_merge($this->_processExtends($config, $sectionName), $dataArray);
211  }
212  parent::__construct($dataArray, $allowModifications);
213  } else {
214  if (!isset($config[$section])) {
215  #require_once 'Zend/Config/Exception.php';
216  throw new Zend_Config_Exception(sprintf(
217  'Section "%s" cannot be found',
218  implode(' ', (array)$section)
219  ));
220  }
221 
222  $dataArray = $this->_processExtends($config, $section);
223  if (!is_array($dataArray)) {
224  // Section in the yaml data contains just one top level string
225  $dataArray = array($section => $dataArray);
226  }
227  parent::__construct($dataArray, $allowModifications);
228  }
229 
230  $this->_loadedSection = $section;
231  }
232 
243  protected function _processExtends(array $data, $section, array $config = array())
244  {
245  if (!isset($data[$section])) {
246  #require_once 'Zend/Config/Exception.php';
247  throw new Zend_Config_Exception(sprintf('Section "%s" cannot be found', $section));
248  }
249 
250  $thisSection = $data[$section];
251 
252  if (is_array($thisSection) && isset($thisSection[self::EXTENDS_NAME])) {
253  $this->_assertValidExtend($section, $thisSection[self::EXTENDS_NAME]);
254 
255  if (!$this->_skipExtends) {
256  $config = $this->_processExtends($data, $thisSection[self::EXTENDS_NAME], $config);
257  }
258  unset($thisSection[self::EXTENDS_NAME]);
259  }
260 
261  $config = $this->_arrayMergeRecursive($config, $thisSection);
262 
263  return $config;
264  }
265 
274  public static function decode($yaml)
275  {
276  $lines = explode("\n", $yaml);
277  reset($lines);
278  return self::_decodeYaml(0, $lines);
279  }
280 
288  protected static function _decodeYaml($currentIndent, &$lines)
289  {
290  $config = array();
291  $inIndent = false;
292  foreach ($line as $n => $line) {
293  $lineno = $n + 1;
294 
295  $line = rtrim(preg_replace("/#.*$/", "", $line));
296  if (strlen($line) == 0) {
297  continue;
298  }
299 
300  $indent = strspn($line, " ");
301 
302  // line without the spaces
303  $line = trim($line);
304  if (strlen($line) == 0) {
305  continue;
306  }
307 
308  if ($indent < $currentIndent) {
309  // this level is done
310  prev($lines);
311  return $config;
312  }
313 
314  if (!$inIndent) {
315  $currentIndent = $indent;
316  $inIndent = true;
317  }
318 
319  if (preg_match("/(?!-)([\w\-]+):\s*(.*)/", $line, $m)) {
320  // key: value
321  if (strlen($m[2])) {
322  // simple key: value
323  $value = preg_replace("/#.*$/", "", $m[2]);
325  } else {
326  // key: and then values on new lines
327  $value = self::_decodeYaml($currentIndent + 1, $lines);
328  if (is_array($value) && !count($value)) {
329  $value = "";
330  }
331  }
332  $config[$m[1]] = $value;
333  } elseif ($line[0] == "-") {
334  // item in the list:
335  // - FOO
336  if (strlen($line) > 2) {
337  $value = substr($line, 2);
338 
340  } else {
341  $config[] = self::_decodeYaml($currentIndent + 1, $lines);
342  }
343  } else {
344  #require_once 'Zend/Config/Exception.php';
345  throw new Zend_Config_Exception(sprintf(
346  'Error parsing YAML at line %d - unsupported syntax: "%s"',
347  $lineno, $line
348  ));
349  }
350  }
351  return $config;
352  }
353 
360  protected static function _parseValue($value)
361  {
362  $value = trim($value);
363 
364  // remove quotes from string.
365  if ('"' == substr($value, 0, 1)) {
366  if ('"' == substr($value, -1)) {
367  $value = substr($value, 1, -1);
368  }
369  } elseif ('\'' == substr($value, 0, 1) && '\'' == substr($value, -1)) {
370  $value = strtr($value, array("''" => "'", "'" => ''));
371  }
372 
373  // Check for booleans and constants
374  if (preg_match('/^(t(rue)?|on|y(es)?)$/i', $value)) {
375  $value = true;
376  } elseif (preg_match('/^(f(alse)?|off|n(o)?)$/i', $value)) {
377  $value = false;
378  } elseif (strcasecmp($value, 'null') === 0) {
379  $value = null;
380  } elseif (!self::$_ignoreConstants) {
381  // test for constants
383  }
384 
385  return $value;
386  }
387 
394  protected static function _replaceConstants($value)
395  {
396  foreach (self::_getConstants() as $constant) {
397  if (strstr($value, $constant)) {
398  $value = str_replace($constant, constant($constant), $value);
399  }
400  }
401  return $value;
402  }
403 
409  protected static function _getConstants()
410  {
411  $constants = array_keys(get_defined_constants());
412  rsort($constants, SORT_STRING);
413  return $constants;
414  }
415 }
__construct($yaml, $section=null, $options=false)
Definition: Yaml.php:131
setYamlDecoder($yamlDecoder)
Definition: Yaml.php:99
static ignoreConstants()
Definition: Yaml.php:78
static _decodeYaml($currentIndent, &$lines)
Definition: Yaml.php:288
static decode($yaml)
Definition: Yaml.php:274
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$config
Definition: fraud_order.php:17
static _replaceConstants($value)
Definition: Yaml.php:394
_processExtends(array $data, $section, array $config=array())
Definition: Yaml.php:243
static _parseValue($value)
Definition: Yaml.php:360
static _getConstants()
Definition: Yaml.php:409
$value
Definition: gender.phtml:16
getYamlDecoder()
Definition: Yaml.php:88
const EXTENDS_NAME
Definition: Yaml.php:40
static $_ignoreConstants
Definition: Yaml.php:60
static setIgnoreConstants($flag)
Definition: Yaml.php:68
_assertValidExtend($extendingSection, $extendedSection)
Definition: Config.php:423
_arrayMergeRecursive($firstArray, $secondArray)
Definition: Config.php:464