Console.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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: yangweijie <yangweijiester@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\debug;
  12. use think\Container;
  13. use think\Db;
  14. use think\Response;
  15. /**
  16. * 浏览器调试输出
  17. */
  18. class Console
  19. {
  20. protected $config = [
  21. 'tabs' => ['base' => '基本', 'file' => '文件', 'info' => '流程', 'notice|error' => '错误', 'sql' => 'SQL', 'debug|log' => '调试'],
  22. ];
  23. // 实例化并传入参数
  24. public function __construct($config = [])
  25. {
  26. if (is_array($config)) {
  27. $this->config = array_merge($this->config, $config);
  28. }
  29. }
  30. /**
  31. * 调试输出接口
  32. * @access public
  33. * @param Response $response Response对象
  34. * @param array $log 日志信息
  35. * @return bool
  36. */
  37. public function output(Response $response, array $log = [])
  38. {
  39. $request = Container::get('request');
  40. $contentType = $response->getHeader('Content-Type');
  41. $accept = $request->header('accept');
  42. if (strpos($accept, 'application/json') === 0 || $request->isAjax()) {
  43. return false;
  44. } elseif (!empty($contentType) && strpos($contentType, 'html') === false) {
  45. return false;
  46. }
  47. // 获取基本信息
  48. $runtime = number_format(microtime(true) - Container::get('app')->getBeginTime(), 10);
  49. $reqs = $runtime > 0 ? number_format(1 / $runtime, 2) : '∞';
  50. $mem = number_format((memory_get_usage() - Container::get('app')->getBeginMem()) / 1024, 2);
  51. if ($request->host()) {
  52. $uri = $request->protocol() . ' ' . $request->method() . ' : ' . $request->url(true);
  53. } else {
  54. $uri = 'cmd:' . implode(' ', $_SERVER['argv']);
  55. }
  56. // 页面Trace信息
  57. $base = [
  58. '请求信息' => date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']) . ' ' . $uri,
  59. '运行时间' => number_format($runtime, 6) . 's [ 吞吐率:' . $reqs . 'req/s ] 内存消耗:' . $mem . 'kb 文件加载:' . count(get_included_files()),
  60. '查询信息' => Db::$queryTimes . ' queries ' . Db::$executeTimes . ' writes ',
  61. '缓存信息' => Container::get('cache')->getReadTimes() . ' reads,' . Container::get('cache')->getWriteTimes() . ' writes',
  62. ];
  63. if (session_id()) {
  64. $base['会话信息'] = 'SESSION_ID=' . session_id();
  65. }
  66. $info = Container::get('debug')->getFile(true);
  67. // 页面Trace信息
  68. $trace = [];
  69. foreach ($this->config['tabs'] as $name => $title) {
  70. $name = strtolower($name);
  71. switch ($name) {
  72. case 'base': // 基本信息
  73. $trace[$title] = $base;
  74. break;
  75. case 'file': // 文件信息
  76. $trace[$title] = $info;
  77. break;
  78. default: // 调试信息
  79. if (strpos($name, '|')) {
  80. // 多组信息
  81. $names = explode('|', $name);
  82. $result = [];
  83. foreach ($names as $name) {
  84. $result = array_merge($result, isset($log[$name]) ? $log[$name] : []);
  85. }
  86. $trace[$title] = $result;
  87. } else {
  88. $trace[$title] = isset($log[$name]) ? $log[$name] : '';
  89. }
  90. }
  91. }
  92. //输出到控制台
  93. $lines = '';
  94. foreach ($trace as $type => $msg) {
  95. $lines .= $this->console($type, $msg);
  96. }
  97. $js = <<<JS
  98. <script type='text/javascript'>
  99. {$lines}
  100. </script>
  101. JS;
  102. return $js;
  103. }
  104. protected function console($type, $msg)
  105. {
  106. $type = strtolower($type);
  107. $trace_tabs = array_values($this->config['tabs']);
  108. $line[] = ($type == $trace_tabs[0] || '调试' == $type || '错误' == $type)
  109. ? "console.group('{$type}');"
  110. : "console.groupCollapsed('{$type}');";
  111. foreach ((array) $msg as $key => $m) {
  112. switch ($type) {
  113. case '调试':
  114. $var_type = gettype($m);
  115. if (in_array($var_type, ['array', 'string'])) {
  116. $line[] = "console.log(" . json_encode($m) . ");";
  117. } else {
  118. $line[] = "console.log(" . json_encode(var_export($m, 1)) . ");";
  119. }
  120. break;
  121. case '错误':
  122. $msg = str_replace("\n", '\n', addslashes(is_scalar($m) ? $m : json_encode($m)));
  123. $style = 'color:#F4006B;font-size:14px;';
  124. $line[] = "console.error(\"%c{$msg}\", \"{$style}\");";
  125. break;
  126. case 'sql':
  127. $msg = str_replace("\n", '\n', addslashes($m));
  128. $style = "color:#009bb4;";
  129. $line[] = "console.log(\"%c{$msg}\", \"{$style}\");";
  130. break;
  131. default:
  132. $m = is_string($key) ? $key . ' ' . $m : $key + 1 . ' ' . $m;
  133. $msg = json_encode($m);
  134. $line[] = "console.log({$msg});";
  135. break;
  136. }
  137. }
  138. $line[] = "console.groupEnd();";
  139. return implode(PHP_EOL, $line);
  140. }
  141. }