You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/app/Services/Config.php

90 lines
2.9 KiB
PHP

<?php
namespace App\Services;
class Config
{
public static function get($key, $default = null)
{
if ($key === 'appName') {
// Site names are managed by the admin domain mappings, not .config.php.
return SiteName::current();
}
global $System_Config;
return array_key_exists($key, $System_Config) ? $System_Config[$key] : $default;
}
public static function set($key, $value)
{
global $System_Config;
$System_Config[$key] = $value;
}
public static function getPublicConfig()
{
return [
"appName" => self::get("appName"),
"baseUrl" => self::get("baseUrl"),
"jump_delay" => self::get("jump_delay"),
];
}
public static function getDbConfig()
{
[$host, $port] = self::databaseAddress('db_host', 'db_port');
return [
'driver' => self::get('db_driver'),
'host' => $host,
'port' => $port,
'database' => self::get('db_database'),
'username' => self::get('db_username'),
'password' => self::get('db_password'),
'charset' => self::get('db_charset'),
'collation' => self::get('db_collation'),
'prefix' => self::get('db_prefix')
];
}
public static function getLogDbConfig()
{
[$host, $port] = self::databaseAddress('log_db_host', 'log_db_port');
return [
'driver' => self::get('db_driver'),
'host' => $host,
'port' => $port,
'database' => self::get('log_db_database'),
'username' => self::get('log_db_user'),
'password' => self::get('log_db_password'),
'charset' => self::get('db_charset'),
'collation' => self::get('db_collation')
];
}
private static function databaseAddress(string $hostKey, string $portKey): array
{
$defaultHost = $hostKey === 'log_db_host' ? 'localhost' : '127.0.0.1';
$host = trim((string) self::get($hostKey, $defaultHost));
if ($host === '') {
$host = $defaultHost;
}
$port = self::get($portKey);
// Retain existing host:port configuration, but give PDO a separate port.
if (preg_match('/^\[([^\]]+)\]:(\d+)$/D', $host, $match) === 1
|| preg_match('/^([^:]+):(\d+)$/D', $host, $match) === 1) {
$host = $match[1];
if ($port === null || $port === '') {
$port = $match[2];
}
} elseif (preg_match('/^\[([^\]]+)\]$/D', $host, $match) === 1) {
$host = $match[1];
}
$port = $port === null || $port === '' ? 3306 : filter_var($port, FILTER_VALIDATE_INT);
if ($port === false || $port < 1 || $port > 65535) {
throw new \InvalidArgumentException('数据库端口必须为 1 至 65535');
}
return [$host, (int) $port];
}
}