1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Debug toolbar integration for template output |
5
|
|
|
* |
6
|
|
|
* PHP Version 5 |
7
|
|
|
* |
8
|
|
|
* @category Core |
9
|
|
|
* @package Debug |
10
|
|
|
* @author Hans-Joachim Piepereit <[email protected]> |
11
|
|
|
* @copyright 2013 cSphere Team |
12
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
13
|
|
|
* @link http://www.csphere.eu |
14
|
|
|
**/ |
15
|
|
|
|
16
|
|
|
namespace csphere\core\debug; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Debug toolbar integration for template output |
20
|
|
|
* |
21
|
|
|
* @category Core |
22
|
|
|
* @package Debug |
23
|
|
|
* @author Hans-Joachim Piepereit <[email protected]> |
24
|
|
|
* @copyright 2013 cSphere Team |
25
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
26
|
|
|
* @link http://www.csphere.eu |
27
|
|
|
**/ |
28
|
|
|
|
29
|
|
|
class Toolbar |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Holds the loader object |
33
|
|
|
**/ |
34
|
|
|
private $_loader = null; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Holds the view object |
38
|
|
|
**/ |
39
|
|
|
private $_view = null; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* These components are separated from logs |
43
|
|
|
**/ |
44
|
|
|
private $_specials = ['database', 'errors', 'includes']; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Holds the most current data |
48
|
|
|
**/ |
49
|
|
|
private $_content = ''; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Create the string to attach to the template |
53
|
|
|
* |
54
|
|
|
* @return \csphere\core\debug\Toolbar |
55
|
|
|
**/ |
|
|
|
|
56
|
|
|
|
57
|
|
|
public function __construct() |
58
|
|
|
{ |
59
|
|
|
// Add stylesheet for debug |
60
|
|
|
\csphere\core\template\Hooks::stylesheet('debug', 'core_debug.css'); |
61
|
|
|
|
62
|
|
|
// Create loader and view object |
63
|
|
|
$this->_loader = \csphere\core\service\Locator::get(); |
64
|
|
|
$this->_view = $this->_loader->load('view'); |
65
|
|
|
|
66
|
|
|
// Get parsetime and memory usage |
67
|
|
|
$parsetime = $this->_view->getOption('parsetime'); |
68
|
|
|
$memory = memory_get_peak_usage(); |
69
|
|
|
|
70
|
|
|
// Start and fill stats array |
71
|
|
|
$stats = ['parsetime' => $parsetime, 'memory' => $memory]; |
72
|
|
|
|
73
|
|
|
// Prepare logs and specials |
74
|
|
|
$logs = $this->_logs(); |
75
|
|
|
|
76
|
|
|
// Generate toolbar content |
77
|
|
|
$this->_content = $this->_format($stats, $logs); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Return the string to attach to the template |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
**/ |
|
|
|
|
85
|
|
|
|
86
|
|
|
public function content() |
87
|
|
|
{ |
88
|
|
|
return $this->_content; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get and improve log array |
93
|
|
|
* |
94
|
|
|
* @return array |
95
|
|
|
**/ |
|
|
|
|
96
|
|
|
|
97
|
|
|
private function _logs() |
98
|
|
|
{ |
99
|
|
|
// Load log content |
100
|
|
|
$logs = $this->_loader->load('logs'); |
101
|
|
|
$info = $logs->info(); |
102
|
|
|
|
103
|
|
|
// Make sure that all needed log keys are set |
104
|
|
|
$info['database'] = empty($info['database']) ? [] : $info['database']; |
105
|
|
|
$info['errors'] = empty($info['errors']) ? [] : $info['errors']; |
106
|
|
|
|
107
|
|
|
ksort($info); |
108
|
|
|
|
109
|
|
|
// Add included files to logs |
110
|
|
|
$includes = get_included_files(); |
111
|
|
|
$path = \csphere\core\init\path(); |
112
|
|
|
|
113
|
|
|
foreach ($includes AS $include) { |
114
|
|
|
|
115
|
|
|
$include = str_replace('\\', '/', $include); |
116
|
|
|
$info['includes'][] = str_replace($path, '', $include); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $info; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Collect the overall debug content |
124
|
|
|
* |
125
|
|
|
* @param array $stats Data of statistics |
126
|
|
|
* @param array $logs Content of log service |
127
|
|
|
* |
128
|
|
|
* @return string |
129
|
|
|
**/ |
|
|
|
|
130
|
|
|
|
131
|
|
|
private function _format(array $stats, array $logs) |
132
|
|
|
{ |
133
|
|
|
// Check for PHP settings that are problematic for developers |
134
|
|
|
$error = $this->_settings(); |
135
|
|
|
|
136
|
|
|
if ($error != '') { |
137
|
|
|
|
138
|
|
|
$logs['errors'][] = $error; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
// Details first to not overwrite something |
142
|
|
|
$data = $this->_formatDetails($logs); |
143
|
|
|
|
144
|
|
|
// Add PHP version and engine |
145
|
|
|
$data['php_full'] = phpversion(); |
146
|
|
|
$data['php_engine'] = 'PHP'; |
147
|
|
|
$sapi = strtolower(php_sapi_name()); |
148
|
|
|
|
149
|
|
|
if ($sapi == 'srv') { |
150
|
|
|
|
151
|
|
|
$data['php_engine'] = 'HHVM'; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
// Move parsetime and memory usage to data array |
155
|
|
|
$data['memory'] = \csphere\core\files\File::size($stats['memory']); |
156
|
|
|
|
157
|
|
|
$data['parsetime'] = \csphere\core\date\Microtime::untilNow( |
158
|
|
|
$stats['parsetime'] |
159
|
|
|
); |
160
|
|
|
|
161
|
|
|
// Format logbar |
162
|
|
|
$data['logbar'] = $this->_formatLogbar($logs); |
163
|
|
|
|
164
|
|
|
// Send data to view and fetch box result |
165
|
|
|
$this->_view->template('debug', 'core_toolbar', $data, true); |
166
|
|
|
$result = $this->_view->box(); |
167
|
|
|
|
168
|
|
|
return $result; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Create the content for the logbar |
173
|
|
|
* |
174
|
|
|
* @param array $logs Content of log service |
175
|
|
|
* |
176
|
|
|
* @return array |
177
|
|
|
**/ |
|
|
|
|
178
|
|
|
|
179
|
|
|
private function _formatLogbar(array $logs) |
180
|
|
|
{ |
181
|
|
|
$data = []; |
182
|
|
|
|
183
|
|
|
foreach ($logs AS $name => $entries) { |
184
|
|
|
|
185
|
|
|
if (!in_array($name, $this->_specials)) { |
186
|
|
|
|
187
|
|
|
$data[] = ['component' => $name, 'count' => count($entries)]; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $data; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Create the content for the details |
196
|
|
|
* |
197
|
|
|
* @param array $logs Content of log service |
198
|
|
|
* |
199
|
|
|
* @return array |
200
|
|
|
**/ |
|
|
|
|
201
|
|
|
|
202
|
|
|
private function _formatDetails(array $logs) |
203
|
|
|
{ |
204
|
|
|
$data = ['count' => [], 'logs' => []]; |
205
|
|
|
|
206
|
|
|
// Format log information for output |
207
|
|
|
foreach ($logs AS $name => $entries) { |
208
|
|
|
|
209
|
|
|
$sub = []; |
210
|
|
|
|
211
|
|
|
foreach ($entries AS $part) { |
212
|
|
|
|
213
|
|
|
$sub[] = ['text' => $part]; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
// Append to logs except for specials |
217
|
|
|
if (in_array($name, $this->_specials)) { |
218
|
|
|
|
219
|
|
|
$data[$name] = $sub; |
220
|
|
|
$data['count'][$name] = count($sub); |
221
|
|
|
|
222
|
|
|
} else { |
223
|
|
|
|
224
|
|
|
$data['logs'][] = ['name' => $name, 'entries' => $sub]; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
$data['count']['logs'] = count($data['logs']); |
229
|
|
|
|
230
|
|
|
return $data; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Check for development specific settings |
235
|
|
|
* |
236
|
|
|
* @return string |
237
|
|
|
**/ |
|
|
|
|
238
|
|
|
|
239
|
|
|
private function _settings() |
240
|
|
|
{ |
241
|
|
|
$error = ''; |
242
|
|
|
|
243
|
|
|
// Check for OPcache validation |
244
|
|
|
if (extension_loaded('Zend OPcache')) { |
245
|
|
|
|
246
|
|
|
$val = ini_get('opcache.validate_timestamps'); |
247
|
|
|
$frq = ini_get('opcache.revalidate_freq'); |
248
|
|
|
|
249
|
|
|
if (empty($val)) { |
250
|
|
|
|
251
|
|
|
$error .= 'opcache.validate_timestamps=1' . "\n"; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
if (!empty($frq)) { |
255
|
|
|
|
256
|
|
|
$error .= 'opcache.revalidate_freq=0' . "\n"; |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
if ($error != '') { |
261
|
|
|
|
262
|
|
|
$lang = \csphere\core\translation\Fetch::key('debug', 'php_ini_dev'); |
263
|
|
|
$error = $lang . ':' . "\n" . $error; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
return $error; |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|
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.