调试trace调试驱动 原来日志Socket驱动纳入trace功能

This commit is contained in:
thinkphp
2016-06-30 23:25:13 +08:00
parent 37fa2bceca
commit a61eda774f
7 changed files with 60 additions and 79 deletions

View File

@@ -11,6 +11,13 @@
namespace think;
use think\Config;
use think\exception\ClassNotFoundException;
use think\Log;
use think\Request;
use think\Response;
use think\response\Redirect;
class Debug
{
// 区间时间信息
@@ -177,4 +184,43 @@ class Debug
}
}
public static function inject(Response $response)
{
$config = Config::get('trace');
$type = isset($config['type']) ? $config['type'] : 'Html';
if (false !== $type) {
$request = Request::instance();
$accept = $request->header('accept');
$contentType = $response->getHeader('Content-Type');
$class = false !== strpos($type, '\\') ? $type : '\\think\\debug\\' . ucwords($type);
unset($config['type']);
if (class_exists($class)) {
$trace = new $class($config);
} else {
throw new ClassNotFoundException('class not exists:' . $class, $class);
}
if ($response instanceof Redirect) {
//TODO 记录
} elseif (strpos($accept, 'application/json') === 0 || $request->isAjax()) {
//TODO 记录
} elseif (!empty($contentType) && strpos($contentType, 'html') === false) {
//TODO 记录
} else {
$output = $trace->output(Log::getLog());
if (is_string($output)) {
// trace调试信息注入
$content = $response->getContent();
$pos = strripos($content, '</body>');
if (false !== $pos) {
$content = substr($content, 0, $pos) . $output . substr($content, $pos);
} else {
$content = $content . $output;
}
$response->content($content);
}
}
}
}
}