From 2da74dbc1ea8e853133578a47e1adcae6208afe0 Mon Sep 17 00:00:00 2001 From: molong Date: Mon, 4 Jul 2016 00:14:28 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E5=86=85=E6=A0=B8=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=202=E3=80=81=E5=90=8E=E5=8F=B0bug=E4=BF=AE=E5=A4=8D=EF=BC=88?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=8F=82=E6=95=B0=E7=9A=84bug=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/admin/controller/Ad.php | 6 +- application/admin/controller/Attribute.php | 4 +- application/admin/controller/Channel.php | 14 ++--- application/admin/controller/Config.php | 20 +++---- application/admin/controller/Content.php | 2 +- application/admin/controller/Link.php | 2 +- application/admin/controller/Menu.php | 4 +- application/admin/controller/Model.php | 2 +- application/admin/controller/Seo.php | 4 +- application/admin/controller/User.php | 2 +- core/convention.php | 15 +++-- core/extend/com/Database.php | 2 +- core/library/think/App.php | 4 +- core/library/think/Cookie.php | 6 +- core/library/think/Debug.php | 57 ++++++++----------- core/library/think/Error.php | 20 +++---- core/library/think/Log.php | 10 ++-- core/library/think/Url.php | 4 ++ core/library/think/cache/driver/File.php | 2 +- core/library/think/cache/driver/Lite.php | 4 +- core/library/think/cache/driver/Memcached.php | 8 ++- core/library/think/cache/driver/Sqlite.php | 4 +- core/library/think/db/Query.php | 10 +++- core/library/think/debug/Console.php | 19 +++++-- core/library/think/debug/Html.php | 20 +++++-- core/library/think/debug/Socket.php | 44 +++++++------- core/library/think/log/driver/File.php | 2 +- core/library/think/model/Relation.php | 8 +-- core/library/think/template/driver/File.php | 2 +- core/library/think/template/driver/Sae.php | 4 +- core/tpl/{make_controller.tpl => make.tpl} | 4 +- 31 files changed, 171 insertions(+), 138 deletions(-) rename core/tpl/{make_controller.tpl => make.tpl} (93%) diff --git a/application/admin/controller/Ad.php b/application/admin/controller/Ad.php index 1dc3a823..37267573 100644 --- a/application/admin/controller/Ad.php +++ b/application/admin/controller/Ad.php @@ -63,7 +63,7 @@ class Ad extends Admin { public function edit($id = null){ $place = model('AdPlace'); if (IS_POST) { - $data = input('post.',''); + $data = $this->request->post(); if ($data) { $result = $place->save($data,array('id'=>$data['id'])); if ($result) { @@ -122,7 +122,7 @@ class Ad extends Admin { public function addad($id){ $ad = model('ad'); if (IS_POST) { - $data = input('post.'); + $data = $this->request->post(); if ($data) { $result = $ad->save($data); if ($result) { @@ -148,7 +148,7 @@ class Ad extends Admin { public function editad($id = null){ $ad = model('ad'); if (IS_POST) { - $data = input('post.'); + $data = $this->request->post(); if ($data) { $result = $ad->save($data,array('id'=>$data['id'])); if ($result) { diff --git a/application/admin/controller/Attribute.php b/application/admin/controller/Attribute.php index 3c6772f5..891cd2da 100644 --- a/application/admin/controller/Attribute.php +++ b/application/admin/controller/Attribute.php @@ -69,7 +69,7 @@ class Attribute extends Admin { * @author colin */ public function add(){ - $model_id = input('get.model_id','','trim,intval'); + $model_id = input('model_id','','trim,intval'); if(IS_POST){ $result = $this->model->change(); if ($result) { @@ -104,7 +104,7 @@ class Attribute extends Admin { return $this->error($this->model->getError(),''); } }else{ - $id = input('get.id','','trim,intval'); + $id = input('id','','trim,intval'); $info = db('Attribute')->find($id); $data = array( 'info' => $info, diff --git a/application/admin/controller/Channel.php b/application/admin/controller/Channel.php index 9b75a3a9..5c86f9e5 100644 --- a/application/admin/controller/Channel.php +++ b/application/admin/controller/Channel.php @@ -17,7 +17,7 @@ class Channel extends Admin{ } public function index(){ - $pid = input('get.pid', 0); + $pid = input('pid', 0); /* 获取频道列表 */ //$map = array('status' => array('gt', -1), 'pid'=>$pid); $map = array('status' => array('gt', -1)); @@ -50,7 +50,7 @@ class Channel extends Admin{ public function add() { if (IS_POST) { $Channel = model('Channel'); - $data = input('post.'); + $data = $this->request->post(); if ($data) { $id = $Channel->save($data); if ($id) { @@ -64,7 +64,7 @@ class Channel extends Admin{ $this->error($Channel->getError()); } } else { - $pid = input('get.pid', 0); + $pid = input('pid', 0); //获取父导航 if (!empty($pid)) { $parent = db('Channel')->where(array('id' => $pid))->field('title')->find(); @@ -86,7 +86,7 @@ class Channel extends Admin{ public function edit($id = 0) { if (IS_POST) { $Channel = model('Channel'); - $data = input('post.'); + $data = $this->request->post(); if ($data) { if (false !== $Channel->save($data,array('id'=>$data['id']))) { //记录行为 @@ -107,7 +107,7 @@ class Channel extends Admin{ return $this->error('获取配置信息错误'); } - $pid = input('get.pid', 0); + $pid = input('pid', 0); //获取父导航 if (!empty($pid)) { $parent = db('Channel')->where(array('id' => $pid))->field('title')->find(); @@ -148,8 +148,8 @@ class Channel extends Admin{ */ public function sort() { if (IS_GET) { - $ids = input('get.ids'); - $pid = input('get.pid'); + $ids = input('ids'); + $pid = input('pid'); //获取排序的数据 $map = array('status' => array('gt', -1)); if (!empty($ids)) { diff --git a/application/admin/controller/Config.php b/application/admin/controller/Config.php index a2ad24bf..d66491e1 100644 --- a/application/admin/controller/Config.php +++ b/application/admin/controller/Config.php @@ -77,10 +77,10 @@ class Config extends Admin{ */ public function add() { if (IS_POST) { - $Config = model('Config'); - $data = input('post.'); + $config = model('Config'); + $data = $this->request->post(); if ($data) { - $id = $Config->validate(true)->save($data); + $id = $config->validate(true)->save($data); if ($id) { cache('db_config_data', null); //记录行为 @@ -92,7 +92,7 @@ class Config extends Admin{ } } else { - return $this->error($Config->getError()); + return $this->error($config->getError()); } } else { @@ -109,20 +109,20 @@ class Config extends Admin{ */ public function edit($id = 0) { if (IS_POST) { - $Config = model('Config'); - $data = input('post.'); + $config = model('Config'); + $data = $this->request->post(); if ($data) { - $result = $Config->validate('Config.edit')->save($data,array('id'=>$data['id'])); + $result = $config->validate('Config.edit')->save($data,array('id'=>$data['id'])); if (false !== $result) { cache('db_config_data', null); //记录行为 action_log('update_config', 'config', $data['id'], session('user_auth.uid')); return $this->success('更新成功', Cookie('__forward__')); } else { - return $this->error($Config->getError(), ''); + return $this->error($config->getError(), ''); } }else { - return $this->error($Config->getError()); + return $this->error($config->getError()); } }else{ $info = array(); @@ -181,7 +181,7 @@ class Config extends Admin{ */ public function sort() { if (IS_GET) { - $ids = input('get.ids'); + $ids = input('ids'); //获取排序的数据 $map = array('status' => array('gt', -1)); if (!empty($ids)) { diff --git a/application/admin/controller/Content.php b/application/admin/controller/Content.php index ea76fb11..ff3f38a3 100644 --- a/application/admin/controller/Content.php +++ b/application/admin/controller/Content.php @@ -145,7 +145,7 @@ class Content extends Admin{ * @author molong */ public function del(){ - $id = input('get.id','','trim'); + $id = input('id','','trim'); $ids = input('post.ids',array()); array_push($ids, $id); if (empty($ids)) { diff --git a/application/admin/controller/Link.php b/application/admin/controller/Link.php index 9c720718..a2ed8e18 100644 --- a/application/admin/controller/Link.php +++ b/application/admin/controller/Link.php @@ -56,7 +56,7 @@ class Link extends Admin{ //修改 public function edit(){ $link = model('Link'); - $id = input('get.id','','trim,intval'); + $id = input('id','','trim,intval'); if(IS_POST){ $data = input('post.'); if ($data) { diff --git a/application/admin/controller/Menu.php b/application/admin/controller/Menu.php index 07cf76ba..d59e536c 100644 --- a/application/admin/controller/Menu.php +++ b/application/admin/controller/Menu.php @@ -226,8 +226,8 @@ class Menu extends Admin{ */ public function sort(){ if(IS_GET){ - $ids = input('get.ids'); - $pid = input('get.pid'); + $ids = input('ids'); + $pid = input('pid'); //获取排序的数据 $map = array('status'=>array('gt',-1)); diff --git a/application/admin/controller/Model.php b/application/admin/controller/Model.php index ad04a559..87704ece 100644 --- a/application/admin/controller/Model.php +++ b/application/admin/controller/Model.php @@ -148,7 +148,7 @@ class Model extends Admin{ * @author colin */ public function status(){ - $map['id'] = input('post.ids/a') ? input('post.ids/a') : input('get.ids/a'); + $map['id'] = $this->request->param('ids'); if(null == $map['id'])return $this->error('参数不正确!'); $data['status'] = input('get.status'); diff --git a/application/admin/controller/Seo.php b/application/admin/controller/Seo.php index c461f518..c6b611b3 100644 --- a/application/admin/controller/Seo.php +++ b/application/admin/controller/Seo.php @@ -49,7 +49,7 @@ class Seo extends Admin{ public function add(){ if (IS_POST) { - $data = input('post.'); + $data = $this->request->post(); if ($data) { $result = model('SeoRule')->save($data); if ($result) { @@ -72,7 +72,7 @@ class Seo extends Admin{ public function edit($id = null){ if (IS_POST) { - $data = input('post.'); + $data = $this->request->post(); if ($data) { $result = model('SeoRule')->save($data,array('id'=>$data['id'])); if (false !== $result) { diff --git a/application/admin/controller/User.php b/application/admin/controller/User.php index 51959ca9..80cfa82b 100644 --- a/application/admin/controller/User.php +++ b/application/admin/controller/User.php @@ -172,7 +172,7 @@ class User extends Admin{ */ private function getUserinfo($uid = null,$pass = null,$errormsg = null){ $user = model('User'); - $uid = $uid ? $uid : input('get.id'); + $uid = $uid ? $uid : input('id'); //如果无UID则修改当前用户 $uid = $uid ? $uid : session('user_auth.uid'); $map['uid'] = $uid; diff --git a/core/convention.php b/core/convention.php index efcf27fc..f3523b5c 100644 --- a/core/convention.php +++ b/core/convention.php @@ -9,6 +9,8 @@ return [ 'app_namespace' => 'app', // 应用调试模式 'app_debug' => true, + // 应用Trace + 'app_trace' => false, // 应用模式状态 'app_status' => '', // 是否支持多模块 @@ -95,7 +97,7 @@ return [ 'var_method' => '_method', // +---------------------------------------------------------------------- - // | 模板引擎设置 + // | 模板设置 // +---------------------------------------------------------------------- 'template' => [ @@ -134,24 +136,26 @@ return [ 'error_message' => '页面错误!请稍后再试~', // 显示错误信息 'show_error_msg' => false, + // 异常处理handle类 留空使用 \think\exception\Handle + 'exception_handle' => '', // +---------------------------------------------------------------------- // | 日志设置 // +---------------------------------------------------------------------- 'log' => [ - // 日志记录方式,支持 file sae + // 日志记录方式,内置 file sae 支持扩展 'type' => 'File', // 日志保存目录 'path' => LOG_PATH, ], // +---------------------------------------------------------------------- - // | Trace设置 + // | Trace设置 开启 app_trace 后 有效 // +---------------------------------------------------------------------- 'trace' => [ - // 支持Html Socket Console 设为false则不显示 - 'type' => false, + // 内置Html Socket Console 支持扩展 + 'type' => 'Html', ], // +---------------------------------------------------------------------- @@ -245,6 +249,7 @@ return [ // 自动写入时间戳字段 'auto_timestamp' => false, ], + //分页配置 'paginate' => [ 'type' => 'bootstrap', diff --git a/core/extend/com/Database.php b/core/extend/com/Database.php index 6854c254..2661ab4e 100644 --- a/core/extend/com/Database.php +++ b/core/extend/com/Database.php @@ -207,6 +207,6 @@ class Database{ * 析构方法,用于关闭文件资源 */ public function __destruct(){ - $this->config['compress'] ? @gzclose($this->fp) : @fclose($this->fp); + //$this->config['compress'] ? @gzclose($this->fp) : @fclose($this->fp); } } \ No newline at end of file diff --git a/core/library/think/App.php b/core/library/think/App.php index b6438d04..9226284e 100644 --- a/core/library/think/App.php +++ b/core/library/think/App.php @@ -155,7 +155,9 @@ class App Hook::listen('app_end', $response); // Trace调试注入 - Debug::inject($response); + if (Config::get('app_trace')) { + Debug::inject($response); + } return $response; } diff --git a/core/library/think/Cookie.php b/core/library/think/Cookie.php index 0ad2ff56..eb31c769 100644 --- a/core/library/think/Cookie.php +++ b/core/library/think/Cookie.php @@ -88,7 +88,7 @@ class Cookie array_walk_recursive($value, 'self::jsonFormatProtect', 'encode'); $value = 'think:' . json_encode($value); } - $expire = !empty($config['expire']) ? time() + intval($config['expire']) : 0; + $expire = !empty($config['expire']) ? $_SERVER['REQUEST_TIME'] + intval($config['expire']) : 0; if ($config['setcookie']) { setcookie($name, $value, $expire, $config['path'], $config['domain'], $config['secure'], $config['httponly']); } @@ -143,7 +143,7 @@ class Cookie $prefix = !is_null($prefix) ? $prefix : $config['prefix']; $name = $prefix . $name; if ($config['setcookie']) { - setcookie($name, '', time() - 3600, $config['path'], $config['domain'], $config['secure'], $config['httponly']); + setcookie($name, '', $_SERVER['REQUEST_TIME'] - 3600, $config['path'], $config['domain'], $config['secure'], $config['httponly']); } // 删除指定cookie unset($_COOKIE[$name]); @@ -169,7 +169,7 @@ class Cookie foreach ($_COOKIE as $key => $val) { if (0 === strpos($key, $prefix)) { if ($config['setcookie']) { - setcookie($key, '', time() - 3600, $config['path'], $config['domain'], $config['secure'], $config['httponly']); + setcookie($key, '', $_SERVER['REQUEST_TIME'] - 3600, $config['path'], $config['domain'], $config['secure'], $config['httponly']); } unset($_COOKIE[$key]); } diff --git a/core/library/think/Debug.php b/core/library/think/Debug.php index 05695400..28b6ab1e 100644 --- a/core/library/think/Debug.php +++ b/core/library/think/Debug.php @@ -186,40 +186,33 @@ class Debug public static function inject(Response $response) { - $config = Config::get('trace'); - $type = isset($config['type']) ? $config['type'] : 'Html'; + $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); + unset($config['type']); + if (class_exists($class)) { + $trace = new $class($config); + } else { + throw new ClassNotFoundException('class not exists:' . $class, $class); + } - 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, ''); - if (false !== $pos) { - $content = substr($content, 0, $pos) . $output . substr($content, $pos); - } else { - $content = $content . $output; - } - $response->content($content); + if ($response instanceof Redirect) { + //TODO 记录 + } else { + $output = $trace->output($response, Log::getLog()); + if (is_string($output)) { + // trace调试信息注入 + $content = $response->getContent(); + $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/core/library/think/Error.php b/core/library/think/Error.php index 047b12d6..28f75882 100644 --- a/core/library/think/Error.php +++ b/core/library/think/Error.php @@ -63,7 +63,7 @@ class Error if (error_reporting() & $errno) { // 将错误信息托管至 think\exception\ErrorException throw $exception; - }else{ + } else { self::getExceptionHandler()->report($exception); } } @@ -98,24 +98,20 @@ class Error /** * Get an instance of the exception handler. * - * @return \think\exception\Handle + * @return Handle */ public static function getExceptionHandler() { static $handle; - if (!$handle) { - - if ($class = Config::get('exception_handle')) { - if (class_exists($class) && is_subclass_of($class, "\\think\\exception\\Handle")) { - $handle = new $class; - } - } - if (!$handle) { - $handle = new Handle(); + // 异常处理handle + $class = Config::get('exception_handle'); + if ($class && class_exists($class) && is_subclass_of($class, "\\think\\exception\\Handle")) { + $handle = new $class; + } else { + $handle = new Handle; } } - return $handle; } } diff --git a/core/library/think/Log.php b/core/library/think/Log.php index 608910a0..1db7dc13 100644 --- a/core/library/think/Log.php +++ b/core/library/think/Log.php @@ -10,12 +10,13 @@ // +---------------------------------------------------------------------- namespace think; + use think\exception\ClassNotFoundException; /** * Class Log * @package think - * + * * @method void log($msg) static * @method void error($msg) static * @method void info($msg) static @@ -48,13 +49,13 @@ class Log * 日志初始化 * @param array $config */ - public static function init($config = []) + public static function init($config = []) { $type = isset($config['type']) ? $config['type'] : 'File'; $class = false !== strpos($type, '\\') ? $type : '\\think\\log\\driver\\' . ucwords($type); self::$config = $config; unset($config['type']); - if(class_exists($class)) { + if (class_exists($class)) { self::$driver = new $class($config); } else { throw new ClassNotFoundException('class not exists:' . $class, $class); @@ -62,7 +63,7 @@ class Log // 记录初始化信息 App::$debug && Log::record('[ LOG ] INIT ' . $type . ': ' . var_export($config, true), 'info'); } - + /** * 获取日志信息 * @param string $type 信息类型 @@ -110,7 +111,6 @@ class Log */ public static function check($config) { - if (self::$key && !empty($config['allow_key']) && !in_array(self::$key, $config['allow_key'])) { return false; } diff --git a/core/library/think/Url.php b/core/library/think/Url.php index 0d9bd418..5f802011 100644 --- a/core/library/think/Url.php +++ b/core/library/think/Url.php @@ -258,6 +258,10 @@ class Url // 生成路由映射并缓存 private static function getRouteAlias() { + static $item = []; + if (!empty($item)) { + return $item; + } if ($item = Cache::get('think_route_map')) { return $item; } diff --git a/core/library/think/cache/driver/File.php b/core/library/think/cache/driver/File.php index 1edda249..53a5ded6 100644 --- a/core/library/think/cache/driver/File.php +++ b/core/library/think/cache/driver/File.php @@ -101,7 +101,7 @@ class File $content = file_get_contents($filename); if (false !== $content) { $expire = (int) substr($content, 8, 12); - if (0 != $expire && time() > filemtime($filename) + $expire) { + if (0 != $expire && $_SERVER['REQUEST_TIME'] > filemtime($filename) + $expire) { //缓存过期删除缓存文件 $this->unlink($filename); return false; diff --git a/core/library/think/cache/driver/Lite.php b/core/library/think/cache/driver/Lite.php index fb9cfa75..551359f7 100644 --- a/core/library/think/cache/driver/Lite.php +++ b/core/library/think/cache/driver/Lite.php @@ -65,7 +65,7 @@ class Lite if (is_file($filename)) { // 判断是否过期 $mtime = filemtime($filename); - if ($mtime < time()) { + if ($mtime < $_SERVER['REQUEST_TIME']) { // 清除已经过期的文件 unlink($filename); return false; @@ -97,7 +97,7 @@ class Lite $ret = file_put_contents($filename, (" 0, 'timeout' => 0, // 超时时间(单位:毫秒) 'prefix' => '', + 'username' => '', //账号 + 'password' => '', //密码 ]; /** @@ -55,6 +57,10 @@ class Memcached $servers[] = [$host, (isset($ports[$i]) ? $ports[$i] : $ports[0]), 1]; } $this->handler->addServers($servers); + if('' != $this->options['username']){ + $this->handler->setOption(\Memcached::OPT_BINARY_PROTOCOL, true); + $this->handler->setSaslAuthData($this->options['username'], $this->options['password']); + } } /** @@ -82,7 +88,7 @@ class Memcached $expire = $this->options['expire']; } $name = $this->options['prefix'] . $name; - $expire = 0 == $expire ? 0 : time() + $expire; + $expire = 0 == $expire ? 0 : $_SERVER['REQUEST_TIME'] + $expire; if ($this->handler->set($name, $value, $expire)) { return true; } diff --git a/core/library/think/cache/driver/Sqlite.php b/core/library/think/cache/driver/Sqlite.php index 681ace18..8a7c19aa 100644 --- a/core/library/think/cache/driver/Sqlite.php +++ b/core/library/think/cache/driver/Sqlite.php @@ -56,7 +56,7 @@ class Sqlite implements CacheInterface public function get($name) { $name = $this->options['prefix'] . sqlite_escape_string($name); - $sql = 'SELECT value FROM ' . $this->options['table'] . ' WHERE var=\'' . $name . '\' AND (expire=0 OR expire >' . time() . ') LIMIT 1'; + $sql = 'SELECT value FROM ' . $this->options['table'] . ' WHERE var=\'' . $name . '\' AND (expire=0 OR expire >' . $_SERVER['REQUEST_TIME'] . ') LIMIT 1'; $result = sqlite_query($this->handler, $sql); if (sqlite_num_rows($result)) { $content = sqlite_fetch_single($result); @@ -84,7 +84,7 @@ class Sqlite implements CacheInterface if (is_null($expire)) { $expire = $this->options['expire']; } - $expire = (0 == $expire) ? 0 : (time() + $expire); //缓存有效期为0表示永久缓存 + $expire = (0 == $expire) ? 0 : ($_SERVER['REQUEST_TIME'] + $expire); //缓存有效期为0表示永久缓存 if (function_exists('gzcompress')) { //数据压缩 $value = gzcompress($value, 3); diff --git a/core/library/think/db/Query.php b/core/library/think/db/Query.php index 39943d6b..8b7c5556 100644 --- a/core/library/think/db/Query.php +++ b/core/library/think/db/Query.php @@ -390,7 +390,11 @@ class Query if (isset($this->options['field'])) { unset($this->options['field']); } - $pdo = $this->field($field)->fetchPdo(true)->find(); + $pdo = $this->field($field)->fetchPdo(true)->find(); + if (is_string($pdo)) { + // 返回SQL语句 + return $pdo; + } $result = $pdo->fetchColumn(); if (isset($cache)) { // 缓存数据 @@ -430,6 +434,10 @@ class Query $field = $key . ',' . $field; } $pdo = $this->field($field)->fetchPdo(true)->select(); + if (is_string($pdo)) { + // 返回SQL语句 + return $pdo; + } if (1 == $pdo->columnCount()) { $result = $pdo->fetchAll(PDO::FETCH_COLUMN); } else { diff --git a/core/library/think/debug/Console.php b/core/library/think/debug/Console.php index 6eacf23c..6933d5af 100644 --- a/core/library/think/debug/Console.php +++ b/core/library/think/debug/Console.php @@ -15,6 +15,8 @@ use think\Cache; use think\Config; use think\Db; use think\Debug; +use think\Request; +use think\Response; /** * 浏览器调试输出 @@ -36,11 +38,20 @@ class Console /** * 调试输出接口 * @access public - * @param array $log 日志信息 + * @param Response $response Response对象 + * @param array $log 日志信息 * @return bool */ - public function output(array $log = []) + public function output(Response $response, array $log = []) { + $request = Request::instance(); + $contentType = $response->getHeader('Content-Type'); + $accept = $request->header('accept'); + if (strpos($accept, 'application/json') === 0 || $request->isAjax()) { + return false; + } elseif (!empty($contentType) && strpos($contentType, 'html') === false) { + return false; + } // 获取基本信息 $runtime = number_format(microtime(true), 8, '.', '') - THINK_START_TIME; $reqs = number_format(1 / $runtime, 2); @@ -49,13 +60,13 @@ class Console if (isset($_SERVER['HTTP_HOST'])) { $uri = $_SERVER['SERVER_PROTOCOL'] . ' ' . $_SERVER['REQUEST_METHOD'] . ' : ' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; } else { - $uri = "cmd:" . implode(' ', $_SERVER['argv']); + $uri = 'cmd:' . implode(' ', $_SERVER['argv']); } // 页面Trace信息 $base = [ '请求信息' => date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']) . ' ' . $uri, - '运行时间' => "{$runtime}s [ 吞吐率:{$reqs}req/s ] 内存消耗:{$mem}kb 文件加载:" . count(get_included_files()), + '运行时间' => number_format($runtime, 6) . 's [ 吞吐率:' . $reqs . 'req/s ] 内存消耗:' . $mem . 'kb 文件加载:' . count(get_included_files()), '查询信息' => Db::$queryTimes . ' queries ' . Db::$executeTimes . ' writes ', '缓存信息' => Cache::$readTimes . ' reads,' . Cache::$writeTimes . ' writes', '配置加载' => count(Config::get()), diff --git a/core/library/think/debug/Html.php b/core/library/think/debug/Html.php index 7f16dc0e..a1d42fb5 100644 --- a/core/library/think/debug/Html.php +++ b/core/library/think/debug/Html.php @@ -15,6 +15,8 @@ use think\Cache; use think\Config; use think\Db; use think\Debug; +use think\Request; +use think\Response; /** * 页面Trace调试 @@ -36,12 +38,20 @@ class Html /** * 调试输出接口 * @access public - * @param array $log 日志信息 + * @param Response $response Response对象 + * @param array $log 日志信息 * @return bool */ - public function output(array $log = []) + public function output(Response $response, array $log = []) { - + $request = Request::instance(); + $contentType = $response->getHeader('Content-Type'); + $accept = $request->header('accept'); + if (strpos($accept, 'application/json') === 0 || $request->isAjax()) { + return false; + } elseif (!empty($contentType) && strpos($contentType, 'html') === false) { + return false; + } // 获取基本信息 $runtime = number_format(microtime(true), 8, '.', '') - THINK_START_TIME; $reqs = number_format(1 / $runtime, 2); @@ -51,11 +61,11 @@ class Html if (isset($_SERVER['HTTP_HOST'])) { $uri = $_SERVER['SERVER_PROTOCOL'] . ' ' . $_SERVER['REQUEST_METHOD'] . ' : ' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; } else { - $uri = "cmd:" . implode(' ', $_SERVER['argv']); + $uri = 'cmd:' . implode(' ', $_SERVER['argv']); } $base = [ '请求信息' => date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']) . ' ' . $uri, - '运行时间' => "{$runtime}s [ 吞吐率:{$reqs}req/s ] 内存消耗:{$mem}kb 文件加载:" . count(get_included_files()), + '运行时间' => number_format($runtime, 6) . 's [ 吞吐率:' . $reqs . 'req/s ] 内存消耗:' . $mem . 'kb 文件加载:' . count(get_included_files()), '查询信息' => Db::$queryTimes . ' queries ' . Db::$executeTimes . ' writes ', '缓存信息' => Cache::$readTimes . ' reads,' . Cache::$writeTimes . ' writes', '配置加载' => count(Config::get()), diff --git a/core/library/think/debug/Socket.php b/core/library/think/debug/Socket.php index ae018a04..2f604ef9 100644 --- a/core/library/think/debug/Socket.php +++ b/core/library/think/debug/Socket.php @@ -11,6 +11,8 @@ namespace think\debug; +use think\Response; + /** * github: https://github.com/luofei614/SocketLog * @author luofei614 @@ -20,24 +22,22 @@ class Socket public $port = 1116; //SocketLog 服务的http的端口号 protected $config = [ - 'enable' => true, //是否记录日志的开关 + // socket服务器地址 'host' => 'localhost', - //是否显示利于优化的参数,如果允许时间,消耗内存等 - 'optimize' => false, + // 是否显示加载的文件列表 'show_included_files' => false, - 'error_handler' => false, - //日志强制记录到配置的client_id + // 日志强制记录到配置的client_id 'force_client_ids' => [], - //限制允许读取日志的client_id + // 限制允许读取日志的client_id 'allow_client_ids' => [], ]; protected $css = [ - 'sql' => 'color:#009bb4;', - 'sql_warn' => 'color:#009bb4;font-size:14px;', - 'error_handler' => 'color:#f4006b;font-size:14px;', - 'page' => 'color:#40e2ff;background:#171717;', - 'big' => 'font-size:20px;color:red;', + 'sql' => 'color:#009bb4;', + 'sql_warn' => 'color:#009bb4;font-size:14px;', + 'error' => 'color:#f4006b;font-size:14px;', + 'page' => 'color:#40e2ff;background:#171717;', + 'big' => 'font-size:20px;color:red;', ]; protected $allowForceClientIds = []; //配置强制推送且被授权的client_id @@ -57,25 +57,26 @@ class Socket /** * 调试输出接口 * @access public - * @param array $logs 日志信息 + * @param Response $response Response对象 + * @param array $log 日志信息 * @return bool */ - public function output(array $logs = []) + public function output(Response $response, array $log = []) { if (!$this->check()) { return false; } $runtime = number_format(microtime(true), 8, '.', '') - THINK_START_TIME; $reqs = number_format(1 / number_format($runtime, 8), 2); - $time_str = " [运行时间:{$runtime}s][吞吐率:{$reqs}req/s]"; + $time_str = ' [运行时间:' . number_format($runtime, 6) . 's][吞吐率:' . $reqs . 'req/s]'; $memory_use = number_format((memory_get_usage() - THINK_START_MEM) / 1024, 2); - $memory_str = " [内存消耗:{$memory_use}kb]"; - $file_load = " [文件加载:" . count(get_included_files()) . "]"; + $memory_str = ' [内存消耗:' . $memory_use . 'kb]'; + $file_load = ' [文件加载:' . count(get_included_files()) . ']'; if (isset($_SERVER['HTTP_HOST'])) { $current_uri = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; } else { - $current_uri = "cmd:" . implode(' ', $_SERVER['argv']); + $current_uri = 'cmd:' . implode(' ', $_SERVER['argv']); } // 基本信息 $trace[] = [ @@ -84,7 +85,7 @@ class Socket 'css' => $this->css['page'], ]; - foreach ($logs as $type => $val) { + foreach ($log as $type => $val) { $trace[] = [ 'type' => 'groupCollapsed', 'msg' => '[ ' . $type . ' ]', @@ -110,7 +111,7 @@ class Socket if ($this->config['show_included_files']) { $trace[] = [ 'type' => 'groupCollapsed', - 'msg' => 'included_files', + 'msg' => '[ file ]', 'css' => '', ]; $trace[] = [ @@ -171,9 +172,6 @@ class Socket protected function check() { - if (!$this->config['enable']) { - return false; - } $tabid = $this->getClientArg('tabid'); //是否记录日志的检查 if (!$tabid && !$this->config['force_client_ids']) { @@ -225,7 +223,7 @@ class Socket } /** - * @param null $host - $host of socket server + * @param string $host - $host of socket server * @param string $message - 发送的消息 * @param string $address - 地址 * @return bool diff --git a/core/library/think/log/driver/File.php b/core/library/think/log/driver/File.php index b3a077b2..a540aac0 100644 --- a/core/library/think/log/driver/File.php +++ b/core/library/think/log/driver/File.php @@ -45,7 +45,7 @@ class File //检测日志文件大小,超过配置大小则备份日志文件重新生成 if (is_file($destination) && floor($this->config['file_size']) <= filesize($destination)) { - rename($destination, dirname($destination) . DS . time() . '-' . basename($destination)); + rename($destination, dirname($destination) . DS . $_SERVER['REQUEST_TIME'] . '-' . basename($destination)); } // 获取基本信息 diff --git a/core/library/think/model/Relation.php b/core/library/think/model/Relation.php index a065524c..d762afd5 100644 --- a/core/library/think/model/Relation.php +++ b/core/library/think/model/Relation.php @@ -197,7 +197,7 @@ class Relation if (!empty($range)) { // 查询关联数据 $data = $this->eagerlyManyToMany($model, [ - 'pivot.' . $foreignKey => [ + 'pivot.' . $localKey => [ 'in', $range, ], @@ -277,7 +277,7 @@ class Relation if (isset($result->$pk)) { $pk = $result->$pk; // 查询管理数据 - $data = $this->eagerlyManyToMany($model, ['pivot.' . $foreignKey => $pk], $relation, $subRelation); + $data = $this->eagerlyManyToMany($model, ['pivot.' . $localKey => $pk], $relation, $subRelation); // 关联数据封装 if (!isset($data[$pk])) { @@ -317,7 +317,7 @@ class Relation // 设置关联模型属性 $list[$relation] = []; } - $result->setAttr($relation, new $model($list[$relation])); + $result->setAttr($relation, (new $model($list[$relation]))->isUpdate(true)); } /** @@ -374,7 +374,7 @@ class Relation } } $set->pivot = new Pivot($pivot, $this->middle); - $data[$set->$foreignKey][] = $set; + $data[$pivot[$localKey]][] = $set; } return $data; } diff --git a/core/library/think/template/driver/File.php b/core/library/think/template/driver/File.php index 8206571c..1cf98a27 100644 --- a/core/library/think/template/driver/File.php +++ b/core/library/think/template/driver/File.php @@ -62,7 +62,7 @@ class File if (!file_exists($cacheFile)) { return false; } - if (0 != $cacheTime && time() > filemtime($cacheFile) + $cacheTime) { + if (0 != $cacheTime && $_SERVER['REQUEST_TIME'] > filemtime($cacheFile) + $cacheTime) { // 缓存是否在有效期 return false; } diff --git a/core/library/think/template/driver/Sae.php b/core/library/think/template/driver/Sae.php index 4c6e98a2..9e5230a5 100644 --- a/core/library/think/template/driver/Sae.php +++ b/core/library/think/template/driver/Sae.php @@ -44,7 +44,7 @@ class Sae public function write($cacheFile, $content) { // 添加写入时间 - $content = time() . $content; + $content = $_SERVER['REQUEST_TIME'] . $content; if (!$this->mc->set($cacheFile, $content, MEMCACHE_COMPRESSED, 0)) { throw new Exception('sae mc write error:' . $cacheFile); } else { @@ -76,7 +76,7 @@ class Sae public function check($cacheFile, $cacheTime) { $mtime = $this->get($cacheFile, 'mtime'); - if (0 != $cacheTime && time() > $mtime + $cacheTime) { + if (0 != $cacheTime && $_SERVER['REQUEST_TIME'] > $mtime + $cacheTime) { // 缓存是否在有效期 return false; } diff --git a/core/tpl/make_controller.tpl b/core/tpl/make.tpl similarity index 93% rename from core/tpl/make_controller.tpl rename to core/tpl/make.tpl index c2e1cbbe..13da3bc5 100644 --- a/core/tpl/make_controller.tpl +++ b/core/tpl/make.tpl @@ -11,7 +11,7 @@ namespace {%namespace%}; -class {%className%} extends {%extend%} +class {%className%} {%extend%} { - +{%content%} }