Files
2026-09-05 15:54:33 +03:30

57 lines
2.0 KiB
PHP

<?php
declare(strict_types=1);
namespace App;
use function App\Support\base_path;
use function App\Support\storage_path;
final class Config
{
private function __construct(private readonly array $values)
{
}
public static function load(): self
{
$values = require base_path('config/app.php');
if (is_file(base_path('config/local.php'))) {
$values = array_replace($values, require base_path('config/local.php'));
}
$values['token'] = getenv('EITAA_APP_TOKEN') ?: $values['token'];
if ($values['token'] === '' && is_readable(storage_path('secrets/token.txt'))) {
$values['token'] = trim(file_get_contents(storage_path('secrets/token.txt')));
}
foreach (['init_data_max_age', 'session_lifetime'] as $key) {
if (!is_int($values[$key]) || $values[$key] < 1) {
throw new \RuntimeException('Invalid duration configuration.');
}
}
foreach (['allow_local_http', 'verify_on_login', 'verify_ip'] as $key) {
if (!is_bool($values[$key])) {
throw new \RuntimeException('Invalid boolean configuration.');
}
}
if (!is_string($values['token'])) {
throw new \RuntimeException('Invalid token configuration.');
}
$url = $values['public_url'];
if (!is_string($url) || ($url !== '' && (!filter_var($url, FILTER_VALIDATE_URL)
|| parse_url($url, PHP_URL_SCHEME) !== 'https'
|| parse_url($url, PHP_URL_USER) !== null
|| parse_url($url, PHP_URL_QUERY) !== null
|| parse_url($url, PHP_URL_FRAGMENT) !== null))) {
throw new \RuntimeException('Invalid public URL configuration.');
}
return new self($values);
}
public function get(string $key): mixed
{
if (!array_key_exists($key, $this->values)) {
throw new \InvalidArgumentException('Unknown configuration key.');
}
return $this->values[$key];
}
}