58 lines
1.3 KiB
PHP
58 lines
1.3 KiB
PHP
<?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));
|
|
}
|
|
}
|