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

https://github.com/orchestra-io/sample-openx · PHP · 212 lines · 102 code · 27 blank · 83 comment · 17 complexity · 85817a60641f1d9201002abe0051242e 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 14175 2009-02-26 22:14:58Z dasprid $
  20. */
  21. /**
  22. * @see Zend_Config_Writer
  23. */
  24. require_once 'Zend/Config/Writer.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_Writer_Ini extends Zend_Config_Writer
  32. {
  33. /**
  34. * Filename to write to
  35. *
  36. * @var string
  37. */
  38. protected $_filename = null;
  39. /**
  40. * Wether to exclusively lock the file or not
  41. *
  42. * @var boolean
  43. */
  44. protected $_exclusiveLock = false;
  45. /**
  46. * String that separates nesting levels of configuration data identifiers
  47. *
  48. * @var string
  49. */
  50. protected $_nestSeparator = '.';
  51. /**
  52. * Set the target filename
  53. *
  54. * @param string $filename
  55. * @return Zend_Config_Writer_Xml
  56. */
  57. public function setFilename($filename)
  58. {
  59. $this->_filename = $filename;
  60. return $this;
  61. }
  62. /**
  63. * Set wether to exclusively lock the file or not
  64. *
  65. * @param boolean $exclusiveLock
  66. * @return Zend_Config_Writer_Array
  67. */
  68. public function setExclusiveLock($exclusiveLock)
  69. {
  70. $this->_exclusiveLock = $exclusiveLock;
  71. return $this;
  72. }
  73. /**
  74. * Set the nest separator
  75. *
  76. * @param string $filename
  77. * @return Zend_Config_Writer_Ini
  78. */
  79. public function setNestSeparator($separator)
  80. {
  81. $this->_nestSeparator = $separator;
  82. return $this;
  83. }
  84. /**
  85. * Defined by Zend_Config_Writer
  86. *
  87. * @param string $filename
  88. * @param Zend_Config $config
  89. * @param boolean $exclusiveLock
  90. * @throws Zend_Config_Exception When filename was not set
  91. * @throws Zend_Config_Exception When filename is not writable
  92. * @return void
  93. */
  94. public function write($filename = null, Zend_Config $config = null, $exclusiveLock = null)
  95. {
  96. if ($filename !== null) {
  97. $this->setFilename($filename);
  98. }
  99. if ($config !== null) {
  100. $this->setConfig($config);
  101. }
  102. if ($exclusiveLock !== null) {
  103. $this->setExclusiveLock($exclusiveLock);
  104. }
  105. if ($this->_filename === null) {
  106. require_once 'Zend/Config/Exception.php';
  107. throw new Zend_Config_Exception('No filename was set');
  108. }
  109. if ($this->_config === null) {
  110. require_once 'Zend/Config/Exception.php';
  111. throw new Zend_Config_Exception('No config was set');
  112. }
  113. $iniString = '';
  114. $extends = $this->_config->getExtends();
  115. $sectionName = $this->_config->getSectionName();
  116. if (is_string($sectionName)) {
  117. $iniString .= '[' . $sectionName . ']' . "\n"
  118. . $this->_addBranch($this->_config)
  119. . "\n";
  120. } else {
  121. foreach ($this->_config as $sectionName => $data) {
  122. if (!($data instanceof Zend_Config)) {
  123. $iniString .= $sectionName
  124. . ' = '
  125. . $this->_prepareValue($data)
  126. . "\n";
  127. } else {
  128. if (isset($extends[$sectionName])) {
  129. $sectionName .= ' : ' . $extends[$sectionName];
  130. }
  131. $iniString .= '[' . $sectionName . ']' . "\n"
  132. . $this->_addBranch($data)
  133. . "\n";
  134. }
  135. }
  136. }
  137. $flags = 0;
  138. if ($this->_exclusiveLock) {
  139. $flags |= LOCK_EX;
  140. }
  141. $result = @file_put_contents($this->_filename, $iniString, $flags);
  142. if ($result === false) {
  143. require_once 'Zend/Config/Exception.php';
  144. throw new Zend_Config_Exception('Could not write to file "' . $this->_filename . '"');
  145. }
  146. }
  147. /**
  148. * Add a branch to an INI string recursively
  149. *
  150. * @param Zend_Config $config
  151. * @return void
  152. */
  153. protected function _addBranch(Zend_Config $config, $parents = array())
  154. {
  155. $iniString = '';
  156. foreach ($config as $key => $value) {
  157. $group = array_merge($parents, array($key));
  158. if ($value instanceof Zend_Config) {
  159. $iniString .= $this->_addBranch($value, $group);
  160. } else {
  161. $iniString .= implode($this->_nestSeparator, $group)
  162. . ' = '
  163. . $this->_prepareValue($value)
  164. . "\n";
  165. }
  166. }
  167. return $iniString;
  168. }
  169. /**
  170. * Prepare a value for INI
  171. *
  172. * @param mixed $value
  173. * @return string
  174. */
  175. protected function _prepareValue($value)
  176. {
  177. if (is_integer($value) || is_float($value)) {
  178. return $value;
  179. } elseif (is_bool($value)) {
  180. return ($value ? 'true' : 'false');
  181. } else {
  182. return '"' . addslashes($value) . '"';
  183. }
  184. }
  185. }