-
Notifications
You must be signed in to change notification settings - Fork 0
/
Config.php
130 lines (104 loc) · 2.36 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
<?php
namespace Modulus\Support;
use Modulus\Support\DEPConfig;
class Config
{
/**
* $all
*
* @var array
*/
public static $all;
/**
* Temporary config
*
* @var array
*/
public static $temp = [];
/**
* Check if setting exists
*
* @param string $key
* @return bool
*/
public static function has(string $key) : bool
{
$expect = explode('.', $key);
$config = Config::all();
foreach($expect as $setting) {
if (!isset($config[$setting])) return false;
$config = $config[$setting];
}
return true;
}
/**
* Get config
*
* @param string $config
* @return mixed $service
*/
public static function get(string $config)
{
if (isset(self::$temp[$config])) return self::$temp[$config];
$conf = explode('.', $config);
$path = DEPConfig::$appdir . 'config' . DIRECTORY_SEPARATOR . $conf[0] . '.php';
if (!file_exists($path)) return null;
$service = require $path;
unset($conf[0]);
foreach($conf as $setting) {
if (isset($service[$setting])) {
$service = $service[$setting];
} else {
return null;
}
}
return $service;
}
/**
* Set temporary config
*
* @param string $name
* @param mixed $value
* @return bool
*/
public static function set(string $name, $value) : bool
{
if (isset(self::$temp[$name])) return false;
return is_array(self::$temp = array_merge(self::$temp, [$name => $value]));
}
/**
* Forget temporary config
*
* @param string $config
* @return bool
*/
public static function forget(string $config) : bool
{
if (array_key_exists($config, self::$temp)) {
unset(self::$temp[$config]);
return true;
}
return false;
}
/**
* Get config files
*
* @param array $appConfig
* @return array $appConfig
*/
public static function all(array $appConfig = []) : array
{
if (Self::$all != null) return Self::$all;
$configs = DEPConfig::$appdir . 'config' . DIRECTORY_SEPARATOR . '*.php';
foreach (\glob($configs) as $config) {
$service = require $config;
if (is_array($service)) {
$path = basename($config);
$name = \substr($path, 0, strlen($path) - 4);
$appConfig = array_merge($appConfig, [$name => $service]);
}
}
Self::$all = $appConfig;
return $appConfig;
}
}