初始化仓库及v1.0.0提交
This commit is contained in:
57
app/Config/AppConfig.php
Normal file
57
app/Config/AppConfig.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace App\Config;
|
||||
|
||||
class AppConfig
|
||||
{
|
||||
private static ?array $cache = null;
|
||||
private static string $configPath = '';
|
||||
|
||||
private static function getConfigPath(): string
|
||||
{
|
||||
if (self::$configPath === '') {
|
||||
self::$configPath = dirname(__DIR__, 2) . '/config/app-config.json';
|
||||
}
|
||||
|
||||
return self::$configPath;
|
||||
}
|
||||
|
||||
private static function load(): array
|
||||
{
|
||||
if (self::$cache !== null) {
|
||||
return self::$cache;
|
||||
}
|
||||
|
||||
$path = self::getConfigPath();
|
||||
|
||||
if (!file_exists($path)) {
|
||||
self::$cache = [];
|
||||
return self::$cache;
|
||||
}
|
||||
|
||||
$content = file_get_contents($path);
|
||||
self::$cache = json_decode($content, true) ?? [];
|
||||
|
||||
return self::$cache;
|
||||
}
|
||||
|
||||
public static function get(string $key, $default = null)
|
||||
{
|
||||
$config = self::load();
|
||||
return $config[$key] ?? $default;
|
||||
}
|
||||
|
||||
public static function set(string $key, $value): void
|
||||
{
|
||||
$config = self::load();
|
||||
$config[$key] = $value;
|
||||
self::$cache = $config;
|
||||
|
||||
$dir = dirname(self::getConfigPath());
|
||||
if (!is_dir($dir)) {
|
||||
mkdir($dir, 0755, true);
|
||||
}
|
||||
|
||||
file_put_contents(self::getConfigPath(), json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user