Html.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\debug;
  12. use think\Cache;
  13. use think\Config;
  14. use think\Db;
  15. use think\Debug;
  16. use think\Request;
  17. use think\Response;
  18. /**
  19. * 页面Trace调试
  20. */
  21. class Html
  22. {
  23. protected $config = [
  24. 'trace_file' => '',
  25. 'trace_tabs' => ['base' => '基本', 'file' => '文件', 'info' => '流程', 'notice|error' => '错误', 'sql' => 'SQL', 'debug|log' => '调试'],
  26. ];
  27. // 实例化并传入参数
  28. public function __construct(array $config = [])
  29. {
  30. $this->config['trace_file'] = THINK_PATH . 'tpl/page_trace.tpl';
  31. $this->config = array_merge($this->config, $config);
  32. }
  33. /**
  34. * 调试输出接口
  35. * @access public
  36. * @param Response $response Response对象
  37. * @param array $log 日志信息
  38. * @return bool
  39. */
  40. public function output(Response $response, array $log = [])
  41. {
  42. $request = Request::instance();
  43. $contentType = $response->getHeader('Content-Type');
  44. $accept = $request->header('accept');
  45. if (strpos($accept, 'application/json') === 0 || $request->isAjax()) {
  46. return false;
  47. } elseif (!empty($contentType) && strpos($contentType, 'html') === false) {
  48. return false;
  49. }
  50. // 获取基本信息
  51. $runtime = number_format(microtime(true) - THINK_START_TIME, 10, '.', '');
  52. $reqs = $runtime > 0 ? number_format(1 / $runtime, 2) : '∞';
  53. $mem = number_format((memory_get_usage() - THINK_START_MEM) / 1024, 2);
  54. // 页面Trace信息
  55. if (isset($_SERVER['HTTP_HOST'])) {
  56. $uri = $_SERVER['SERVER_PROTOCOL'] . ' ' . $_SERVER['REQUEST_METHOD'] . ' : ' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  57. } else {
  58. $uri = 'cmd:' . implode(' ', $_SERVER['argv']);
  59. }
  60. $base = [
  61. '请求信息' => date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']) . ' ' . $uri,
  62. '运行时间' => number_format($runtime, 6) . 's [ 吞吐率:' . $reqs . 'req/s ] 内存消耗:' . $mem . 'kb 文件加载:' . count(get_included_files()),
  63. '查询信息' => Db::$queryTimes . ' queries ' . Db::$executeTimes . ' writes ',
  64. '缓存信息' => Cache::$readTimes . ' reads,' . Cache::$writeTimes . ' writes',
  65. '配置加载' => count(Config::get()),
  66. ];
  67. if (session_id()) {
  68. $base['会话信息'] = 'SESSION_ID=' . session_id();
  69. }
  70. $info = Debug::getFile(true);
  71. // 页面Trace信息
  72. $trace = [];
  73. foreach ($this->config['trace_tabs'] as $name => $title) {
  74. $name = strtolower($name);
  75. switch ($name) {
  76. case 'base': // 基本信息
  77. $trace[$title] = $base;
  78. break;
  79. case 'file': // 文件信息
  80. $trace[$title] = $info;
  81. break;
  82. default: // 调试信息
  83. if (strpos($name, '|')) {
  84. // 多组信息
  85. $names = explode('|', $name);
  86. $result = [];
  87. foreach ($names as $name) {
  88. $result = array_merge($result, isset($log[$name]) ? $log[$name] : []);
  89. }
  90. $trace[$title] = $result;
  91. } else {
  92. $trace[$title] = isset($log[$name]) ? $log[$name] : '';
  93. }
  94. }
  95. }
  96. // 调用Trace页面模板
  97. ob_start();
  98. include $this->config['trace_file'];
  99. return ob_get_clean();
  100. }
  101. }