Initial repo created
[timetracker.git] / WEB-INF / lib / smarty / sysplugins / smarty_internal_debug.php
1 <?php
2
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 /**
14 * Smarty Internal Plugin Debug Class
15 */
16 class Smarty_Internal_Debug extends Smarty_Internal_Data {
17         // template data
18         static $template_data = array();
19
20         /**
21         * Start logging of compile time
22         */
23         public static function start_compile($template)
24         {
25                 $key = self::get_key($template);
26                 self::$template_data[$key]['start_time'] = microtime(true);
27         }
28
29         /**
30         * End logging of compile time
31         */
32         public static function end_compile($template)
33         {
34                 $key = self::get_key($template);
35                 self::$template_data[$key]['compile_time'] += microtime(true) - self::$template_data[$key]['start_time'];
36         }
37
38         /**
39         * Start logging of render time
40         */
41         public static function start_render($template)
42         {
43                 $key = self::get_key($template);
44                 self::$template_data[$key]['start_time'] = microtime(true);
45         }
46
47         /**
48         * End logging of compile time
49         */
50         public static function end_render($template)
51         {
52                 $key = self::get_key($template);
53                 self::$template_data[$key]['render_time'] += microtime(true) - self::$template_data[$key]['start_time'];
54         }
55
56         /**
57         * Start logging of cache time
58         */
59         public static function start_cache($template)
60         {
61                 $key = self::get_key($template);
62                 self::$template_data[$key]['start_time'] = microtime(true);
63         }
64
65         /**
66         * End logging of cache time
67         */
68         public static function end_cache($template)
69         {
70                 $key = self::get_key($template);
71                 self::$template_data[$key]['cache_time'] += microtime(true) - self::$template_data[$key]['start_time'];
72         }
73         /**
74         * Opens a window for the Smarty Debugging Consol and display the data
75         */
76         public static function display_debug($obj)
77         {
78                 // prepare information of assigned variables
79                 $ptr = self::get_debug_vars($obj);
80                 if ($obj instanceof Smarty) {
81                         $smarty = $obj;
82                 } else {
83                         $smarty = $obj->smarty;
84                 }
85                 $_assigned_vars = $ptr->tpl_vars;
86                 ksort($_assigned_vars);
87                 $_config_vars = $ptr->config_vars;
88                 ksort($_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);
101                 }
102                 if ($obj instanceof Smarty) {
103                         $_template->assign('template_data', self::$template_data);
104                 } else {
105                         $_template->assign('template_data', null);
106                 }
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;
113         }
114         /*
115         * Recursively gets variables from all template/data scopes
116         */
117         public static function get_debug_vars($obj)
118         {
119                 $config_vars = $obj->config_vars;
120                 $tpl_vars = array();
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';
127                         } else {
128                                 $tpl_vars[$key]->scope = 'Smarty root';
129                         }
130                 }
131
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);
136                 } else {
137                         foreach (Smarty::$global_tpl_vars as $name => $var) {
138                                 if (!array_key_exists($name, $tpl_vars)) {
139                                         $clone = clone $var;
140                                         $clone->scope = 'Global';
141                                         $tpl_vars[$name] = $clone;
142                                 }
143                         }
144                 }
145                 return (object) array('tpl_vars' => $tpl_vars, 'config_vars' => $config_vars);
146         }
147
148         /**
149         * get_key
150         */
151         static function get_key($template)
152         {
153                 // calculate Uid if not already done
154                 if ($template->templateUid == '') {
155                         $template->getTemplateFilepath();
156                 }
157                 $key = $template->templateUid;
158                 if (isset(self::$template_data[$key])) {
159                         return $key;
160                 } else {
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;
165                         return $key;
166                 }
167         }
168 }
169
170 ?>