PageRenderTime 37ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins_repo/openXWorkflow/www/admin/plugins/openXWorkflow/library/Zend/Config/Ini.php

https://github.com/orchestra-io/sample-openx
PHP | 293 lines | 129 code | 21 blank | 143 comment | 29 complexity | bed51c6b9430118fa089653aaa88b894 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 14667 2009-04-05 09:18:21Z 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-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. * String that separates the parent section name
  41. *
  42. * @var string
  43. */
  44. protected $_sectionSeparator = ':';
  45. /**
  46. * Wether 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 = array_merge_recursive($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 = array_merge($this->_processSection($iniArray, $sectionName), $dataArray);
  143. }
  144. parent::__construct($dataArray, $allowModifications);
  145. }
  146. $this->_loadedSection = $section;
  147. }
  148. /**
  149. * Load the ini file and preprocess the section separator (':' in the
  150. * section name (that is used for section extension) so that the resultant
  151. * array has the correct section names and the extension information is
  152. * stored in a sub-key called ';extends'. We use ';extends' as this can
  153. * never be a valid key name in an INI file that has been loaded using
  154. * parse_ini_file().
  155. *
  156. * @param string $filename
  157. * @throws Zend_Config_Exception
  158. * @return array
  159. */
  160. protected function _loadIniFile($filename)
  161. {
  162. set_error_handler(array($this, '_loadFileErrorHandler'));
  163. $loaded = parse_ini_file($filename, true); // Warnings and errors are suppressed
  164. restore_error_handler();
  165. // Check if there was a error while loading file
  166. if ($this->_loadFileErrorStr !== null) {
  167. /**
  168. * @see Zend_Config_Exception
  169. */
  170. require_once 'Zend/Config/Exception.php';
  171. throw new Zend_Config_Exception($this->_loadFileErrorStr);
  172. }
  173. $iniArray = array();
  174. foreach ($loaded as $key => $data)
  175. {
  176. $pieces = explode($this->_sectionSeparator, $key);
  177. $thisSection = trim($pieces[0]);
  178. switch (count($pieces)) {
  179. case 1:
  180. $iniArray[$thisSection] = $data;
  181. break;
  182. case 2:
  183. $extendedSection = trim($pieces[1]);
  184. $iniArray[$thisSection] = array_merge(array(';extends'=>$extendedSection), $data);
  185. break;
  186. default:
  187. /**
  188. * @see Zend_Config_Exception
  189. */
  190. require_once 'Zend/Config/Exception.php';
  191. throw new Zend_Config_Exception("Section '$thisSection' may not extend multiple sections in $filename");
  192. }
  193. }
  194. return $iniArray;
  195. }
  196. /**
  197. * Process each element in the section and handle the ";extends" inheritance
  198. * key. Passes control to _processKey() to handle the nest separator
  199. * sub-property syntax that may be used within the key name.
  200. *
  201. * @param array $iniArray
  202. * @param string $section
  203. * @param array $config
  204. * @throws Zend_Config_Exception
  205. * @return array
  206. */
  207. protected function _processSection($iniArray, $section, $config = array())
  208. {
  209. $thisSection = $iniArray[$section];
  210. foreach ($thisSection as $key => $value) {
  211. if (strtolower($key) == ';extends') {
  212. if (isset($iniArray[$value])) {
  213. $this->_assertValidExtend($section, $value);
  214. if (!$this->_skipExtends) {
  215. $config = $this->_processSection($iniArray, $value, $config);
  216. }
  217. } else {
  218. /**
  219. * @see Zend_Config_Exception
  220. */
  221. require_once 'Zend/Config/Exception.php';
  222. throw new Zend_Config_Exception("Parent section '$section' cannot be found");
  223. }
  224. } else {
  225. $config = $this->_processKey($config, $key, $value);
  226. }
  227. }
  228. return $config;
  229. }
  230. /**
  231. * Assign the key's value to the property list. Handles the
  232. * nest separator for sub-properties.
  233. *
  234. * @param array $config
  235. * @param string $key
  236. * @param string $value
  237. * @throws Zend_Config_Exception
  238. * @return array
  239. */
  240. protected function _processKey($config, $key, $value)
  241. {
  242. if (strpos($key, $this->_nestSeparator) !== false) {
  243. $pieces = explode($this->_nestSeparator, $key, 2);
  244. if (strlen($pieces[0]) && strlen($pieces[1])) {
  245. if (!isset($config[$pieces[0]])) {
  246. if ($pieces[0] === '0' && !empty($config)) {
  247. // convert the current values in $config into an array
  248. $config = array($pieces[0] => $config);
  249. } else {
  250. $config[$pieces[0]] = array();
  251. }
  252. } elseif (!is_array($config[$pieces[0]])) {
  253. /**
  254. * @see Zend_Config_Exception
  255. */
  256. require_once 'Zend/Config/Exception.php';
  257. throw new Zend_Config_Exception("Cannot create sub-key for '{$pieces[0]}' as key already exists");
  258. }
  259. $config[$pieces[0]] = $this->_processKey($config[$pieces[0]], $pieces[1], $value);
  260. } else {
  261. /**
  262. * @see Zend_Config_Exception
  263. */
  264. require_once 'Zend/Config/Exception.php';
  265. throw new Zend_Config_Exception("Invalid key '$key'");
  266. }
  267. } else {
  268. $config[$key] = $value;
  269. }
  270. return $config;
  271. }
  272. }