From 1a5f9dc5e1a1a93680ceff0203e196ff261dd0cc Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 22 Aug 2016 11:53:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E9=A1=B5=E9=9D=A2trace?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E8=AE=B0=E5=BD=95=E4=B8=8D=E5=85=A8=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/App.php | 5 ----- library/think/Debug.php | 16 ++++++---------- library/think/Response.php | 27 +++++++++++++++++---------- 3 files changed, 23 insertions(+), 25 deletions(-) diff --git a/library/think/App.php b/library/think/App.php index 37748dc2..3cecba90 100644 --- a/library/think/App.php +++ b/library/think/App.php @@ -161,11 +161,6 @@ class App // 监听app_end Hook::listen('app_end', $response); - // Trace调试注入 - if (Config::get('app_trace')) { - Debug::inject($response); - } - return $response; } diff --git a/library/think/Debug.php b/library/think/Debug.php index e69ab86a..e36a764e 100644 --- a/library/think/Debug.php +++ b/library/think/Debug.php @@ -185,14 +185,12 @@ class Debug } } - public static function inject(Response $response) + public static function inject(Response $response, &$content) { - $config = Config::get('trace'); - $type = isset($config['type']) ? $config['type'] : 'Html'; - $request = Request::instance(); - $accept = $request->header('accept'); - $contentType = $response->getHeader('Content-Type'); - $class = false !== strpos($type, '\\') ? $type : '\\think\\debug\\' . ucwords($type); + $config = Config::get('trace'); + $type = isset($config['type']) ? $config['type'] : 'Html'; + $request = Request::instance(); + $class = false !== strpos($type, '\\') ? $type : '\\think\\debug\\' . ucwords($type); unset($config['type']); if (class_exists($class)) { $trace = new $class($config); @@ -206,14 +204,12 @@ class Debug $output = $trace->output($response, Log::getLog()); if (is_string($output)) { // trace调试信息注入 - $content = $response->getContent(); - $pos = strripos($content, ''); + $pos = strripos($content, ''); if (false !== $pos) { $content = substr($content, 0, $pos) . $output . substr($content, $pos); } else { $content = $content . $output; } - $response->content($content); } } } diff --git a/library/think/Response.php b/library/think/Response.php index 59d6a0cf..df97d97b 100644 --- a/library/think/Response.php +++ b/library/think/Response.php @@ -11,6 +11,8 @@ namespace think; +use think\Config; +use think\Debug; use think\response\Json as JsonResponse; use think\response\Jsonp as JsonpResponse; use think\response\Redirect as RedirectResponse; @@ -93,6 +95,11 @@ class Response // 处理输出数据 $data = $this->getContent(); + // Trace调试注入 + if (Config::get('app_trace')) { + Debug::inject($this, $data); + } + if (!headers_sent() && !empty($this->header)) { // 发送状态码 http_response_code($this->code); @@ -170,14 +177,14 @@ class Response public function content($content) { if (null !== $content && !is_string($content) && !is_numeric($content) && !is_callable([ - $content, - '__toString', - ]) + $content, + '__toString', + ]) ) { throw new \InvalidArgumentException(sprintf('variable type error: %s', gettype($content))); } - $this->content = (string)$content; + $this->content = (string) $content; return $this; } @@ -248,7 +255,7 @@ class Response $this->header['Content-Type'] = $contentType . '; charset=' . $charset; return $this; } - + /** * 获取头部信息 * @param string $name 头部名称 @@ -274,18 +281,18 @@ class Response */ public function getContent() { - if ($this->content == null) { + if (null == $this->content) { $content = $this->output($this->data); if (null !== $content && !is_string($content) && !is_numeric($content) && !is_callable([ - $content, - '__toString', - ]) + $content, + '__toString', + ]) ) { throw new \InvalidArgumentException(sprintf('variable type error: %s', gettype($content))); } - $this->content = (string)$content; + $this->content = (string) $content; } return $this->content; }