|
1
|
|
|
<?php |
|
2
|
|
|
// +---------------------------------------------------------------------- |
|
|
|
|
|
|
3
|
|
|
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ] |
|
4
|
|
|
// +---------------------------------------------------------------------- |
|
5
|
|
|
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved. |
|
6
|
|
|
// +---------------------------------------------------------------------- |
|
7
|
|
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) |
|
8
|
|
|
// +---------------------------------------------------------------------- |
|
9
|
|
|
// | Author: yangweijie <[email protected]> |
|
10
|
|
|
// +---------------------------------------------------------------------- |
|
11
|
|
|
declare (strict_types = 1); |
|
12
|
|
|
namespace think\debug; |
|
13
|
|
|
|
|
14
|
|
|
use think\App; |
|
15
|
|
|
use think\Response; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* 浏览器调试输出 |
|
19
|
|
|
*/ |
|
|
|
|
|
|
20
|
|
|
class Console |
|
21
|
|
|
{ |
|
22
|
|
|
protected $config = [ |
|
23
|
|
|
'tabs' => ['base' => '基本', 'file' => '文件', 'info' => '流程', 'notice|error' => '错误', 'sql' => 'SQL', 'debug|log' => '调试'], |
|
24
|
|
|
]; |
|
25
|
|
|
|
|
26
|
|
|
// 实例化并传入参数 |
|
27
|
|
|
public function __construct(array $config = []) |
|
|
|
|
|
|
28
|
|
|
{ |
|
29
|
|
|
$this->config = array_merge($this->config, $config); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
|
|
|
|
|
33
|
|
|
* 调试输出接口 |
|
34
|
|
|
* @access public |
|
35
|
|
|
* @param Response $response Response对象 |
|
|
|
|
|
|
36
|
|
|
* @param array $log 日志信息 |
|
|
|
|
|
|
37
|
|
|
* @return string|bool |
|
38
|
|
|
*/ |
|
39
|
|
|
public function output(App $app, Response $response, array $log = []) |
|
40
|
|
|
{ |
|
41
|
|
|
$request = $app->request; |
|
42
|
|
|
$contentType = $response->getHeader('Content-Type'); |
|
43
|
|
|
$accept = $request->header('accept'); |
|
44
|
|
|
if (strpos($accept, 'application/json') === 0 || $request->isAjax()) { |
|
|
|
|
|
|
45
|
|
|
return false; |
|
46
|
|
|
} elseif (!empty($contentType) && strpos($contentType, 'html') === false) { |
|
47
|
|
|
return false; |
|
48
|
|
|
} |
|
49
|
|
|
// 获取基本信息 |
|
50
|
|
|
$runtime = number_format(microtime(true) - $app->getBeginTime(), 10); |
|
51
|
|
|
$reqs = $runtime > 0 ? number_format(1 / $runtime, 2) : '∞'; |
|
52
|
|
|
$mem = number_format((memory_get_usage() - $app->getBeginMem()) / 1024, 2); |
|
53
|
|
|
|
|
54
|
|
|
if ($request->host()) { |
|
55
|
|
|
$uri = $request->protocol() . ' ' . $request->method() . ' : ' . $request->url(true); |
|
56
|
|
|
} else { |
|
57
|
|
|
$uri = 'cmd:' . implode(' ', $_SERVER['argv']); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
// 页面Trace信息 |
|
61
|
|
|
$base = [ |
|
62
|
|
|
'请求信息' => date('Y-m-d H:i:s', $request->time()) . ' ' . $uri, |
|
|
|
|
|
|
63
|
|
|
'运行时间' => number_format((float) $runtime, 6) . 's [ 吞吐率:' . $reqs . 'req/s ] 内存消耗:' . $mem . 'kb 文件加载:' . count(get_included_files()), |
|
64
|
|
|
'查询信息' => $app->db->getQueryTimes() . ' queries', |
|
65
|
|
|
'缓存信息' => $app->cache->getReadTimes() . ' reads,' . $app->cache->getWriteTimes() . ' writes', |
|
|
|
|
|
|
66
|
|
|
]; |
|
67
|
|
|
|
|
68
|
|
|
if ($app->session->getId(false)) { |
|
69
|
|
|
$base['会话信息'] = 'SESSION_ID=' . $app->session->getId(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$info = $this->getFileInfo(); |
|
73
|
|
|
|
|
74
|
|
|
// 页面Trace信息 |
|
75
|
|
|
$trace = []; |
|
76
|
|
|
foreach ($this->config['tabs'] as $name => $title) { |
|
77
|
|
|
$name = strtolower($name); |
|
78
|
|
|
switch ($name) { |
|
79
|
|
|
case 'base': // 基本信息 |
|
|
|
|
|
|
80
|
|
|
$trace[$title] = $base; |
|
81
|
|
|
break; |
|
82
|
|
|
case 'file': // 文件信息 |
|
|
|
|
|
|
83
|
|
|
$trace[$title] = $info; |
|
84
|
|
|
break; |
|
85
|
|
|
default: // 调试信息 |
|
|
|
|
|
|
86
|
|
|
if (strpos($name, '|')) { |
|
|
|
|
|
|
87
|
|
|
// 多组信息 |
|
88
|
|
|
$names = explode('|', $name); |
|
89
|
|
|
$result = []; |
|
90
|
|
|
foreach ($names as $item) { |
|
|
|
|
|
|
91
|
|
|
$result = array_merge($result, $log[$item] ?? []); |
|
92
|
|
|
} |
|
|
|
|
|
|
93
|
|
|
$trace[$title] = $result; |
|
94
|
|
|
} else { |
|
|
|
|
|
|
95
|
|
|
$trace[$title] = $log[$name] ?? ''; |
|
96
|
|
|
} |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
//输出到控制台 |
|
101
|
|
|
$lines = ''; |
|
102
|
|
|
foreach ($trace as $type => $msg) { |
|
103
|
|
|
$lines .= $this->console($type, $msg); |
|
104
|
|
|
} |
|
105
|
|
|
$js = <<<JS |
|
106
|
|
|
|
|
107
|
|
|
<script type='text/javascript'> |
|
108
|
|
|
{$lines} |
|
109
|
|
|
</script> |
|
110
|
|
|
JS; |
|
111
|
|
|
return $js; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
protected function console(string $type, $msg) |
|
|
|
|
|
|
115
|
|
|
{ |
|
116
|
|
|
$type = strtolower($type); |
|
117
|
|
|
$trace_tabs = array_values($this->config['tabs']); |
|
118
|
|
|
$line = []; |
|
119
|
|
|
$line[] = ($type == $trace_tabs[0] || '调试' == $type || '错误' == $type) |
|
120
|
|
|
? "console.group('{$type}');" |
|
121
|
|
|
: "console.groupCollapsed('{$type}');"; |
|
122
|
|
|
|
|
123
|
|
|
foreach ((array) $msg as $key => $m) { |
|
124
|
|
|
switch ($type) { |
|
125
|
|
|
case '调试': |
|
|
|
|
|
|
126
|
|
|
$var_type = gettype($m); |
|
127
|
|
|
if (in_array($var_type, ['array', 'string'])) { |
|
|
|
|
|
|
128
|
|
|
$line[] = "console.log(" . json_encode($m) . ");"; |
|
129
|
|
|
} else { |
|
|
|
|
|
|
130
|
|
|
$line[] = "console.log(" . json_encode(var_export($m, true)) . ");"; |
|
131
|
|
|
} |
|
|
|
|
|
|
132
|
|
|
break; |
|
133
|
|
|
case '错误': |
|
|
|
|
|
|
134
|
|
|
$msg = str_replace("\n", '\n', addslashes(is_scalar($m) ? $m : json_encode($m))); |
|
135
|
|
|
$style = 'color:#F4006B;font-size:14px;'; |
|
136
|
|
|
$line[] = "console.error(\"%c{$msg}\", \"{$style}\");"; |
|
137
|
|
|
break; |
|
138
|
|
|
case 'sql': |
|
|
|
|
|
|
139
|
|
|
$msg = str_replace("\n", '\n', addslashes($m)); |
|
140
|
|
|
$style = "color:#009bb4;"; |
|
141
|
|
|
$line[] = "console.log(\"%c{$msg}\", \"{$style}\");"; |
|
142
|
|
|
break; |
|
143
|
|
|
default: |
|
|
|
|
|
|
144
|
|
|
$m = is_string($key) ? $key . ' ' . $m : $key + 1 . ' ' . $m; |
|
145
|
|
|
$msg = json_encode($m); |
|
146
|
|
|
$line[] = "console.log({$msg});"; |
|
147
|
|
|
break; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
$line[] = "console.groupEnd();"; |
|
151
|
|
|
return implode(PHP_EOL, $line); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* 获取文件加载信息 |
|
156
|
|
|
* @access protected |
|
157
|
|
|
* @return integer|array |
|
158
|
|
|
*/ |
|
159
|
|
|
protected function getFileInfo() |
|
160
|
|
|
{ |
|
161
|
|
|
$files = get_included_files(); |
|
162
|
|
|
$info = []; |
|
163
|
|
|
|
|
164
|
|
|
foreach ($files as $key => $file) { |
|
165
|
|
|
$info[] = $file . ' ( ' . number_format(filesize($file) / 1024, 2) . ' KB )'; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
return $info; |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|