Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Json.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Config.php';
26 
30 #require_once 'Zend/Json.php';
31 
41 {
45  const EXTENDS_NAME = "_extends";
46 
55  protected $_ignoreConstants = false;
56 
62  protected $_skipExtends = false;
63 
82  public function __construct($json, $section = null, $options = false)
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  }
175 
186  protected function _processExtends(array $data, $section, array $config = array())
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  }
212 
219  protected function _replaceConstants($value)
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  }
230 
236  protected function _getConstants()
237  {
238  $constants = array_keys(get_defined_constants());
239  rsort($constants, SORT_STRING);
240  return $constants;
241  }
242 }
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
$replacement
Definition: website.php:23
_processExtends(array $data, $section, array $config=array())
Definition: Json.php:186
$value
Definition: gender.phtml:16
const EXTENDS_NAME
Definition: Json.php:45
_assertValidExtend($extendingSection, $extendedSection)
Definition: Config.php:423
_arrayMergeRecursive($firstArray, $secondArray)
Definition: Config.php:464
__construct($json, $section=null, $options=false)
Definition: Json.php:82