Ini.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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-2009 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_Writer
  23. */
  24. require_once 'Zend/Config/Writer/FileAbstract.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Config
  28. * @copyright Copyright (c) 2005-2009 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_FileAbstract
  32. {
  33. /**
  34. * String that separates nesting levels of configuration data identifiers
  35. *
  36. * @var string
  37. */
  38. protected $_nestSeparator = '.';
  39. /**
  40. * If true the ini string is rendered in the global namespace without sections.
  41. *
  42. * @var bool
  43. */
  44. protected $_renderWithoutSections = false;
  45. /**
  46. * Set the nest separator
  47. *
  48. * @param string $filename
  49. * @return Zend_Config_Writer_Ini
  50. */
  51. public function setNestSeparator($separator)
  52. {
  53. $this->_nestSeparator = $separator;
  54. return $this;
  55. }
  56. /**
  57. * Set if rendering should occour without sections or not.
  58. *
  59. * If set to true, the INI file is rendered without sections completely
  60. * into the global namespace of the INI file.
  61. *
  62. * @param bool $withoutSections
  63. * @return Zend_Config_Writer_Ini
  64. */
  65. public function setRenderWithoutSections($withoutSections=true)
  66. {
  67. $this->_renderWithoutSections = (bool)$withoutSections;
  68. return $this;
  69. }
  70. /**
  71. * Render a Zend_Config into a INI config string.
  72. *
  73. * @since 1.10
  74. * @return string
  75. */
  76. public function render()
  77. {
  78. $iniString = '';
  79. $extends = $this->_config->getExtends();
  80. $sectionName = $this->_config->getSectionName();
  81. if($this->_renderWithoutSections == true) {
  82. $iniString .= $this->_addBranch($this->_config);
  83. } else if (is_string($sectionName)) {
  84. $iniString .= '[' . $sectionName . ']' . "\n"
  85. . $this->_addBranch($this->_config)
  86. . "\n";
  87. } else {
  88. foreach ($this->_config as $sectionName => $data) {
  89. if (!($data instanceof Zend_Config)) {
  90. $iniString .= $sectionName
  91. . ' = '
  92. . $this->_prepareValue($data)
  93. . "\n";
  94. } else {
  95. if (isset($extends[$sectionName])) {
  96. $sectionName .= ' : ' . $extends[$sectionName];
  97. }
  98. $iniString .= '[' . $sectionName . ']' . "\n"
  99. . $this->_addBranch($data)
  100. . "\n";
  101. }
  102. }
  103. }
  104. return $iniString;
  105. }
  106. /**
  107. * Add a branch to an INI string recursively
  108. *
  109. * @param Zend_Config $config
  110. * @return void
  111. */
  112. protected function _addBranch(Zend_Config $config, $parents = array())
  113. {
  114. $iniString = '';
  115. foreach ($config as $key => $value) {
  116. $group = array_merge($parents, array($key));
  117. if ($value instanceof Zend_Config) {
  118. $iniString .= $this->_addBranch($value, $group);
  119. } else {
  120. $iniString .= implode($this->_nestSeparator, $group)
  121. . ' = '
  122. . $this->_prepareValue($value)
  123. . "\n";
  124. }
  125. }
  126. return $iniString;
  127. }
  128. /**
  129. * Prepare a value for INI
  130. *
  131. * @param mixed $value
  132. * @return string
  133. */
  134. protected function _prepareValue($value)
  135. {
  136. if (is_integer($value) || is_float($value)) {
  137. return $value;
  138. } elseif (is_bool($value)) {
  139. return ($value ? 'true' : 'false');
  140. } else {
  141. return '"' . addslashes($value) . '"';
  142. }
  143. }
  144. }