Ini.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. * Set the nest separator
  41. *
  42. * @param string $filename
  43. * @return Zend_Config_Writer_Ini
  44. */
  45. public function setNestSeparator($separator)
  46. {
  47. $this->_nestSeparator = $separator;
  48. return $this;
  49. }
  50. /**
  51. * Render a Zend_Config into a INI config string.
  52. *
  53. * @since 1.10
  54. * @return string
  55. */
  56. public function render()
  57. {
  58. $iniString = '';
  59. $extends = $this->_config->getExtends();
  60. $sectionName = $this->_config->getSectionName();
  61. if (is_string($sectionName)) {
  62. $iniString .= '[' . $sectionName . ']' . "\n"
  63. . $this->_addBranch($this->_config)
  64. . "\n";
  65. } else {
  66. foreach ($this->_config as $sectionName => $data) {
  67. if (!($data instanceof Zend_Config)) {
  68. $iniString .= $sectionName
  69. . ' = '
  70. . $this->_prepareValue($data)
  71. . "\n";
  72. } else {
  73. if (isset($extends[$sectionName])) {
  74. $sectionName .= ' : ' . $extends[$sectionName];
  75. }
  76. $iniString .= '[' . $sectionName . ']' . "\n"
  77. . $this->_addBranch($data)
  78. . "\n";
  79. }
  80. }
  81. }
  82. return $iniString;
  83. }
  84. /**
  85. * Add a branch to an INI string recursively
  86. *
  87. * @param Zend_Config $config
  88. * @return void
  89. */
  90. protected function _addBranch(Zend_Config $config, $parents = array())
  91. {
  92. $iniString = '';
  93. foreach ($config as $key => $value) {
  94. $group = array_merge($parents, array($key));
  95. if ($value instanceof Zend_Config) {
  96. $iniString .= $this->_addBranch($value, $group);
  97. } else {
  98. $iniString .= implode($this->_nestSeparator, $group)
  99. . ' = '
  100. . $this->_prepareValue($value)
  101. . "\n";
  102. }
  103. }
  104. return $iniString;
  105. }
  106. /**
  107. * Prepare a value for INI
  108. *
  109. * @param mixed $value
  110. * @return string
  111. */
  112. protected function _prepareValue($value)
  113. {
  114. if (is_integer($value) || is_float($value)) {
  115. return $value;
  116. } elseif (is_bool($value)) {
  117. return ($value ? 'true' : 'false');
  118. } else {
  119. return '"' . addslashes($value) . '"';
  120. }
  121. }
  122. }