63 lines
1.6 KiB
PHP
63 lines
1.6 KiB
PHP
<?php
|
|
namespace app\common\library;
|
|
|
|
use think\facade\App;
|
|
|
|
class PerformanceAwareCallerInfo
|
|
{
|
|
private static $enabled = true;
|
|
|
|
/**
|
|
* 启用/禁用调用者信息(生产环境可禁用)
|
|
*/
|
|
public static function setEnabled(bool $enabled): void
|
|
{
|
|
self::$enabled = $enabled;
|
|
}
|
|
|
|
/**
|
|
* 检查是否启用
|
|
*/
|
|
public static function isEnabled(): bool
|
|
{
|
|
return self::$enabled;
|
|
}
|
|
|
|
/**
|
|
* 高性能的调用者信息获取
|
|
*/
|
|
public static function getCallerInfoOpt(int $depth = 1): array
|
|
{
|
|
if (!self::$enabled) {
|
|
return ['file' => 'disabled', 'line' => 0];
|
|
}
|
|
|
|
// 生产环境使用更轻量的方式
|
|
if (app()->isDebug()) {
|
|
// 开发环境:详细信息
|
|
return self::getDetailedCallerInfo($depth);
|
|
} else {
|
|
// 生产环境:基本信息
|
|
return self::getBasicCallerInfo($depth);
|
|
}
|
|
}
|
|
|
|
private static function getDetailedCallerInfo(int $depth): array
|
|
{
|
|
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $depth + 2);
|
|
return $backtrace[$depth] ?? ['file' => 'unknown', 'line' => 0];
|
|
}
|
|
|
|
private static function getBasicCallerInfo(int $depth): array
|
|
{
|
|
// 使用更轻量的方法
|
|
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $depth + 2);
|
|
$caller = $backtrace[$depth] ?? [];
|
|
|
|
return [
|
|
'file' => $caller['file'] ?? 'unknown',
|
|
'line' => $caller['line'] ?? 0,
|
|
'function' => $caller['function'] ?? 'unknown'
|
|
];
|
|
}
|
|
} |