|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/***************************************** |
|
4
|
|
|
* Title : Php Quick Profiler Display Class |
|
5
|
|
|
* Author : Created by Ryan Campbell |
|
6
|
|
|
* URL : http://particletree.com/features/php-quick-profiler/ |
|
7
|
|
|
* Description : This is a hacky way of pushing profiling logic to the |
|
8
|
|
|
* PQP HTML. This is great because it will just work in your project, |
|
9
|
|
|
* but it is hard to maintain and read. |
|
10
|
|
|
*****************************************/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Particletree\Pqp; |
|
13
|
|
|
|
|
14
|
|
|
class Display |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** @var array */ |
|
18
|
|
|
protected $output; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct() |
|
21
|
|
|
{ |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function setConsole(Console $console) |
|
25
|
|
|
{ |
|
26
|
|
|
$console_data = array( |
|
27
|
|
|
'messages' => array(), |
|
28
|
|
|
'count' => array( |
|
29
|
|
|
'log' => 0, |
|
30
|
|
|
'memory' => 0, |
|
31
|
|
|
'error' => 0, |
|
32
|
|
|
'speed' => 0 |
|
33
|
|
|
) |
|
34
|
|
|
); |
|
35
|
|
|
foreach ($console->getLogs() as $log) { |
|
36
|
|
|
switch($log['type']) { |
|
37
|
|
View Code Duplication |
case 'log': |
|
|
|
|
|
|
38
|
|
|
$message = array( |
|
39
|
|
|
'data' => print_r($log['data'], true), |
|
40
|
|
|
'type' => 'log' |
|
41
|
|
|
); |
|
42
|
|
|
$console_data['count']['log']++; |
|
43
|
|
|
break; |
|
44
|
|
|
case 'memory': |
|
45
|
|
|
$message = array( |
|
46
|
|
|
'name' => $log['name'], |
|
47
|
|
|
'data' => self::getReadableMemory($log['data']), |
|
48
|
|
|
'type' => 'memory' |
|
49
|
|
|
); |
|
50
|
|
|
if (!empty($log['data_type'])) { |
|
51
|
|
|
$message['data_type'] = $log['data_type']; |
|
52
|
|
|
} |
|
53
|
|
|
$console_data['count']['memory']++; |
|
54
|
|
|
break; |
|
55
|
|
|
case 'error': |
|
56
|
|
|
$message = array( |
|
57
|
|
|
'data' => $log['data'], |
|
58
|
|
|
'file' => $log['file'], |
|
59
|
|
|
'line' => $log['line'], |
|
60
|
|
|
'type' => 'error' |
|
61
|
|
|
); |
|
62
|
|
|
$console_data['count']['error']++; |
|
63
|
|
|
break; |
|
64
|
|
|
case 'speed': |
|
65
|
|
|
$elapsedTime = $log['data'] - $this->startTime; |
|
|
|
|
|
|
66
|
|
|
$message = array( |
|
67
|
|
|
'name' => $log['name'], |
|
68
|
|
|
'data' => self::getReadableTime($elapsedTime), |
|
69
|
|
|
'type' => 'speed' |
|
70
|
|
|
); |
|
71
|
|
|
$console_data['count']['speed']++; |
|
72
|
|
|
break; |
|
73
|
|
View Code Duplication |
default: |
|
|
|
|
|
|
74
|
|
|
$message = array( |
|
75
|
|
|
'data' => "Unrecognized console log type: {$log['type']}", |
|
76
|
|
|
'type' => 'error' |
|
77
|
|
|
); |
|
78
|
|
|
$console_data['count']['error']++; |
|
79
|
|
|
break; |
|
80
|
|
|
} |
|
81
|
|
|
array_push($console_data['messages'], $message); |
|
82
|
|
|
} |
|
83
|
|
|
$this->output['console'] = $console_data; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function setFileData(array $file_data) |
|
87
|
|
|
{ |
|
88
|
|
|
$this->output['files'] = $file_data['files']; |
|
89
|
|
|
$this->output['fileTotals'] = $file_data['fileTotals']; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Sets memory data |
|
94
|
|
|
* |
|
95
|
|
|
* @param array $data |
|
96
|
|
|
*/ |
|
97
|
|
|
public function setMemoryData(array $data) |
|
98
|
|
|
{ |
|
99
|
|
|
$this->output['memory'] = array( |
|
100
|
|
|
'used' => self::getReadableMemory($data['used']), |
|
101
|
|
|
'allowed' => $data['allowed'] |
|
102
|
|
|
); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function setQueryData(array $query_data) |
|
|
|
|
|
|
106
|
|
|
{ |
|
107
|
|
|
// the void |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Sets speed data |
|
112
|
|
|
* |
|
113
|
|
|
* @param array $data |
|
114
|
|
|
*/ |
|
115
|
|
|
public function setSpeedData(array $data) |
|
116
|
|
|
{ |
|
117
|
|
|
$this->output['speed'] = array( |
|
118
|
|
|
'elapsed' => self::getReadableTime($data['elapsed']), |
|
119
|
|
|
'allowed' => self::getReadableTime($data['allowed'], 0) |
|
120
|
|
|
); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Static formatter for human-readable time |
|
125
|
|
|
* Only handles time up to 60 minutes gracefully |
|
126
|
|
|
* |
|
127
|
|
|
* @param double $time |
|
128
|
|
|
* @param integer $decimals |
|
129
|
|
|
* @return string |
|
130
|
|
|
*/ |
|
131
|
|
View Code Duplication |
public static function getReadableTime($time, $decimals = 3) |
|
|
|
|
|
|
132
|
|
|
{ |
|
133
|
|
|
$unit = 's'; |
|
134
|
|
|
if ($time < 1) { |
|
135
|
|
|
$time *= 1000; |
|
136
|
|
|
$unit = 'ms'; |
|
137
|
|
|
} else if ($time > 60) { |
|
138
|
|
|
$time /= 60; |
|
139
|
|
|
$unit = 'm'; |
|
140
|
|
|
} |
|
141
|
|
|
$time = number_format($time, $decimals); |
|
142
|
|
|
return "{$time} {$unit}"; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Static formatter for human-readable memory |
|
147
|
|
|
* |
|
148
|
|
|
* @param double $size |
|
149
|
|
|
* @param integer $decimals |
|
150
|
|
|
*/ |
|
151
|
|
|
public static function getReadableMemory($size, $decimals = 2) |
|
152
|
|
|
{ |
|
153
|
|
|
$unitOptions = array('b', 'k', 'M', 'G'); |
|
154
|
|
|
|
|
155
|
|
|
$base = log($size, 1024); |
|
156
|
|
|
|
|
157
|
|
|
$memory = round(pow(1024, $base - floor($base)), $decimals); |
|
158
|
|
|
$unit = $unitOptions[floor($base)]; |
|
159
|
|
|
return "{$memory} {$unit}"; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
public function __invoke() |
|
163
|
|
|
{ |
|
164
|
|
|
$output = $this->output; |
|
|
|
|
|
|
165
|
|
|
require_once __DIR__ .'/../asset/display.tpl.php'; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.