-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcfg.php
More file actions
227 lines (209 loc) · 7.62 KB
/
cfg.php
File metadata and controls
227 lines (209 loc) · 7.62 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
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
<?php
function cfg_init(string $cfg_file = null, string $cfg_cache_file = null, bool $reload = false)
{
static $cfg = null;
if ($reload) {
$cfg = null;
}
if ($cfg !== null) {
return $cfg;
}
/* try cache, only when using http */
if (is_string($cfg_cache_file) && is_file($cfg_cache_file) && tool_is_http_request()) {
/* this file is written by cache_config() in config.php */
if (extension_loaded('igbinary')) {
$cfg = igbinary_unserialize(@file_get_contents($cfg_cache_file));
} else {
$cfg = json_decode(@file_get_contents($cfg_cache_file), true);
}
if ($cfg) {
log_verbose('Configuration loaded from cache');
return $cfg;
}
}
/* use default? */
if (!$cfg_file) {
$cfg_file = __DIR__ . '/../../../config/config.yml';
}
/* load base config */
$cfg = tool_yaml_load([$cfg_file, dirname($cfg_file) . '/' . basename($cfg_file, '.yml') . '-local.yml'], false);
if (empty($cfg)) {
$cfg = [];
}
/* setup defaults, if something is not already set in config */
$cfg_default = array(
'setup' => array(
'debug' => false,
'lang' => 'en',
),
'url' => array(
'base' => '/',
'error' => '/404/',
'login' => '/login/',
'assets' => '/',
),
'path' => array(
'root' => realpath(dirname($cfg_file) . '/../'),
'config' => realpath(dirname($cfg_file)),
'models' => 'models',
// 'routes' => 'routes',
// 'views' => 'views',
'cache' => 'cache',
'data' => 'data',
'tmp' => '/tmp',
// 'web' => 'web',
'log' => 'log',
'vendor' => 'vendor',
),
);
$cfg = array_replace_recursive($cfg_default, $cfg);
/* add root to paths that are relative */
foreach ($cfg['path'] as $name => $value) {
/* skip root itself */
if ($name == 'root') {
/* root must always be absolute */
if ($value[0] != '/') {
throw new Exception('Configuration error, root path must be absolute!');
}
continue;
}
/* if there is no slash ('/') as first character, assume this is not an absolute path */
if ($value[0] !== '/') {
/* relative path, prepend with root */
$value = $cfg['path']['root'] . '/' . $value;
}
/* check that each path actually exists */
$path = realpath($value);
if (!$path || !is_dir($path)) {
throw new Exception('Non-accesible path for ' . $name . ' set in config: ' . $value);
}
/* set path value to resolved one */
$cfg['path'][$name] = $path;
}
/* set locale if defined */
if (isset($cfg['setup']['locale']) && is_string($cfg['setup']['locale'])) {
$locale = setlocale(LC_ALL, $cfg['setup']['locale']);
putenv('LC_ALL=' . $locale);
}
/* auto-expand strings: expansion result can be other than string if only singular value is pointed at and it is not a string */
$expand = function (&$c) use (&$expand) {
if (is_array($c)) {
foreach ($c as &$subc) {
$expand($subc);
}
} else if (is_string($c)) {
/* do auto-expansion at most five(5) times */
for ($i = 0; $i < 5 && preg_match_all('/{[a-zA-Z0-9.\\\\]+}/', $c, $matches, PREG_OFFSET_CAPTURE) > 0; $i++) {
$replaced = 0;
$parts = [];
$left = 0;
foreach ($matches[0] as $match) {
$key = trim($match[0], '{}');
$parts[] = substr($c, $left, $match[1] - $left);
/* we are able to call cfg() here since $cfg is set, even though not fully initialized */
$parts[] = cfg($key, $match[0]);
$left = $match[1] + strlen($match[0]);
$replaced++;
}
$left = substr($c, $left);
if ($replaced == 1 && $left == '' && $parts[0] == '' && !is_string($parts[1])) {
/* only singlular replacement and it pointed to non-string value, set directly */
$c = $parts[1];
break;
} else {
/* string value replacement */
$c = implode('', $parts) . $left;
}
}
}
};
$expand($cfg);
/* include autoloader under models */
$file = $cfg['path']['models'] . '/autoload.php';
if (file_exists($file)) {
require_once $file;
}
return $cfg;
}
/**
* Return value from configuration, or null if value with given key-chain
* is not found.
*
* @param mixed $arg1 key or object which is used to find config value under models-section
* @param mixed $arg2 default or key depending on first argument
* @param mixed $arg3 default or ignored depending on first argument
* @return mixed value of the given key (can be array etc)
*/
function cfg($arg1, $arg2 = null, $arg3 = null)
{
$path = null;
$default = null;
/* check arguments */
if (is_string($arg1) || is_array($arg1)) {
$path = is_array($arg1) ? $arg1 : explode('.', $arg1);
$default = $arg2;
} else if (is_object($arg1) && (is_string($arg2) || is_array($arg2))) {
$path = is_array($arg2) ? $arg2 : explode('.', $arg2);
array_unshift($path, 'models', get_class($arg1));
$default = $arg3;
} else {
throw new Exception('invalid cfg() parameter');
}
/* load configuration array */
$value = cfg_init();
/* check for invalid characters (with the exception of accepting "\" and "@", these are from PSR-16 invalid key characters) */
if (strpbrk(implode('.', $path), '{}()/:')) {
$caller = debug_backtrace(0, 2);
log_warning('Not recommended character, one of these "{}()/:", in configuration key: {0} ({1}:{2})', [implode('.', $path), $caller[0]['file'], $caller[0]['line']]);
}
/* find value that was asked */
foreach ($path as $key) {
if (isset($value[$key])) {
/* key found, continue to next key */
$value = $value[$key];
} else {
/* not found */
$value = $default;
break;
}
}
return $value;
}
/**
* Helper to check if we are in debug mode.
*/
function cfg_debug()
{
$cfg = cfg_init();
return $cfg['setup']['debug'] === true;
}
function cfg_system_file(string $type, $postfix = null, string $filename = '', $create = true)
{
if (is_array($postfix)) {
$postfix = implode('/', $postfix);
}
$path = cfg(['path', $type]);
if (!is_string($path)) {
log_error('Tried to use system file with invalid type: ' . $type);
return false;
}
$path = $path . '/__kehikko' . (is_string($postfix) ? '/' . $postfix : '');
if (!file_exists($path)) {
mkdir($path, 0700, true);
}
if (!is_dir($path)) {
log_error('Failed to create system ' . $type . ' (sub-)path: ' . $path);
return false;
}
if (strlen($filename) > 0) {
if ($create && !file_exists($path . '/' . $filename)) {
touch($path . '/' . $filename);
}
if (!is_file($path . '/' . $filename)) {
log_error('System ' . $type . ' file ' . ($create ? 'could not be created' : 'does not exists') . ': ' . $path . '/' . $filename);
return false;
}
return $path . '/' . $filename;
}
return $path;
}