-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathConfig.php
More file actions
151 lines (132 loc) · 4.01 KB
/
Copy pathConfig.php
File metadata and controls
151 lines (132 loc) · 4.01 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
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
<?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 RuntimeException;
class Config
{
public const string 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'
'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' => [],
/*
'job' => 'config/job.php',
*/
'job' => null
];
/**
* Merge default configuration and user configuration
*/
private static function init(): array
{
$configFile = App::root(self::FILE);
if (!file_exists($configFile)) {
throw new RuntimeException('Missing configuration file: ' . self::FILE);
}
$userConfig = require $configFile;
return array_replace_recursive(self::$default, $userConfig);
}
/**
* Get config values
*/
public static function get(string ...$keys): mixed
{
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
*/
public static function set(string $class, ?string $option, mixed $value): void
{
if (!self::$config) {
self::$config = self::init();
}
if ($option) {
self::$config[$class][$option] = $value;
} else {
self::$config[$class] = $value;
}
}
}