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

https://github.com/MontmereLimited/zf2 · PHP · 198 lines · 90 code · 21 blank · 87 comment · 10 complexity · dce7db2ef0a21031b5d1fa2268b44aea 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. */
  20. namespace Zend\Config\Writer;
  21. use Zend\Config\Exception;
  22. /**
  23. * @category Zend
  24. * @package Zend_Config
  25. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  26. * @license http://framework.zend.com/license/new-bsd New BSD License
  27. */
  28. class Ini extends AbstractWriter
  29. {
  30. /**
  31. * Separator for nesting levels of configuration data identifiers.
  32. *
  33. * @var string
  34. */
  35. protected $nestSeparator = '.';
  36. /**
  37. * If true the INI string is rendered in the global namespace without
  38. * sections.
  39. *
  40. * @var bool
  41. */
  42. protected $renderWithoutSections = false;
  43. /**
  44. * Set nest separator.
  45. *
  46. * @param stirng $separator
  47. * @return self
  48. */
  49. public function setNestSeparator($separator)
  50. {
  51. $this->nestSeparator = $separator;
  52. return $this;
  53. }
  54. /**
  55. * Get nest separator.
  56. *
  57. * @return string
  58. */
  59. public function getNestSeparator()
  60. {
  61. return $this->nestSeparator;
  62. }
  63. /**
  64. * Set if rendering should occour without sections or not.
  65. *
  66. * If set to true, the INI file is rendered without sections completely
  67. * into the global namespace of the INI file.
  68. *
  69. * @param bool $withoutSections
  70. * @return Ini
  71. */
  72. public function setRenderWithoutSectionsFlags($withoutSections)
  73. {
  74. $this->renderWithoutSections = (bool) $withoutSections;
  75. return $this;
  76. }
  77. /**
  78. * Return whether the writer should render without sections.
  79. *
  80. * @return boolean
  81. */
  82. public function shouldRenderWithoutSections()
  83. {
  84. return $this->renderWithoutSections;
  85. }
  86. /**
  87. * processConfig(): defined by AbstractWriter.
  88. *
  89. * @param array $config
  90. * @return string
  91. */
  92. public function processConfig(array $config)
  93. {
  94. $iniString = '';
  95. if ($this->shouldRenderWithoutSections()) {
  96. $iniString .= $this->addBranch($config);
  97. } else {
  98. $config = $this->sortRootElements($config);
  99. foreach ($config as $sectionName => $data) {
  100. if (!is_array($data)) {
  101. $iniString .= $sectionName
  102. . ' = '
  103. . $this->prepareValue($data)
  104. . "\n";
  105. } else {
  106. $iniString .= '[' . $sectionName . ']' . "\n"
  107. . $this->addBranch($data)
  108. . "\n";
  109. }
  110. }
  111. }
  112. return $iniString;
  113. }
  114. /**
  115. * Add a branch to an INI string recursively.
  116. *
  117. * @param array $config
  118. * @return string
  119. */
  120. protected function addBranch(array $config, $parents = array())
  121. {
  122. $iniString = '';
  123. foreach ($config as $key => $value) {
  124. $group = array_merge($parents, array($key));
  125. if (is_array($value)) {
  126. $iniString .= $this->addBranch($value, $group);
  127. } else {
  128. $iniString .= implode($this->nestSeparator, $group)
  129. . ' = '
  130. . $this->prepareValue($value)
  131. . "\n";
  132. }
  133. }
  134. return $iniString;
  135. }
  136. /**
  137. * Prepare a value for INI.
  138. *
  139. * @param mixed $value
  140. * @return string
  141. */
  142. protected function prepareValue($value)
  143. {
  144. if (is_integer($value) || is_float($value)) {
  145. return $value;
  146. } elseif (is_bool($value)) {
  147. return ($value ? 'true' : 'false');
  148. } elseif (false === strpos($value, '"')) {
  149. return '"' . $value . '"';
  150. } else {
  151. throw new Exception\RuntimeException('Value can not contain double quotes');
  152. }
  153. }
  154. /**
  155. * Root elements that are not assigned to any section needs to be on the
  156. * top of config.
  157. *
  158. * @param array $config
  159. * @return array
  160. */
  161. protected function sortRootElements(array $config)
  162. {
  163. $sections = array();
  164. // Remove sections from config array.
  165. foreach ($config as $key => $value) {
  166. if (is_array($value)) {
  167. $sections[$key] = $value;
  168. unset($config[$key]);
  169. }
  170. }
  171. // Read sections to the end.
  172. foreach ($sections as $key => $value) {
  173. $config[$key] = $value;
  174. }
  175. return $config;
  176. }
  177. }