|
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
|
|
|
return $this->toJson(); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Converts all properties into a JSON string |
|
188
|
|
|
* @return string json representation of configuration properties |
|
189
|
|
|
*/ |
|
190
|
|
|
public function toJson(): string { |
|
191
|
|
|
return json_encode($this->_properties, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Function will write the current Configuration object to |
|
196
|
|
|
* a specified `$file` with the given `$permissions`. |
|
197
|
|
|
* |
|
198
|
|
|
* @param string $file |
|
199
|
|
|
* the path of the file to write. |
|
200
|
|
|
* @param integer|null $permissions (optional) |
|
201
|
|
|
* the permissions as an octal number to set set on the resulting file. |
|
202
|
|
|
* If this is not provided it will use the permissions defined in [`write_mode`][`file`] |
|
203
|
|
|
* @return boolean |
|
204
|
|
|
*/ |
|
205
|
|
|
public function write(string $file = null, int $permissions = null) |
|
|
|
|
|
|
206
|
|
|
{ |
|
207
|
|
|
$file = $file ?? CONFIG; |
|
|
|
|
|
|
208
|
|
|
|
|
209
|
|
|
if (null == $permissions && isset($this->_properties['file']['write_mode'])) { |
|
|
|
|
|
|
210
|
|
|
$permissions = $this->_properties['file']['write_mode']; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
return General::writeFile($file, (string)$this, $permissions); |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|