4 * Smarty Internal Plugin Debug
6 * Class to collect data for the Smarty Debugging Consol
14 * Smarty Internal Plugin Debug Class
16 class Smarty_Internal_Debug extends Smarty_Internal_Data {
18 static $template_data = array();
21 * Start logging of compile time
23 public static function start_compile($template)
25 $key = self::get_key($template);
26 self::$template_data[$key]['start_time'] = microtime(true);
30 * End logging of compile time
32 public static function end_compile($template)
34 $key = self::get_key($template);
35 self::$template_data[$key]['compile_time'] += microtime(true) - self::$template_data[$key]['start_time'];
39 * Start logging of render time
41 public static function start_render($template)
43 $key = self::get_key($template);
44 self::$template_data[$key]['start_time'] = microtime(true);
48 * End logging of compile time
50 public static function end_render($template)
52 $key = self::get_key($template);
53 self::$template_data[$key]['render_time'] += microtime(true) - self::$template_data[$key]['start_time'];
57 * Start logging of cache time
59 public static function start_cache($template)
61 $key = self::get_key($template);
62 self::$template_data[$key]['start_time'] = microtime(true);
66 * End logging of cache time
68 public static function end_cache($template)
70 $key = self::get_key($template);
71 self::$template_data[$key]['cache_time'] += microtime(true) - self::$template_data[$key]['start_time'];
74 * Opens a window for the Smarty Debugging Consol and display the data
76 public static function display_debug($obj)
78 // prepare information of assigned variables
79 $ptr = self::get_debug_vars($obj);
80 if ($obj instanceof Smarty) {
83 $smarty = $obj->smarty;
85 $_assigned_vars = $ptr->tpl_vars;
86 ksort($_assigned_vars);
87 $_config_vars = $ptr->config_vars;
89 $ldelim = $smarty->left_delimiter;
90 $rdelim = $smarty->right_delimiter;
91 $smarty->left_delimiter = '{';
92 $smarty->right_delimiter = '}';
93 $_template = new Smarty_Internal_Template ($smarty->debug_tpl, $smarty);
94 $_template->caching = false;
95 $_template->force_compile = false;
96 $_template->disableSecurity();
97 $_template->cache_id = null;
98 $_template->compile_id = null;
99 if ($obj instanceof Smarty_Internal_Template) {
100 $_template->assign('template_name',$obj->resource_type.':'.$obj->resource_name);
102 if ($obj instanceof Smarty) {
103 $_template->assign('template_data', self::$template_data);
105 $_template->assign('template_data', null);
107 $_template->assign('assigned_vars', $_assigned_vars);
108 $_template->assign('config_vars', $_config_vars);
109 $_template->assign('execution_time', microtime(true) - $smarty->start_time);
110 echo $_template->getRenderedTemplate();
111 $smarty->left_delimiter = $ldelim;
112 $smarty->right_delimiter = $rdelim;
115 * Recursively gets variables from all template/data scopes
117 public static function get_debug_vars($obj)
119 $config_vars = $obj->config_vars;
121 foreach ($obj->tpl_vars as $key => $var) {
122 $tpl_vars[$key] = clone $var;
123 if ($obj instanceof Smarty_Internal_Template) {
124 $tpl_vars[$key]->scope = $obj->resource_type.':'.$obj->resource_name;
125 } elseif ($obj instanceof Smarty_Data) {
126 $tpl_vars[$key]->scope = 'Data object';
128 $tpl_vars[$key]->scope = 'Smarty root';
132 if (isset($obj->parent)) {
133 $parent = self::get_debug_vars($obj->parent);
134 $tpl_vars = array_merge($parent->tpl_vars, $tpl_vars);
135 $config_vars = array_merge($parent->config_vars, $config_vars);
137 foreach (Smarty::$global_tpl_vars as $name => $var) {
138 if (!array_key_exists($name, $tpl_vars)) {
140 $clone->scope = 'Global';
141 $tpl_vars[$name] = $clone;
145 return (object) array('tpl_vars' => $tpl_vars, 'config_vars' => $config_vars);
151 static function get_key($template)
153 // calculate Uid if not already done
154 if ($template->templateUid == '') {
155 $template->getTemplateFilepath();
157 $key = $template->templateUid;
158 if (isset(self::$template_data[$key])) {
161 self::$template_data[$key]['name'] = $template->getTemplateFilepath();
162 self::$template_data[$key]['compile_time'] = 0;
163 self::$template_data[$key]['render_time'] = 0;
164 self::$template_data[$key]['cache_time'] = 0;