1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Kotori.php |
4
|
|
|
* |
5
|
|
|
* A Tiny Model-View-Controller PHP Framework |
6
|
|
|
* |
7
|
|
|
* This content is released under the Apache 2 License |
8
|
|
|
* |
9
|
|
|
* Copyright (c) 2015-2017 Kotori Technology. All rights reserved. |
10
|
|
|
* |
11
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
12
|
|
|
* you may not use this file except in compliance with the License. |
13
|
|
|
* You may obtain a copy of the License at |
14
|
|
|
* |
15
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
16
|
|
|
* |
17
|
|
|
* Unless required by applicable law or agreed to in writing, software |
18
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
19
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
20
|
|
|
* See the License for the specific language governing permissions and |
21
|
|
|
* limitations under the License. |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Trace Class |
26
|
|
|
* |
27
|
|
|
* @package Kotori |
28
|
|
|
* @subpackage Debug |
29
|
|
|
* @author Kokororin |
30
|
|
|
* @link https://kotori.love |
31
|
|
|
*/ |
32
|
|
|
namespace Kotori\Debug; |
33
|
|
|
|
34
|
|
|
use Kotori\Core\Container; |
35
|
|
|
use Kotori\Core\Database; |
36
|
|
|
use Kotori\Core\Handle; |
37
|
|
|
use Kotori\Core\Helper; |
38
|
|
|
use WyriHaximus\HtmlCompress\Factory as HtmlCompress; |
39
|
|
|
|
40
|
|
|
class Trace |
41
|
|
|
{ |
42
|
|
|
/** |
43
|
|
|
* traceTab |
44
|
|
|
* |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
protected $traceTabs = [ |
48
|
|
|
'BASE' => 'Basic', |
49
|
|
|
'CONFIG' => 'Config', |
50
|
|
|
'SERVER' => 'Server', |
51
|
|
|
'COOKIE' => 'Cookie', |
52
|
|
|
'FILE' => 'File', |
53
|
|
|
'FLOW' => 'Flow', |
54
|
|
|
'ERROR' => 'Error', |
55
|
|
|
'SQL' => 'SQL', |
56
|
|
|
'SUPPORT' => 'Support', |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Class constructor |
61
|
|
|
* |
62
|
|
|
* Initialize Trace. |
63
|
|
|
* |
64
|
|
|
* @return void |
|
|
|
|
65
|
|
|
*/ |
66
|
1 |
|
public function __construct() |
67
|
|
|
{ |
68
|
1 |
|
Hook::listen(__CLASS__); |
69
|
1 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get Page Trace |
73
|
|
|
* |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
protected function getTrace() |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$files = get_included_files(); |
79
|
|
|
$config = Container::get('config')->getArray(); |
80
|
|
|
$server = $_SERVER; |
81
|
|
|
$cookie = $_COOKIE; |
82
|
|
|
$info = []; |
83
|
|
|
foreach ($files as $key => $file) { |
84
|
|
|
$info[] = $file . ' ( ' . number_format(filesize($file) / 1024, 2) . ' KB )'; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$hook = Hook::getTags(); |
88
|
|
|
foreach ($hook as $key => $value) { |
89
|
|
|
$hook[$key] = ' ( ' . $value . ' μs )'; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$error = Handle::$errors; |
93
|
|
|
|
94
|
|
|
$sql = Database::$queries; |
95
|
|
|
|
96
|
|
|
$base = [ |
97
|
|
|
'Request Info' => date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']) . ' ' . $_SERVER['SERVER_PROTOCOL'] . ' ' . $_SERVER['REQUEST_METHOD'] . ' : ' . $_SERVER['PHP_SELF'], |
98
|
|
|
'Run Time' => Hook::listen(\Kotori\App::class) . 'μs', |
99
|
|
|
'TPR' => Hook::listen(\Kotori\App::class) != 0 ? pow(10, 6) / Hook::listen(\Kotori\App::class) . ' req/s' : '+inf', |
100
|
|
|
'Memory Uses' => number_format((memory_get_usage() - KOTORI_START_MEMORY) / 1024, 2) . ' kb', |
101
|
|
|
'SQL Queries' => count($sql) . ' queries ', |
102
|
|
|
'File Loaded' => count(get_included_files()), |
103
|
|
|
'Session Info' => 'SESSION_ID=' . session_id(), |
104
|
|
|
]; |
105
|
|
|
|
106
|
|
|
$support = [ |
107
|
|
|
'<a target="_blank" href="https://github.com/kokororin/Kotori.php">GitHub</a>', |
108
|
|
|
'<a target="_blank" href="https://kotori.love/archives/kotori-php-framework.html">Blog</a>', |
109
|
|
|
]; |
110
|
|
|
|
111
|
|
|
$trace = []; |
112
|
|
|
foreach ($this->traceTabs as $name => $title) { |
113
|
|
|
switch (strtoupper($name)) { |
114
|
|
|
case 'BASE': |
115
|
|
|
$trace[$title] = $base; |
116
|
|
|
break; |
117
|
|
|
case 'CONFIG': |
118
|
|
|
$trace[$title] = $config; |
119
|
|
|
break; |
120
|
|
|
case 'SERVER': |
121
|
|
|
$trace[$title] = $server; |
122
|
|
|
break; |
123
|
|
|
case 'COOKIE': |
124
|
|
|
$trace[$title] = $cookie; |
125
|
|
|
break; |
126
|
|
|
case 'FILE': |
127
|
|
|
$trace[$title] = $info; |
128
|
|
|
break; |
129
|
|
|
case 'FLOW': |
130
|
|
|
$trace[$title] = $hook; |
131
|
|
|
break; |
132
|
|
|
case 'ERROR': |
133
|
|
|
$trace[$title] = $error; |
134
|
|
|
break; |
135
|
|
|
case 'SQL': |
136
|
|
|
$trace[$title] = $sql; |
137
|
|
|
break; |
138
|
|
|
case 'SUPPORT': |
139
|
|
|
$trace[$title] = $support; |
140
|
|
|
break; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $trace; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Show Page Trace in Output |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
public function showTrace() |
153
|
|
|
{ |
154
|
|
|
if (!Container::get('config')->get('app_debug')) { |
155
|
|
|
return; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$trace = $this->getTrace(); |
159
|
|
|
$tpl = ' |
160
|
|
|
<!-- Kotori Page Trace (If you want to hide this feature, please set APP_DEBUG to false.)--> |
161
|
|
|
<div id="page_trace" style="position:fixed;bottom:0;right:0;font-size:14px;width:100%;z-index: 999999;color: #000;text-align:left;font-family:\'Hiragino Sans GB\',\'Microsoft YaHei\',\'WenQuanYi Micro Hei\';"> |
162
|
|
|
<div id="page_trace_tab" style="display:none;background:white;margin:0;height:250px;"> |
163
|
|
|
<div id="page_trace_tab_tit" style="height:30px;padding: 6px 12px 0;border-bottom:1px solid #ececec;border-top:1px solid #ececec;font-size:16px">'; |
164
|
|
|
foreach ($trace as $key => $value) { |
165
|
|
|
$tpl .= '<span id="page_trace_tab_tit_' . strtolower($key) . '" style="color:#000;padding-right:12px;height:30px;line-height: 30px;display:inline-block;margin-right:3px;cursor: pointer;font-weight:700">' . $key . '</span>'; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$tpl .= '</div> |
169
|
|
|
<div id="page_trace_tab_cont" style="overflow:auto;height:212px;padding:0;line-height:24px">'; |
170
|
|
|
foreach ($trace as $key => $info) { |
171
|
|
|
$tpl .= '<div id="page_trace_tab_cont_' . strtolower($key) . '" style="display:none;"> |
172
|
|
|
<ol style="padding: 0; margin:0">'; |
173
|
|
|
if (is_array($info)) { |
174
|
|
|
foreach ($info as $k => $val) { |
175
|
|
|
$val = is_array($val) ? print_r($val, true) : (is_bool($val) ? json_encode($val) : $val); |
176
|
|
|
$val = (in_array($key, ['Support'])) ? $val : htmlentities($val, ENT_COMPAT, 'utf-8'); |
177
|
|
|
$tpl .= '<li style="border-bottom:1px solid #EEE;font-size:14px;padding:0 12px">' . (is_numeric($k) ? '' : $k . ' : ') . $val . '</li>'; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$tpl .= '</ol> |
182
|
|
|
</div>'; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
$tpl .= '</div> |
186
|
|
|
</div> |
187
|
|
|
<div id="page_trace_close" style="display:none;text-align:right;height:15px;position:absolute;top:10px;right:12px;cursor: pointer;"><img style="vertical-align:top;" src="data:image/gif;base64,R0lGODlhDwAPAJEAAAAAAAMDA////wAAACH/C1hNUCBEYXRhWE1QPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4gPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgNS4wLWMwNjAgNjEuMTM0Nzc3LCAyMDEwLzAyLzEyLTE3OjMyOjAwICAgICAgICAiPiA8cmRmOlJERiB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiPiA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtbG5zOnhtcE1NPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvbW0vIiB4bWxuczpzdFJlZj0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL3NUeXBlL1Jlc291cmNlUmVmIyIgeG1wOkNyZWF0b3JUb29sPSJBZG9iZSBQaG90b3Nob3AgQ1M1IFdpbmRvd3MiIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6MUQxMjc1MUJCQUJDMTFFMTk0OUVGRjc3QzU4RURFNkEiIHhtcE1NOkRvY3VtZW50SUQ9InhtcC5kaWQ6MUQxMjc1MUNCQUJDMTFFMTk0OUVGRjc3QzU4RURFNkEiPiA8eG1wTU06RGVyaXZlZEZyb20gc3RSZWY6aW5zdGFuY2VJRD0ieG1wLmlpZDoxRDEyNzUxOUJBQkMxMUUxOTQ5RUZGNzdDNThFREU2QSIgc3RSZWY6ZG9jdW1lbnRJRD0ieG1wLmRpZDoxRDEyNzUxQUJBQkMxMUUxOTQ5RUZGNzdDNThFREU2QSIvPiA8L3JkZjpEZXNjcmlwdGlvbj4gPC9yZGY6UkRGPiA8L3g6eG1wbWV0YT4gPD94cGFja2V0IGVuZD0iciI/PgH//v38+/r5+Pf29fTz8vHw7+7t7Ovq6ejn5uXk4+Lh4N/e3dzb2tnY19bV1NPS0dDPzs3My8rJyMfGxcTDwsHAv769vLu6ubi3trW0s7KxsK+urayrqqmop6alpKOioaCfnp2cm5qZmJeWlZSTkpGQj46NjIuKiYiHhoWEg4KBgH9+fXx7enl4d3Z1dHNycXBvbm1sa2ppaGdmZWRjYmFgX15dXFtaWVhXVlVUU1JRUE9OTUxLSklIR0ZFRENCQUA/Pj08Ozo5ODc2NTQzMjEwLy4tLCsqKSgnJiUkIyIhIB8eHRwbGhkYFxYVFBMSERAPDg0MCwoJCAcGBQQDAgEAACH5BAAAAAAALAAAAAAPAA8AAAIdjI6JZqotoJPR1fnsgRR3C2jZl3Ai9aWZZooV+RQAOw==" /></div> |
188
|
|
|
</div> |
189
|
|
|
<div id="page_trace_open" style="height:30px;float:right;text-align: right;overflow:hidden;position:fixed;bottom:0;right:0;color:#000;line-height:30px;cursor:pointer;"><div style="background:#232323;color:#FFF;padding:0 6px;float:right;line-height:30px;font-size:14px">'; |
190
|
|
|
$errorCount = count(Handle::$errors); |
191
|
|
|
|
192
|
|
|
if ($errorCount == 0) { |
193
|
|
|
$tpl .= Hook::listen(\Kotori\App::class) . 'μs'; |
194
|
|
|
} else { |
195
|
|
|
$tpl .= $errorCount . ' errors'; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
$tpl .= '</div><img width="30" style="border-left:2px solid black;border-top:2px solid black;border-bottom:2px solid black;" title="ShowPageTrace" src="' . Helper::logo() . '"></div>'; |
199
|
|
|
$tpl .= HtmlCompress::construct()->compress('<script type="text/javascript"> |
200
|
|
|
(function() { |
201
|
|
|
\'use strict\'; |
202
|
|
|
var tab_tit = document.getElementById(\'page_trace_tab_tit\').getElementsByTagName(\'span\'), |
203
|
|
|
tab_cont = document.getElementById(\'page_trace_tab_cont\').getElementsByTagName(\'div\'), |
204
|
|
|
open = document.getElementById(\'page_trace_open\'), |
205
|
|
|
close = document.getElementById(\'page_trace_close\').children[0], |
206
|
|
|
trace = document.getElementById(\'page_trace_tab\'), |
207
|
|
|
storage = localStorage.getItem(\'kotori_show_page_trace\'), |
208
|
|
|
history = (storage !== null && storage.split(\'|\')) || [0,0], |
209
|
|
|
bindClick = function(dom, listener) { |
210
|
|
|
if (dom.addEventListener) { |
211
|
|
|
dom.addEventListener(\'click\', listener, false); |
212
|
|
|
} else { |
213
|
|
|
dom.attachEvent(\'onclick\', listener); |
214
|
|
|
} |
215
|
|
|
}; |
216
|
|
|
bindClick(open, function() { |
217
|
|
|
trace.style.display = \'block\'; |
218
|
|
|
this.style.display = \'none\'; |
219
|
|
|
close.parentNode.style.display = \'block\'; |
220
|
|
|
history[0] = 1; |
221
|
|
|
localStorage.setItem(\'kotori_show_page_trace\', history.join(\'|\')); |
222
|
|
|
}); |
223
|
|
|
bindClick(close, function() { |
224
|
|
|
trace.style.display = \'none\'; |
225
|
|
|
this.parentNode.style.display = \'none\'; |
226
|
|
|
open.style.display = \'block\'; |
227
|
|
|
history[0] = 0; |
228
|
|
|
localStorage.setItem(\'kotori_show_page_trace\', history.join(\'|\')); |
229
|
|
|
}); |
230
|
|
|
for (var i = 0; i < tab_tit.length; i++) { |
231
|
|
|
bindClick(tab_tit[i], (function(i) { |
232
|
|
|
return function() { |
233
|
|
|
for (var j = 0; j < tab_cont.length; j++) { |
234
|
|
|
tab_cont[j].style.display = \'none\'; |
235
|
|
|
tab_tit[j].style.color = \'#999\'; |
236
|
|
|
} |
237
|
|
|
tab_cont[i].style.display = \'block\'; |
238
|
|
|
tab_tit[i].style.color = \'#000\'; |
239
|
|
|
history[1] = i; |
240
|
|
|
localStorage.setItem(\'kotori_show_page_trace\', history.join(\'|\')); |
241
|
|
|
}; |
242
|
|
|
})(i)); |
243
|
|
|
} |
244
|
|
|
parseInt(history[0]) && open.click(); |
245
|
|
|
tab_tit[history[1]].click(); |
246
|
|
|
})(); |
247
|
|
|
</script>'); |
248
|
|
|
return $tpl; |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.