PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/taste/zf2
PHP | 193 lines | 92 code | 19 blank | 82 comment | 14 complexity | b9b0f6ac96c182e4947cdec2021412d9 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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * @namespace
  22. */
  23. namespace Zend\Config\Writer;
  24. use Zend\Config;
  25. /**
  26. * @uses \Zend\Config\Exception
  27. * @uses \Zend\Config\Writer\FileAbstract
  28. * @category Zend
  29. * @package Zend_Config
  30. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Ini extends AbstractFileWriter
  34. {
  35. /**
  36. * String that separates nesting levels of configuration data identifiers
  37. *
  38. * @var string
  39. */
  40. protected $_nestSeparator = '.';
  41. /**
  42. * If true the ini string is rendered in the global namespace without sections.
  43. *
  44. * @var bool
  45. */
  46. protected $_renderWithoutSections = false;
  47. /**
  48. * Set the nest separator
  49. *
  50. * @param string $filename
  51. * @return \Zend\Config\Writer\Ini
  52. */
  53. public function setNestSeparator($separator)
  54. {
  55. $this->_nestSeparator = $separator;
  56. return $this;
  57. }
  58. /**
  59. * Set if rendering should occour without sections or not.
  60. *
  61. * If set to true, the INI file is rendered without sections completely
  62. * into the global namespace of the INI file.
  63. *
  64. * @param bool $withoutSections
  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 Config\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 $config
  113. * @return void
  114. */
  115. protected function _addBranch(Config\Config $config, $parents = array())
  116. {
  117. $iniString = '';
  118. foreach ($config as $key => $value) {
  119. $group = array_merge($parents, array($key));
  120. if ($value instanceof Config\Config) {
  121. $iniString .= $this->_addBranch($value, $group);
  122. } else {
  123. $iniString .= implode($this->_nestSeparator, $group)
  124. . ' = '
  125. . $this->_prepareValue($value)
  126. . "\n";
  127. }
  128. }
  129. return $iniString;
  130. }
  131. /**
  132. * Prepare a value for INI
  133. *
  134. * @param mixed $value
  135. * @return string
  136. */
  137. protected function _prepareValue($value)
  138. {
  139. if (is_integer($value) || is_float($value)) {
  140. return $value;
  141. } elseif (is_bool($value)) {
  142. return ($value ? 'true' : 'false');
  143. } elseif (strpos($value, '"') === false) {
  144. return '"' . $value . '"';
  145. } else {
  146. throw new 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 $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\Config($configArray);
  173. }
  174. }