|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package core |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* The Configuration class acts as a property => value store for settings |
|
9
|
|
|
* used throughout Symphony. The result of this class is a string containing |
|
10
|
|
|
* a PHP representation of the properties (and their values) set by the Configuration. |
|
11
|
|
|
* Symphony's configuration file is saved at `CONFIG`. The initial |
|
12
|
|
|
* file is generated by the Symphony installer, and then subsequent use of Symphony |
|
13
|
|
|
* loads in this file for each page view. Like minded properties can be grouped. |
|
14
|
|
|
*/ |
|
15
|
|
|
class Configuration |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* An associative array of the properties for this Configuration object |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
private $_properties = array(); |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Whether all properties and group keys will be forced to be lowercase. |
|
25
|
|
|
* By default this is false, which makes all properties case sensitive |
|
26
|
|
|
* @var boolean |
|
27
|
|
|
*/ |
|
28
|
|
|
private $_forceLowerCase = false; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The string representing the tab characters used to serialize the configuration |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
const TAB = ' '; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The constructor for the Configuration class takes one parameter, |
|
38
|
|
|
* `$forceLowerCase` which will make all property and |
|
39
|
|
|
* group names lowercase or not. By default they are left to the case |
|
40
|
|
|
* the user provides |
|
41
|
|
|
* |
|
42
|
|
|
* @param boolean $forceLowerCase |
|
43
|
|
|
* False by default, if true this will make all property and group names |
|
44
|
|
|
* lowercase |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct($forceLowerCase = false) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->_forceLowerCase = $forceLowerCase; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Setter for the `$this->_properties`. The properties array |
|
53
|
|
|
* can be grouped to be an 'array' of an 'array' of properties. For instance |
|
54
|
|
|
* a 'region' key may be an array of 'properties' (that is name/value), or it |
|
55
|
|
|
* may be a 'value' itself. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $name |
|
58
|
|
|
* The name of the property to set, eg 'timezone' |
|
59
|
|
|
* @param array|string|integer|float|boolean $value |
|
60
|
|
|
* The value for the property to set, eg. '+10:00' |
|
61
|
|
|
* @param string $group |
|
62
|
|
|
* The group for this property, eg. 'region' |
|
63
|
|
|
*/ |
|
64
|
|
|
public function set($name, $value, $group = null) |
|
65
|
|
|
{ |
|
66
|
|
|
if ($this->_forceLowerCase) { |
|
67
|
|
|
$name = strtolower($name); |
|
68
|
|
|
$group = strtolower($group); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if ($group) { |
|
|
|
|
|
|
72
|
|
|
$this->_properties[$group][$name] = $value; |
|
73
|
|
|
} else { |
|
74
|
|
|
$this->_properties[$name] = $value; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* A quick way to set a large number of properties. Given an associative |
|
80
|
|
|
* array or a nested associative array (where the key will be the group), |
|
81
|
|
|
* this function will merge the `$array` with the existing configuration. |
|
82
|
|
|
* By default the given `$array` will overwrite any existing keys unless |
|
83
|
|
|
* the `$overwrite` parameter is passed as false. |
|
84
|
|
|
* |
|
85
|
|
|
* @since Symphony 2.3.2 The `$overwrite` parameter is available |
|
86
|
|
|
* @param array $array |
|
87
|
|
|
* An associative array of properties, 'property' => 'value' or |
|
88
|
|
|
* 'group' => array('property' => 'value') |
|
89
|
|
|
* @param boolean $overwrite |
|
90
|
|
|
* An optional boolean parameter to indicate if it is safe to use array_merge |
|
91
|
|
|
* or if the provided array should be integrated using the 'set()' method |
|
92
|
|
|
* to avoid possible change collision. Defaults to false. |
|
93
|
|
|
*/ |
|
94
|
|
|
public function setArray(array $array, $overwrite = false) |
|
95
|
|
|
{ |
|
96
|
|
|
if ($overwrite) { |
|
97
|
|
|
$this->_properties = array_merge($this->_properties, $array); |
|
98
|
|
|
} else { |
|
99
|
|
|
foreach ($array as $set => $values) { |
|
100
|
|
|
foreach ($values as $key => $val) { |
|
101
|
|
|
self::set($key, $val, $set); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Accessor function for the `$this->_properties`. |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $name |
|
111
|
|
|
* The name of the property to retrieve |
|
112
|
|
|
* @param string $group |
|
113
|
|
|
* The group that this property will be in |
|
114
|
|
|
* @return array|string|integer|float|boolean |
|
115
|
|
|
* If `$name` or `$group` are not |
|
116
|
|
|
* provided this function will return the full `$this->_properties` |
|
117
|
|
|
* array. |
|
118
|
|
|
*/ |
|
119
|
|
|
public function get($name = null, $group = null) |
|
120
|
|
|
{ |
|
121
|
|
|
|
|
122
|
|
|
// Return the whole array if no name or index is requested |
|
123
|
|
|
if (!$name && !$group) { |
|
|
|
|
|
|
124
|
|
|
return $this->_properties; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
if ($this->_forceLowerCase) { |
|
128
|
|
|
$name = strtolower($name); |
|
129
|
|
|
$group = strtolower($group); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
if ($group) { |
|
|
|
|
|
|
133
|
|
|
return (isset($this->_properties[$group][$name]) ? $this->_properties[$group][$name] : null); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return (isset($this->_properties[$name]) ? $this->_properties[$name] : null); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* The remove function will unset a property by `$name`. |
|
141
|
|
|
* It is possible to remove an entire 'group' by passing the group |
|
142
|
|
|
* name as the `$name` |
|
143
|
|
|
* |
|
144
|
|
|
* @param string $name |
|
145
|
|
|
* The name of the property to unset. This can also be the group name |
|
146
|
|
|
* @param string $group |
|
147
|
|
|
* The group of the property to unset |
|
148
|
|
|
*/ |
|
149
|
|
|
public function remove($name, $group = null) |
|
150
|
|
|
{ |
|
151
|
|
|
if ($this->_forceLowerCase) { |
|
152
|
|
|
$name = strtolower($name); |
|
153
|
|
|
$group = strtolower($group); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
if ($group && isset($this->_properties[$group][$name])) { |
|
|
|
|
|
|
157
|
|
|
unset($this->_properties[$group][$name]); |
|
158
|
|
|
} elseif ($this->_properties[$name]) { |
|
159
|
|
|
unset($this->_properties[$name]); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Empties all the Configuration values by setting `$this->_properties` |
|
165
|
|
|
* to an empty array |
|
166
|
|
|
*/ |
|
167
|
|
|
public function flush() |
|
168
|
|
|
{ |
|
169
|
|
|
$this->_properties = array(); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* This magic `__toString` function converts the internal `$this->_properties` |
|
174
|
|
|
* array into a string representation. Symphony generates the `MANIFEST/config.php` |
|
175
|
|
|
* file in this manner. |
|
176
|
|
|
* @see Configuration::serializeArray() |
|
177
|
|
|
* @return string |
|
178
|
|
|
* A string that contains a array representation of `$this->_properties`. |
|
179
|
|
|
* This is used by Symphony to write the `config.php` file. |
|
180
|
|
|
*/ |
|
181
|
|
|
public function __toString() |
|
182
|
|
|
{ |
|
183
|
|
|
$string = 'array('; |
|
184
|
|
|
|
|
185
|
|
|
$tab = static::TAB; |
|
186
|
|
|
|
|
187
|
|
|
foreach ($this->_properties as $group => $data) { |
|
188
|
|
|
$string .= str_repeat(PHP_EOL, 3) . "$tab$tab###### ".strtoupper($group)." ######"; |
|
189
|
|
|
$string .= PHP_EOL . "$tab$tab'$group' => "; |
|
190
|
|
|
|
|
191
|
|
|
$string .= $this->serializeArray($data, 3, $tab); |
|
192
|
|
|
|
|
193
|
|
|
$string .= ","; |
|
194
|
|
|
$string .= PHP_EOL . "$tab$tab########"; |
|
195
|
|
|
} |
|
196
|
|
|
$string .= PHP_EOL . "$tab)"; |
|
197
|
|
|
|
|
198
|
|
|
return $string; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* The `serializeArray` function will properly format and indent multidimensional |
|
203
|
|
|
* arrays using recursivity. |
|
204
|
|
|
* |
|
205
|
|
|
* `__toString()` call `serializeArray` to use the recursive condition. |
|
206
|
|
|
* The keys (int) in array won't have apostrophe. |
|
207
|
|
|
* Array without multidimensional array will be output with normal indentation. |
|
208
|
|
|
* @return string |
|
209
|
|
|
* A string that contains a array representation of the '$data parameter'. |
|
210
|
|
|
* @param array $arr |
|
211
|
|
|
* A array of properties to serialize. |
|
212
|
|
|
* @param integer $indentation |
|
213
|
|
|
* The current level of indentation. |
|
214
|
|
|
* @param string $tab |
|
215
|
|
|
* A horizontal tab |
|
216
|
|
|
*/ |
|
217
|
|
|
|
|
218
|
|
|
protected function serializeArray(array $arr, $indentation = 0, $tab = self::TAB) |
|
219
|
|
|
{ |
|
220
|
|
|
$tabs = ''; |
|
221
|
|
|
$closeTabs = ''; |
|
222
|
|
|
for ($i = 0; $i < $indentation; $i++) { |
|
223
|
|
|
$tabs .= $tab; |
|
224
|
|
|
if ($i < $indentation - 1) { |
|
225
|
|
|
$closeTabs .= $tab; |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
$string = 'array('; |
|
229
|
|
|
foreach ($arr as $key => $value) { |
|
230
|
|
|
$string .= (is_numeric($key) ? PHP_EOL . "$tabs $key => " : PHP_EOL . "$tabs'$key' => "); |
|
231
|
|
|
if (is_array($value)) { |
|
232
|
|
|
if (empty($value)) { |
|
233
|
|
|
$string .= 'array()'; |
|
234
|
|
|
} else { |
|
235
|
|
|
$string .= $this->serializeArray($value, $indentation + 1, $tab); |
|
236
|
|
|
} |
|
237
|
|
|
} else { |
|
238
|
|
|
$string .= (General::strlen($value) > 0 ? var_export($value, true) : 'null'); |
|
239
|
|
|
} |
|
240
|
|
|
$string .= ","; |
|
241
|
|
|
} |
|
242
|
|
|
$string .= PHP_EOL . "$closeTabs)"; |
|
243
|
|
|
return $string; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Function will write the current Configuration object to |
|
248
|
|
|
* a specified `$file` with the given `$permissions`. |
|
249
|
|
|
* |
|
250
|
|
|
* @param string $file |
|
251
|
|
|
* the path of the file to write. |
|
252
|
|
|
* @param integer|null $permissions (optional) |
|
253
|
|
|
* the permissions as an octal number to set set on the resulting file. |
|
254
|
|
|
* If this is not provided it will use the permissions defined in [`write_mode`][`file`] |
|
255
|
|
|
* @return boolean |
|
256
|
|
|
*/ |
|
257
|
|
|
public function write($file = null, $permissions = null) |
|
258
|
|
|
{ |
|
259
|
|
|
if (is_null($permissions) && isset($this->_properties['file']['write_mode'])) { |
|
260
|
|
|
$permissions = $this->_properties['file']['write_mode']; |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
if (is_null($file)) { |
|
264
|
|
|
$file = CONFIG; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
$tab = static::TAB; |
|
268
|
|
|
$eol = PHP_EOL; |
|
269
|
|
|
|
|
270
|
|
|
$string = "<?php$eol$tab\$settings = " . (string)$this . ";$eol"; |
|
271
|
|
|
|
|
272
|
|
|
return General::writeFile($file, $string, $permissions); |
|
273
|
|
|
} |
|
274
|
|
|
} |
|
275
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: