forked from leafsphp/leaf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.php
More file actions
90 lines (79 loc) · 2.21 KB
/
Config.php
File metadata and controls
90 lines (79 loc) · 2.21 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
<?php
declare(strict_types=1);
namespace Leaf;
/**
* Leaf Config
* -------------
* Configure your leaf app
*/
class Config
{
protected static $settings = [
'app' => ['down' => false, 'instance' => null],
'mode' => 'development',
'debug' => true,
'log' => [
'writer' => null,
'level' => null,
'enabled' => false,
'dir' => __DIR__ . '/../../../../storage/logs/',
'file' => 'log.txt',
'open' => true,
],
'http' => ['version' => '1.1'],
'views' => ['path' => null, 'cachePath' => null],
];
/**
* Set configuration value(s)
*
* @param string|array $item The config(s) to set
* @param mixed $value The value for config. Ignored if $item is an array.
*/
public static function set($item, $value = null)
{
if (is_string($item)) {
if (!strpos($item, '.')) {
static::$settings[$item] = $value;
} else {
static::$settings = array_merge(
static::$settings,
static::mapConfig($item, $value)
);
}
} else {
foreach ($item as $k => $v) {
static::set($k, $v);
}
}
}
/**
* Map nested config to their parents recursively
*/
protected static function mapConfig(string $item, $value = null)
{
$config = explode('.', $item);
if (count($config) > 2) {
trigger_error('Nested config can\'t be more than 1 level deep');
}
return [$config[0] => array_merge(
static::$settings[$config[0]] ?? [],
[$config[1] => $value]
)];
}
/**
* Get configuration
*
* @param string|null $item The config to get. Returns all items if nothing is specified.
*/
public static function get($item = null)
{
if ($item) {
$items = explode('.', $item);
if (count($items) > 1) {
return static::$settings[$items[0]][$items[1]] ?? null;
}
return static::$settings[$item] ?? null;
}
return static::$settings;
}
}