126 lines
3.1 KiB
PHP
126 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace app\config;
|
|
|
|
|
|
class Config
|
|
{
|
|
/**
|
|
* @var int 普通日志轮转时间默认 14 天
|
|
*/
|
|
public int $logRotationTimeByDay {
|
|
get => $this->logRotationTimeByDay;
|
|
}
|
|
|
|
/**
|
|
* @var int 错误日志轮转时间默认 30 天
|
|
*/
|
|
public int $errorLogRotationTimeByDay {
|
|
get => $this->errorLogRotationTimeByDay;
|
|
}
|
|
|
|
public DatabaseConfig $database {
|
|
get => $this->database;
|
|
}
|
|
|
|
public bool $dbDebug {
|
|
get => $this->dbDebug;
|
|
}
|
|
|
|
/**
|
|
* 方式 类:方法
|
|
* 方式 类
|
|
* 方式 :方法
|
|
* 允许使用正则 * 作为通配符
|
|
* @var array 日志过滤器
|
|
*/
|
|
public array $logFilter {
|
|
get => $this->logFilter;
|
|
}
|
|
|
|
public int $logLevel {
|
|
get => $this->logLevel;
|
|
}
|
|
|
|
/**
|
|
* 阻断模式
|
|
*/
|
|
public bool $blockMode {
|
|
get => $this->blockMode;
|
|
}
|
|
|
|
/**
|
|
* TCP Server 进程数量
|
|
*/
|
|
public int $tcpServerProcessNum = 1 {
|
|
get => $this->tcpServerProcessNum;
|
|
}
|
|
|
|
/**
|
|
* TCP_SERVER_PORT
|
|
*/
|
|
public int $tcpServerPort = 50000 {
|
|
get => $this->tcpServerPort;
|
|
}
|
|
|
|
private function __construct()
|
|
{
|
|
$this->database = new DatabaseConfig();
|
|
$this->dbDebug = self::getBoolEnv('DB_DEBUG');
|
|
$this->logFilter = self::getStringArrayEnv('LOG_FILTER', []);
|
|
$this->logLevel = match (strtoupper(self::getStringEnv('LOG_LEVEL', 'DEBUG'))) {
|
|
'INFO' => \Monolog\Logger::INFO,
|
|
'WARNING' => \Monolog\Logger::WARNING,
|
|
'ERROR' => \Monolog\Logger::ERROR,
|
|
'ALERT' => \Monolog\Logger::ALERT,
|
|
'EMERGENCY' => \Monolog\Logger::EMERGENCY,
|
|
'CRITICAL' => \Monolog\Logger::CRITICAL,
|
|
'NOTICE' => \Monolog\Logger::NOTICE,
|
|
default => \Monolog\Logger::DEBUG
|
|
};
|
|
$this->tcpServerPort = self::getIntEnv('TCP_SERVER_PORT', 50000);
|
|
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && $this->tcpServerProcessNum > 1) {
|
|
$this->tcpServerProcessNum = 1;
|
|
echo "Warning: TCP_SERVER_PROCESS_NUM set to 1 on Windows.\n";
|
|
}
|
|
}
|
|
|
|
private function __clone()
|
|
{
|
|
}
|
|
|
|
public static function getBoolEnv($name, $default = false)
|
|
{
|
|
$value = getenv($name);
|
|
return empty($value) ? $default : filter_var($value, FILTER_VALIDATE_BOOLEAN);
|
|
}
|
|
|
|
public static function getIntEnv($name, $default = 0)
|
|
{
|
|
$value = getenv($name);
|
|
return empty($value) ? $default : (int)$value;
|
|
}
|
|
|
|
public static function getStringEnv($name, $default = null)
|
|
{
|
|
$value = getenv($name);
|
|
return empty($value) ? $default : $value;
|
|
}
|
|
|
|
private static ?Config $instance = null;
|
|
|
|
public static function getInstance(): Config
|
|
{
|
|
if (self::$instance === null) {
|
|
self::$instance = new self();
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
private static function getStringArrayEnv(string $string, array $array): array
|
|
{
|
|
$value = getenv($string);
|
|
return empty($value) ? $array : array_map('trim', explode(';', $value));
|
|
}
|
|
|
|
} |