实现后台及前台通过API访问UV埋点,所有代码全部保存
This commit is contained in:
63
src/app/common/library/PerformanceAwareCallerInfo.php
Normal file
63
src/app/common/library/PerformanceAwareCallerInfo.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?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'
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user