From 7dec0b2f9d9593e141380cbe565164c36379d78e Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 28 Jan 2016 14:57:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=BA=E5=8C=96=E6=97=A5=E5=BF=97=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=20=E4=BB=85=E8=B0=83=E8=AF=95=E6=A8=A1=E5=BC=8F?= =?UTF-8?q?=E4=B8=8B=E6=9C=89=E6=95=88=20Input=E7=B1=BB=E7=9A=84=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E6=94=AF=E6=8C=81=20=E5=88=A4=E6=96=AD=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E5=8F=98=E9=87=8F=E6=98=AF=E5=90=A6=E5=AD=98=E5=9C=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helper.php | 8 +++++++- library/think/App.php | 8 ++++---- library/think/Cache.php | 2 ++ library/think/Config.php | 2 ++ library/think/Db.php | 2 ++ library/think/Input.php | 28 +++++++++++++++++++++++----- library/think/Lang.php | 2 ++ library/think/Loader.php | 10 +++++++++- library/think/Log.php | 4 ++++ library/think/Model.php | 10 +++++++--- library/think/Route.php | 2 +- library/think/Session.php | 2 ++ library/think/View.php | 2 +- library/think/db/Driver.php | 2 ++ library/think/log/driver/Trace.php | 8 ++++---- 15 files changed, 72 insertions(+), 20 deletions(-) diff --git a/helper.php b/helper.php index bd688eba..61cb5c98 100644 --- a/helper.php +++ b/helper.php @@ -32,6 +32,12 @@ function C($name = '', $value = null, $range = '') // 获取输入数据 支持默认值和过滤 function I($key, $default = null, $filter = '', $merge = false, $data = []) { + if (0 === strpos($key, '?')) { + $key = substr($key, 1); + $has = '?'; + } else { + $has = ''; + } if ($pos = strpos($key, '.')) { // 指定参数来源 $method = substr($key, 0, $pos); @@ -44,7 +50,7 @@ function I($key, $default = null, $filter = '', $merge = false, $data = []) // 默认为自动判断 $method = 'param'; } - return \think\Input::$method($key, $default, $filter, $merge, $data); + return \think\Input::$method($has . $key, $default, $filter, $merge, $data); } /** diff --git a/library/think/App.php b/library/think/App.php index b0c6831a..fa3733ea 100644 --- a/library/think/App.php +++ b/library/think/App.php @@ -79,7 +79,7 @@ class App self::route($config); } // 记录路由信息 - Log::record('[ ROUTE ] ' . var_export(self::$dispatch, true), 'info'); + APP_DEBUG && Log::record('[ ROUTE ] ' . var_export(self::$dispatch, true), 'info'); // 监听app_begin APP_HOOK && Hook::listen('app_begin'); @@ -120,7 +120,7 @@ class App $reflect = new \ReflectionFunction($function); $args = self::bindParams($reflect, $vars); // 记录执行信息 - Log::record('[ RUN ] ' . $reflect->getFileName() . '[ ' . var_export($vars, true) . ' ]', 'info'); + APP_DEBUG && Log::record('[ RUN ] ' . $reflect->getFileName() . '[ ' . var_export($vars, true) . ' ]', 'info'); return $reflect->invokeArgs($args); } @@ -149,7 +149,7 @@ class App } $args = self::bindParams($reflect, $vars); // 记录执行信息 - Log::record('[ RUN ] ' . $reflect->getFileName() . '[ ' . var_export($args, true) . ' ]', 'info'); + APP_DEBUG && Log::record('[ RUN ] ' . $reflect->getFileName() . '[ ' . var_export($args, true) . ' ]', 'info'); return $reflect->invokeArgs(isset($class) ? $class : null, $args); } @@ -248,7 +248,7 @@ class App if (method_exists($instance, '_empty')) { $method = new \ReflectionMethod($instance, '_empty'); $data = $method->invokeArgs($instance, [$action, '']); - Log::record('[ RUN ] ' . $method->getFileName(), 'info'); + APP_DEBUG && Log::record('[ RUN ] ' . $method->getFileName(), 'info'); } else { throw new Exception('method [ ' . (new \ReflectionClass($instance))->getName() . '->' . $action . ' ] not exists ', 10002); } diff --git a/library/think/Cache.php b/library/think/Cache.php index 66046bd0..eaaad34c 100644 --- a/library/think/Cache.php +++ b/library/think/Cache.php @@ -38,6 +38,8 @@ class Cache $class = (!empty($options['namespace']) ? $options['namespace'] : '\\think\\cache\\driver\\') . ucwords($type); unset($options['type']); self::$instance[$md5] = new $class($options); + // 记录初始化信息 + APP_DEBUG && Log::record('[ CACHE ] INIT ' . $type . ':' . var_export($options, true), 'info'); } self::$handler = self::$instance[$md5]; return self::$handler; diff --git a/library/think/Config.php b/library/think/Config.php index b6a4d365..b656e8a5 100644 --- a/library/think/Config.php +++ b/library/think/Config.php @@ -58,6 +58,8 @@ class Config if (!isset(self::$config[$range])) { self::$config[$range] = []; } + // 记录加载信息 + APP_DEBUG && Log::record('[ CONFIG ] ' . $file, 'info'); return is_file($file) ? self::set(include $file, $name, $range) : self::$config[$range]; } diff --git a/library/think/Db.php b/library/think/Db.php index 26734f00..408a1015 100644 --- a/library/think/Db.php +++ b/library/think/Db.php @@ -43,6 +43,8 @@ class Db } $class = (!empty($options['namespace']) ? $options['namespace'] : '\\think\\db\\driver\\') . ucwords($options['type']); self::$instances[$md5] = new $class($options); + // 记录初始化信息 + APP_DEBUG && Log::record('[ DB ] INIT ' . $options['type'] . ':' . var_export($options, true), 'info'); } self::$instance = self::$instances[$md5]; return self::$instance; diff --git a/library/think/Input.php b/library/think/Input.php index a95995f1..73928820 100644 --- a/library/think/Input.php +++ b/library/think/Input.php @@ -203,9 +203,12 @@ class Input */ public static function data($name, $default = null, $filter = null, $merge = false, $input = null) { + if (0 === strpos($name, '?')) { + return self::has(substr($name, 1), $input); + } if (is_null($input) && !empty($name)) { $input = $name; - $name = ''; + $name = ''; } if (!empty($input)) { $data = $input; @@ -231,7 +234,7 @@ class Input if (is_array($data)) { array_walk_recursive($data, 'self::filter', $filters); } else { - self::filter($data, $name?:0, $filters); + self::filter($data, $name ?: 0, $filters); } if (isset($type) && $data !== $default) { // 强制类型转换 @@ -243,6 +246,22 @@ class Input return $data; } + /** + * 判断一个变量是否设置 + * @param string $name + * @param array $data + * @return bool + */ + public static function has($name, $data) + { + foreach (explode('.', $name) as $val) { + if (!isset($data[$val])) { + return false; + } + } + return true; + } + /** * 设置默认的过滤函数 * @param string|array $name @@ -288,7 +307,7 @@ class Input $value = call_user_func($filter, $value); } else { $begin = substr($filter, 0, 1); - if (in_array($begin, ['/','#','~']) && $begin == $end = substr($filter, -1)) { + if (in_array($begin, ['/', '#', '~']) && $begin == $end = substr($filter, -1)) { // 正则过滤 if (!preg_match($filter, $value)) { // 匹配不成功返回默认值 @@ -355,13 +374,12 @@ class Input { if (is_null(static::$filters)) { // 从配置项中读取 - $filters = \think\Config::get('default_filter'); + $filters = \think\Config::get('default_filter'); static::$filters = empty($filters) ? [] : (is_array($filters) ? $filters : explode(',', $filters)); } return static::$filters; } - /** * 强类型转换 * @param string $data diff --git a/library/think/Lang.php b/library/think/Lang.php index 93e97478..54db3fc8 100644 --- a/library/think/Lang.php +++ b/library/think/Lang.php @@ -67,6 +67,8 @@ class Lang } $lang = []; foreach ($file as $_file) { + // 记录加载信息 + APP_DEBUG && Log::record('[ LANG ] ' . $file, 'info'); $_lang = is_file($_file) ? include $_file : []; $lang = array_merge($lang, array_change_key_case($_lang)); } diff --git a/library/think/Loader.php b/library/think/Loader.php index 2a49b982..fb58e6ce 100644 --- a/library/think/Loader.php +++ b/library/think/Loader.php @@ -15,6 +15,8 @@ class Loader { // 类名映射 protected static $map = []; + // 加载列表 + protected static $load = []; // 命名空间 protected static $namespace = []; // PSR-4 @@ -29,10 +31,14 @@ class Loader // 检查是否定义类库映射 if (isset(self::$map[$class])) { if (is_file(self::$map[$class])) { + // 记录加载信息 + APP_DEBUG && self::$load[] = self::$map[$class]; include self::$map[$class]; } } elseif ($file = self::findFileInComposer($class)) { // Composer自动加载 + // 记录加载信息 + APP_DEBUG && self::$load[] = $file; include $file; } else { // 命名空间自动加载 @@ -58,6 +64,8 @@ class Loader if (APP_DEBUG && IS_WIN && false === strpos(realpath($filename), $class . EXT)) { return; } + // 记录加载信息 + APP_DEBUG && self::$load[] = $filename; include $filename; } else { Log::record('autoloader error : ' . $filename, 'notic'); @@ -347,7 +355,7 @@ class Loader } $method = new \ReflectionMethod($class, $action . Config::get('action_suffix')); // 记录执行信息 - Log::record('[ RUN ] ' . $method->getFileName(), 'info'); + APP_DEBUG && Log::record('[ RUN ] ' . $method->getFileName(), 'info'); return $method->invokeArgs($class, $vars); } } diff --git a/library/think/Log.php b/library/think/Log.php index cf80a266..aaecfbf2 100644 --- a/library/think/Log.php +++ b/library/think/Log.php @@ -36,6 +36,8 @@ class Log $class = (!empty($config['namespace']) ? $config['namespace'] : '\\think\\log\\driver\\') . ucwords($type); unset($config['type']); self::$driver = new $class($config); + // 记录初始化信息 + APP_DEBUG && Log::record('[ LOG ] INIT ' . $type . ':' . var_export($config, true), 'info'); } // 通知初始化 @@ -45,6 +47,8 @@ class Log $class = (!empty($config['namespace']) ? $config['namespace'] : '\\think\\log\\alarm\\') . ucwords($type); unset($config['type']); self::$alarm = new $class($config['alarm']); + // 记录初始化信息 + APP_DEBUG && Log::record('[ CACHE ] ALARM ' . $type . ':' . var_export($config, true), 'info'); } /** diff --git a/library/think/Model.php b/library/think/Model.php index b18926cc..4da6c8db 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -27,13 +27,13 @@ class Model // 主键名称 protected $pk = 'id'; // 数据表前缀 - protected $tablePrefix = ''; + protected $tablePrefix = null; // 模型名称 protected $name = ''; // 数据库名称 protected $dbName = ''; //数据库配置 - protected $connection = ''; + protected $connection = []; // 数据表名(不包含表前缀) protected $tableName = ''; // 实际数据表名(包含表前缀) @@ -74,7 +74,11 @@ class Model list($this->dbName, $this->name) = explode('.', $this->name); } - $this->tablePrefix = !empty($config['prefix']) ? $config['prefix'] : Config::get('database.prefix'); + if (!empty($config['prefix'])) { + $this->tablePrefix = $config['prefix']; + } elseif (is_null($this->tablePrefix)) { + $this->tablePrefix = Config::get('database.prefix'); + } if (!empty($config['connection'])) { $this->connection = $config['connection']; } diff --git a/library/think/Route.php b/library/think/Route.php index 5e041c69..693a9fc0 100644 --- a/library/think/Route.php +++ b/library/think/Route.php @@ -420,7 +420,7 @@ class Route { if (!empty(self::$bind['type'])) { // 记录绑定信息 - Log::record('[ BIND ] ' . var_export(self::$bind, true), 'info'); + APP_DEBUG && Log::record('[ BIND ] ' . var_export(self::$bind, true), 'info'); // 如果有URL绑定 则进行绑定检测 switch (self::$bind['type']) { case 'class': diff --git a/library/think/Session.php b/library/think/Session.php index b7296ef2..df620e90 100644 --- a/library/think/Session.php +++ b/library/think/Session.php @@ -38,6 +38,8 @@ class Session */ public static function init(array $config = []) { + // 记录初始化信息 + APP_DEBUG && Log::record('[ SESSION ] INIT ' . var_export($config, true), 'info'); $isDoStart = false; if (isset($config['use_trans_sid'])) { ini_set('session.use_trans_sid', $config['use_trans_sid'] ? 1 : 0); diff --git a/library/think/View.php b/library/think/View.php index 94efce44..76be55b4 100644 --- a/library/think/View.php +++ b/library/think/View.php @@ -160,7 +160,7 @@ class View throw new Exception('template file not exists:' . $template, 10700); } // 记录视图信息 - Log::record('[ VIEW ] ' . $template . ' [ VARS : ' . var_export($vars, true) . ' ]', 'info'); + APP_DEBUG && Log::record('[ VIEW ] ' . $template . ' [ ' . var_export($vars, true) . ' ]', 'info'); } // 页面缓存 ob_start(); diff --git a/library/think/db/Driver.php b/library/think/db/Driver.php index 8c795fce..b23b3fd9 100644 --- a/library/think/db/Driver.php +++ b/library/think/db/Driver.php @@ -121,6 +121,8 @@ abstract class Driver $config['dsn'] = $this->parseDsn($config); } $this->links[$linkNum] = new PDO($config['dsn'], $config['username'], $config['password'], $this->options); + // 记录数据库连接信息 + APP_DEBUG && Log::record('[ DB ] CONNECT: ' . $config['dsn'], 'info'); } catch (\PDOException $e) { if ($autoConnection) { Log::record($e->getMessage(), 'error'); diff --git a/library/think/log/driver/Trace.php b/library/think/log/driver/Trace.php index ebb2d2fc..c33010bf 100644 --- a/library/think/log/driver/Trace.php +++ b/library/think/log/driver/Trace.php @@ -15,7 +15,7 @@ namespace think\log\driver; */ class Trace { - protected $tabs = ['base' => '基本', 'file' => '文件', 'notic|error' => '错误', 'sql' => 'SQL', 'info|debug|log' => '调试']; + protected $tabs = ['base' => '基本', 'file' => '文件', 'info' => '流程', 'notic|error' => '错误', 'sql' => 'SQL', 'debug|log' => '调试']; protected $config = [ 'trace_file' => '', ]; @@ -67,13 +67,13 @@ class Trace foreach ($this->tabs as $name => $title) { $name = strtolower($name); switch ($name) { - case 'base': // 基本信息 + case 'base': // 基本信息 $trace[$title] = $base; break; - case 'file': // 文件信息 + case 'file': // 文件信息 $trace[$title] = $info; break; - default: // 调试信息 + default: // 调试信息 if (strpos($name, '|')) { // 多组信息 $names = explode('|', $name);