PageRenderTime 35ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Config/Ini.php

https://bitbucket.org/baruffaldi/website-2008-computer-shopping-3
PHP | 254 lines | 116 code | 16 blank | 122 comment | 24 complexity | 816c0f16622c7d909427892e257b55ab 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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Ini.php 11206 2008-09-03 14:36:32Z ralph $
  20. */
  21. /**
  22. * @see Zend_Config
  23. */
  24. require_once 'Zend/Config.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Config
  28. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Config_Ini extends Zend_Config
  32. {
  33. /**
  34. * String that separates nesting levels of configuration data identifiers
  35. *
  36. * @var string
  37. */
  38. protected $_nestSeparator = '.';
  39. /**
  40. * Loads the section $section from the config file $filename for
  41. * access facilitated by nested object properties.
  42. *
  43. * If the section name contains a ":" then the section name to the right
  44. * is loaded and included into the properties. Note that the keys in
  45. * this $section will override any keys of the same
  46. * name in the sections that have been included via ":".
  47. *
  48. * If the $section is null, then all sections in the ini file are loaded.
  49. *
  50. * If any key includes a ".", then this will act as a separator to
  51. * create a sub-property.
  52. *
  53. * example ini file:
  54. * [all]
  55. * db.connection = database
  56. * hostname = live
  57. *
  58. * [staging : all]
  59. * hostname = staging
  60. *
  61. * after calling $data = new Zend_Config_Ini($file, 'staging'); then
  62. * $data->hostname === "staging"
  63. * $data->db->connection === "database"
  64. *
  65. * The $options parameter may be provided as either a boolean or an array.
  66. * If provided as a boolean, this sets the $allowModifications option of
  67. * Zend_Config. If provided as an array, there are two configuration
  68. * directives that may be set. For example:
  69. *
  70. * $options = array(
  71. * 'allowModifications' => false,
  72. * 'nestSeparator' => '->'
  73. * );
  74. *
  75. * @param string $filename
  76. * @param string|null $section
  77. * @param boolean|array $options
  78. * @throws Zend_Config_Exception
  79. * @return void
  80. */
  81. public function __construct($filename, $section = null, $options = false)
  82. {
  83. if (empty($filename)) {
  84. /**
  85. * @see Zend_Config_Exception
  86. */
  87. require_once 'Zend/Config/Exception.php';
  88. throw new Zend_Config_Exception('Filename is not set');
  89. }
  90. $allowModifications = false;
  91. if (is_bool($options)) {
  92. $allowModifications = $options;
  93. } elseif (is_array($options)) {
  94. if (isset($options['allowModifications'])) {
  95. $allowModifications = (bool) $options['allowModifications'];
  96. }
  97. if (isset($options['nestSeparator'])) {
  98. $this->_nestSeparator = (string) $options['nestSeparator'];
  99. }
  100. }
  101. set_error_handler(array($this, '_loadFileErrorHandler'));
  102. $iniArray = parse_ini_file($filename, true); // Warnings and errors are suppressed
  103. restore_error_handler();
  104. // Check if there was a error while loading file
  105. if ($this->_loadFileErrorStr !== null) {
  106. /**
  107. * @see Zend_Config_Exception
  108. */
  109. require_once 'Zend/Config/Exception.php';
  110. throw new Zend_Config_Exception($this->_loadFileErrorStr);
  111. }
  112. $preProcessedArray = array();
  113. foreach ($iniArray as $key => $data)
  114. {
  115. $bits = explode(':', $key);
  116. $thisSection = trim($bits[0]);
  117. switch (count($bits)) {
  118. case 1:
  119. $preProcessedArray[$thisSection] = $data;
  120. break;
  121. case 2:
  122. $extendedSection = trim($bits[1]);
  123. $preProcessedArray[$thisSection] = array_merge(array(';extends'=>$extendedSection), $data);
  124. break;
  125. default:
  126. /**
  127. * @see Zend_Config_Exception
  128. */
  129. require_once 'Zend/Config/Exception.php';
  130. throw new Zend_Config_Exception("Section '$thisSection' may not extend multiple sections in $filename");
  131. }
  132. }
  133. if (null === $section) {
  134. $dataArray = array();
  135. foreach ($preProcessedArray as $sectionName => $sectionData) {
  136. if(!is_array($sectionData)) {
  137. $dataArray = array_merge_recursive($dataArray, $this->_processKey(array(), $sectionName, $sectionData));
  138. } else {
  139. $dataArray[$sectionName] = $this->_processExtends($preProcessedArray, $sectionName);
  140. }
  141. }
  142. parent::__construct($dataArray, $allowModifications);
  143. } elseif (is_array($section)) {
  144. $dataArray = array();
  145. foreach ($section as $sectionName) {
  146. if (!isset($preProcessedArray[$sectionName])) {
  147. /**
  148. * @see Zend_Config_Exception
  149. */
  150. require_once 'Zend/Config/Exception.php';
  151. throw new Zend_Config_Exception("Section '$sectionName' cannot be found in $filename");
  152. }
  153. $dataArray = array_merge($this->_processExtends($preProcessedArray, $sectionName), $dataArray);
  154. }
  155. parent::__construct($dataArray, $allowModifications);
  156. } else {
  157. if (!isset($preProcessedArray[$section])) {
  158. /**
  159. * @see Zend_Config_Exception
  160. */
  161. require_once 'Zend/Config/Exception.php';
  162. throw new Zend_Config_Exception("Section '$section' cannot be found in $filename");
  163. }
  164. parent::__construct($this->_processExtends($preProcessedArray, $section), $allowModifications);
  165. }
  166. $this->_loadedSection = $section;
  167. }
  168. /**
  169. * Helper function to process each element in the section and handle
  170. * the "extends" inheritance keyword. Passes control to _processKey()
  171. * to handle the "dot" sub-property syntax in each key.
  172. *
  173. * @param array $iniArray
  174. * @param string $section
  175. * @param array $config
  176. * @throws Zend_Config_Exception
  177. * @return array
  178. */
  179. protected function _processExtends($iniArray, $section, $config = array())
  180. {
  181. $thisSection = $iniArray[$section];
  182. foreach ($thisSection as $key => $value) {
  183. if (strtolower($key) == ';extends') {
  184. if (isset($iniArray[$value])) {
  185. $this->_assertValidExtend($section, $value);
  186. $config = $this->_processExtends($iniArray, $value, $config);
  187. } else {
  188. /**
  189. * @see Zend_Config_Exception
  190. */
  191. require_once 'Zend/Config/Exception.php';
  192. throw new Zend_Config_Exception("Section '$section' cannot be found");
  193. }
  194. } else {
  195. $config = $this->_processKey($config, $key, $value);
  196. }
  197. }
  198. return $config;
  199. }
  200. /**
  201. * Assign the key's value to the property list. Handle the "dot"
  202. * notation for sub-properties by passing control to
  203. * processLevelsInKey().
  204. *
  205. * @param array $config
  206. * @param string $key
  207. * @param string $value
  208. * @throws Zend_Config_Exception
  209. * @return array
  210. */
  211. protected function _processKey($config, $key, $value)
  212. {
  213. if (strpos($key, $this->_nestSeparator) !== false) {
  214. $pieces = explode($this->_nestSeparator, $key, 2);
  215. if (strlen($pieces[0]) && strlen($pieces[1])) {
  216. if (!isset($config[$pieces[0]])) {
  217. $config[$pieces[0]] = array();
  218. } elseif (!is_array($config[$pieces[0]])) {
  219. /**
  220. * @see Zend_Config_Exception
  221. */
  222. require_once 'Zend/Config/Exception.php';
  223. throw new Zend_Config_Exception("Cannot create sub-key for '{$pieces[0]}' as key already exists");
  224. }
  225. $config[$pieces[0]] = $this->_processKey($config[$pieces[0]], $pieces[1], $value);
  226. } else {
  227. /**
  228. * @see Zend_Config_Exception
  229. */
  230. require_once 'Zend/Config/Exception.php';
  231. throw new Zend_Config_Exception("Invalid key '$key'");
  232. }
  233. } else {
  234. $config[$key] = $value;
  235. }
  236. return $config;
  237. }
  238. }