- improvement of {debug} tag output

This commit is contained in:
uwe.tews@googlemail.com
2010-12-01 21:19:20 +00:00
parent e80dd75958
commit 176c3d625c
3 changed files with 171 additions and 124 deletions

View File

@@ -1,4 +1,7 @@
===== SVN trunk ===== ===== SVN trunk =====
01/12/2010
- improvement of {debug} tag output
27/11/2010 27/11/2010
-change run output filter before cache file is written. (same as in Smarty2) -change run output filter before cache file is written. (same as in Smarty2)

View File

@@ -86,8 +86,9 @@ td {
</head> </head>
<body> <body>
<h1>Smarty Debug Console - Total Time {$execution_time|string_format:"%.5f"}</h1> <h1>Smarty Debug Console - {if isset($template_name)}{$template_name|debug_print_var}{else}Total Time {$execution_time|string_format:"%.5f"}{/if}</h1>
{if !empty($template_data)}
<h2>included templates &amp; config files (load time in seconds)</h2> <h2>included templates &amp; config files (load time in seconds)</h2>
<div> <div>
@@ -99,6 +100,7 @@ td {
<br> <br>
{/foreach} {/foreach}
</div> </div>
{/if}
<h2>assigned template variables</h2> <h2>assigned template variables</h2>
@@ -124,13 +126,14 @@ td {
</html> </html>
{/capture} {/capture}
<script type="text/javascript"> <script type="text/javascript">
{literal} if ( self.name == '' ) { {$id = $template_name|default:''|md5}
var title = 'Console'; if ( self.name == '' ) {
var title = 'Console{$id}';
} }
else { else {
var title = 'Console_' + self.name; var title = 'Console_{$id}' + self.name;
}{/literal} }
_smarty_console = window.open("",title.value,"width=680,height=600,resizable,scrollbars=yes"); _smarty_console = window.open("","console{$id}","width=680,height=600,resizable,scrollbars=yes");
_smarty_console.document.write("{$debug_output|escape:'javascript'}"); _smarty_console.document.write("{$debug_output|escape:'javascript'}");
_smarty_console.document.close(); _smarty_console.document.close();
</script> </script>

View File

@@ -1,133 +1,174 @@
<?php <?php
/** /**
* Smarty Internal Plugin Debug * Smarty Internal Plugin Debug
* *
* Class to collect data for the Smarty Debugging Consol * Class to collect data for the Smarty Debugging Consol
* *
* @package Smarty * @package Smarty
* @subpackage Debug * @subpackage Debug
* @author Uwe Tews * @author Uwe Tews
*/ */
/** /**
* Smarty Internal Plugin Debug Class * Smarty Internal Plugin Debug Class
*/ */
class Smarty_Internal_Debug extends Smarty_Internal_Data { class Smarty_Internal_Debug extends Smarty_Internal_Data {
// template data // template data
static $template_data = array(); static $template_data = array();
/** /**
* Start logging of compile time * Start logging of compile time
*/ */
public static function start_compile($template) public static function start_compile($template)
{ {
$key = self::get_key($template); $key = self::get_key($template);
self::$template_data[$key]['start_time'] = microtime(true); self::$template_data[$key]['start_time'] = microtime(true);
} }
/** /**
* End logging of compile time * End logging of compile time
*/ */
public static function end_compile($template) public static function end_compile($template)
{ {
$key = self::get_key($template); $key = self::get_key($template);
self::$template_data[$key]['compile_time'] += microtime(true) - self::$template_data[$key]['start_time']; self::$template_data[$key]['compile_time'] += microtime(true) - self::$template_data[$key]['start_time'];
} }
/** /**
* Start logging of render time * Start logging of render time
*/ */
public static function start_render($template) public static function start_render($template)
{ {
$key = self::get_key($template); $key = self::get_key($template);
self::$template_data[$key]['start_time'] = microtime(true); self::$template_data[$key]['start_time'] = microtime(true);
} }
/** /**
* End logging of compile time * End logging of compile time
*/ */
public static function end_render($template) public static function end_render($template)
{ {
$key = self::get_key($template); $key = self::get_key($template);
self::$template_data[$key]['render_time'] += microtime(true) - self::$template_data[$key]['start_time']; self::$template_data[$key]['render_time'] += microtime(true) - self::$template_data[$key]['start_time'];
} }
/** /**
* Start logging of cache time * Start logging of cache time
*/ */
public static function start_cache($template) public static function start_cache($template)
{ {
$key = self::get_key($template); $key = self::get_key($template);
self::$template_data[$key]['start_time'] = microtime(true); self::$template_data[$key]['start_time'] = microtime(true);
} }
/** /**
* End logging of cache time * End logging of cache time
*/ */
public static function end_cache($template) public static function end_cache($template)
{ {
$key = self::get_key($template); $key = self::get_key($template);
self::$template_data[$key]['cache_time'] += microtime(true) - self::$template_data[$key]['start_time']; self::$template_data[$key]['cache_time'] += microtime(true) - self::$template_data[$key]['start_time'];
} }
/** /**
* Opens a window for the Smarty Debugging Consol and display the data * Opens a window for the Smarty Debugging Consol and display the data
*/ */
public static function display_debug($obj) public static function display_debug($obj)
{ {
// prepare information of assigned variables // prepare information of assigned variables
$ptr = $obj; $ptr = self::get_debug_vars($obj);
while (isset($ptr->parent)) { if ($obj instanceof Smarty) {
$ptr = $ptr->parent; $smarty = $obj;
} } else {
if ($obj instanceof Smarty) { $smarty = $obj->smarty;
$smarty = $obj; }
} else { $_assigned_vars = $ptr->tpl_vars;
$smarty = $obj->smarty; ksort($_assigned_vars);
} $_config_vars = $ptr->config_vars;
$_assigned_vars = $ptr->tpl_vars; ksort($_config_vars);
ksort($_assigned_vars); $ldelim = $smarty->left_delimiter;
$_config_vars = $ptr->config_vars; $rdelim = $smarty->right_delimiter;
ksort($_config_vars); $smarty->left_delimiter = '{';
$ldelim = $smarty->left_delimiter; $smarty->right_delimiter = '}';
$rdelim = $smarty->right_delimiter; $_template = new Smarty_Internal_Template ($smarty->debug_tpl, $smarty);
$smarty->left_delimiter = '{'; $_template->caching = false;
$smarty->right_delimiter = '}'; $_template->force_compile = false;
$_template = new Smarty_Internal_Template ($smarty->debug_tpl, $smarty); $_template->disableSecurity();
$_template->caching = false; $_template->cache_id = null;
$_template->force_compile = false; $_template->compile_id = null;
$_template->disableSecurity(); if ($obj instanceof Smarty_Internal_Template) {
$_template->cache_id = null; $_template->assign('template_name',$obj->resource_type.':'.$obj->resource_name);
$_template->compile_id = null; }
$_template->assign('template_data', self::$template_data); if ($obj instanceof Smarty) {
$_template->assign('assigned_vars', $_assigned_vars); $_template->assign('template_data', self::$template_data);
$_template->assign('config_vars', $_config_vars); } else {
$_template->assign('execution_time', microtime(true) - $smarty->start_time); $_template->assign('template_data', null);
echo $_template->getRenderedTemplate(); }
$smarty->left_delimiter = $ldelim; $_template->assign('assigned_vars', $_assigned_vars);
$smarty->right_delimiter = $rdelim; $_template->assign('config_vars', $_config_vars);
} $_template->assign('execution_time', microtime(true) - $smarty->start_time);
echo $_template->getRenderedTemplate();
$smarty->left_delimiter = $ldelim;
$smarty->right_delimiter = $rdelim;
}
/*
* Recursively gets variables from all template/data scopes
*/
public static function get_debug_vars($obj, $is_parent = false)
{
$config_vars = $obj->config_vars;
$tpl_vars = array();
foreach ($obj->tpl_vars as $key => $var) {
$tpl_vars[$key] = clone $var;
if (true) {
if ($obj instanceof Smarty_Internal_Template) {
$tpl_vars[$key]->scope = $obj->resource_type.':'.$obj->resource_name;
} elseif ($obj instanceof Smarty_Data) {
$tpl_vars[$key]->scope = 'Data object';
} else {
$tpl_vars[$key]->scope = 'Smarty root';
}
} else {
$tpl_vars[$key]->scope = 'local';
}
}
/** if (isset($obj->parent)) {
* get_key $parent = self::get_debug_vars($obj->parent, true);
*/ $tpl_vars = array_merge($parent->tpl_vars, $tpl_vars);
static function get_key($template) $config_vars = array_merge($parent->config_vars, $config_vars);
{ } else {
// calculate Uid if not already done foreach (Smarty::$global_tpl_vars as $name => $var) {
if ($template->templateUid == '') { if (!array_key_exists($name, $tpl_vars)) {
$template->getTemplateFilepath(); $clone = clone $var;
} $clone->scope = 'Global';
$key = $template->templateUid; $tpl_vars[$name] = $clone;
if (isset(self::$template_data[$key])) { }
return $key; }
} else { }
self::$template_data[$key]['name'] = $template->getTemplateFilepath(); return (object) array('tpl_vars' => $tpl_vars, 'config_vars' => $config_vars);
self::$template_data[$key]['compile_time'] = 0; }
self::$template_data[$key]['render_time'] = 0;
self::$template_data[$key]['cache_time'] = 0; /**
return $key; * get_key
} */
} static function get_key($template)
} {
// calculate Uid if not already done
if ($template->templateUid == '') {
$template->getTemplateFilepath();
}
$key = $template->templateUid;
if (isset(self::$template_data[$key])) {
return $key;
} else {
self::$template_data[$key]['name'] = $template->getTemplateFilepath();
self::$template_data[$key]['compile_time'] = 0;
self::$template_data[$key]['render_time'] = 0;
self::$template_data[$key]['cache_time'] = 0;
return $key;
}
}
}
?> ?>