1 <?php
3 /**
4 * Smarty Internal Plugin Debug
5 *
6 * Class to collect data for the Smarty Debugging Consol
7 *
8 * @package Smarty
9 * @subpackage Debug
10 * @author Uwe Tews
11 */
12 /**
13 * Smarty Internal Plugin Debug Class
14 */
15 class Smarty_Internal_Debug extends Smarty_Internal_Data {
16 // template data
17 static $template_data = array();
19 /**
20 * Start logging of compile time
21 */
22 public static function start_compile($template)
23 {
24 $key = self::get_key($template);
25 self::$template_data[$key]['start_time'] = self::get_time();
26 }
28 /**
29 * End logging of compile time
30 */
31 public static function end_compile($template)
32 {
33 $key = self::get_key($template);
34 self::$template_data[$key]['compile_time'] += self::get_time() - self::$template_data[$key]['start_time'];
35 }
37 /**
38 * Start logging of render time
39 */
40 public static function start_render($template)
41 {
42 $key = self::get_key($template);
43 self::$template_data[$key]['start_time'] = self::get_time();
44 }
46 /**
47 * End logging of compile time
48 */
49 public static function end_render($template)
50 {
51 $key = self::get_key($template);
52 self::$template_data[$key]['render_time'] += self::get_time() - self::$template_data[$key]['start_time'];
53 }
55 /**
56 * Start logging of cache time
57 */
58 public static function start_cache($template)
59 {
60 $key = self::get_key($template);
61 self::$template_data[$key]['start_time'] = self::get_time();
62 }
64 /**
65 * End logging of cache time
66 */
67 public static function end_cache($template)
68 {
69 $key = self::get_key($template);
70 self::$template_data[$key]['cache_time'] += self::get_time() - self::$template_data[$key]['start_time'];
71 }
72 /**
73 * Opens a window for the Smarty Debugging Consol and display the data
74 */
75 public static function display_debug($smarty)
76 {
77 // prepare information of assigned variables
78 $_assigned_vars = $smarty->tpl_vars;
79 ksort($_assigned_vars);
80 $_config_vars = $smarty->config_vars;
81 ksort($_config_vars);
82 $_template = new Smarty_Template ($smarty->debug_tpl, $smarty);
83 $_template->caching = false;
84 $_template->force_compile = false;
85 $_template->security = false;
86 $_template->cache_id = null;
87 $_template->compile_id = null;
88 $_template->assign('template_data', self::$template_data);
89 $_template->assign('assigned_vars', $_assigned_vars);
90 $_template->assign('config_vars', $_config_vars);
91 $_template->assign('execution_time', $smarty->_get_time() - $smarty->start_time);
92 echo $smarty->fetch($_template);
93 }
95 /**
96 * get_key
97 */
98 static function get_key($template)
99 {
100 // calculate Uid if not already done
101 if ($template->templateUid == '') {
102 $template->getTemplateFilepath();
103 }
104 $key = $template->templateUid;
105 if (isset(self::$template_data[$key])) {
106 return $key;
107 } else {
108 self::$template_data[$key]['name'] = $template->getTemplateFilepath();
109 self::$template_data[$key]['compile_time'] = 0;
110 self::$template_data[$key]['render_time'] = 0;
111 self::$template_data[$key]['cache_time'] = 0;
112 return $key;
113 }
114 }
116 /**
117 * return current time
118 *
119 * @returns double current time
120 */
121 static function get_time()
122 {
123 $_mtime = microtime();
124 $_mtime = explode(" ", $_mtime);
125 return (double)($_mtime[1]) + (double)($_mtime[0]);
126 }
127 }
129 ?>