36 lines
1.1 KiB
PHP
36 lines
1.1 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
namespace app\service;
|
|
|
|
final class Settings
|
|
{
|
|
public static function get(string $key, string $default = ''): string
|
|
{
|
|
$process = getenv($key);
|
|
return $process !== false ? $process : (string) env($key, $default);
|
|
}
|
|
|
|
public static function root(): string
|
|
{
|
|
return dirname(__DIR__, 2);
|
|
}
|
|
|
|
public static function sqlitePath(): string
|
|
{
|
|
$path = self::get('IAM_SQLITE_PATH', 'runtime/identity.sqlite');
|
|
if ($path === '' || str_contains($path, "\0")) {
|
|
throw new \RuntimeException('无效的 SQLite 路径');
|
|
}
|
|
return preg_match('~^(?:[A-Za-z]:[\\\\/]|/)~', $path) ? $path : self::root() . '/' . $path;
|
|
}
|
|
|
|
public static function runtimePath(): string
|
|
{
|
|
$path = self::get('IAM_RUNTIME_PATH', 'runtime');
|
|
if ($path === '' || str_contains($path, "\0")) {
|
|
throw new \RuntimeException('无效的运行目录');
|
|
}
|
|
return preg_match('~^(?:[A-Za-z]:[\\\\/]|/)~', $path) ? $path : self::root() . '/' . $path;
|
|
}
|
|
}
|