强化日志信息 仅调试模式下有效 Input类的方法支持 判断一个变量是否存在

This commit is contained in:
thinkphp
2016-01-28 14:57:30 +08:00
parent ad0b7255e4
commit 7dec0b2f9d
15 changed files with 72 additions and 20 deletions

View File

@@ -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);
}
/**

View File

@@ -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);
}

View File

@@ -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;

View File

@@ -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];
}

View File

@@ -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;

View File

@@ -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

View File

@@ -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));
}

View File

@@ -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);
}
}

View File

@@ -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');
}
/**

View File

@@ -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'];
}

View File

@@ -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':

View File

@@ -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);

View File

@@ -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();

View File

@@ -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');

View File

@@ -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);