feat: 同步下新环境

This commit is contained in:
笔记本里的永平
2025-07-07 11:31:25 +08:00
parent 3d1050db3d
commit 86c261ba70
196 changed files with 13146 additions and 29319 deletions

View File

@@ -30,8 +30,6 @@ class Socket
'force_client_ids' => [],
// 限制允许读取日志的client_id
'allow_client_ids' => [],
//输出到浏览器默认展开的日志级别
'expand_level' => ['debug'],
];
protected $css = [
@@ -43,17 +41,14 @@ class Socket
];
protected $allowForceClientIds = []; //配置强制推送且被授权的client_id
protected $app;
/**
* 构函数
* 构函数
* @param array $config 缓存参数
* @access public
* @param array $config 缓存参数
*/
public function __construct(App $app, array $config = [])
public function __construct(array $config = [])
{
$this->app = $app;
if (!empty($config)) {
$this->config = array_merge($this->config, $config);
}
@@ -62,7 +57,7 @@ class Socket
/**
* 调试输出接口
* @access public
* @param array $log 日志信息
* @param array $log 日志信息
* @return bool
*/
public function save(array $log = [], $append = false)
@@ -70,14 +65,12 @@ class Socket
if (!$this->check()) {
return false;
}
$trace = [];
if ($this->app->isDebug()) {
$runtime = round(microtime(true) - $this->app->getBeginTime(), 10);
if (App::$debug) {
$runtime = round(microtime(true) - THINK_START_TIME, 10);
$reqs = $runtime > 0 ? number_format(1 / $runtime, 2) : '∞';
$time_str = ' [运行时间:' . number_format($runtime, 6) . 's][吞吐率:' . $reqs . 'req/s]';
$memory_use = number_format((memory_get_usage() - $this->app->getBeginMem()) / 1024, 2);
$memory_use = number_format((memory_get_usage() - THINK_START_MEM) / 1024, 2);
$memory_str = ' [内存消耗:' . $memory_use . 'kb]';
$file_load = ' [文件加载:' . count(get_included_files()) . ']';
@@ -86,7 +79,6 @@ class Socket
} else {
$current_uri = 'cmd:' . implode(' ', $_SERVER['argv']);
}
// 基本信息
$trace[] = [
'type' => 'group',
@@ -97,11 +89,10 @@ class Socket
foreach ($log as $type => $val) {
$trace[] = [
'type' => in_array($type, $this->config['expand_level']) ? 'group' : 'groupCollapsed',
'type' => 'groupCollapsed',
'msg' => '[ ' . $type . ' ]',
'css' => isset($this->css[$type]) ? $this->css[$type] : '',
];
foreach ($val as $msg) {
if (!is_string($msg)) {
$msg = var_export($msg, true);
@@ -112,7 +103,6 @@ class Socket
'css' => '',
];
}
$trace[] = [
'type' => 'groupEnd',
'msg' => '',
@@ -126,13 +116,11 @@ class Socket
'msg' => '[ file ]',
'css' => '',
];
$trace[] = [
'type' => 'log',
'msg' => implode("\n", get_included_files()),
'css' => '',
];
$trace[] = [
'type' => 'groupEnd',
'msg' => '',
@@ -147,7 +135,6 @@ class Socket
];
$tabid = $this->getClientArg('tabid');
if (!$client_id = $this->getClientArg('client_id')) {
$client_id = '';
}
@@ -161,18 +148,16 @@ class Socket
} else {
$this->sendToClient($tabid, $client_id, $trace, '');
}
return true;
}
/**
* 发送给指定客户端
* @access protected
* @author Zjmainstay
* @param $tabid
* @param $client_id
* @param $logs
* @param $force_client_id
* @param $tabid
* @param $client_id
* @param $logs
* @param $force_client_id
*/
protected function sendToClient($tabid, $client_id, $logs, $force_client_id)
{
@@ -182,25 +167,20 @@ class Socket
'logs' => $logs,
'force_client_id' => $force_client_id,
];
$msg = @json_encode($logs);
$address = '/' . $client_id; //将client_id作为地址 server端通过地址判断将日志发布给谁
$this->send($this->config['host'], $msg, $address);
}
protected function check()
{
$tabid = $this->getClientArg('tabid');
//是否记录日志的检查
if (!$tabid && !$this->config['force_client_ids']) {
return false;
}
//用户认证
$allow_client_ids = $this->config['allow_client_ids'];
if (!empty($allow_client_ids)) {
//通过数组交集得出授权强制推送的client_id
$this->allowForceClientIds = array_intersect($allow_client_ids, $this->config['force_client_ids']);
@@ -215,7 +195,6 @@ class Socket
} else {
$this->allowForceClientIds = $this->config['force_client_ids'];
}
return true;
}
@@ -232,7 +211,6 @@ class Socket
if (!isset($_SERVER[$key])) {
return;
}
if (empty($args)) {
if (!preg_match('/SocketLog\((.*?)\)/', $_SERVER[$key], $match)) {
$args = ['tabid' => null];
@@ -240,39 +218,32 @@ class Socket
}
parse_str($match[1], $args);
}
if (isset($args[$name])) {
return $args[$name];
}
return;
}
/**
* @access protected
* @param string $host - $host of socket server
* @param string $message - 发送的消息
* @param string $address - 地址
* @param string $host - $host of socket server
* @param string $message - 发送的消息
* @param string $address - 地址
* @return bool
*/
protected function send($host, $message = '', $address = '/')
{
$url = 'http://' . $host . ':' . $this->port . $address;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $message);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$headers = [
"Content-Type: application/json;charset=UTF-8",
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //设置header
return curl_exec($ch);
}