3 * Smarty Internal Plugin Debug
5 * Class to collect data for the Smarty Debugging Consol
13 * Smarty Internal Plugin Debug Class
18 class Smarty_Internal_Debug extends Smarty_Internal_Data {
25 public static $template_data = array();
28 * Start logging of compile time
30 * @param object $template
32 public static function start_compile($template)
34 $key = self::get_key($template);
35 self::$template_data[$key]['start_time'] = microtime(true);
39 * End logging of compile time
41 * @param object $template
43 public static function end_compile($template)
45 $key = self::get_key($template);
46 self::$template_data[$key]['compile_time'] += microtime(true) - self::$template_data[$key]['start_time'];
50 * Start logging of render time
52 * @param object $template
54 public static function start_render($template)
56 $key = self::get_key($template);
57 self::$template_data[$key]['start_time'] = microtime(true);
61 * End logging of compile time
63 * @param object $template
65 public static function end_render($template)
67 $key = self::get_key($template);
68 self::$template_data[$key]['render_time'] += microtime(true) - self::$template_data[$key]['start_time'];
72 * Start logging of cache time
74 * @param object $template cached template
76 public static function start_cache($template)
78 $key = self::get_key($template);
79 self::$template_data[$key]['start_time'] = microtime(true);
83 * End logging of cache time
85 * @param object $template cached template
87 public static function end_cache($template)
89 $key = self::get_key($template);
90 self::$template_data[$key]['cache_time'] += microtime(true) - self::$template_data[$key]['start_time'];
94 * Opens a window for the Smarty Debugging Consol and display the data
96 * @param Smarty_Internal_Template|Smarty $obj object to debug
98 public static function display_debug($obj)
100 // prepare information of assigned variables
101 $ptr = self::get_debug_vars($obj);
102 if ($obj instanceof Smarty) {
103 $smarty = clone $obj;
105 $smarty = clone $obj->smarty;
107 $_assigned_vars = $ptr->tpl_vars;
108 ksort($_assigned_vars);
109 $_config_vars = $ptr->config_vars;
110 ksort($_config_vars);
111 $smarty->registered_filters = array();
112 $smarty->autoload_filters = array();
113 $smarty->default_modifiers = array();
114 $smarty->force_compile = false;
115 $smarty->left_delimiter = '{';
116 $smarty->right_delimiter = '}';
117 $smarty->debugging = false;
118 $smarty->force_compile = false;
119 $_template = new Smarty_Internal_Template($smarty->debug_tpl, $smarty);
120 $_template->caching = false;
121 $_template->disableSecurity();
122 $_template->cache_id = null;
123 $_template->compile_id = null;
124 if ($obj instanceof Smarty_Internal_Template) {
125 $_template->assign('template_name', $obj->source->type . ':' . $obj->source->name);
127 if ($obj instanceof Smarty) {
128 $_template->assign('template_data', self::$template_data);
130 $_template->assign('template_data', null);
132 $_template->assign('assigned_vars', $_assigned_vars);
133 $_template->assign('config_vars', $_config_vars);
134 $_template->assign('execution_time', microtime(true) - $smarty->start_time);
135 echo $_template->fetch();
139 * Recursively gets variables from all template/data scopes
141 * @param Smarty_Internal_Template|Smarty_Data $obj object to debug
144 public static function get_debug_vars($obj)
146 $config_vars = $obj->config_vars;
148 foreach ($obj->tpl_vars as $key => $var) {
149 $tpl_vars[$key] = clone $var;
150 if ($obj instanceof Smarty_Internal_Template) {
151 $tpl_vars[$key]->scope = $obj->source->type . ':' . $obj->source->name;
152 } elseif ($obj instanceof Smarty_Data) {
153 $tpl_vars[$key]->scope = 'Data object';
155 $tpl_vars[$key]->scope = 'Smarty root';
159 if (isset($obj->parent)) {
160 $parent = self::get_debug_vars($obj->parent);
161 $tpl_vars = array_merge($parent->tpl_vars, $tpl_vars);
162 $config_vars = array_merge($parent->config_vars, $config_vars);
164 foreach (Smarty::$global_tpl_vars as $name => $var) {
165 if (!array_key_exists($name, $tpl_vars)) {
167 $clone->scope = 'Global';
168 $tpl_vars[$name] = $clone;
172 return (object) array('tpl_vars' => $tpl_vars, 'config_vars' => $config_vars);
176 * Return key into $template_data for template
178 * @param object $template template object
179 * @return string key into $template_data
181 private static function get_key($template)
183 static $_is_stringy = array('string' => true, 'eval' => true);
184 // calculate Uid if not already done
185 if ($template->source->uid == '') {
186 $template->source->filepath;
188 $key = $template->source->uid;
189 if (isset(self::$template_data[$key])) {
192 if (isset($_is_stringy[$template->source->type])) {
193 self::$template_data[$key]['name'] = '\''.substr($template->source->name,0,25).'...\'';
195 self::$template_data[$key]['name'] = $template->source->filepath;
197 self::$template_data[$key]['compile_time'] = 0;
198 self::$template_data[$key]['render_time'] = 0;
199 self::$template_data[$key]['cache_time'] = 0;