This repository has been archived by the owner on Jan 29, 2020. It is now read-only.
/
Ini.php
183 lines (162 loc) · 4.58 KB
/
Ini.php
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
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Config\Writer;
use Zend\Config\Exception;
class Ini extends AbstractWriter
{
/**
* Separator for 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 nest separator.
*
* @param string $separator
* @return self
*/
public function setNestSeparator($separator)
{
$this->nestSeparator = $separator;
return $this;
}
/**
* Get nest separator.
*
* @return string
*/
public function getNestSeparator()
{
return $this->nestSeparator;
}
/**
* Set if rendering should occur 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 Ini
*/
public function setRenderWithoutSectionsFlags($withoutSections)
{
$this->renderWithoutSections = (bool) $withoutSections;
return $this;
}
/**
* Return whether the writer should render without sections.
*
* @return bool
*/
public function shouldRenderWithoutSections()
{
return $this->renderWithoutSections;
}
/**
* processConfig(): defined by AbstractWriter.
*
* @param array $config
* @return string
*/
public function processConfig(array $config)
{
$iniString = '';
if ($this->shouldRenderWithoutSections()) {
$iniString .= $this->addBranch($config);
} else {
$config = $this->sortRootElements($config);
foreach ($config as $sectionName => $data) {
if (!is_array($data)) {
$iniString .= $sectionName
. ' = '
. $this->prepareValue($data)
. "\n";
} else {
$iniString .= '[' . $sectionName . ']' . "\n"
. $this->addBranch($data)
. "\n";
}
}
}
return $iniString;
}
/**
* Add a branch to an INI string recursively.
*
* @param array $config
* @param array $parents
* @return string
*/
protected function addBranch(array $config, $parents = array())
{
$iniString = '';
foreach ($config as $key => $value) {
$group = array_merge($parents, array($key));
if (is_array($value)) {
$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
* @throws Exception\RuntimeException
*/
protected function prepareValue($value)
{
if (is_integer($value) || is_float($value)) {
return $value;
} elseif (is_bool($value)) {
return ($value ? 'true' : 'false');
} elseif (false === strpos($value, '"')) {
return '"' . $value . '"';
} else {
throw new Exception\RuntimeException('Value can not contain double quotes');
}
}
/**
* Root elements that are not assigned to any section needs to be on the
* top of config.
*
* @param array $config
* @return array
*/
protected function sortRootElements(array $config)
{
$sections = array();
// Remove sections from config array.
foreach ($config as $key => $value) {
if (is_array($value)) {
$sections[$key] = $value;
unset($config[$key]);
}
}
// Read sections to the end.
foreach ($sections as $key => $value) {
$config[$key] = $value;
}
return $config;
}
}