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

Public Member Functions

 getYamlDecoder ()
 
 setYamlDecoder ($yamlDecoder)
 
 __construct ($yaml, $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)
 

Static Public Member Functions

static setIgnoreConstants ($flag)
 
static ignoreConstants ()
 
static decode ($yaml)
 

Data Fields

const EXTENDS_NAME = "_extends"
 

Protected Member Functions

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

Static Protected Member Functions

static _decodeYaml ($currentIndent, &$lines)
 
static _parseValue ($value)
 
static _replaceConstants ($value)
 
static _getConstants ()
 

Protected Attributes

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

Static Protected Attributes

static $_ignoreConstants = false
 

Detailed Description

Definition at line 35 of file Yaml.php.

Constructor & Destructor Documentation

◆ __construct()

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

Loads the section $section from the config file encoded as YAML

Sections are defined as properties of the main object

In order to extend another section, a section defines the "_extends" property 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".

Options may include:

  • allow_modifications: whether or not the config object is mutable
  • skip_extends: whether or not to skip processing of parent configuration
  • yaml_decoder: a callback to use to decode the Yaml source
Parameters
string$yamlYAML file to process
mixed$sectionSection to process
array | boolean$options

Definition at line 131 of file Yaml.php.

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  }
setYamlDecoder($yamlDecoder)
Definition: Yaml.php:99
static ignoreConstants()
Definition: Yaml.php:78
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$config
Definition: fraud_order.php:17
_processExtends(array $data, $section, array $config=array())
Definition: Yaml.php:243
$value
Definition: gender.phtml:16
getYamlDecoder()
Definition: Yaml.php:88
static setIgnoreConstants($flag)
Definition: Yaml.php:68

Member Function Documentation

◆ _decodeYaml()

static _decodeYaml (   $currentIndent,
$lines 
)
staticprotected

Service function to decode YAML

Parameters
int$currentIndentCurrent indent level
array$linesYAML lines
Returns
array|string

Definition at line 288 of file Yaml.php.

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  }
static _decodeYaml($currentIndent, &$lines)
Definition: Yaml.php:288
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$config
Definition: fraud_order.php:17
static _parseValue($value)
Definition: Yaml.php:360
$value
Definition: gender.phtml:16

◆ _getConstants()

static _getConstants ( )
staticprotected

Get (reverse) sorted list of defined constant names

Returns
array

Definition at line 409 of file Yaml.php.

410  {
411  $constants = array_keys(get_defined_constants());
412  rsort($constants, SORT_STRING);
413  return $constants;
414  }

◆ _parseValue()

static _parseValue (   $value)
staticprotected

Parse values

Parameters
string$value
Returns
string

Definition at line 360 of file Yaml.php.

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  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
static _replaceConstants($value)
Definition: Yaml.php:394
$value
Definition: gender.phtml:16

◆ _processExtends()

_processExtends ( array  $data,
  $section,
array  $config = array() 
)
protected

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

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

Definition at line 243 of file Yaml.php.

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  }
$config
Definition: fraud_order.php:17
_processExtends(array $data, $section, array $config=array())
Definition: Yaml.php:243
_assertValidExtend($extendingSection, $extendedSection)
Definition: Config.php:423
_arrayMergeRecursive($firstArray, $secondArray)
Definition: Config.php:464

◆ _replaceConstants()

static _replaceConstants (   $value)
staticprotected

Replace any constants referenced in a string with their values

Parameters
string$value
Returns
string

Definition at line 394 of file Yaml.php.

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  }
$value
Definition: gender.phtml:16

◆ decode()

static decode (   $yaml)
static

Very dumb YAML parser

Until we have Zend_Yaml...

Parameters
string$yamlYAML source
Returns
array Decoded data

Definition at line 274 of file Yaml.php.

275  {
276  $lines = explode("\n", $yaml);
277  reset($lines);
278  return self::_decodeYaml(0, $lines);
279  }
static _decodeYaml($currentIndent, &$lines)
Definition: Yaml.php:288

◆ getYamlDecoder()

getYamlDecoder ( )

Get callback for decoding YAML

Returns
callable

Definition at line 88 of file Yaml.php.

89  {
90  return $this->_yamlDecoder;
91  }

◆ ignoreConstants()

static ignoreConstants ( )
static

Whether parser should ignore constants or not

Returns
bool

Definition at line 78 of file Yaml.php.

79  {
81  }
static $_ignoreConstants
Definition: Yaml.php:60

◆ setIgnoreConstants()

static setIgnoreConstants (   $flag)
static

Indicate whether parser should ignore constants or not

Parameters
bool$flag
Returns
void

Definition at line 68 of file Yaml.php.

69  {
70  self::$_ignoreConstants = (bool) $flag;
71  }

◆ setYamlDecoder()

setYamlDecoder (   $yamlDecoder)

Set callback for decoding YAML

Parameters
callable$yamlDecoderthe decoder to set
Returns
Zend_Config_Yaml

Definition at line 99 of file Yaml.php.

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  }

Field Documentation

◆ $_ignoreConstants

$_ignoreConstants = false
staticprotected

Definition at line 60 of file Yaml.php.

◆ $_skipExtends

$_skipExtends = false
protected

Definition at line 47 of file Yaml.php.

◆ $_yamlDecoder

$_yamlDecoder = array(__CLASS__, 'decode')
protected

Definition at line 54 of file Yaml.php.

◆ EXTENDS_NAME

const EXTENDS_NAME = "_extends"

Attribute name that indicates what section a config extends from

Definition at line 40 of file Yaml.php.


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