-
Notifications
You must be signed in to change notification settings - Fork 715
/
Debug.php
380 lines (353 loc) · 12.1 KB
/
Debug.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<?php
namespace Smarty;
/**
* Smarty Internal Plugin Debug
* Class to collect data for the Smarty Debugging Console
*
* @author Uwe Tews
*/
/**
* Smarty Internal Plugin Debug Class
*
*/
class Debug extends Data
{
/**
* template data
*
* @var array
*/
public $template_data = array();
/**
* List of uid's which shall be ignored
*
* @var array
*/
public $ignore_uid = array();
/**
* Index of display() and fetch() calls
*
* @var int
*/
public $index = 0;
/**
* Counter for window offset
*
* @var int
*/
public $offset = 0;
/**
* Start logging template
*
* @param \Smarty\Template $template template
* @param null $mode true: display false: fetch null: subtemplate
*/
public function start_template(\Smarty\Template $template, $mode = null)
{
if (isset($mode) && !$template->_isSubTpl()) {
$this->index++;
$this->offset++;
$this->template_data[ $this->index ] = null;
}
$key = $this->get_key($template);
$this->template_data[ $this->index ][ $key ][ 'start_template_time' ] = microtime(true);
}
/**
* End logging of cache time
*
* @param \Smarty\Template $template cached template
*/
public function end_template(\Smarty\Template $template)
{
$key = $this->get_key($template);
$this->template_data[ $this->index ][ $key ][ 'total_time' ] +=
microtime(true) - $this->template_data[ $this->index ][ $key ][ 'start_template_time' ];
}
/**
* Start logging of compile time
*
* @param \Smarty\Template $template
*/
public function start_compile(\Smarty\Template $template)
{
static $_is_stringy = array('string' => true, 'eval' => true);
if (!empty($template->getCompiler()->trace_uid)) {
$key = $template->getCompiler()->trace_uid;
if (!isset($this->template_data[ $this->index ][ $key ])) {
$this->saveTemplateData($_is_stringy, $template, $key);
}
} else {
if (isset($this->ignore_uid[ $template->getSource()->uid ])) {
return;
}
$key = $this->get_key($template);
}
$this->template_data[ $this->index ][ $key ][ 'start_time' ] = microtime(true);
}
/**
* End logging of compile time
*
* @param \Smarty\Template $template
*/
public function end_compile(\Smarty\Template $template)
{
if (!empty($template->getCompiler()->trace_uid)) {
$key = $template->getCompiler()->trace_uid;
} else {
if (isset($this->ignore_uid[ $template->getSource()->uid ])) {
return;
}
$key = $this->get_key($template);
}
$this->template_data[ $this->index ][ $key ][ 'compile_time' ] +=
microtime(true) - $this->template_data[ $this->index ][ $key ][ 'start_time' ];
}
/**
* Start logging of render time
*
* @param \Smarty\Template $template
*/
public function start_render(\Smarty\Template $template)
{
$key = $this->get_key($template);
$this->template_data[ $this->index ][ $key ][ 'start_time' ] = microtime(true);
}
/**
* End logging of compile time
*
* @param \Smarty\Template $template
*/
public function end_render(\Smarty\Template $template)
{
$key = $this->get_key($template);
$this->template_data[ $this->index ][ $key ][ 'render_time' ] +=
microtime(true) - $this->template_data[ $this->index ][ $key ][ 'start_time' ];
}
/**
* Start logging of cache time
*
* @param \Smarty\Template $template cached template
*/
public function start_cache(\Smarty\Template $template)
{
$key = $this->get_key($template);
$this->template_data[ $this->index ][ $key ][ 'start_time' ] = microtime(true);
}
/**
* End logging of cache time
*
* @param \Smarty\Template $template cached template
*/
public function end_cache(\Smarty\Template $template)
{
$key = $this->get_key($template);
$this->template_data[ $this->index ][ $key ][ 'cache_time' ] +=
microtime(true) - $this->template_data[ $this->index ][ $key ][ 'start_time' ];
}
/**
* Register template object
*
* @param \Smarty\Template $template cached template
*/
public function register_template(\Smarty\Template $template)
{
}
/**
* Register data object
*
* @param Data $data data object
*/
public static function register_data(Data $data)
{
}
/**
* Opens a window for the Smarty Debugging Console and display the data
*
* @param \Smarty\Template|\Smarty $obj object to debug
* @param bool $full
*
* @throws \Exception
* @throws \Smarty\Exception
*/
public function display_debug($obj, $full = false)
{
if (!$full) {
$this->offset++;
$savedIndex = $this->index;
$this->index = 9999;
}
$smarty = $obj->getSmarty();
// create fresh instance of smarty for displaying the debug console
// to avoid problems if the application did overload the Smarty class
$debObj = new \Smarty\Smarty();
// copy the working dirs from application
$debObj->setCompileDir($smarty->getCompileDir());
$debObj->compile_check = \Smarty::COMPILECHECK_ON;
$debObj->security_policy = null;
$debObj->debugging = false;
$debObj->debugging_ctrl = 'NONE';
$debObj->error_reporting = E_ALL & ~E_NOTICE;
$debObj->debug_tpl = $smarty->debug_tpl ?? 'file:' . __DIR__ . '/../debug.tpl';
$debObj->registered_resources = array();
$debObj->registered_filters = array();
$debObj->escape_html = true;
$debObj->caching = \Smarty::CACHING_OFF;
// prepare information of assigned variables
$ptr = $this->get_debug_vars($obj);
$_assigned_vars = $ptr->tpl_vars;
ksort($_assigned_vars);
$_config_vars = $ptr->config_vars;
ksort($_config_vars);
$debugging = $smarty->debugging;
$templateName = $obj->getSource()->type . ':' . $obj->getSource()->name;
$displayMode = $debugging === 2 || !$full;
$offset = $this->offset * 50;
$_template = $debObj->doCreateTemplate($debObj->debug_tpl);
if ($obj instanceof \Smarty\Template) {
$_template->assign('template_name', $templateName);
} elseif ($obj instanceof Smarty || $full) {
$_template->assign('template_data', $this->template_data[$this->index]);
} else {
$_template->assign('template_data', null);
}
$_template->assign('assigned_vars', $_assigned_vars);
$_template->assign('config_vars', $_config_vars);
$_template->assign('execution_time', microtime(true) - $smarty->start_time);
$_template->assign('targetWindow', $displayMode ? md5("$offset$templateName") : '__Smarty__');
$_template->assign('offset', $offset);
echo $_template->fetch();
if (isset($full)) {
$this->index--;
}
if (!$full) {
$this->index = $savedIndex;
}
}
/**
* Recursively gets variables from all template/data scopes
*
* @param \Smarty\Data $obj object to debug
*
* @return \StdClass
*/
private function get_debug_vars($obj)
{
$config_vars = array();
foreach ($obj->config_vars as $key => $var) {
$config_vars[$key]['value'] = $var;
$config_vars[$key]['scope'] = get_class($obj) . ':' . spl_object_id($obj);
}
$tpl_vars = array();
foreach ($obj->tpl_vars as $key => $var) {
foreach ($var as $varkey => $varvalue) {
if ($varkey === 'value') {
$tpl_vars[ $key ][ $varkey ] = $varvalue;
} else {
if ($varkey === 'nocache') {
if ($varvalue === true) {
$tpl_vars[ $key ][ $varkey ] = $varvalue;
}
} else {
if ($varkey !== 'scope' || $varvalue !== 0) {
$tpl_vars[ $key ][ 'attributes' ][ $varkey ] = $varvalue;
}
}
}
}
$tpl_vars[$key]['scope'] = get_class($obj) . ':' . spl_object_id($obj);
}
if (isset($obj->parent)) {
$parent = $this->get_debug_vars($obj->parent);
foreach ($parent->tpl_vars as $name => $pvar) {
if (isset($tpl_vars[ $name ]) && $tpl_vars[ $name ][ 'value' ] === $pvar[ 'value' ]) {
$tpl_vars[ $name ][ 'scope' ] = $pvar[ 'scope' ];
}
}
$tpl_vars = array_merge($parent->tpl_vars, $tpl_vars);
foreach ($parent->config_vars as $name => $pvar) {
if (isset($config_vars[ $name ]) && $config_vars[ $name ][ 'value' ] === $pvar[ 'value' ]) {
$config_vars[ $name ][ 'scope' ] = $pvar[ 'scope' ];
}
}
$config_vars = array_merge($parent->config_vars, $config_vars);
}
return (object)array('tpl_vars' => $tpl_vars, 'config_vars' => $config_vars);
}
/**
* Return key into $template_data for template
*
* @param \Smarty\Template $template template object
*
* @return string key into $template_data
*/
private function get_key(\Smarty\Template $template)
{
static $_is_stringy = array('string' => true, 'eval' => true);
$key = $template->getSource()->uid;
if (isset($this->template_data[ $this->index ][ $key ])) {
return $key;
} else {
$this->saveTemplateData($_is_stringy, $template, $key);
$this->template_data[ $this->index ][ $key ][ 'total_time' ] = 0;
return $key;
}
}
/**
* Ignore template
*
* @param \Smarty\Template $template
*/
public function ignore(\Smarty\Template $template)
{
$this->ignore_uid[$template->getSource()->uid] = true;
}
/**
* handle 'URL' debugging mode
*
* @param \Smarty $smarty
*/
public function debugUrl(\Smarty $smarty)
{
if (isset($_SERVER[ 'QUERY_STRING' ])) {
$_query_string = $_SERVER[ 'QUERY_STRING' ];
} else {
$_query_string = '';
}
if (false !== strpos($_query_string, $smarty->smarty_debug_id)) {
if (false !== strpos($_query_string, $smarty->smarty_debug_id . '=on')) {
// enable debugging for this browser session
setcookie('SMARTY_DEBUG', true);
$smarty->debugging = true;
} elseif (false !== strpos($_query_string, $smarty->smarty_debug_id . '=off')) {
// disable debugging for this browser session
setcookie('SMARTY_DEBUG', false);
$smarty->debugging = false;
} else {
// enable debugging for this page
$smarty->debugging = true;
}
} else {
if (isset($_COOKIE[ 'SMARTY_DEBUG' ])) {
$smarty->debugging = true;
}
}
}
/**
* @param array $_is_stringy
* @param \Smarty\Template $template
* @param string $key
*
* @return void
*/
private function saveTemplateData(array $_is_stringy, \Smarty\Template $template, string $key): void {
if (isset($_is_stringy[$template->getSource()->type])) {
$this->template_data[$this->index][$key]['name'] =
'\'' . substr($template->getSource()->name, 0, 25) . '...\'';
} else {
$this->template_data[$this->index][$key]['name'] = $template->getSource()->getResourceName();
}
$this->template_data[$this->index][$key]['compile_time'] = 0;
$this->template_data[$this->index][$key]['render_time'] = 0;
$this->template_data[$this->index][$key]['cache_time'] = 0;
}
}