Ini.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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-2010 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. * @category Zend
  27. * @package Zend_Config
  28. * @copyright Copyright (c) 2005-2010 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. * String that separates the parent section name
  41. *
  42. * @var string
  43. */
  44. protected $_sectionSeparator = ':';
  45. /**
  46. * Whether to skip extends or not
  47. *
  48. * @var boolean
  49. */
  50. protected $_skipExtends = false;
  51. /**
  52. * Loads the section $section from the config file $filename for
  53. * access facilitated by nested object properties.
  54. *
  55. * If the section name contains a ":" then the section name to the right
  56. * is loaded and included into the properties. Note that the keys in
  57. * this $section will override any keys of the same
  58. * name in the sections that have been included via ":".
  59. *
  60. * If the $section is null, then all sections in the ini file are loaded.
  61. *
  62. * If any key includes a ".", then this will act as a separator to
  63. * create a sub-property.
  64. *
  65. * example ini file:
  66. * [all]
  67. * db.connection = database
  68. * hostname = live
  69. *
  70. * [staging : all]
  71. * hostname = staging
  72. *
  73. * after calling $data = new Zend_Config_Ini($file, 'staging'); then
  74. * $data->hostname === "staging"
  75. * $data->db->connection === "database"
  76. *
  77. * The $options parameter may be provided as either a boolean or an array.
  78. * If provided as a boolean, this sets the $allowModifications option of
  79. * Zend_Config. If provided as an array, there are two configuration
  80. * directives that may be set. For example:
  81. *
  82. * $options = array(
  83. * 'allowModifications' => false,
  84. * 'nestSeparator' => '->'
  85. * );
  86. *
  87. * @param string $filename
  88. * @param string|null $section
  89. * @param boolean|array $options
  90. * @throws Zend_Config_Exception
  91. * @return void
  92. */
  93. public function __construct($filename, $section = null, $options = false)
  94. {
  95. if (empty($filename)) {
  96. /**
  97. * @see Zend_Config_Exception
  98. */
  99. require_once 'Zend/Config/Exception.php';
  100. throw new Zend_Config_Exception('Filename is not set');
  101. }
  102. $allowModifications = false;
  103. if (is_bool($options)) {
  104. $allowModifications = $options;
  105. } elseif (is_array($options)) {
  106. if (isset($options['allowModifications'])) {
  107. $allowModifications = (bool) $options['allowModifications'];
  108. }
  109. if (isset($options['nestSeparator'])) {
  110. $this->_nestSeparator = (string) $options['nestSeparator'];
  111. }
  112. if (isset($options['skipExtends'])) {
  113. $this->_skipExtends = (bool) $options['skipExtends'];
  114. }
  115. }
  116. $iniArray = $this->_loadIniFile($filename);
  117. if (null === $section) {
  118. // Load entire file
  119. $dataArray = array();
  120. foreach ($iniArray as $sectionName => $sectionData) {
  121. if(!is_array($sectionData)) {
  122. $dataArray = $this->_arrayMergeRecursive($dataArray, $this->_processKey(array(), $sectionName, $sectionData));
  123. } else {
  124. $dataArray[$sectionName] = $this->_processSection($iniArray, $sectionName);
  125. }
  126. }
  127. parent::__construct($dataArray, $allowModifications);
  128. } else {
  129. // Load one or more sections
  130. if (!is_array($section)) {
  131. $section = array($section);
  132. }
  133. $dataArray = array();
  134. foreach ($section as $sectionName) {
  135. if (!isset($iniArray[$sectionName])) {
  136. /**
  137. * @see Zend_Config_Exception
  138. */
  139. require_once 'Zend/Config/Exception.php';
  140. throw new Zend_Config_Exception("Section '$sectionName' cannot be found in $filename");
  141. }
  142. $dataArray = $this->_arrayMergeRecursive($this->_processSection($iniArray, $sectionName), $dataArray);
  143. }
  144. parent::__construct($dataArray, $allowModifications);
  145. }
  146. $this->_loadedSection = $section;
  147. }
  148. /**
  149. * Load the INI file from disk using parse_ini_file(). Use a private error
  150. * handler to convert any loading errors into a Zend_Config_Exception
  151. *
  152. * @param string $filename
  153. * @throws Zend_Config_Exception
  154. * @return array
  155. */
  156. protected function _parseIniFile($filename)
  157. {
  158. set_error_handler(array($this, '_loadFileErrorHandler'));
  159. $iniArray = parse_ini_file($filename, true); // Warnings and errors are suppressed
  160. restore_error_handler();
  161. // Check if there was a error while loading file
  162. if ($this->_loadFileErrorStr !== null) {
  163. /**
  164. * @see Zend_Config_Exception
  165. */
  166. require_once 'Zend/Config/Exception.php';
  167. throw new Zend_Config_Exception($this->_loadFileErrorStr);
  168. }
  169. return $iniArray;
  170. }
  171. /**
  172. * Load the ini file and preprocess the section separator (':' in the
  173. * section name (that is used for section extension) so that the resultant
  174. * array has the correct section names and the extension information is
  175. * stored in a sub-key called ';extends'. We use ';extends' as this can
  176. * never be a valid key name in an INI file that has been loaded using
  177. * parse_ini_file().
  178. *
  179. * @param string $filename
  180. * @throws Zend_Config_Exception
  181. * @return array
  182. */
  183. protected function _loadIniFile($filename)
  184. {
  185. $loaded = $this->_parseIniFile($filename);
  186. $iniArray = array();
  187. foreach ($loaded as $key => $data)
  188. {
  189. $pieces = explode($this->_sectionSeparator, $key);
  190. $thisSection = trim($pieces[0]);
  191. switch (count($pieces)) {
  192. case 1:
  193. $iniArray[$thisSection] = $data;
  194. break;
  195. case 2:
  196. $extendedSection = trim($pieces[1]);
  197. $iniArray[$thisSection] = array_merge(array(';extends'=>$extendedSection), $data);
  198. break;
  199. default:
  200. /**
  201. * @see Zend_Config_Exception
  202. */
  203. require_once 'Zend/Config/Exception.php';
  204. throw new Zend_Config_Exception("Section '$thisSection' may not extend multiple sections in $filename");
  205. }
  206. }
  207. return $iniArray;
  208. }
  209. /**
  210. * Process each element in the section and handle the ";extends" inheritance
  211. * key. Passes control to _processKey() to handle the nest separator
  212. * sub-property syntax that may be used within the key name.
  213. *
  214. * @param array $iniArray
  215. * @param string $section
  216. * @param array $config
  217. * @throws Zend_Config_Exception
  218. * @return array
  219. */
  220. protected function _processSection($iniArray, $section, $config = array())
  221. {
  222. $thisSection = $iniArray[$section];
  223. foreach ($thisSection as $key => $value) {
  224. if (strtolower($key) == ';extends') {
  225. if (isset($iniArray[$value])) {
  226. $this->_assertValidExtend($section, $value);
  227. if (!$this->_skipExtends) {
  228. $config = $this->_processSection($iniArray, $value, $config);
  229. }
  230. } else {
  231. /**
  232. * @see Zend_Config_Exception
  233. */
  234. require_once 'Zend/Config/Exception.php';
  235. throw new Zend_Config_Exception("Parent section '$section' cannot be found");
  236. }
  237. } else {
  238. $config = $this->_processKey($config, $key, $value);
  239. }
  240. }
  241. return $config;
  242. }
  243. /**
  244. * Assign the key's value to the property list. Handles the
  245. * nest separator for sub-properties.
  246. *
  247. * @param array $config
  248. * @param string $key
  249. * @param string $value
  250. * @throws Zend_Config_Exception
  251. * @return array
  252. */
  253. protected function _processKey($config, $key, $value)
  254. {
  255. if (strpos($key, $this->_nestSeparator) !== false) {
  256. $pieces = explode($this->_nestSeparator, $key, 2);
  257. if (strlen($pieces[0]) && strlen($pieces[1])) {
  258. if (!isset($config[$pieces[0]])) {
  259. if ($pieces[0] === '0' && !empty($config)) {
  260. // convert the current values in $config into an array
  261. $config = array($pieces[0] => $config);
  262. } else {
  263. $config[$pieces[0]] = array();
  264. }
  265. } elseif (!is_array($config[$pieces[0]])) {
  266. /**
  267. * @see Zend_Config_Exception
  268. */
  269. require_once 'Zend/Config/Exception.php';
  270. throw new Zend_Config_Exception("Cannot create sub-key for '{$pieces[0]}' as key already exists");
  271. }
  272. $config[$pieces[0]] = $this->_processKey($config[$pieces[0]], $pieces[1], $value);
  273. } else {
  274. /**
  275. * @see Zend_Config_Exception
  276. */
  277. require_once 'Zend/Config/Exception.php';
  278. throw new Zend_Config_Exception("Invalid key '$key'");
  279. }
  280. } else {
  281. $config[$key] = $value;
  282. }
  283. return $config;
  284. }
  285. }