smarty_internal_debug.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. * Smarty Internal Plugin Debug Class
  13. *
  14. * @package Smarty
  15. * @subpackage Debug
  16. */
  17. class Smarty_Internal_Debug extends Smarty_Internal_Data {
  18. /**
  19. * template data
  20. *
  21. * @var array
  22. */
  23. public static $template_data = array();
  24. /**
  25. * Start logging of compile time
  26. *
  27. * @param object $template
  28. */
  29. public static function start_compile($template)
  30. {
  31. $key = self::get_key($template);
  32. self::$template_data[$key]['start_time'] = microtime(true);
  33. }
  34. /**
  35. * End logging of compile time
  36. *
  37. * @param object $template
  38. */
  39. public static function end_compile($template)
  40. {
  41. $key = self::get_key($template);
  42. self::$template_data[$key]['compile_time'] += microtime(true) - self::$template_data[$key]['start_time'];
  43. }
  44. /**
  45. * Start logging of render time
  46. *
  47. * @param object $template
  48. */
  49. public static function start_render($template)
  50. {
  51. $key = self::get_key($template);
  52. self::$template_data[$key]['start_time'] = microtime(true);
  53. }
  54. /**
  55. * End logging of compile time
  56. *
  57. * @param object $template
  58. */
  59. public static function end_render($template)
  60. {
  61. $key = self::get_key($template);
  62. self::$template_data[$key]['render_time'] += microtime(true) - self::$template_data[$key]['start_time'];
  63. }
  64. /**
  65. * Start logging of cache time
  66. *
  67. * @param object $template cached template
  68. */
  69. public static function start_cache($template)
  70. {
  71. $key = self::get_key($template);
  72. self::$template_data[$key]['start_time'] = microtime(true);
  73. }
  74. /**
  75. * End logging of cache time
  76. *
  77. * @param object $template cached template
  78. */
  79. public static function end_cache($template)
  80. {
  81. $key = self::get_key($template);
  82. self::$template_data[$key]['cache_time'] += microtime(true) - self::$template_data[$key]['start_time'];
  83. }
  84. /**
  85. * Opens a window for the Smarty Debugging Consol and display the data
  86. *
  87. * @param Smarty_Internal_Template|Smarty $obj object to debug
  88. */
  89. public static function display_debug($obj)
  90. {
  91. // prepare information of assigned variables
  92. $ptr = self::get_debug_vars($obj);
  93. if ($obj instanceof Smarty) {
  94. $smarty = clone $obj;
  95. } else {
  96. $smarty = clone $obj->smarty;
  97. }
  98. $_assigned_vars = $ptr->tpl_vars;
  99. ksort($_assigned_vars);
  100. $_config_vars = $ptr->config_vars;
  101. ksort($_config_vars);
  102. $smarty->registered_filters = array();
  103. $smarty->autoload_filters = array();
  104. $smarty->default_modifiers = array();
  105. $smarty->force_compile = false;
  106. $smarty->left_delimiter = '{';
  107. $smarty->right_delimiter = '}';
  108. $smarty->debugging = false;
  109. $smarty->force_compile = false;
  110. $_template = new Smarty_Internal_Template($smarty->debug_tpl, $smarty);
  111. $_template->caching = false;
  112. $_template->disableSecurity();
  113. $_template->cache_id = null;
  114. $_template->compile_id = null;
  115. if ($obj instanceof Smarty_Internal_Template) {
  116. $_template->assign('template_name', $obj->source->type . ':' . $obj->source->name);
  117. }
  118. if ($obj instanceof Smarty) {
  119. $_template->assign('template_data', self::$template_data);
  120. } else {
  121. $_template->assign('template_data', null);
  122. }
  123. $_template->assign('assigned_vars', $_assigned_vars);
  124. $_template->assign('config_vars', $_config_vars);
  125. $_template->assign('execution_time', microtime(true) - $smarty->start_time);
  126. echo $_template->fetch();
  127. }
  128. /**
  129. * Recursively gets variables from all template/data scopes
  130. *
  131. * @param Smarty_Internal_Template|Smarty_Data $obj object to debug
  132. * @return StdClass
  133. */
  134. public static function get_debug_vars($obj)
  135. {
  136. $config_vars = $obj->config_vars;
  137. $tpl_vars = array();
  138. foreach ($obj->tpl_vars as $key => $var) {
  139. $tpl_vars[$key] = clone $var;
  140. if ($obj instanceof Smarty_Internal_Template) {
  141. $tpl_vars[$key]->scope = $obj->source->type . ':' . $obj->source->name;
  142. } elseif ($obj instanceof Smarty_Data) {
  143. $tpl_vars[$key]->scope = 'Data object';
  144. } else {
  145. $tpl_vars[$key]->scope = 'Smarty root';
  146. }
  147. }
  148. if (isset($obj->parent)) {
  149. $parent = self::get_debug_vars($obj->parent);
  150. $tpl_vars = array_merge($parent->tpl_vars, $tpl_vars);
  151. $config_vars = array_merge($parent->config_vars, $config_vars);
  152. } else {
  153. foreach (Smarty::$global_tpl_vars as $name => $var) {
  154. if (!array_key_exists($name, $tpl_vars)) {
  155. $clone = clone $var;
  156. $clone->scope = 'Global';
  157. $tpl_vars[$name] = $clone;
  158. }
  159. }
  160. }
  161. return (object) array('tpl_vars' => $tpl_vars, 'config_vars' => $config_vars);
  162. }
  163. /**
  164. * Return key into $template_data for template
  165. *
  166. * @param object $template template object
  167. * @return string key into $template_data
  168. */
  169. private static function get_key($template)
  170. {
  171. static $_is_stringy = array('string' => true, 'eval' => true);
  172. // calculate Uid if not already done
  173. if ($template->source->uid == '') {
  174. $template->source->filepath;
  175. }
  176. $key = $template->source->uid;
  177. if (isset(self::$template_data[$key])) {
  178. return $key;
  179. } else {
  180. if (isset($_is_stringy[$template->source->type])) {
  181. self::$template_data[$key]['name'] = '\''.substr($template->source->name,0,25).'...\'';
  182. } else {
  183. self::$template_data[$key]['name'] = $template->source->filepath;
  184. }
  185. self::$template_data[$key]['compile_time'] = 0;
  186. self::$template_data[$key]['render_time'] = 0;
  187. self::$template_data[$key]['cache_time'] = 0;
  188. return $key;
  189. }
  190. }
  191. }
  192. ?>