/
Config.php
164 lines (147 loc) · 4.38 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
<?php
declare(strict_types=1);
/*
* This file is part of the Alight package.
*
* (c) June So <june@alight.cc>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alight;
use Exception;
class Config
{
public const FILE = 'config/app.php';
private static array $config = [];
private static array $default = [
'app' => [
'debug' => false, // Whether to enable error message output
'timezone' => null, // Default timezone follows php.ini
'storagePath' => 'storage', // The storage path of the files generated at runtime by framework
'domainLevel' => 2, // Get subdomains for route. For example, set 3 to match 'a' when the domain is like 'a.b.co.jp'
'corsDomain' => null, // Set a default domain array for CORS, or follow 'origin' header when set 'origin'
'corsHeaders' => null, // Set a default header array for CORS
'corsMethods' => null, // Set a default header array for CORS
'cacheAdapter' => null, // Extended cache adapter based on symfony/cache
'errorHandler' => null, // Override error handler
'errorPageHandler' => null, // Override error page handler
],
/*
'route' => 'config/route.php',
'route' => ['config/route/www.php', 'config/route/api.php'],
'route' => [
'*' => 'config/route/www.php',
'api' => ['config/route/api.php', 'config/route/api2.php'],
],
*/
'route' => null,
/*
'database' => [
'type' => 'mysql',
'host' => '127.0.0.1',
'database' => 'alight',
'username' => '',
'password' => '',
],
'database' => [
'main' => [
'type' => 'mysql',
'host' => '127.0.0.1',
'database' => 'alight',
'username' => '',
'password' => '',
],
'remote' => [
'type' => 'mysql',
'host' => '1.1.1.1',
'database' => 'alight_remote',
'username' => '',
'password' => '',
],
],
*/
'database' => [], //More options see https://medoo.in/api/new
/*
'cache' => [
'file' => [
'type' => 'file',
],
'memcached' => [
'type' => 'memcached',
'dsn' => 'memcached://localhost',
],
'redis' => [
'type' => 'redis',
'dsn' => 'redis://localhost',
],
],
*/
'cache' => [
'type' => 'file',
],
/*
'job' => 'config/job.php',
*/
'job' => null
];
/**
* Merge default configuration and user configuration
*
* @return array
* @throws Exception
*/
private static function init()
{
$configFile = App::root(self::FILE);
if (!file_exists($configFile)) {
throw new Exception('Missing configuration file: ' . self::FILE);
}
$userConfig = require $configFile;
return array_replace_recursive(self::$default, $userConfig);
}
/**
* Get config values
*
* @param string[] $keys
* @return mixed
* @throws Exception
*/
public static function get(string ...$keys)
{
if (!self::$config) {
self::$config = self::init();
}
$value = self::$config;
if ($keys) {
foreach ($keys as $key) {
if (isset($value[$key])) {
$value = $value[$key];
} else {
$value = null;
break;
}
}
}
return $value;
}
/**
* Set config values
*
* @param string $class
* @param null|string $option
* @param mixed $value
* @throws Exception
*/
public static function set(string $class, ?string $option, $value)
{
if (!self::$config) {
self::$config = self::init();
}
if ($option) {
self::$config[$class][$option] = $value;
} else {
self::$config[$class] = $value;
}
}
}