Html.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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\Container;
  13. use think\Db;
  14. use think\Response;
  15. /**
  16. * 页面Trace调试
  17. */
  18. class Html
  19. {
  20. protected $config = [
  21. 'file' => '',
  22. 'tabs' => ['base' => '基本', 'file' => '文件', 'info' => '流程', 'notice|error' => '错误', 'sql' => 'SQL', 'debug|log' => '调试'],
  23. ];
  24. // 实例化并传入参数
  25. public function __construct(array $config = [])
  26. {
  27. $this->config = array_merge($this->config, $config);
  28. }
  29. /**
  30. * 调试输出接口
  31. * @access public
  32. * @param Response $response Response对象
  33. * @param array $log 日志信息
  34. * @return bool
  35. */
  36. public function output(Response $response, array $log = [])
  37. {
  38. $request = Container::get('request');
  39. $contentType = $response->getHeader('Content-Type');
  40. $accept = $request->header('accept');
  41. if (strpos($accept, 'application/json') === 0 || $request->isAjax()) {
  42. return false;
  43. } elseif (!empty($contentType) && strpos($contentType, 'html') === false) {
  44. return false;
  45. }
  46. // 获取基本信息
  47. $runtime = number_format(microtime(true) - Container::get('app')->getBeginTime(), 10, '.', '');
  48. $reqs = $runtime > 0 ? number_format(1 / $runtime, 2) : '∞';
  49. $mem = number_format((memory_get_usage() - Container::get('app')->getBeginMem()) / 1024, 2);
  50. // 页面Trace信息
  51. if ($request->host()) {
  52. $uri = $request->protocol() . ' ' . $request->method() . ' : ' . $request->url(true);
  53. } else {
  54. $uri = 'cmd:' . implode(' ', $_SERVER['argv']);
  55. }
  56. $base = [
  57. '请求信息' => date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']) . ' ' . $uri,
  58. '运行时间' => number_format($runtime, 6) . 's [ 吞吐率:' . $reqs . 'req/s ] 内存消耗:' . $mem . 'kb 文件加载:' . count(get_included_files()),
  59. '查询信息' => Db::$queryTimes . ' queries ' . Db::$executeTimes . ' writes ',
  60. '缓存信息' => Container::get('cache')->getReadTimes() . ' reads,' . Container::get('cache')->getWriteTimes() . ' writes',
  61. ];
  62. if (session_id()) {
  63. $base['会话信息'] = 'SESSION_ID=' . session_id();
  64. }
  65. $info = Container::get('debug')->getFile(true);
  66. // 页面Trace信息
  67. $trace = [];
  68. foreach ($this->config['tabs'] as $name => $title) {
  69. $name = strtolower($name);
  70. switch ($name) {
  71. case 'base': // 基本信息
  72. $trace[$title] = $base;
  73. break;
  74. case 'file': // 文件信息
  75. $trace[$title] = $info;
  76. break;
  77. default: // 调试信息
  78. if (strpos($name, '|')) {
  79. // 多组信息
  80. $names = explode('|', $name);
  81. $result = [];
  82. foreach ($names as $name) {
  83. $result = array_merge($result, isset($log[$name]) ? $log[$name] : []);
  84. }
  85. $trace[$title] = $result;
  86. } else {
  87. $trace[$title] = isset($log[$name]) ? $log[$name] : '';
  88. }
  89. }
  90. }
  91. // 调用Trace页面模板
  92. ob_start();
  93. include $this->config['file'];
  94. return ob_get_clean();
  95. }
  96. }