PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Config/Writer/Ini.php

https://bitbucket.org/Ebozavrik/test-application
PHP | 200 lines | 92 code | 20 blank | 88 comment | 14 complexity | 26c51eaf23ae7256c337c0ffe4900f6f 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-2012 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 24593 2012-01-05 20:35:02Z matthew $
  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-2012 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. *
  50. * @return Zend_Config_Writer_Ini
  51. */
  52. public function setNestSeparator ($separator)
  53. {
  54. $this->_nestSeparator = $separator;
  55. return $this;
  56. }
  57. /**
  58. * Set if rendering should occour without sections or not.
  59. *
  60. * If set to true, the INI file is rendered without sections completely
  61. * into the global namespace of the INI file.
  62. *
  63. * @param bool $withoutSections
  64. *
  65. * @return Zend_Config_Writer_Ini
  66. */
  67. public function setRenderWithoutSections ($withoutSections = true)
  68. {
  69. $this->_renderWithoutSections = (bool)$withoutSections;
  70. return $this;
  71. }
  72. /**
  73. * Render a Zend_Config into a INI config string.
  74. *
  75. * @since 1.10
  76. * @return string
  77. */
  78. public function render ()
  79. {
  80. $iniString = '';
  81. $extends = $this->_config->getExtends();
  82. $sectionName = $this->_config->getSectionName();
  83. if ($this->_renderWithoutSections == true) {
  84. $iniString .= $this->_addBranch($this->_config);
  85. } else if (is_string($sectionName)) {
  86. $iniString .= '[' . $sectionName . ']' . "\n"
  87. . $this->_addBranch($this->_config)
  88. . "\n";
  89. } else {
  90. $config = $this->_sortRootElements($this->_config);
  91. foreach ($config as $sectionName => $data) {
  92. if (!( $data instanceof Zend_Config )) {
  93. $iniString .= $sectionName
  94. . ' = '
  95. . $this->_prepareValue($data)
  96. . "\n";
  97. } else {
  98. if (isset( $extends[$sectionName] )) {
  99. $sectionName .= ' : ' . $extends[$sectionName];
  100. }
  101. $iniString .= '[' . $sectionName . ']' . "\n"
  102. . $this->_addBranch($data)
  103. . "\n";
  104. }
  105. }
  106. }
  107. return $iniString;
  108. }
  109. /**
  110. * Add a branch to an INI string recursively
  111. *
  112. * @param Zend_Config $config
  113. *
  114. * @return void
  115. */
  116. protected function _addBranch (Zend_Config $config, $parents = array())
  117. {
  118. $iniString = '';
  119. foreach ($config as $key => $value) {
  120. $group = array_merge($parents, array( $key ));
  121. if ($value instanceof Zend_Config) {
  122. $iniString .= $this->_addBranch($value, $group);
  123. } else {
  124. $iniString .= implode($this->_nestSeparator, $group)
  125. . ' = '
  126. . $this->_prepareValue($value)
  127. . "\n";
  128. }
  129. }
  130. return $iniString;
  131. }
  132. /**
  133. * Prepare a value for INI
  134. *
  135. * @param mixed $value
  136. *
  137. * @return string
  138. */
  139. protected function _prepareValue ($value)
  140. {
  141. if (is_integer($value) || is_float($value)) {
  142. return $value;
  143. } elseif (is_bool($value)) {
  144. return ( $value ? 'true' : 'false' );
  145. } elseif (strpos($value, '"') === false) {
  146. return '"' . $value . '"';
  147. } else {
  148. /** @see Zend_Config_Exception */
  149. require_once 'Zend/Config/Exception.php';
  150. throw new Zend_Config_Exception( 'Value can not contain double quotes "' );
  151. }
  152. }
  153. /**
  154. * Root elements that are not assigned to any section needs to be
  155. * on the top of config.
  156. *
  157. * @see http://framework.zend.com/issues/browse/ZF-6289
  158. *
  159. * @param Zend_Config
  160. *
  161. * @return Zend_Config
  162. */
  163. protected function _sortRootElements (Zend_Config $config)
  164. {
  165. $configArray = $config->toArray();
  166. $sections = array();
  167. // remove sections from config array
  168. foreach ($configArray as $key => $value) {
  169. if (is_array($value)) {
  170. $sections[$key] = $value;
  171. unset( $configArray[$key] );
  172. }
  173. }
  174. // readd sections to the end
  175. foreach ($sections as $key => $value) {
  176. $configArray[$key] = $value;
  177. }
  178. return new Zend_Config( $configArray );
  179. }
  180. }