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_Json Class Reference
Inheritance diagram for Zend_Config_Json:
Zend_Config

Public Member Functions

 __construct ($json, $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 EXTENDS_NAME = "_extends"
 

Protected Member Functions

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

Protected Attributes

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

Detailed Description

Definition at line 40 of file Json.php.

Constructor & Destructor Documentation

◆ __construct()

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

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

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".

Parameters
string$jsonJSON file or string to process
mixed$sectionSection to process
boolean$optionsWhether modifiacations are allowed at runtime
Exceptions
Zend_Config_ExceptionWhen JSON text is not set or cannot be loaded
Zend_Config_ExceptionWhen section $sectionName cannot be found in $json

Definition at line 82 of file Json.php.

83  {
84  if (empty($json)) {
85  #require_once 'Zend/Config/Exception.php';
86  throw new Zend_Config_Exception('Filename is not set');
87  }
88 
89  $allowModifications = false;
90  if (is_bool($options)) {
91  $allowModifications = $options;
92  } elseif (is_array($options)) {
93  foreach ($options as $key => $value) {
94  switch (strtolower($key)) {
95  case 'allow_modifications':
96  case 'allowmodifications':
97  $allowModifications = (bool) $value;
98  break;
99  case 'skip_extends':
100  case 'skipextends':
101  $this->_skipExtends = (bool) $value;
102  break;
103  case 'ignore_constants':
104  case 'ignoreconstants':
105  $this->_ignoreConstants = (bool) $value;
106  break;
107  default:
108  break;
109  }
110  }
111  }
112 
113  set_error_handler(array($this, '_loadFileErrorHandler')); // Warnings and errors are suppressed
114  if ($json[0] != '{') {
115  $json = file_get_contents($json);
116  }
117  restore_error_handler();
118 
119  // Check if there was a error while loading file
120  if ($this->_loadFileErrorStr !== null) {
121  #require_once 'Zend/Config/Exception.php';
122  throw new Zend_Config_Exception($this->_loadFileErrorStr);
123  }
124 
125  // Replace constants
126  if (!$this->_ignoreConstants) {
127  $json = $this->_replaceConstants($json);
128  }
129 
130  // Parse/decode
131  try {
132  $config = Zend_Json::decode($json);
133  } catch (Zend_Json_Exception $e) {
134  // decode failed
135  #require_once 'Zend/Config/Exception.php';
136  throw new Zend_Config_Exception("Error parsing JSON data");
137  }
138 
139  if ($section === null) {
140  $dataArray = array();
141  foreach ($config as $sectionName => $sectionData) {
142  $dataArray[$sectionName] = $this->_processExtends($config, $sectionName);
143  }
144 
145  parent::__construct($dataArray, $allowModifications);
146  } elseif (is_array($section)) {
147  $dataArray = array();
148  foreach ($section as $sectionName) {
149  if (!isset($config[$sectionName])) {
150  #require_once 'Zend/Config/Exception.php';
151  throw new Zend_Config_Exception(sprintf('Section "%s" cannot be found', $sectionName));
152  }
153 
154  $dataArray = array_merge($this->_processExtends($config, $sectionName), $dataArray);
155  }
156 
157  parent::__construct($dataArray, $allowModifications);
158  } else {
159  if (!isset($config[$section])) {
160  #require_once 'Zend/Config/Exception.php';
161  throw new Zend_Config_Exception(sprintf('Section "%s" cannot be found', $section));
162  }
163 
164  $dataArray = $this->_processExtends($config, $section);
165  if (!is_array($dataArray)) {
166  // Section in the JSON data contains just one top level string
167  $dataArray = array($section => $dataArray);
168  }
169 
170  parent::__construct($dataArray, $allowModifications);
171  }
172 
173  $this->_loadedSection = $section;
174  }
static decode($encodedValue, $objectDecodeType=Zend_Json::TYPE_ARRAY)
Definition: Json.php:74
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$config
Definition: fraud_order.php:17
_replaceConstants($value)
Definition: Json.php:219
_processExtends(array $data, $section, array $config=array())
Definition: Json.php:186
$value
Definition: gender.phtml:16

Member Function Documentation

◆ _getConstants()

_getConstants ( )
protected

Get (reverse) sorted list of defined constant names

Returns
array

Definition at line 236 of file Json.php.

237  {
238  $constants = array_keys(get_defined_constants());
239  rsort($constants, SORT_STRING);
240  return $constants;
241  }

◆ _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
Exceptions
Zend_Config_ExceptionWhen $section cannot be found
Returns
array

Definition at line 186 of file Json.php.

187  {
188  if (!isset($data[$section])) {
189  #require_once 'Zend/Config/Exception.php';
190  throw new Zend_Config_Exception(sprintf('Section "%s" cannot be found', $section));
191  }
192 
193  $thisSection = $data[$section];
194 
195  if (is_array($thisSection) && isset($thisSection[self::EXTENDS_NAME])) {
196  if (is_array($thisSection[self::EXTENDS_NAME])) {
197  #require_once 'Zend/Config/Exception.php';
198  throw new Zend_Config_Exception('Invalid extends clause: must be a string; array received');
199  }
200  $this->_assertValidExtend($section, $thisSection[self::EXTENDS_NAME]);
201 
202  if (!$this->_skipExtends) {
203  $config = $this->_processExtends($data, $thisSection[self::EXTENDS_NAME], $config);
204  }
205  unset($thisSection[self::EXTENDS_NAME]);
206  }
207 
208  $config = $this->_arrayMergeRecursive($config, $thisSection);
209 
210  return $config;
211  }
$config
Definition: fraud_order.php:17
_processExtends(array $data, $section, array $config=array())
Definition: Json.php:186
_assertValidExtend($extendingSection, $extendedSection)
Definition: Config.php:423
_arrayMergeRecursive($firstArray, $secondArray)
Definition: Config.php:464

◆ _replaceConstants()

_replaceConstants (   $value)
protected

Replace any constants referenced in a string with their values

Parameters
string$value
Returns
string

Definition at line 219 of file Json.php.

220  {
221  foreach ($this->_getConstants() as $constant) {
222  if (strstr($value, $constant)) {
223  // handle backslashes that may represent windows path names for instance
224  $replacement = str_replace('\\', '\\\\', constant($constant));
225  $value = str_replace($constant, $replacement, $value);
226  }
227  }
228  return $value;
229  }
$replacement
Definition: website.php:23
$value
Definition: gender.phtml:16

Field Documentation

◆ $_ignoreConstants

$_ignoreConstants = false
protected

Definition at line 55 of file Json.php.

◆ $_skipExtends

$_skipExtends = false
protected

Definition at line 62 of file Json.php.

◆ EXTENDS_NAME

const EXTENDS_NAME = "_extends"

Name of object key indicating section current section extends

Definition at line 45 of file Json.php.


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