Total Complexity | 68 |
Total Lines | 405 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Smarty_Internal_Debug often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Smarty_Internal_Debug, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class Smarty_Internal_Debug extends Smarty_Internal_Data |
||
18 | { |
||
19 | /** |
||
20 | * template data |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | public $template_data = array(); |
||
25 | |||
26 | /** |
||
27 | * List of uid's which shall be ignored |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | public $ignore_uid = array(); |
||
32 | |||
33 | /** |
||
34 | * Index of display() and fetch() calls |
||
35 | * |
||
36 | * @var int |
||
37 | */ |
||
38 | public $index = 0; |
||
39 | |||
40 | /** |
||
41 | * Counter for window offset |
||
42 | * |
||
43 | * @var int |
||
44 | */ |
||
45 | public $offset = 0; |
||
46 | |||
47 | /** |
||
48 | * Start logging template |
||
49 | * |
||
50 | * @param \Smarty_Internal_Template $template template |
||
51 | * @param null $mode true: display false: fetch null: subtemplate |
||
|
|||
52 | */ |
||
53 | public function start_template(Smarty_Internal_Template $template, $mode = null) |
||
54 | { |
||
55 | if (isset($mode) && !$template->_isSubTpl()) { |
||
56 | $this->index++; |
||
57 | $this->offset++; |
||
58 | $this->template_data[ $this->index ] = null; |
||
59 | } |
||
60 | $key = $this->get_key($template); |
||
61 | $this->template_data[ $this->index ][ $key ][ 'start_template_time' ] = microtime(true); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * End logging of cache time |
||
66 | * |
||
67 | * @param \Smarty_Internal_Template $template cached template |
||
68 | */ |
||
69 | public function end_template(Smarty_Internal_Template $template) |
||
70 | { |
||
71 | $key = $this->get_key($template); |
||
72 | $this->template_data[ $this->index ][ $key ][ 'total_time' ] += |
||
73 | microtime(true) - $this->template_data[ $this->index ][ $key ][ 'start_template_time' ]; |
||
74 | //$this->template_data[$this->index][$key]['properties'] = $template->properties; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Start logging of compile time |
||
79 | * |
||
80 | * @param \Smarty_Internal_Template $template |
||
81 | */ |
||
82 | public function start_compile(Smarty_Internal_Template $template) |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * End logging of compile time |
||
109 | * |
||
110 | * @param \Smarty_Internal_Template $template |
||
111 | */ |
||
112 | public function end_compile(Smarty_Internal_Template $template) |
||
113 | { |
||
114 | if (!empty($template->compiler->trace_uid)) { |
||
115 | $key = $template->compiler->trace_uid; |
||
116 | } else { |
||
117 | if (isset($this->ignore_uid[ $template->source->uid ])) { |
||
118 | return; |
||
119 | } |
||
120 | $key = $this->get_key($template); |
||
121 | } |
||
122 | $this->template_data[ $this->index ][ $key ][ 'compile_time' ] += |
||
123 | microtime(true) - $this->template_data[ $this->index ][ $key ][ 'start_time' ]; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Start logging of render time |
||
128 | * |
||
129 | * @param \Smarty_Internal_Template $template |
||
130 | */ |
||
131 | public function start_render(Smarty_Internal_Template $template) |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * End logging of compile time |
||
139 | * |
||
140 | * @param \Smarty_Internal_Template $template |
||
141 | */ |
||
142 | public function end_render(Smarty_Internal_Template $template) |
||
143 | { |
||
144 | $key = $this->get_key($template); |
||
145 | $this->template_data[ $this->index ][ $key ][ 'render_time' ] += |
||
146 | microtime(true) - $this->template_data[ $this->index ][ $key ][ 'start_time' ]; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Start logging of cache time |
||
151 | * |
||
152 | * @param \Smarty_Internal_Template $template cached template |
||
153 | */ |
||
154 | public function start_cache(Smarty_Internal_Template $template) |
||
155 | { |
||
156 | $key = $this->get_key($template); |
||
157 | $this->template_data[ $this->index ][ $key ][ 'start_time' ] = microtime(true); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * End logging of cache time |
||
162 | * |
||
163 | * @param \Smarty_Internal_Template $template cached template |
||
164 | */ |
||
165 | public function end_cache(Smarty_Internal_Template $template) |
||
166 | { |
||
167 | $key = $this->get_key($template); |
||
168 | $this->template_data[ $this->index ][ $key ][ 'cache_time' ] += |
||
169 | microtime(true) - $this->template_data[ $this->index ][ $key ][ 'start_time' ]; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Register template object |
||
174 | * |
||
175 | * @param \Smarty_Internal_Template $template cached template |
||
176 | */ |
||
177 | public function register_template(Smarty_Internal_Template $template) |
||
178 | { |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Register data object |
||
183 | * |
||
184 | * @param \Smarty_Data $data data object |
||
185 | */ |
||
186 | public static function register_data(Smarty_Data $data) |
||
187 | { |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Opens a window for the Smarty Debugging Console and display the data |
||
192 | * |
||
193 | * @param Smarty_Internal_Template|Smarty $obj object to debug |
||
194 | * @param bool $full |
||
195 | * |
||
196 | * @throws \Exception |
||
197 | * @throws \SmartyException |
||
198 | */ |
||
199 | public function display_debug($obj, $full = false) |
||
200 | { |
||
201 | if (!$full) { |
||
202 | $this->offset++; |
||
203 | $savedIndex = $this->index; |
||
204 | $this->index = 9999; |
||
205 | } |
||
206 | $smarty = $obj->_getSmartyObj(); |
||
207 | // create fresh instance of smarty for displaying the debug console |
||
208 | // to avoid problems if the application did overload the Smarty class |
||
209 | $debObj = new Smarty(); |
||
210 | // copy the working dirs from application |
||
211 | $debObj->setCompileDir($smarty->getCompileDir()); |
||
212 | // init properties by hand as user may have edited the original Smarty class |
||
213 | $debObj->setPluginsDir(is_dir(dirname(__FILE__) . '/../plugins') ? dirname(__FILE__) . |
||
214 | '/../plugins' : $smarty->getPluginsDir()); |
||
215 | $debObj->force_compile = false; |
||
216 | $debObj->compile_check = Smarty::COMPILECHECK_ON; |
||
217 | $debObj->left_delimiter = '{'; |
||
218 | $debObj->right_delimiter = '}'; |
||
219 | $debObj->security_policy = null; |
||
220 | $debObj->debugging = false; |
||
221 | $debObj->debugging_ctrl = 'NONE'; |
||
222 | $debObj->error_reporting = E_ALL & ~E_NOTICE; |
||
223 | $debObj->debug_tpl = |
||
224 | isset($smarty->debug_tpl) ? $smarty->debug_tpl : 'file:' . dirname(__FILE__) . '/../debug.tpl'; |
||
225 | $debObj->registered_plugins = array(); |
||
226 | $debObj->registered_resources = array(); |
||
227 | $debObj->registered_filters = array(); |
||
228 | $debObj->autoload_filters = array(); |
||
229 | $debObj->default_modifiers = array(); |
||
230 | $debObj->escape_html = true; |
||
231 | $debObj->caching = Smarty::CACHING_OFF; |
||
232 | $debObj->compile_id = null; |
||
233 | $debObj->cache_id = null; |
||
234 | // prepare information of assigned variables |
||
235 | $ptr = $this->get_debug_vars($obj); |
||
236 | $_assigned_vars = $ptr->tpl_vars; |
||
237 | ksort($_assigned_vars); |
||
238 | $_config_vars = $ptr->config_vars; |
||
239 | ksort($_config_vars); |
||
240 | $debugging = $smarty->debugging; |
||
241 | $_template = new Smarty_Internal_Template($debObj->debug_tpl, $debObj); |
||
242 | if ($obj->_isTplObj()) { |
||
243 | $_template->assign('template_name', $obj->source->type . ':' . $obj->source->name); |
||
244 | } |
||
245 | if ($obj->_objType === 1 || $full) { |
||
246 | $_template->assign('template_data', $this->template_data[ $this->index ]); |
||
247 | } else { |
||
248 | $_template->assign('template_data', null); |
||
249 | } |
||
250 | $_template->assign('assigned_vars', $_assigned_vars); |
||
251 | $_template->assign('config_vars', $_config_vars); |
||
252 | $_template->assign('execution_time', microtime(true) - $smarty->start_time); |
||
253 | $_template->assign('display_mode', $debugging === 2 || !$full); |
||
254 | $_template->assign('offset', $this->offset * 50); |
||
255 | echo $_template->fetch(); |
||
256 | if (isset($full)) { |
||
257 | $this->index--; |
||
258 | } |
||
259 | if (!$full) { |
||
260 | $this->index = $savedIndex; |
||
261 | } |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Recursively gets variables from all template/data scopes |
||
266 | * |
||
267 | * @param Smarty_Internal_Template|Smarty_Data $obj object to debug |
||
268 | * |
||
269 | * @return StdClass |
||
270 | */ |
||
271 | public function get_debug_vars($obj) |
||
346 | } |
||
347 | |||
348 | /** |
||
349 | * Return key into $template_data for template |
||
350 | * |
||
351 | * @param \Smarty_Internal_Template $template template object |
||
352 | * |
||
353 | * @return string key into $template_data |
||
354 | */ |
||
355 | private function get_key(Smarty_Internal_Template $template) |
||
377 | } |
||
378 | } |
||
379 | |||
380 | /** |
||
381 | * Ignore template |
||
382 | * |
||
383 | * @param \Smarty_Internal_Template $template |
||
384 | */ |
||
385 | public function ignore(Smarty_Internal_Template $template) |
||
386 | { |
||
387 | // calculate Uid if not already done |
||
388 | if ($template->source->uid === '') { |
||
389 | $template->source->filepath; |
||
390 | } |
||
391 | $this->ignore_uid[ $template->source->uid ] = true; |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * handle 'URL' debugging mode |
||
396 | * |
||
397 | * @param Smarty $smarty |
||
398 | */ |
||
399 | public function debugUrl(Smarty $smarty) |
||
422 | } |
||
423 | } |
||
424 | } |
||
425 | } |
||
426 |