willy.club/app/App/Config.php

49 lines
1.5 KiB
PHP

<?php
namespace App;
abstract class Config
{
private static string $default_file;
private static string $override_file;
private static array $config;
static function file_location(string $default, string $override)
{
self::$default_file = $default;
self::$override_file = $override;
}
static function get(string $key): string|int|float|bool
{
if (isset(self::$config)) {
return self::$config[$key];
}
self::$config = require self::$default_file;
if (!file_exists(self::$override_file)) {
return self::$config[$key];
}
foreach (require self::$override_file as $override_key => $override_value) {
if (!array_key_exists($override_key, self::$config)) {
trigger_error(
'Failed to validate config: ' .
"Undefined option '$override_key'"
, E_USER_ERROR);
}
if (gettype($override_value) !== gettype(self::$config[$override_key])) {
trigger_error(
'Failed to validate config: ' .
"Type mismatch in config file: '$override_key' should be of type "
. gettype(self::$config[$override_key]) .
' not '
. gettype($override_value)
, E_USER_ERROR);
}
self::$config[$override_key] = $override_value;
}
return self::$config[$key];
}
}