misuzu/src/config.php

37 lines
784 B
PHP
Raw Normal View History

2018-10-04 21:01:17 +02:00
<?php
2019-06-10 19:04:53 +02:00
function config_store(?array $append = null): array {
2019-05-07 10:08:27 +02:00
static $store = [];
if(!is_null($append)) {
$store = array_merge_recursive($store, $append);
}
return $store;
}
2018-10-04 21:01:17 +02:00
2019-06-10 19:04:53 +02:00
function config_load(string $path, bool $isText = false): void {
2018-10-04 21:01:17 +02:00
$config = $isText
? parse_ini_string($path, true, INI_SCANNER_TYPED)
: parse_ini_file($path, true, INI_SCANNER_TYPED);
2019-05-07 10:08:27 +02:00
config_store($config);
2018-10-04 21:01:17 +02:00
}
2019-06-10 19:04:53 +02:00
function config_get(string ...$key) {
2019-05-07 10:08:27 +02:00
$value = config_store();
2018-10-04 21:01:17 +02:00
2019-06-10 19:04:53 +02:00
for($i = 0; $i < count($key); $i++) {
if(empty($value[$key[$i]])) {
return null;
}
$value = $value[$key[$i]];
2018-10-04 21:01:17 +02:00
}
return $value;
}
2019-06-10 19:04:53 +02:00
function config_get_default($default, string ...$key) {
return config_get(...$key) ?? $default;
2018-10-04 21:01:17 +02:00
}