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 */
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();
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 }
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 }
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 }
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 }
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 }
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 = clone $obj;
82 } else {
83 $smarty = clone $obj->smarty;
84 }
85 $_assigned_vars = $ptr->tpl_vars;
86 ksort($_assigned_vars);
87 $_config_vars = $ptr->config_vars;
88 ksort($_config_vars);
89 $smarty->left_delimiter = '{';
90 $smarty->right_delimiter = '}';
91 $smarty->registered_filters = array();
92 $smarty->autoload_filters = array();
93 $smarty->default_modifiers = array();
94 $_template = new Smarty_Internal_Template ($smarty->debug_tpl, $smarty);
95 $_template->caching = false;
96 $_template->force_compile = false;
97 $_template->disableSecurity();
98 $_template->cache_id = null;
99 $_template->compile_id = null;
100 if ($obj instanceof Smarty_Internal_Template) {
101 $_template->assign('template_name',$obj->resource_type.':'.$obj->resource_name);
102 }
103 if ($obj instanceof Smarty) {
104 $_template->assign('template_data', self::$template_data);
105 } else {
106 $_template->assign('template_data', null);
107 }
108 $_template->assign('assigned_vars', $_assigned_vars);
109 $_template->assign('config_vars', $_config_vars);
110 $_template->assign('execution_time', microtime(true) - $smarty->start_time);
111 echo $_template->getRenderedTemplate();
112 }
113 /*
114 * Recursively gets variables from all template/data scopes
115 */
116 public static function get_debug_vars($obj)
117 {
118 $config_vars = $obj->config_vars;
119 $tpl_vars = array();
120 foreach ($obj->tpl_vars as $key => $var) {
121 $tpl_vars[$key] = clone $var;
122 if ($obj instanceof Smarty_Internal_Template) {
123 $tpl_vars[$key]->scope = $obj->resource_type.':'.$obj->resource_name;
124 } elseif ($obj instanceof Smarty_Data) {
125 $tpl_vars[$key]->scope = 'Data object';
126 } else {
127 $tpl_vars[$key]->scope = 'Smarty root';
128 }
129 }
131 if (isset($obj->parent)) {
132 $parent = self::get_debug_vars($obj->parent);
133 $tpl_vars = array_merge($parent->tpl_vars, $tpl_vars);
134 $config_vars = array_merge($parent->config_vars, $config_vars);
135 } else {
136 foreach (Smarty::$global_tpl_vars as $name => $var) {
137 if (!array_key_exists($name, $tpl_vars)) {
138 $clone = clone $var;
139 $clone->scope = 'Global';
140 $tpl_vars[$name] = $clone;
141 }
142 }
143 }
144 return (object) array('tpl_vars' => $tpl_vars, 'config_vars' => $config_vars);
145 }
147 /**
148 * get_key
149 */
150 static function get_key($template)
151 {
152 // calculate Uid if not already done
153 if ($template->templateUid == '') {
154 $template->getTemplateFilepath();
155 }
156 $key = $template->templateUid;
157 if (isset(self::$template_data[$key])) {
158 return $key;
159 } else {
160 self::$template_data[$key]['name'] = $template->getTemplateFilepath();
161 self::$template_data[$key]['compile_time'] = 0;
162 self::$template_data[$key]['render_time'] = 0;
163 self::$template_data[$key]['cache_time'] = 0;
164 return $key;
165 }
166 }
167 }
169 ?>