Ini.class.php
5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.Util.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@Util.com so we can send you a copy immediately.
*
* @category Util
* @package Util_Common_Config
* @copyright Copyright (c) 2005-2010 Util Technologies USA Inc. (http://www.Zend.com)
* @license http://framework.Zend.com/license/new-bsd New BSD License
* @version $Id: Ini.class.php 21983 2010-04-25 08:09:09Z jan $
*/
/**
* @see Util_Common_Config_Writer
*/
// require_once 'Util/Config/Writer/FileAbstract.php';
/**
* @category Util
* @package Util_Common_Config
* @copyright Copyright (c) 2005-2010 Util Technologies USA Inc. (http://www.Util.com)
* @license http://framework.Util.com/license/new-bsd New BSD License
*/
class Util_Common_Config_Writer_Ini extends Util_Common_Config_Writer_FileAbstract
{
/**
* String that separates nesting levels of configuration data identifiers
*
* @var string
*/
protected $_nestSeparator = '.';
/**
* If true the ini string is rendered in the global namespace without sections.
*
* @var bool
*/
protected $_renderWithoutSections = false;
/**
* Set the nest separator
*
* @param string $filename
* @return Util_Common_Config_Writer_Ini
*/
public function setNestSeparator($separator)
{
$this->_nestSeparator = $separator;
return $this;
}
/**
* Set if rendering should occour without sections or not.
*
* If set to true, the INI file is rendered without sections completely
* into the global namespace of the INI file.
*
* @param bool $withoutSections
* @return Util_Common_Config_Writer_Ini
*/
public function setRenderWithoutSections($withoutSections=true)
{
$this->_renderWithoutSections = (bool)$withoutSections;
return $this;
}
/**
* Render a Util_Common_Config into a INI config string.
*
* @since 1.10
* @return string
*/
public function render()
{
$iniString = '';
$extends = $this->_config->getExtends();
$sectionName = $this->_config->getSectionName();
if($this->_renderWithoutSections == true) {
$iniString .= $this->_addBranch($this->_config);
} else if (is_string($sectionName)) {
$iniString .= '[' . $sectionName . ']' . "\n"
. $this->_addBranch($this->_config)
. "\n";
} else {
$config = $this->_sortRootElements($this->_config);
foreach ($config as $sectionName => $data) {
if (!($data instanceof Util_Common_Config)) {
$iniString .= $sectionName
. ' = '
. $this->_prepareValue($data)
. "\n";
} else {
if (isset($extends[$sectionName])) {
$sectionName .= ' : ' . $extends[$sectionName];
}
$iniString .= '[' . $sectionName . ']' . "\n"
. $this->_addBranch($data)
. "\n";
}
}
}
return $iniString;
}
/**
* Add a branch to an INI string recursively
*
* @param Util_Common_Config $config
* @return void
*/
protected function _addBranch(Util_Common_Config $config, $parents = array())
{
$iniString = '';
foreach ($config as $key => $value) {
$group = array_merge($parents, array($key));
if ($value instanceof Util_Common_Config) {
$iniString .= $this->_addBranch($value, $group);
} else {
$iniString .= implode($this->_nestSeparator, $group)
. ' = '
. $this->_prepareValue($value)
. "\n";
}
}
return $iniString;
}
/**
* Prepare a value for INI
*
* @param mixed $value
* @return string
*/
protected function _prepareValue($value)
{
if (is_integer($value) || is_float($value)) {
return $value;
} elseif (is_bool($value)) {
return ($value ? 'true' : 'false');
} elseif (strpos($value, '"') === false) {
return '"' . $value . '"';
} else {
/** @see Util_Common_Config_Exception */
// require_once 'Util/Config/Exception.php';
throw new Util_Common_Config_Exception('Value can not contain double quotes "');
}
}
/**
* Root elements that are not assigned to any section needs to be
* on the top of config.
*
* @see http://framework.Util.com/issues/browse/ZF-6289
* @param Util_Common_Config
* @return Util_Common_Config
*/
protected function _sortRootElements(Util_Common_Config $config)
{
$configArray = $config->toArray();
$sections = array();
// remove sections from config array
foreach ($configArray as $key => $value) {
if (is_array($value)) {
$sections[$key] = $value;
unset($configArray[$key]);
}
}
// readd sections to the end
foreach ($sections as $key => $value) {
$configArray[$key] = $value;
}
return new Util_Common_Config($configArray);
}
}