Json.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. try {
  121. $config = Zend_Json::decode($json);
  122. } catch (Zend_Json_Exception $e) {
  123. // decode failed
  124. #require_once 'Zend/Config/Exception.php';
  125. throw new Zend_Config_Exception("Error parsing JSON data");
  126. }
  127. if ($section === null) {
  128. $dataArray = array();
  129. foreach ($config as $sectionName => $sectionData) {
  130. $dataArray[$sectionName] = $this->_processExtends($config, $sectionName);
  131. }
  132. parent::__construct($dataArray, $allowModifications);
  133. } elseif (is_array($section)) {
  134. $dataArray = array();
  135. foreach ($section as $sectionName) {
  136. if (!isset($config[$sectionName])) {
  137. #require_once 'Zend/Config/Exception.php';
  138. throw new Zend_Config_Exception(sprintf('Section "%s" cannot be found', $sectionName));
  139. }
  140. $dataArray = array_merge($this->_processExtends($config, $sectionName), $dataArray);
  141. }
  142. parent::__construct($dataArray, $allowModifications);
  143. } else {
  144. if (!isset($config[$section])) {
  145. #require_once 'Zend/Config/Exception.php';
  146. throw new Zend_Config_Exception(sprintf('Section "%s" cannot be found', $section));
  147. }
  148. $dataArray = $this->_processExtends($config, $section);
  149. if (!is_array($dataArray)) {
  150. // Section in the JSON data contains just one top level string
  151. $dataArray = array($section => $dataArray);
  152. }
  153. parent::__construct($dataArray, $allowModifications);
  154. }
  155. $this->_loadedSection = $section;
  156. }
  157. /**
  158. * Helper function to process each element in the section and handle
  159. * the "_extends" inheritance attribute.
  160. *
  161. * @param array $data Data array to process
  162. * @param string $section Section to process
  163. * @param array $config Configuration which was parsed yet
  164. * @throws Zend_Config_Exception When $section cannot be found
  165. * @return array
  166. */
  167. protected function _processExtends(array $data, $section, array $config = array())
  168. {
  169. if (!isset($data[$section])) {
  170. #require_once 'Zend/Config/Exception.php';
  171. throw new Zend_Config_Exception(sprintf('Section "%s" cannot be found', $section));
  172. }
  173. $thisSection = $data[$section];
  174. if (is_array($thisSection) && isset($thisSection[self::EXTENDS_NAME])) {
  175. if (is_array($thisSection[self::EXTENDS_NAME])) {
  176. #require_once 'Zend/Config/Exception.php';
  177. throw new Zend_Config_Exception('Invalid extends clause: must be a string; array received');
  178. }
  179. $this->_assertValidExtend($section, $thisSection[self::EXTENDS_NAME]);
  180. if (!$this->_skipExtends) {
  181. $config = $this->_processExtends($data, $thisSection[self::EXTENDS_NAME], $config);
  182. }
  183. unset($thisSection[self::EXTENDS_NAME]);
  184. }
  185. $config = $this->_arrayMergeRecursive($config, $thisSection);
  186. return $config;
  187. }
  188. /**
  189. * Replace any constants referenced in a string with their values
  190. *
  191. * @param string $value
  192. * @return string
  193. */
  194. protected function _replaceConstants($value)
  195. {
  196. foreach ($this->_getConstants() as $constant) {
  197. if (strstr($value, $constant)) {
  198. // handle backslashes that may represent windows path names for instance
  199. $replacement = str_replace('\\', '\\\\', constant($constant));
  200. $value = str_replace($constant, $replacement, $value);
  201. }
  202. }
  203. return $value;
  204. }
  205. /**
  206. * Get (reverse) sorted list of defined constant names
  207. *
  208. * @return array
  209. */
  210. protected function _getConstants()
  211. {
  212. $constants = array_keys(get_defined_constants());
  213. rsort($constants, SORT_STRING);
  214. return $constants;
  215. }
  216. }