PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Config/Ini.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 309 lines | 134 code | 23 blank | 152 comment | 29 complexity | 759653655d99fec8a5f4459306e09a2a 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-2011 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 24045 2011-05-23 12:45:11Z rob $
  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-2011 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 three configuration
  80. * directives that may be set. For example:
  81. *
  82. * $options = array(
  83. * 'allowModifications' => false,
  84. * 'nestSeparator' => ':',
  85. * 'skipExtends' => false,
  86. * );
  87. *
  88. * @param string $filename
  89. * @param mixed $section
  90. * @param boolean|array $options
  91. * @throws Zend_Config_Exception
  92. * @return void
  93. */
  94. public function __construct($filename, $section = null, $options = false)
  95. {
  96. if (empty($filename)) {
  97. /**
  98. * @see Zend_Config_Exception
  99. */
  100. require_once 'Zend/Config/Exception.php';
  101. throw new Zend_Config_Exception('Filename is not set');
  102. }
  103. $allowModifications = false;
  104. if (is_bool($options)) {
  105. $allowModifications = $options;
  106. } elseif (is_array($options)) {
  107. if (isset($options['allowModifications'])) {
  108. $allowModifications = (bool) $options['allowModifications'];
  109. }
  110. if (isset($options['nestSeparator'])) {
  111. $this->_nestSeparator = (string) $options['nestSeparator'];
  112. }
  113. if (isset($options['skipExtends'])) {
  114. $this->_skipExtends = (bool) $options['skipExtends'];
  115. }
  116. }
  117. $iniArray = $this->_loadIniFile($filename);
  118. if (null === $section) {
  119. // Load entire file
  120. $dataArray = array();
  121. foreach ($iniArray as $sectionName => $sectionData) {
  122. if(!is_array($sectionData)) {
  123. $dataArray = $this->_arrayMergeRecursive($dataArray, $this->_processKey(array(), $sectionName, $sectionData));
  124. } else {
  125. $dataArray[$sectionName] = $this->_processSection($iniArray, $sectionName);
  126. }
  127. }
  128. parent::__construct($dataArray, $allowModifications);
  129. } else {
  130. // Load one or more sections
  131. if (!is_array($section)) {
  132. $section = array($section);
  133. }
  134. $dataArray = array();
  135. foreach ($section as $sectionName) {
  136. if (!isset($iniArray[$sectionName])) {
  137. /**
  138. * @see Zend_Config_Exception
  139. */
  140. require_once 'Zend/Config/Exception.php';
  141. throw new Zend_Config_Exception("Section '$sectionName' cannot be found in $filename");
  142. }
  143. $dataArray = $this->_arrayMergeRecursive($this->_processSection($iniArray, $sectionName), $dataArray);
  144. }
  145. parent::__construct($dataArray, $allowModifications);
  146. }
  147. $this->_loadedSection = $section;
  148. }
  149. /**
  150. * Load the INI file from disk using parse_ini_file(). Use a private error
  151. * handler to convert any loading errors into a Zend_Config_Exception
  152. *
  153. * @param string $filename
  154. * @throws Zend_Config_Exception
  155. * @return array
  156. */
  157. protected function _parseIniFile($filename)
  158. {
  159. set_error_handler(array($this, '_loadFileErrorHandler'));
  160. $iniArray = parse_ini_file($filename, true); // Warnings and errors are suppressed
  161. restore_error_handler();
  162. // Check if there was a error while loading file
  163. if ($this->_loadFileErrorStr !== null) {
  164. /**
  165. * @see Zend_Config_Exception
  166. */
  167. require_once 'Zend/Config/Exception.php';
  168. throw new Zend_Config_Exception($this->_loadFileErrorStr);
  169. }
  170. return $iniArray;
  171. }
  172. /**
  173. * Load the ini file and preprocess the section separator (':' in the
  174. * section name (that is used for section extension) so that the resultant
  175. * array has the correct section names and the extension information is
  176. * stored in a sub-key called ';extends'. We use ';extends' as this can
  177. * never be a valid key name in an INI file that has been loaded using
  178. * parse_ini_file().
  179. *
  180. * @param string $filename
  181. * @throws Zend_Config_Exception
  182. * @return array
  183. */
  184. protected function _loadIniFile($filename)
  185. {
  186. $loaded = $this->_parseIniFile($filename);
  187. $iniArray = array();
  188. foreach ($loaded as $key => $data)
  189. {
  190. $pieces = explode($this->_sectionSeparator, $key);
  191. $thisSection = trim($pieces[0]);
  192. switch (count($pieces)) {
  193. case 1:
  194. $iniArray[$thisSection] = $data;
  195. break;
  196. case 2:
  197. $extendedSection = trim($pieces[1]);
  198. $iniArray[$thisSection] = array_merge(array(';extends'=>$extendedSection), $data);
  199. break;
  200. default:
  201. /**
  202. * @see Zend_Config_Exception
  203. */
  204. require_once 'Zend/Config/Exception.php';
  205. throw new Zend_Config_Exception("Section '$thisSection' may not extend multiple sections in $filename");
  206. }
  207. }
  208. return $iniArray;
  209. }
  210. /**
  211. * Process each element in the section and handle the ";extends" inheritance
  212. * key. Passes control to _processKey() to handle the nest separator
  213. * sub-property syntax that may be used within the key name.
  214. *
  215. * @param array $iniArray
  216. * @param string $section
  217. * @param array $config
  218. * @throws Zend_Config_Exception
  219. * @return array
  220. */
  221. protected function _processSection($iniArray, $section, $config = array())
  222. {
  223. $thisSection = $iniArray[$section];
  224. foreach ($thisSection as $key => $value) {
  225. if (strtolower($key) == ';extends') {
  226. if (isset($iniArray[$value])) {
  227. $this->_assertValidExtend($section, $value);
  228. if (!$this->_skipExtends) {
  229. $config = $this->_processSection($iniArray, $value, $config);
  230. }
  231. } else {
  232. /**
  233. * @see Zend_Config_Exception
  234. */
  235. require_once 'Zend/Config/Exception.php';
  236. throw new Zend_Config_Exception("Parent section '$section' cannot be found");
  237. }
  238. } else {
  239. $config = $this->_processKey($config, $key, $value);
  240. }
  241. }
  242. return $config;
  243. }
  244. /**
  245. * Assign the key's value to the property list. Handles the
  246. * nest separator for sub-properties.
  247. *
  248. * @param array $config
  249. * @param string $key
  250. * @param string $value
  251. * @throws Zend_Config_Exception
  252. * @return array
  253. */
  254. protected function _processKey($config, $key, $value)
  255. {
  256. if (strpos($key, $this->_nestSeparator) !== false) {
  257. $pieces = explode($this->_nestSeparator, $key, 2);
  258. if (strlen($pieces[0]) && strlen($pieces[1])) {
  259. if (!isset($config[$pieces[0]])) {
  260. if ($pieces[0] === '0' && !empty($config)) {
  261. // convert the current values in $config into an array
  262. $config = array($pieces[0] => $config);
  263. } else {
  264. $config[$pieces[0]] = array();
  265. }
  266. } elseif (!is_array($config[$pieces[0]])) {
  267. /**
  268. * @see Zend_Config_Exception
  269. */
  270. require_once 'Zend/Config/Exception.php';
  271. throw new Zend_Config_Exception("Cannot create sub-key for '{$pieces[0]}' as key already exists");
  272. }
  273. $config[$pieces[0]] = $this->_processKey($config[$pieces[0]], $pieces[1], $value);
  274. } else {
  275. /**
  276. * @see Zend_Config_Exception
  277. */
  278. require_once 'Zend/Config/Exception.php';
  279. throw new Zend_Config_Exception("Invalid key '$key'");
  280. }
  281. } else {
  282. $config[$key] = $value;
  283. }
  284. return $config;
  285. }
  286. }