PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Config/Json.php

https://github.com/EvanDotPro/zf1-mirror
PHP | 240 lines | 120 code | 27 blank | 93 comment | 19 complexity | 3b3a6c6867caeeb2c3f53604af724c02 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Config
  17. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Config
  23. */
  24. require_once 'Zend/Config.php';
  25. /**
  26. * @see Zend_Json
  27. */
  28. require_once 'Zend/Json.php';
  29. /**
  30. * JSON Adapter for Zend_Config
  31. *
  32. * @category Zend
  33. * @package Zend_Config
  34. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_Config_Json extends Zend_Config
  38. {
  39. /**
  40. * Name of object key indicating section current section extends
  41. */
  42. const EXTENDS_NAME = "_extends";
  43. /**
  44. * Whether or not to ignore constants in the JSON string
  45. *
  46. * Note: if you do not have constant names in quotations in your JSON
  47. * string, they may lead to syntax errors when parsing.
  48. *
  49. * @var bool
  50. */
  51. protected $_ignoreConstants = false;
  52. /**
  53. * Whether to skip extends or not
  54. *
  55. * @var boolean
  56. */
  57. protected $_skipExtends = false;
  58. /**
  59. * Loads the section $section from the config file encoded as JSON
  60. *
  61. * Sections are defined as properties of the main object
  62. *
  63. * In order to extend another section, a section defines the "_extends"
  64. * property having a value of the section name from which the extending
  65. * section inherits values.
  66. *
  67. * Note that the keys in $section will override any keys of the same
  68. * name in the sections that have been included via "_extends".
  69. *
  70. * @param string $json JSON file or string to process
  71. * @param mixed $section Section to process
  72. * @param boolean $options Whether modifiacations are allowed at runtime
  73. * @throws Zend_Config_Exception When JSON text is not set or cannot be loaded
  74. * @throws Zend_Config_Exception When section $sectionName cannot be found in $json
  75. */
  76. public function __construct($json, $section = null, $options = false)
  77. {
  78. if (empty($json)) {
  79. require_once 'Zend/Config/Exception.php';
  80. throw new Zend_Config_Exception('Filename is not set');
  81. }
  82. $allowModifications = false;
  83. if (is_bool($options)) {
  84. $allowModifications = $options;
  85. } elseif (is_array($options)) {
  86. foreach ($options as $key => $value) {
  87. switch (strtolower($key)) {
  88. case 'allow_modifications':
  89. case 'allowmodifications':
  90. $allowModifications = (bool) $value;
  91. break;
  92. case 'skip_extends':
  93. case 'skipextends':
  94. $this->_skipExtends = (bool) $value;
  95. break;
  96. case 'ignore_constants':
  97. case 'ignoreconstants':
  98. $this->_ignoreConstants = (bool) $value;
  99. break;
  100. default:
  101. break;
  102. }
  103. }
  104. }
  105. set_error_handler(array($this, '_loadFileErrorHandler')); // Warnings and errors are suppressed
  106. if ($json[0] != '{') {
  107. $json = file_get_contents($json);
  108. }
  109. restore_error_handler();
  110. // Check if there was a error while loading file
  111. if ($this->_loadFileErrorStr !== null) {
  112. require_once 'Zend/Config/Exception.php';
  113. throw new Zend_Config_Exception($this->_loadFileErrorStr);
  114. }
  115. // Replace constants
  116. if (!$this->_ignoreConstants) {
  117. $json = $this->_replaceConstants($json);
  118. }
  119. // Parse/decode
  120. $config = Zend_Json::decode($json);
  121. if (null === $config) {
  122. // decode failed
  123. require_once 'Zend/Config/Exception.php';
  124. throw new Zend_Config_Exception("Error parsing JSON data");
  125. }
  126. if ($section === null) {
  127. $dataArray = array();
  128. foreach ($config as $sectionName => $sectionData) {
  129. $dataArray[$sectionName] = $this->_processExtends($config, $sectionName);
  130. }
  131. parent::__construct($dataArray, $allowModifications);
  132. } elseif (is_array($section)) {
  133. $dataArray = array();
  134. foreach ($section as $sectionName) {
  135. if (!isset($config[$sectionName])) {
  136. require_once 'Zend/Config/Exception.php';
  137. throw new Zend_Config_Exception(sprintf('Section "%s" cannot be found', $sectionName));
  138. }
  139. $dataArray = array_merge($this->_processExtends($config, $sectionName), $dataArray);
  140. }
  141. parent::__construct($dataArray, $allowModifications);
  142. } else {
  143. if (!isset($config[$section])) {
  144. require_once 'Zend/Config/Exception.php';
  145. throw new Zend_Config_Exception(sprintf('Section "%s" cannot be found', $section));
  146. }
  147. $dataArray = $this->_processExtends($config, $section);
  148. if (!is_array($dataArray)) {
  149. // Section in the JSON data contains just one top level string
  150. $dataArray = array($section => $dataArray);
  151. }
  152. parent::__construct($dataArray, $allowModifications);
  153. }
  154. $this->_loadedSection = $section;
  155. }
  156. /**
  157. * Helper function to process each element in the section and handle
  158. * the "_extends" inheritance attribute.
  159. *
  160. * @param array $data Data array to process
  161. * @param string $section Section to process
  162. * @param array $config Configuration which was parsed yet
  163. * @throws Zend_Config_Exception When $section cannot be found
  164. * @return array
  165. */
  166. protected function _processExtends(array $data, $section, array $config = array())
  167. {
  168. if (!isset($data[$section])) {
  169. require_once 'Zend/Config/Exception.php';
  170. throw new Zend_Config_Exception(sprintf('Section "%s" cannot be found', $section));
  171. }
  172. $thisSection = $data[$section];
  173. if (is_array($thisSection) && isset($thisSection[self::EXTENDS_NAME])) {
  174. if (is_array($thisSection[self::EXTENDS_NAME])) {
  175. require_once 'Zend/Config/Exception.php';
  176. throw new Zend_Config_Exception('Invalid extends clause: must be a string; array received');
  177. }
  178. $this->_assertValidExtend($section, $thisSection[self::EXTENDS_NAME]);
  179. if (!$this->_skipExtends) {
  180. $config = $this->_processExtends($data, $thisSection[self::EXTENDS_NAME], $config);
  181. }
  182. unset($thisSection[self::EXTENDS_NAME]);
  183. }
  184. $config = $this->_arrayMergeRecursive($config, $thisSection);
  185. return $config;
  186. }
  187. /**
  188. * Replace any constants referenced in a string with their values
  189. *
  190. * @param string $value
  191. * @return string
  192. */
  193. protected function _replaceConstants($value)
  194. {
  195. foreach ($this->_getConstants() as $constant) {
  196. if (strstr($value, $constant)) {
  197. $value = str_replace($constant, constant($constant), $value);
  198. }
  199. }
  200. return $value;
  201. }
  202. /**
  203. * Get (reverse) sorted list of defined constant names
  204. *
  205. * @return array
  206. */
  207. protected function _getConstants()
  208. {
  209. $constants = array_keys(get_defined_constants());
  210. rsort($constants, SORT_STRING);
  211. return $constants;
  212. }
  213. }