Files
dsserver/app/config/Config.php
T
zimoyin 1a84e92384 init
2026-04-03 11:32:19 +08:00

200 lines
5.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;
}
/**
* 摄像头配置列表
*/
public array $cameraConfig {
get => $this->cameraConfig;
}
/**
* ZLM配置
*/
public ZLMConfig $zlm {
get => $this->zlm;
}
public string $httpIP{
get => $this->httpIP;
}
public int $httpPort{
get => $this->httpPort;
}
public bool $enableRequestTimeLog{
get => $this->enableRequestTimeLog;
}
/**
* 人员检测回调函数
* @var callable|null
*/
private $personDetectionCallback = null;
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);
$this->logRotationTimeByDay = self::getIntEnv('LOG_ROTATION_TIME_BY_DAY', 14);
$this->errorLogRotationTimeByDay = self::getIntEnv('ERROR_LOG_ROTATION_TIME_BY_DAY', 30);
$this->cameraConfig = $this->loadCameraConfig();
$this->httpIP = self::getStringEnv('HTTP_IP', '0.0.0.0');
$this->httpPort = self::getIntEnv('HTTP_PORT', 8080);
$this->zlm = new ZLMConfig();
$this->personDetectionCallback = null;
$this->enableRequestTimeLog = self::getBoolEnv('ENABLE_REQUEST_TIME_LOG', false);
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 loadCameraConfig(): array
{
// 从环境变量加载摄像头配置
// 格式: CAMERA_CONFIG={"camera_01":{"rtsp_url":"rtsp://...","enabled":true}}
$configJson = self::getStringEnv('CAMERA_CONFIG', '');
if (!empty($configJson)) {
$config = json_decode($configJson, true);
if (is_array($config)) {
return $config;
}
}
// 默认空配置
return [];
}
/**
* 获取人员检测回调函数
* @return callable|null
*/
public function getPersonDetectionCallback(): ?callable
{
return $this->personDetectionCallback;
}
/**
* 设置人员检测回调函数
* @param callable|null $callback
*/
public function setPersonDetectionCallback(?callable $callback): void
{
$this->personDetectionCallback = $callback;
}
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));
}
}