1 | <?php |
---|
2 | /** |
---|
3 | * Smarty Internal Plugin Debug |
---|
4 | * |
---|
5 | * Class to collect data for the Smarty Debugging Consol |
---|
6 | * |
---|
7 | * @package Smarty |
---|
8 | * @subpackage Debug |
---|
9 | * @author Uwe Tews |
---|
10 | */ |
---|
11 | |
---|
12 | /** |
---|
13 | * Smarty Internal Plugin Debug Class |
---|
14 | * |
---|
15 | * @package Smarty |
---|
16 | * @subpackage Debug |
---|
17 | */ |
---|
18 | class Smarty_Internal_Debug extends Smarty_Internal_Data { |
---|
19 | |
---|
20 | /** |
---|
21 | * template data |
---|
22 | * |
---|
23 | * @var array |
---|
24 | */ |
---|
25 | public static $template_data = array(); |
---|
26 | |
---|
27 | /** |
---|
28 | * Start logging of compile time |
---|
29 | * |
---|
30 | * @param object $template |
---|
31 | */ |
---|
32 | public static function start_compile($template) |
---|
33 | { |
---|
34 | $key = self::get_key($template); |
---|
35 | self::$template_data[$key]['start_time'] = microtime(true); |
---|
36 | } |
---|
37 | |
---|
38 | /** |
---|
39 | * End logging of compile time |
---|
40 | * |
---|
41 | * @param object $template |
---|
42 | */ |
---|
43 | public static function end_compile($template) |
---|
44 | { |
---|
45 | $key = self::get_key($template); |
---|
46 | self::$template_data[$key]['compile_time'] += microtime(true) - self::$template_data[$key]['start_time']; |
---|
47 | } |
---|
48 | |
---|
49 | /** |
---|
50 | * Start logging of render time |
---|
51 | * |
---|
52 | * @param object $template |
---|
53 | */ |
---|
54 | public static function start_render($template) |
---|
55 | { |
---|
56 | $key = self::get_key($template); |
---|
57 | self::$template_data[$key]['start_time'] = microtime(true); |
---|
58 | } |
---|
59 | |
---|
60 | /** |
---|
61 | * End logging of compile time |
---|
62 | * |
---|
63 | * @param object $template |
---|
64 | */ |
---|
65 | public static function end_render($template) |
---|
66 | { |
---|
67 | $key = self::get_key($template); |
---|
68 | self::$template_data[$key]['render_time'] += microtime(true) - self::$template_data[$key]['start_time']; |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | * Start logging of cache time |
---|
73 | * |
---|
74 | * @param object $template cached template |
---|
75 | */ |
---|
76 | public static function start_cache($template) |
---|
77 | { |
---|
78 | $key = self::get_key($template); |
---|
79 | self::$template_data[$key]['start_time'] = microtime(true); |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * End logging of cache time |
---|
84 | * |
---|
85 | * @param object $template cached template |
---|
86 | */ |
---|
87 | public static function end_cache($template) |
---|
88 | { |
---|
89 | $key = self::get_key($template); |
---|
90 | self::$template_data[$key]['cache_time'] += microtime(true) - self::$template_data[$key]['start_time']; |
---|
91 | } |
---|
92 | |
---|
93 | /** |
---|
94 | * Opens a window for the Smarty Debugging Consol and display the data |
---|
95 | * |
---|
96 | * @param Smarty_Internal_Template|Smarty $obj object to debug |
---|
97 | */ |
---|
98 | public static function display_debug($obj) |
---|
99 | { |
---|
100 | // prepare information of assigned variables |
---|
101 | $ptr = self::get_debug_vars($obj); |
---|
102 | if ($obj instanceof Smarty) { |
---|
103 | $smarty = clone $obj; |
---|
104 | } else { |
---|
105 | $smarty = clone $obj->smarty; |
---|
106 | } |
---|
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); |
---|
126 | } |
---|
127 | if ($obj instanceof Smarty) { |
---|
128 | $_template->assign('template_data', self::$template_data); |
---|
129 | } else { |
---|
130 | $_template->assign('template_data', null); |
---|
131 | } |
---|
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(); |
---|
136 | } |
---|
137 | |
---|
138 | /** |
---|
139 | * Recursively gets variables from all template/data scopes |
---|
140 | * |
---|
141 | * @param Smarty_Internal_Template|Smarty_Data $obj object to debug |
---|
142 | * @return StdClass |
---|
143 | */ |
---|
144 | public static function get_debug_vars($obj) |
---|
145 | { |
---|
146 | $config_vars = $obj->config_vars; |
---|
147 | $tpl_vars = array(); |
---|
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'; |
---|
154 | } else { |
---|
155 | $tpl_vars[$key]->scope = 'Smarty root'; |
---|
156 | } |
---|
157 | } |
---|
158 | |
---|
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); |
---|
163 | } else { |
---|
164 | foreach (Smarty::$global_tpl_vars as $name => $var) { |
---|
165 | if (!array_key_exists($name, $tpl_vars)) { |
---|
166 | $clone = clone $var; |
---|
167 | $clone->scope = 'Global'; |
---|
168 | $tpl_vars[$name] = $clone; |
---|
169 | } |
---|
170 | } |
---|
171 | } |
---|
172 | return (object) array('tpl_vars' => $tpl_vars, 'config_vars' => $config_vars); |
---|
173 | } |
---|
174 | |
---|
175 | /** |
---|
176 | * Return key into $template_data for template |
---|
177 | * |
---|
178 | * @param object $template template object |
---|
179 | * @return string key into $template_data |
---|
180 | */ |
---|
181 | private static function get_key($template) |
---|
182 | { |
---|
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; |
---|
187 | } |
---|
188 | $key = $template->source->uid; |
---|
189 | if (isset(self::$template_data[$key])) { |
---|
190 | return $key; |
---|
191 | } else { |
---|
192 | if (isset($_is_stringy[$template->source->type])) { |
---|
193 | self::$template_data[$key]['name'] = '\''.substr($template->source->name,0,25).'...\''; |
---|
194 | } else { |
---|
195 | self::$template_data[$key]['name'] = $template->source->filepath; |
---|
196 | } |
---|
197 | self::$template_data[$key]['compile_time'] = 0; |
---|
198 | self::$template_data[$key]['render_time'] = 0; |
---|
199 | self::$template_data[$key]['cache_time'] = 0; |
---|
200 | return $key; |
---|
201 | } |
---|
202 | } |
---|
203 | |
---|
204 | } |
---|
205 | |
---|
206 | ?> |
---|