3 * Smarty Internal Plugin Debug
4 * Class to collect data for the Smarty Debugging Consol
12 * Smarty Internal Plugin Debug Class
17 class Smarty_Internal_Debug extends Smarty_Internal_Data
24 public static $template_data = array();
27 * List of uid's which shall be ignored
31 public static $ignore_uid = array();
36 * @param object $template
38 public static function ignore($template)
40 // calculate Uid if not already done
41 if ($template->source->uid == '') {
42 $template->source->filepath;
44 self::$ignore_uid[$template->source->uid] = true;
48 * Start logging of compile time
50 * @param object $template
52 public static function start_compile($template)
54 static $_is_stringy = array('string' => true, 'eval' => true);
55 if (!empty($template->compiler->trace_uid)) {
56 $key = $template->compiler->trace_uid;
57 if (!isset(self::$template_data[$key])) {
58 if (isset($_is_stringy[$template->source->type])) {
59 self::$template_data[$key]['name'] = '\'' . substr($template->source->name, 0, 25) . '...\'';
61 self::$template_data[$key]['name'] = $template->source->filepath;
63 self::$template_data[$key]['compile_time'] = 0;
64 self::$template_data[$key]['render_time'] = 0;
65 self::$template_data[$key]['cache_time'] = 0;
68 if (isset(self::$ignore_uid[$template->source->uid])) {
71 $key = self::get_key($template);
73 self::$template_data[$key]['start_time'] = microtime(true);
77 * End logging of compile time
79 * @param object $template
81 public static function end_compile($template)
83 if (!empty($template->compiler->trace_uid)) {
84 $key = $template->compiler->trace_uid;
86 if (isset(self::$ignore_uid[$template->source->uid])) {
90 $key = self::get_key($template);
92 self::$template_data[$key]['compile_time'] += microtime(true) - self::$template_data[$key]['start_time'];
96 * Start logging of render time
98 * @param object $template
100 public static function start_render($template)
102 $key = self::get_key($template);
103 self::$template_data[$key]['start_time'] = microtime(true);
107 * End logging of compile time
109 * @param object $template
111 public static function end_render($template)
113 $key = self::get_key($template);
114 self::$template_data[$key]['render_time'] += microtime(true) - self::$template_data[$key]['start_time'];
118 * Start logging of cache time
120 * @param object $template cached template
122 public static function start_cache($template)
124 $key = self::get_key($template);
125 self::$template_data[$key]['start_time'] = microtime(true);
129 * End logging of cache time
131 * @param object $template cached template
133 public static function end_cache($template)
135 $key = self::get_key($template);
136 self::$template_data[$key]['cache_time'] += microtime(true) - self::$template_data[$key]['start_time'];
140 * Opens a window for the Smarty Debugging Consol and display the data
142 * @param Smarty_Internal_Template|Smarty $obj object to debug
144 public static function display_debug($obj)
146 // prepare information of assigned variables
147 $ptr = self::get_debug_vars($obj);
148 if ($obj instanceof Smarty) {
149 $smarty = clone $obj;
151 $smarty = clone $obj->smarty;
153 $_assigned_vars = $ptr->tpl_vars;
154 ksort($_assigned_vars);
155 $_config_vars = $ptr->config_vars;
156 ksort($_config_vars);
157 $smarty->registered_filters = array();
158 $smarty->autoload_filters = array();
159 $smarty->default_modifiers = array();
160 $smarty->force_compile = false;
161 $smarty->left_delimiter = '{';
162 $smarty->right_delimiter = '}';
163 $smarty->debugging = false;
164 $smarty->debugging_ctrl = 'NONE';
165 $smarty->force_compile = false;
166 $_template = new Smarty_Internal_Template($smarty->debug_tpl, $smarty);
167 $_template->caching = false;
168 $_template->disableSecurity();
169 $_template->cache_id = null;
170 $_template->compile_id = null;
171 if ($obj instanceof Smarty_Internal_Template) {
172 $_template->assign('template_name', $obj->source->type . ':' . $obj->source->name);
174 if ($obj instanceof Smarty) {
175 $_template->assign('template_data', self::$template_data);
177 $_template->assign('template_data', null);
179 $_template->assign('assigned_vars', $_assigned_vars);
180 $_template->assign('config_vars', $_config_vars);
181 $_template->assign('execution_time', microtime(true) - $smarty->start_time);
182 echo $_template->fetch();
186 * Recursively gets variables from all template/data scopes
188 * @param Smarty_Internal_Template|Smarty_Data $obj object to debug
192 public static function get_debug_vars($obj)
194 $config_vars = $obj->config_vars;
196 foreach ($obj->tpl_vars as $key => $var) {
197 $tpl_vars[$key] = clone $var;
198 if ($obj instanceof Smarty_Internal_Template) {
199 $tpl_vars[$key]->scope = $obj->source->type . ':' . $obj->source->name;
200 } elseif ($obj instanceof Smarty_Data) {
201 $tpl_vars[$key]->scope = 'Data object';
203 $tpl_vars[$key]->scope = 'Smarty root';
207 if (isset($obj->parent)) {
208 $parent = self::get_debug_vars($obj->parent);
209 $tpl_vars = array_merge($parent->tpl_vars, $tpl_vars);
210 $config_vars = array_merge($parent->config_vars, $config_vars);
212 foreach (Smarty::$global_tpl_vars as $name => $var) {
213 if (!array_key_exists($name, $tpl_vars)) {
215 $clone->scope = 'Global';
216 $tpl_vars[$name] = $clone;
221 return (object) array('tpl_vars' => $tpl_vars, 'config_vars' => $config_vars);
225 * Return key into $template_data for template
227 * @param object $template template object
229 * @return string key into $template_data
231 private static function get_key($template)
233 static $_is_stringy = array('string' => true, 'eval' => true);
234 // calculate Uid if not already done
235 if ($template->source->uid == '') {
236 $template->source->filepath;
238 $key = $template->source->uid;
239 if (isset(self::$template_data[$key])) {
242 if (isset($_is_stringy[$template->source->type])) {
243 self::$template_data[$key]['name'] = '\'' . substr($template->source->name, 0, 25) . '...\'';
245 self::$template_data[$key]['name'] = $template->source->filepath;
247 self::$template_data[$key]['compile_time'] = 0;
248 self::$template_data[$key]['render_time'] = 0;
249 self::$template_data[$key]['cache_time'] = 0;