1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: 12: 13: 14: 15: 16:
17: class Smarty_Internal_Debug extends Smarty_Internal_Data
18: {
19: 20: 21: 22: 23:
24: public static $template_data = array();
25:
26: 27: 28: 29: 30:
31: public static $ignore_uid = array();
32:
33: 34: 35: 36: 37:
38: public static function ignore($template)
39: {
40:
41: if ($template->source->uid == '') {
42: $template->source->filepath;
43: }
44: self::$ignore_uid[$template->source->uid] = true;
45: }
46:
47: 48: 49: 50: 51:
52: public static function start_compile($template)
53: {
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) . '...\'';
60: } else {
61: self::$template_data[$key]['name'] = $template->source->filepath;
62: }
63: self::$template_data[$key]['compile_time'] = 0;
64: self::$template_data[$key]['render_time'] = 0;
65: self::$template_data[$key]['cache_time'] = 0;
66: }
67: } else {
68: if (isset(self::$ignore_uid[$template->source->uid])) {
69: return;
70: }
71: $key = self::get_key($template);
72: }
73: self::$template_data[$key]['start_time'] = microtime(true);
74: }
75:
76: 77: 78: 79: 80:
81: public static function end_compile($template)
82: {
83: if (!empty($template->compiler->trace_uid)) {
84: $key = $template->compiler->trace_uid;
85: } else {
86: if (isset(self::$ignore_uid[$template->source->uid])) {
87: return;
88: }
89:
90: $key = self::get_key($template);
91: }
92: self::$template_data[$key]['compile_time'] += microtime(true) - self::$template_data[$key]['start_time'];
93: }
94:
95: 96: 97: 98: 99:
100: public static function start_render($template)
101: {
102: $key = self::get_key($template);
103: self::$template_data[$key]['start_time'] = microtime(true);
104: }
105:
106: 107: 108: 109: 110:
111: public static function end_render($template)
112: {
113: $key = self::get_key($template);
114: self::$template_data[$key]['render_time'] += microtime(true) - self::$template_data[$key]['start_time'];
115: }
116:
117: 118: 119: 120: 121:
122: public static function start_cache($template)
123: {
124: $key = self::get_key($template);
125: self::$template_data[$key]['start_time'] = microtime(true);
126: }
127:
128: 129: 130: 131: 132:
133: public static function end_cache($template)
134: {
135: $key = self::get_key($template);
136: self::$template_data[$key]['cache_time'] += microtime(true) - self::$template_data[$key]['start_time'];
137: }
138:
139: 140: 141: 142: 143:
144: public static function display_debug($obj)
145: {
146:
147: $ptr = self::get_debug_vars($obj);
148: if ($obj instanceof Smarty) {
149: $smarty = clone $obj;
150: } else {
151: $smarty = clone $obj->smarty;
152: }
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);
173: }
174: if ($obj instanceof Smarty) {
175: $_template->assign('template_data', self::$template_data);
176: } else {
177: $_template->assign('template_data', null);
178: }
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();
183: }
184:
185: 186: 187: 188: 189: 190: 191:
192: public static function get_debug_vars($obj)
193: {
194: $config_vars = $obj->config_vars;
195: $tpl_vars = array();
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';
202: } else {
203: $tpl_vars[$key]->scope = 'Smarty root';
204: }
205: }
206:
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);
211: } else {
212: foreach (Smarty::$global_tpl_vars as $name => $var) {
213: if (!array_key_exists($name, $tpl_vars)) {
214: $clone = clone $var;
215: $clone->scope = 'Global';
216: $tpl_vars[$name] = $clone;
217: }
218: }
219: }
220:
221: return (object) array('tpl_vars' => $tpl_vars, 'config_vars' => $config_vars);
222: }
223:
224: 225: 226: 227: 228: 229: 230:
231: private static function get_key($template)
232: {
233: static $_is_stringy = array('string' => true, 'eval' => true);
234:
235: if ($template->source->uid == '') {
236: $template->source->filepath;
237: }
238: $key = $template->source->uid;
239: if (isset(self::$template_data[$key])) {
240: return $key;
241: } else {
242: if (isset($_is_stringy[$template->source->type])) {
243: self::$template_data[$key]['name'] = '\'' . substr($template->source->name, 0, 25) . '...\'';
244: } else {
245: self::$template_data[$key]['name'] = $template->source->filepath;
246: }
247: self::$template_data[$key]['compile_time'] = 0;
248: self::$template_data[$key]['render_time'] = 0;
249: self::$template_data[$key]['cache_time'] = 0;
250:
251: return $key;
252: }
253: }
254: }
255: