/
Config.php
334 lines (278 loc) · 7.68 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
325
326
327
328
329
330
331
332
333
<?php
/*
* This file is part of the NoFramework package.
*
* (c) Roman Zaykin <roman@noframework.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NoFramework;
class Config
{
/**
* service.pidfile: !local_path service.pid
*
* http.template.path: !start_path template
* http.template.cache: !cache_path template_cache
*/
protected $path = [
'start' => '',
'class' => 'class.php',
'config' => '.config',
'cache' => '.cache',
'local' => '.local',
];
public function __construct($path = [])
{
$this->path = array_filter(array_map(
function ($path) {
if (!$this->isRelativePath($path)) {
return realpath($path);
}
$start = dirname($_SERVER['SCRIPT_FILENAME']);
do {
$start .= DIRECTORY_SEPARATOR;
if (is_dir($start . $path)) {
return realpath($start . $path);
}
$start .= '..';
} while (
strtok(' ' . realpath($start), DIRECTORY_SEPARATOR) and
strtok(DIRECTORY_SEPARATOR)
);
},
$path + $this->path
));
}
public function isRelativePath($path)
{
$path = str_replace(DIRECTORY_SEPARATOR, '/', $path);
return
0 !== strpos($path, '/') and
':' !== substr(strtok($path, '/'), -1)
;
}
public function path($name, $path = '')
{
$prefix = isset($this->path[$name]) ? $this->path[$name] : '';
return
$prefix .
($prefix && $path ? DIRECTORY_SEPARATOR : '') .
$path
;
}
public function parse($source, $type = 'file', $is_object = true)
{
if ('file' === $type and $this->isRelativePath($source)) {
$source = $this->path('config', $source);
}
$resolve = [];
foreach ($this->path as $tag => $ignored) {
$resolve['!' . $tag . '_path'] = function ($value) use ($tag) {
return $this->path($tag, $value);
};
}
foreach (get_class_methods($this) as $method) {
if (0 === strpos($method, '__resolve_')) {
$tag = substr($method, strlen('__resolve_'));
$resolve['!' . $tag] = [$this, $method];
}
}
$parse = [
'file' => 'yaml_parse_file',
'url' => 'yaml_parse_url',
'string' => 'yaml_parse',
][$type];
$out = $parse($source, 0, $ignored, $resolve);
if ($is_object) {
$class = &$out['class'];
unset($out['class']);
$out = $class ? new $class($out) : new Factory($out);
}
gc_collect_cycles();
return $out;
}
public function parseLater($source, $type = 'file', $is_object = true)
{
yield $this->parse($source, $type, $is_object);
}
public function autoload($value = [])
{
$value = is_string($value) ? ['namespace' => $value] : $value;
$namespace = &$value['namespace'];
if (!$namespace or $namespace === __NAMESPACE__) {
if (!class_exists(__NAMESPACE__ . '\Autoload', false)) {
require __DIR__ . '/Autoload.php';
}
return (new Autoload)->register();
}
$path = &$value['path'];
if (!isset($path)) {
$path = str_replace(
array_replace(['separator' => '\\'], $value)['separator'],
DIRECTORY_SEPARATOR,
$namespace
);
}
if ($this->isRelativePath($path)) {
$path = $this->path('class', $path);
}
$class = &$value['class'];
unset($value['class']);
$out = $class ? new $class($value) : new Autoload($value);
return $out->register();
}
/**
* format: 1d 1h 1m 1s
*/
public function period($value)
{
if (is_numeric($value)) {
return $value;
}
preg_match(
'~^((\d+)d)?((\d+)h)?((\d+)m)?((\d+)s)?$~i',
str_replace(' ', '', $value),
$out
);
$out = array_pad($out, 9, null);
return
($out[2] * 24 * 60 * 60) +
($out[4] * 60 * 60) +
($out[6] * 60) +
$out[8]
;
}
/**
* ini_set: !ini_set
* date.timezone: UTC
*/
protected function __resolve_ini_set($value)
{
foreach ($value as $key => $set) {
ini_set($key, $set);
}
return $value;
}
/**
* locale: !locale ru_RU.utf8
*/
protected function __resolve_locale($value)
{
setlocale(LC_ALL, $value);
return $value;
}
/**
* time_limit: !time_limit 1h
*/
protected function __resolve_time_limit($value)
{
set_time_limit($this->period($value));
return $value;
}
/**
* autoload: !autoload
* - NoFramework
* - namespace: Twig
* path: Twig/lib/Twig
* separator: _
* - Example
* - class: Example\Autoload\SomeNonPsr0
*/
protected function __resolve_autoload($value)
{
return
(
is_array($value) and
array_keys($value) === range(0, count($value) - 1)
)
? array_map([$this, 'autoload'], $value)
: $this->autoload($value)
;
}
/**
* error_handler: !error_handler
* - recoverable error
* - user error
* - warning
* - user warning
* - notice
* - user notice
* - strict
* - deprecated
* - user deprecated
*/
protected function __resolve_error_handler($value)
{
return (new ErrorHandler(
$value
? array_reduce($value, function ($result, $item) {
return $result | constant(
'E_' . str_replace(' ', '_', strtoupper($item))
);
}, 0)
: -1
))->register();
}
/**
* timeout: !period 30m
*/
protected function __resolve_period($value)
{
return $this->period($value);
}
/**
* config: !config
*/
protected function __resolve_config($value)
{
return $this;
}
/**
* model: !parse model.yaml
*
* data: !parse
* file: data.yaml
* is_object: false
*/
protected function __resolve_parse($value)
{
$value = is_string($value) ? ['file' => $value] : $value;
$value += [
'is_object' => true,
'is_root' => true,
];
$parsed = $this->parseLater($value['file'], 'file', false);
return
$value['is_object']
? [$value['is_root'] ? '$newRoot' : '$new' => $parsed]
: $parsed
;
}
/**
* db: !new
* class: NoFramework\Database\Mongo
* name: example
* username: example
* password: secret
*/
protected function __resolve_new($value)
{
return ['$new' => $value];
}
/**
* model: !newRoot
* class: NoFramework\Model\Collection
* db: !use global.db
* collection1.fs: !use collection2.fs # store files in common gridfs
*/
protected function __resolve_newRoot($value)
{
return ['$newRoot' => $value];
}
protected function __resolve_use($value)
{
return ['$use' => $value];
}
}