-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.php
324 lines (299 loc) · 8.33 KB
/
Config.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
/**
* @file framework/Config/Config.php
*
* depage config module
*
*
* copyright (c) 2002-2018 Frank Hellenkamp [[email protected]]
*
* @author Frank Hellenkamp [[email protected]]
*/
namespace Depage\Config;
class Config implements \Iterator, \ArrayAccess
{
protected $data = array();
// {{{ constructor
/**
* instatiates config class
*
* @param $options (array) named options for base class
*
* @return null
*/
public function __construct($defaults = []) {
if (!empty($defaults)) {
$this->setConfig($defaults);
}
}
// }}}
// {{{ readConfig
/**
* reads configuration from a file
*
* @param $options (array) named options for base class
*
* @return null
*/
public function readConfig($configFile) {
$values = include $configFile;
if (!isset($_SERVER['HTTP_HOST'])) {
$_SERVER['HTTP_HOST'] = "";
$_SERVER['REQUEST_URI'] = "";
}
// test url against settings
if (php_sapi_name() == 'cli') {
$acturl = DEPAGE_CLI_URL;
} else {
$acturl = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
}
$this->setConfigForUrl($values, $acturl);
}
// }}}
// {{{ setConfigForUrl()
/**
* @brief setConfigForUrl
*
* @param mixed $config, $url
* @return void
**/
public function setConfigForUrl($values, $currentUrl)
{
$depage_base = "";
$urls = array_keys($values);
// sort that shorter urls with same beginning are tested first for a match
// @todo change sort order to have the inherited always at the end
usort($urls, function($a, $b) {
return strlen($a) > strlen($b);
});
// remove url-parameters before matching
list($currentUrl) = explode("?", $currentUrl, 2);
$simplepatterns = self::getSimplePatterns();
foreach ($urls as $url) {
$pattern = "/(" . str_replace(array_keys($simplepatterns), array_values($simplepatterns), $url) . ")/";
if (preg_match($pattern, $currentUrl, $matches)) {
// url fits into pattern
if (isset($values[$url]['base']) && $values[$url]['base'] == "inherit") {
// don't set the base when it is set to "inherit"
} else {
$depage_base = $matches[0];
}
$this->setConfig($values[$url]);
}
}
// set protocol
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != "off") {
$protocol = "https://";
} else {
$protocol = "http://";
}
// set base-url
if (!defined("DEPAGE_BASE")) {
if (!isset($depage_base[0])) {
define("DEPAGE_BASE", "");
} else if ($depage_base[0] != "*") {
define("DEPAGE_BASE", $protocol . $depage_base);
} else {
define("DEPAGE_BASE", $protocol . $_SERVER['HTTP_HOST']);
}
}
}
// }}}
// {{{ setConfig
/**
* sets configuration options as array
*
* @param $options (array) named options for base class
*
* @return null
*/
public function setConfig($values) {
if (is_array($values) || $values instanceof \Iterator) {
foreach ($values as $key => $value) {
if (is_array($value)) {
$this->data[$key] = new self($value);
} else {
$this->data[$key] = $value;
}
}
}
}
// }}}
// {{{ toArray
/**
* returns options as array
*
* @return options as array
*/
public function toArray() {
$data = [];
foreach ($this->data as $key => $value) {
if ($value instanceof self) {
$data[$key] = $value->toArray();
} else {
$data[$key] = $value;
}
}
return $data;
}
// }}}
// {{{ getFromDefaults
/**
* returns options based on defaults as array
*
* @param $defaults (array) default options from class
*
* @return options as array
*/
public function getFromDefaults($defaults) {
$data = array();
if (is_array($defaults) || $values instanceof \Iterator) {
foreach ($defaults as $key => $value) {
if (isset($this->data[$key]) && !is_null($this->data[$key])) {
$data[$key] = $this->data[$key];
} else {
$data[$key] = $value;
}
if (is_array($data[$key])) {
$data[$key] = new self($data[$key]);
}
}
}
return new self($data);
}
// }}}
// {{{ getDefaultsFromClass
/**
* returns options based on defaults as array
*
* @param $object (object) object to get defaults from
*
* @return options as object
*/
public function getDefaultsFromClass($object) {
$data = array();
$defaults = array();
$class = get_class($object);
while ($class) {
// go through class hierarchy for defaults and merge with parent's defaults
$class_vars = get_class_vars($class);
$defaults = array_merge($class_vars['defaults'], $defaults);
$class = get_parent_class($class);
}
return $this->getFromDefaults($defaults);
}
// }}}
// {{{ getSimplePatterns
/**
* returns array of simple patterns
*
* @return array of replacements
*/
static public function getSimplePatterns() {
return array(
"." => "\.", // dot
"/" => "\/", // slash
"?" => "([^\/])", // single character
"**" => "(.+)?", // multiple characters including slash
"*" => "([^\/]*)?", // multiple character without slash
);
}
// }}}
// {{{ __get
/**
* gets a value from configuration
*
* @param $name (string) name of option
*
* @return null
*/
public function __get($name) {
if (array_key_exists($name, $this->data)) {
if (is_array($this->data[$name])) {
return "sub $name";
} else {
return $this->data[$name];
}
}
}
// }}}
// {{{ __set
/**
* gets a value from configuration
*
* @param $name (string) name of option
*
* @return null
*/
public function __set($name, $value) {
// make readonly
if (php_sapi_name() != 'cli') {
error_log("cannot set '$name': config objects are read only");
}
}
// }}}
// {{{ __isset
/**
* checks, if value exists
*
* @param $name (string) name of option
*
* @return null
*/
public function __isset($name) {
return isset($this->data[$name]);
}
// }}}
// {{{ rewind()
public function rewind() {
reset($this->data);
}
// }}}
// {{{ current()
public function current() {
return current($this->data);
}
// }}}
// {{{ key()
public function key() {
return key($this->data);
}
// }}}
// {{{ next()
public function next() {
return next($this->data);
}
// }}}
// {{{ valid()
public function valid() {
return $this->current() !== false;
}
// }}}
// {{{ offsetSet()
public function offsetSet($offset, $value) {
// make readonly
if (php_sapi_name() != 'cli') {
error_log("cannot set '$offset': config objects are read only");
}
}
// }}}
// {{{ offsetExists()
public function offsetExists($offset) {
return isset($this->data[$offset]);
}
// }}}
// {{{ offsetUnset()
public function offsetUnset($offset) {
// make readonly
if (php_sapi_name() != 'cli') {
error_log("cannot unset '$offset': config objects are read only");
}
}
// }}}
// {{{ offsetGet()
public function offsetGet($offset) {
return isset($this->data[$offset]) ? $this->data[$offset] : null;
}
// }}}
}
/* vim:set ft=php sw=4 sts=4 fdm=marker et : */