-
-
Notifications
You must be signed in to change notification settings - Fork 315
Expand file tree
/
Copy pathConfig.php
More file actions
189 lines (163 loc) · 5.61 KB
/
Copy pathConfig.php
File metadata and controls
189 lines (163 loc) · 5.61 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/**
* Provides static methods to get and set configuration values from the `core/config.php` file.
*
* @package NamelessMC\Core
* @author Samerton
* @version 2.0.0
* @license MIT
*/
class Config
{
private static ?array $_config_cache = null;
/**
* @return bool Whether `/core` folder is writable to create `config.php` file in,
* or if the file exists and is writable.
*/
public static function writeable(): bool
{
clearstatcache();
if (self::exists()) {
return is_writable(ROOT_PATH . '/core/config.php');
}
return is_writable(ROOT_PATH . '/core');
}
/**
* @return bool Whether config file exists
*/
public static function exists(): bool
{
return file_exists(ROOT_PATH . '/core/config.php');
}
/**
* Read `core/config.php` file and load into cache.
*
* @return array The entire config array
*/
public static function all(): array
{
if (self::$_config_cache !== null) {
return self::$_config_cache;
}
if (!self::exists()) {
throw new RuntimeException('Config file does not exist. If this happened during installation, please restart the installation in a new private/incognito browser window.');
}
return self::$_config_cache = require(ROOT_PATH . '/core/config.php');
}
/**
* Overwrite new `core/config.php` file.
*
* @param array $config New config array to store.
*/
public static function write(array $config): void
{
$contents = '<?php' . PHP_EOL . PHP_EOL . 'return ' . self::arrayToString($config) . ';';
if (file_put_contents(ROOT_PATH . '/core/config.php', $contents) === false) {
throw new RuntimeException('Failed to write to config file');
}
if (function_exists('opcache_invalidate')) {
opcache_invalidate(ROOT_PATH . '/core/config.php', true);
}
self::$_config_cache = $config;
}
/**
* Get a config value from `core/config.php` file.
*
* @param string $path `.` seperated path of key to get from config file.
* @param mixed $fallback Value to return if option is not present in config file. If set to null, false is returned.
* @throws RuntimeException If the config file is not found.
* @return false|mixed Returns false if key doesn't exist, otherwise returns the value.
*/
public static function get(string $path, $fallback = null)
{
$config = self::all();
$parsed_path = self::parsePath($path);
if (!is_array($parsed_path)) {
return $config[$parsed_path] ?? false;
}
foreach ($parsed_path as $bit) {
if (isset($config[$bit])) {
$config = $config[$bit];
} else {
$not_matched = true;
}
}
if (!isset($not_matched)) {
return $config;
}
return $fallback ?? false;
}
/**
* Write a value to `core/config.php` file.
*
* @param string $key `.` seperated path of key to set.
* @param mixed $value Value to set under $key.
*/
public static function set(string $key, $value): void
{
$config = self::all();
$path = self::parsePath($key);
if (!is_array($path)) {
$config[$key] = $value;
} else {
$loc = &$config;
foreach ($path as $step) {
$loc = &$loc[$step];
}
// Check if it is a string here so that `null` is not converted to `''`
$loc = !is_string($value) ? $value : addslashes($value);
}
static::write((array) $config);
}
/**
* Write multiple values to `core/config.php` file.
*
* @param array $values Array of key/value pairs
*/
public static function setMultiple(array $values): void
{
$config = self::all();
foreach ($values as $key => $value) {
$path = self::parsePath($key);
if (!is_array($path)) {
$config[$key] = $value;
} else {
$loc = &$config;
foreach ($path as $step) {
$loc = &$loc[$step];
}
$loc = !is_string($value) ? $value : addslashes($value);
}
}
static::write((array) $config);
}
/**
* Parse a string path to an array of config paths.
* Will log a warning if a legacy path (using `/` is used).
*
* @param string $path Path to parse.
* @return string|array Path split into sections or plain string if no section separator was found.
*/
private static function parsePath(string $path)
{
if (str_contains($path, '.')) {
return explode('.', $path);
}
return $path;
}
/**
* Converts an array to a string to be inserted into the config file, with shorthand array syntax.
*
* @link https://gist.github.com/Bogdaan/ffa287f77568fcbb4cffa0082e954022
* @param array $config Config array to convert to string.
* @return string PHP code for the config array
*/
private static function arrayToString(array $config): string
{
$export = var_export($config, true);
$export = preg_replace("/^(' '*)(.*)/m", '$1$1$2', $export);
$array = preg_split("/\r\n|\n|\r/", $export);
$array = preg_replace(["/\s*array\s\($/", "/\)(,)?$/", "/\s=>\s$/"], [null, ']$1', ' => ['], $array);
return implode(PHP_EOL, array_filter(['['] + ($array ?: [])));
}
}