/smarty/sysplugins/smarty_internal_debug.php

https://github.com/taptouchclick/The-Event-Day · PHP · 169 lines · 116 code · 11 blank · 42 comment · 14 complexity · 58074a902d1c2b1bea00957ba1bf505a MD5 · raw file

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