Phred/src/Support/Config.php
2025-12-14 17:10:01 -06:00

34 lines
861 B
PHP

<?php
declare(strict_types=1);
namespace Phred\Support;
final class Config
{
/**
* Get a config value from environment variables or return default.
* Accepts both UPPER_CASE and dot.notation keys (the latter is for future file-based config).
*/
public static function get(string $key, mixed $default = null): mixed
{
// Support dot.notation by converting to uppercase with underscores for env lookup.
$envKey = strtoupper(str_replace('.', '_', $key));
$value = getenv($envKey);
if ($value !== false) {
return $value;
}
// Fallback to server superglobal if present.
if (isset($_SERVER[$envKey])) {
return $_SERVER[$envKey];
}
if (isset($_ENV[$envKey])) {
return $_ENV[$envKey];
}
return $default;
}
}