PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/devtoannh/cafe
PHP | 193 lines | 92 code | 19 blank | 82 comment | 14 complexity | 8d122b79720ae0afc09f946b5a438c24 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-2011 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 23775 2011-03-01 17:25:24Z ralph $
  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-2011 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. $config = $this->_sortRootElements($this->_config);
  89. foreach ($config as $sectionName => $data) {
  90. if (!($data instanceof Zend_Config)) {
  91. $iniString .= $sectionName
  92. . ' = '
  93. . $this->_prepareValue($data)
  94. . "\n";
  95. } else {
  96. if (isset($extends[$sectionName])) {
  97. $sectionName .= ' : ' . $extends[$sectionName];
  98. }
  99. $iniString .= '[' . $sectionName . ']' . "\n"
  100. . $this->_addBranch($data)
  101. . "\n";
  102. }
  103. }
  104. }
  105. return $iniString;
  106. }
  107. /**
  108. * Add a branch to an INI string recursively
  109. *
  110. * @param Zend_Config $config
  111. * @return void
  112. */
  113. protected function _addBranch(Zend_Config $config, $parents = array())
  114. {
  115. $iniString = '';
  116. foreach ($config as $key => $value) {
  117. $group = array_merge($parents, array($key));
  118. if ($value instanceof Zend_Config) {
  119. $iniString .= $this->_addBranch($value, $group);
  120. } else {
  121. $iniString .= implode($this->_nestSeparator, $group)
  122. . ' = '
  123. . $this->_prepareValue($value)
  124. . "\n";
  125. }
  126. }
  127. return $iniString;
  128. }
  129. /**
  130. * Prepare a value for INI
  131. *
  132. * @param mixed $value
  133. * @return string
  134. */
  135. protected function _prepareValue($value)
  136. {
  137. if (is_integer($value) || is_float($value)) {
  138. return $value;
  139. } elseif (is_bool($value)) {
  140. return ($value ? 'true' : 'false');
  141. } elseif (strpos($value, '"') === false) {
  142. return '"' . $value . '"';
  143. } else {
  144. /** @see Zend_Config_Exception */
  145. require_once 'Zend/Config/Exception.php';
  146. throw new Zend_Config_Exception('Value can not contain double quotes "');
  147. }
  148. }
  149. /**
  150. * Root elements that are not assigned to any section needs to be
  151. * on the top of config.
  152. *
  153. * @see http://framework.zend.com/issues/browse/ZF-6289
  154. * @param Zend_Config
  155. * @return Zend_Config
  156. */
  157. protected function _sortRootElements(Zend_Config $config)
  158. {
  159. $configArray = $config->toArray();
  160. $sections = array();
  161. // remove sections from config array
  162. foreach ($configArray as $key => $value) {
  163. if (is_array($value)) {
  164. $sections[$key] = $value;
  165. unset($configArray[$key]);
  166. }
  167. }
  168. // readd sections to the end
  169. foreach ($sections as $key => $value) {
  170. $configArray[$key] = $value;
  171. }
  172. return new Zend_Config($configArray);
  173. }
  174. }