1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System Framework package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @author Steeve Andrian Salim |
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
13
|
|
|
|
14
|
|
|
namespace O2System\Gear; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Toolbar |
20
|
|
|
* |
21
|
|
|
* @package O2System\Gear |
22
|
|
|
*/ |
23
|
|
|
class Toolbar |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Toolbar::__toString |
27
|
|
|
* |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
|
|
public function __toString() |
31
|
|
|
{ |
32
|
|
|
return $this->getOutput(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
// ------------------------------------------------------------------------ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Toolbar::getOutput |
39
|
|
|
* |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
public function getOutput() |
43
|
|
|
{ |
44
|
|
|
$totalExecution = profiler()->getTotalExecution(); |
|
|
|
|
45
|
|
|
$metrics = profiler()->getMetrics(); |
46
|
|
|
|
47
|
|
|
$totalTime = 0; |
48
|
|
|
$totalMemory = 0; |
49
|
|
|
|
50
|
|
|
foreach ($metrics as $metric) { |
51
|
|
|
$totalTime += ($metric->endTime - $metric->startTime); |
52
|
|
|
$totalMemory += ($metric->endMemory - $metric->startMemory); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$segmentDuration = $this->roundTo($totalTime * 1000, 5); |
56
|
|
|
$segmentCount = (int)ceil($totalTime / $segmentDuration); |
57
|
|
|
|
58
|
|
|
$displayTime = $segmentCount * $segmentDuration; |
59
|
|
|
|
60
|
|
|
foreach ($metrics as $metric) { |
61
|
|
|
$metric->offset = (($metric->startTime - $totalExecution->startTime) * 1000 / $displayTime) * 100; |
62
|
|
|
$metric->length = (($metric->endTime - $metric->startTime) * 1000 / $displayTime) * 100; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$totalTime = $totalExecution->getFormattedTime($totalTime, 2); |
66
|
|
|
$totalMemory = $totalExecution->getFormattedMemorySize($totalMemory); |
67
|
|
|
$allocatedMemory = $totalExecution->getFormattedMemorySize(memory_get_usage(true)); |
68
|
|
|
$peakMemory = $totalExecution->getFormattedMemorySize(memory_get_peak_usage(true)); |
69
|
|
|
|
70
|
|
|
$files = $this->getFiles(); |
71
|
|
|
$logs = $this->getLogs(); |
72
|
|
|
$vars = $this->getVars(); |
73
|
|
|
$database = $this->getDatabase(); |
74
|
|
|
|
75
|
|
|
ob_start(); |
76
|
|
|
include __DIR__ . '/Views/Toolbar.php'; |
77
|
|
|
$output = ob_get_contents(); |
78
|
|
|
ob_end_clean(); |
79
|
|
|
|
80
|
|
|
return $output; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// ------------------------------------------------------------------------ |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Toolbar::roundTo |
87
|
|
|
* |
88
|
|
|
* Rounds a number to the nearest incremental value. |
89
|
|
|
* |
90
|
|
|
* @param float $number |
91
|
|
|
* @param int $increments |
92
|
|
|
* |
93
|
|
|
* @return float |
94
|
|
|
*/ |
95
|
|
|
protected function roundTo($number, $increments = 5) |
96
|
|
|
{ |
97
|
|
|
$increments = 1 / $increments; |
98
|
|
|
|
99
|
|
|
return (ceil($number * $increments) / $increments); |
100
|
|
|
} |
101
|
|
|
//-------------------------------------------------------------------- |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Toolbar::getFiles |
105
|
|
|
* |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
|
public function getFiles() |
109
|
|
|
{ |
110
|
|
|
$files = get_included_files(); |
111
|
|
|
|
112
|
|
|
if (class_exists('\O2System\Framework', false)) { |
113
|
|
|
foreach ($files as $key => $file) { |
114
|
|
|
|
115
|
|
|
if (strpos($file, 'autoload.php') !== false) { |
116
|
|
|
unset($files[ $key ]); |
117
|
|
|
continue; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$files[ $key ] = str_replace(PATH_ROOT, DIRECTORY_SEPARATOR, $file); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $files; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
// ------------------------------------------------------------------------ |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Toolbar::getLogs |
131
|
|
|
* |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
|
|
public function getLogs() |
135
|
|
|
{ |
136
|
|
|
$logs = []; |
137
|
|
|
|
138
|
|
|
if (function_exists('logger')) { |
139
|
|
|
$logs = logger()->getLines(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $logs; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
// ------------------------------------------------------------------------ |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Toolbar::getVars |
149
|
|
|
* |
150
|
|
|
* @return \ArrayObject |
151
|
|
|
*/ |
152
|
|
|
public function getVars() |
153
|
|
|
{ |
154
|
|
|
$vars = new \ArrayObject([], \ArrayObject::ARRAY_AS_PROPS); |
155
|
|
|
|
156
|
|
|
$vars->env = $_ENV; |
|
|
|
|
157
|
|
|
$vars->server = $_SERVER; |
|
|
|
|
158
|
|
|
$vars->session = $_SESSION; |
|
|
|
|
159
|
|
|
$vars->cookies = $_COOKIE; |
|
|
|
|
160
|
|
|
$vars->get = $_GET; |
|
|
|
|
161
|
|
|
$vars->post = $_POST; |
|
|
|
|
162
|
|
|
$vars->files = $_FILES; |
|
|
|
|
163
|
|
|
|
164
|
|
|
if (function_exists('apache_request_headers')) { |
165
|
|
|
$vars->headers = apache_request_headers(); |
|
|
|
|
166
|
|
|
} elseif (function_exists('getallheaders')) { |
167
|
|
|
$vars->headers = getallheaders(); |
168
|
|
|
} else { |
169
|
|
|
$vars->headers = []; |
170
|
|
|
foreach ($_SERVER as $name => $value) { |
171
|
|
|
if (substr($name, 0, 5) == 'HTTP_') { |
172
|
|
|
$vars->headers[ str_replace(' ', '-', |
173
|
|
|
ucwords(strtolower(str_replace('_', ' ', substr($name, 5))))) ] = $value; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $vars; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
// ------------------------------------------------------------------------ |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Toolbar::getDatabase |
185
|
|
|
* |
186
|
|
|
* @return array |
187
|
|
|
*/ |
188
|
|
|
public function getDatabase() |
189
|
|
|
{ |
190
|
|
|
$database = []; |
191
|
|
|
|
192
|
|
|
if (class_exists('O2System\Framework', false)) { |
193
|
|
|
if (services()->has('database')) { |
|
|
|
|
194
|
|
|
$connections = database()->getIterator(); |
|
|
|
|
195
|
|
|
|
196
|
|
|
foreach ($connections as $offset => $connection) { |
197
|
|
|
$database[ $offset ] = $connection->getQueries(); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
return $database; |
203
|
|
|
} |
204
|
|
|
} |