3 * Smarty Internal Plugin Debug
4 * Class to collect data for the Smarty Debugging Console
12 * Smarty Internal Plugin Debug Class
17 class Smarty_Internal_Debug extends Smarty_Internal_Data
24 public $template_data = array();
27 * List of uid's which shall be ignored
31 public $ignore_uid = array();
34 * Index of display() and fetch() calls
41 * Counter for window offset
48 * Start logging template
50 * @param \Smarty_Internal_Template $template template
51 * @param null $mode true: display false: fetch null: subtemplate
53 public function start_template(Smarty_Internal_Template $template, $mode = null)
55 if (isset($mode) && !$template->_isSubTpl()) {
58 $this->template_data[ $this->index ] = null;
60 $key = $this->get_key($template);
61 $this->template_data[ $this->index ][ $key ][ 'start_template_time' ] = microtime(true);
65 * End logging of cache time
67 * @param \Smarty_Internal_Template $template cached template
69 public function end_template(Smarty_Internal_Template $template)
71 $key = $this->get_key($template);
72 $this->template_data[ $this->index ][ $key ][ 'total_time' ] +=
73 microtime(true) - $this->template_data[ $this->index ][ $key ][ 'start_template_time' ];
74 //$this->template_data[$this->index][$key]['properties'] = $template->properties;
78 * Start logging of compile time
80 * @param \Smarty_Internal_Template $template
82 public function start_compile(Smarty_Internal_Template $template)
84 static $_is_stringy = array('string' => true, 'eval' => true);
85 if (!empty($template->compiler->trace_uid)) {
86 $key = $template->compiler->trace_uid;
87 if (!isset($this->template_data[ $this->index ][ $key ])) {
88 if (isset($_is_stringy[ $template->source->type ])) {
89 $this->template_data[ $this->index ][ $key ][ 'name' ] =
90 '\'' . substr($template->source->name, 0, 25) . '...\'';
92 $this->template_data[ $this->index ][ $key ][ 'name' ] = $template->source->filepath;
94 $this->template_data[ $this->index ][ $key ][ 'compile_time' ] = 0;
95 $this->template_data[ $this->index ][ $key ][ 'render_time' ] = 0;
96 $this->template_data[ $this->index ][ $key ][ 'cache_time' ] = 0;
99 if (isset($this->ignore_uid[ $template->source->uid ])) {
102 $key = $this->get_key($template);
104 $this->template_data[ $this->index ][ $key ][ 'start_time' ] = microtime(true);
108 * End logging of compile time
110 * @param \Smarty_Internal_Template $template
112 public function end_compile(Smarty_Internal_Template $template)
114 if (!empty($template->compiler->trace_uid)) {
115 $key = $template->compiler->trace_uid;
117 if (isset($this->ignore_uid[ $template->source->uid ])) {
121 $key = $this->get_key($template);
123 $this->template_data[ $this->index ][ $key ][ 'compile_time' ] +=
124 microtime(true) - $this->template_data[ $this->index ][ $key ][ 'start_time' ];
128 * Start logging of render time
130 * @param \Smarty_Internal_Template $template
132 public function start_render(Smarty_Internal_Template $template)
134 $key = $this->get_key($template);
135 $this->template_data[ $this->index ][ $key ][ 'start_time' ] = microtime(true);
139 * End logging of compile time
141 * @param \Smarty_Internal_Template $template
143 public function end_render(Smarty_Internal_Template $template)
145 $key = $this->get_key($template);
146 $this->template_data[ $this->index ][ $key ][ 'render_time' ] +=
147 microtime(true) - $this->template_data[ $this->index ][ $key ][ 'start_time' ];
151 * Start logging of cache time
153 * @param \Smarty_Internal_Template $template cached template
155 public function start_cache(Smarty_Internal_Template $template)
157 $key = $this->get_key($template);
158 $this->template_data[ $this->index ][ $key ][ 'start_time' ] = microtime(true);
162 * End logging of cache time
164 * @param \Smarty_Internal_Template $template cached template
166 public function end_cache(Smarty_Internal_Template $template)
168 $key = $this->get_key($template);
169 $this->template_data[ $this->index ][ $key ][ 'cache_time' ] +=
170 microtime(true) - $this->template_data[ $this->index ][ $key ][ 'start_time' ];
174 * Register template object
176 * @param \Smarty_Internal_Template $template cached template
178 public function register_template(Smarty_Internal_Template $template)
183 * Register data object
185 * @param \Smarty_Data $data data object
187 public static function register_data(Smarty_Data $data)
192 * Opens a window for the Smarty Debugging Console and display the data
194 * @param Smarty_Internal_Template|Smarty $obj object to debug
197 public function display_debug($obj, $full = false)
201 $savedIndex = $this->index;
204 $smarty = $obj->_getSmartyObj();
205 // create fresh instance of smarty for displaying the debug console
206 // to avoid problems if the application did overload the Smarty class
207 $debObj = new Smarty();
208 // copy the working dirs from application
209 $debObj->setCompileDir($smarty->getCompileDir());
210 // init properties by hand as user may have edited the original Smarty class
211 $debObj->setPluginsDir(is_dir(__DIR__ . '/../plugins') ? __DIR__ . '/../plugins' : $smarty->getPluginsDir());
212 $debObj->force_compile = false;
213 $debObj->compile_check = true;
214 $debObj->left_delimiter = '{';
215 $debObj->right_delimiter = '}';
216 $debObj->security_policy = null;
217 $debObj->debugging = false;
218 $debObj->debugging_ctrl = 'NONE';
219 $debObj->error_reporting = E_ALL & ~E_NOTICE;
220 $debObj->debug_tpl = isset($smarty->debug_tpl) ? $smarty->debug_tpl : 'file:' . __DIR__ . '/../debug.tpl';
221 $debObj->registered_plugins = array();
222 $debObj->registered_resources = array();
223 $debObj->registered_filters = array();
224 $debObj->autoload_filters = array();
225 $debObj->default_modifiers = array();
226 $debObj->escape_html = true;
227 $debObj->caching = false;
228 $debObj->compile_id = null;
229 $debObj->cache_id = null;
230 // prepare information of assigned variables
231 $ptr = $this->get_debug_vars($obj);
232 $_assigned_vars = $ptr->tpl_vars;
233 ksort($_assigned_vars);
234 $_config_vars = $ptr->config_vars;
235 ksort($_config_vars);
236 $debugging = $smarty->debugging;
238 $_template = new Smarty_Internal_Template($debObj->debug_tpl, $debObj);
239 if ($obj->_isTplObj()) {
240 $_template->assign('template_name', $obj->source->type . ':' . $obj->source->name);
242 if ($obj->_objType == 1 || $full) {
243 $_template->assign('template_data', $this->template_data[ $this->index ]);
245 $_template->assign('template_data', null);
247 $_template->assign('assigned_vars', $_assigned_vars);
248 $_template->assign('config_vars', $_config_vars);
249 $_template->assign('execution_time', microtime(true) - $smarty->start_time);
250 $_template->assign('display_mode', $debugging == 2 || !$full);
251 $_template->assign('offset', $this->offset * 50);
252 echo $_template->fetch();
257 $this->index = $savedIndex;
262 * Recursively gets variables from all template/data scopes
264 * @param Smarty_Internal_Template|Smarty_Data $obj object to debug
268 public function get_debug_vars($obj)
270 $config_vars = array();
271 foreach ($obj->config_vars as $key => $var) {
272 $config_vars[ $key ][ 'value' ] = $var;
273 if ($obj->_isTplObj()) {
274 $config_vars[ $key ][ 'scope' ] = $obj->source->type . ':' . $obj->source->name;
275 } elseif ($obj->_isDataObj()) {
276 $tpl_vars[ $key ][ 'scope' ] = $obj->dataObjectName;
278 $config_vars[ $key ][ 'scope' ] = 'Smarty object';
282 foreach ($obj->tpl_vars as $key => $var) {
283 foreach ($var as $varkey => $varvalue) {
284 if ($varkey == 'value') {
285 $tpl_vars[ $key ][ $varkey ] = $varvalue;
287 if ($varkey == 'nocache') {
288 if ($varvalue == true) {
289 $tpl_vars[ $key ][ $varkey ] = $varvalue;
292 if ($varkey != 'scope' || $varvalue !== 0) {
293 $tpl_vars[ $key ][ 'attributes' ][ $varkey ] = $varvalue;
298 if ($obj->_isTplObj()) {
299 $tpl_vars[ $key ][ 'scope' ] = $obj->source->type . ':' . $obj->source->name;
300 } elseif ($obj->_isDataObj()) {
301 $tpl_vars[ $key ][ 'scope' ] = $obj->dataObjectName;
303 $tpl_vars[ $key ][ 'scope' ] = 'Smarty object';
307 if (isset($obj->parent)) {
308 $parent = $this->get_debug_vars($obj->parent);
309 foreach ($parent->tpl_vars as $name => $pvar) {
310 if (isset($tpl_vars[ $name ]) && $tpl_vars[ $name ][ 'value' ] === $pvar[ 'value' ]) {
311 $tpl_vars[ $name ][ 'scope' ] = $pvar[ 'scope' ];
314 $tpl_vars = array_merge($parent->tpl_vars, $tpl_vars);
316 foreach ($parent->config_vars as $name => $pvar) {
317 if (isset($config_vars[ $name ]) && $config_vars[ $name ][ 'value' ] === $pvar[ 'value' ]) {
318 $config_vars[ $name ][ 'scope' ] = $pvar[ 'scope' ];
321 $config_vars = array_merge($parent->config_vars, $config_vars);
323 foreach (Smarty::$global_tpl_vars as $key => $var) {
324 if (!array_key_exists($key, $tpl_vars)) {
325 foreach ($var as $varkey => $varvalue) {
326 if ($varkey == 'value') {
327 $tpl_vars[ $key ][ $varkey ] = $varvalue;
329 if ($varkey == 'nocache') {
330 if ($varvalue == true) {
331 $tpl_vars[ $key ][ $varkey ] = $varvalue;
334 if ($varkey != 'scope' || $varvalue !== 0) {
335 $tpl_vars[ $key ][ 'attributes' ][ $varkey ] = $varvalue;
340 $tpl_vars[ $key ][ 'scope' ] = 'Global';
345 return (object) array('tpl_vars' => $tpl_vars, 'config_vars' => $config_vars);
349 * Return key into $template_data for template
351 * @param \Smarty_Internal_Template $template template object
353 * @return string key into $template_data
355 private function get_key(Smarty_Internal_Template $template)
357 static $_is_stringy = array('string' => true, 'eval' => true);
358 // calculate Uid if not already done
359 if ($template->source->uid == '') {
360 $template->source->filepath;
362 $key = $template->source->uid;
363 if (isset($this->template_data[ $this->index ][ $key ])) {
366 if (isset($_is_stringy[ $template->source->type ])) {
367 $this->template_data[ $this->index ][ $key ][ 'name' ] =
368 '\'' . substr($template->source->name, 0, 25) . '...\'';
370 $this->template_data[ $this->index ][ $key ][ 'name' ] = $template->source->filepath;
372 $this->template_data[ $this->index ][ $key ][ 'compile_time' ] = 0;
373 $this->template_data[ $this->index ][ $key ][ 'render_time' ] = 0;
374 $this->template_data[ $this->index ][ $key ][ 'cache_time' ] = 0;
375 $this->template_data[ $this->index ][ $key ][ 'total_time' ] = 0;
384 * @param \Smarty_Internal_Template $template
386 public function ignore(Smarty_Internal_Template $template)
388 // calculate Uid if not already done
389 if ($template->source->uid == '') {
390 $template->source->filepath;
392 $this->ignore_uid[ $template->source->uid ] = true;
396 * handle 'URL' debugging mode
398 * @param Smarty $smarty
400 public function debugUrl(Smarty $smarty)
402 if (isset($_SERVER[ 'QUERY_STRING' ])) {
403 $_query_string = $_SERVER[ 'QUERY_STRING' ];
407 if (false !== strpos($_query_string, $smarty->smarty_debug_id)) {
408 if (false !== strpos($_query_string, $smarty->smarty_debug_id . '=on')) {
409 // enable debugging for this browser session
410 setcookie('SMARTY_DEBUG', true);
411 $smarty->debugging = true;
412 } elseif (false !== strpos($_query_string, $smarty->smarty_debug_id . '=off')) {
413 // disable debugging for this browser session
414 setcookie('SMARTY_DEBUG', false);
415 $smarty->debugging = false;
417 // enable debugging for this page
418 $smarty->debugging = true;
421 if (isset($_COOKIE[ 'SMARTY_DEBUG' ])) {
422 $smarty->debugging = true;