1、更新内核

2、代码格式化
This commit is contained in:
2016-07-21 14:29:32 +08:00
parent 3cc2c38dc7
commit a72ba86faa
20 changed files with 1214 additions and 1083 deletions

View File

@@ -115,7 +115,7 @@ if (!function_exists('input')) {
* @param string $filter 过滤方法
* @return mixed
*/
function input($key, $default = null, $filter = null)
function input($key = '', $default = null, $filter = null)
{
if (0 === strpos($key, '?')) {
$key = substr($key, 1);
@@ -124,7 +124,7 @@ if (!function_exists('input')) {
if ($pos = strpos($key, '.')) {
// 指定参数来源
$method = substr($key, 0, $pos);
if (in_array($method, ['get', 'post', 'put', 'delete', 'param', 'request', 'session', 'cookie', 'server', 'env', 'path', 'file'])) {
if (in_array($method, ['get', 'post', 'put', 'patch', 'delete', 'param', 'request', 'session', 'cookie', 'server', 'env', 'path', 'file'])) {
$key = substr($key, $pos + 1);
} else {
$method = 'param';
@@ -191,7 +191,7 @@ if (!function_exists('db')) {
*/
function db($name = '', $config = [])
{
return Db::connect($config)->name($name);
return Db::connect($config, true)->name($name);
}
}
@@ -421,12 +421,13 @@ if (!function_exists('view')) {
* 渲染模板输出
* @param string $template 模板文件
* @param array $vars 模板变量
* @param array $replace 模板替换
* @param integer $code 状态码
* @return \think\response\View
*/
function view($template = '', $vars = [], $code = 200)
function view($template = '', $vars = [], $replace = [], $code = 200)
{
return Response::create($template, 'view', $code)->vars($vars);
return Response::create($template, 'view', $code)->replace($replace)->assign($vars);
}
}

View File

@@ -337,9 +337,9 @@ class App
} catch (\ReflectionException $e) {
// 操作不存在
if (method_exists($instance, '_empty')) {
$method = new \ReflectionMethod($instance, '_empty');
$data = $method->invokeArgs($instance, [$action, '']);
self::$debug && Log::record('[ RUN ] ' . $method->__toString(), 'info');
$reflect = new \ReflectionMethod($instance, '_empty');
$data = $reflect->invokeArgs($instance, [$action]);
self::$debug && Log::record('[ RUN ] ' . $reflect->__toString(), 'info');
} else {
throw new HttpException(404, 'method not exists:' . (new \ReflectionClass($instance))->getName() . '->' . $action);
}

View File

@@ -195,11 +195,8 @@ class File extends SplFileObject
{
$extension = strtolower(pathinfo($this->getInfo('name'), PATHINFO_EXTENSION));
/* 对图像文件进行严格检测 */
if (in_array($extension, array('gif', 'jpg', 'jpeg', 'bmp', 'png', 'swf'))) {
$imginfo = getimagesize($this->filename);
if (empty($imginfo) || ('gif' == $extension && empty($imginfo['bits']))) {
return false;
}
if (in_array($extension, ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'swf']) && !in_array(exif_imagetype($this->filename), [1, 2, 3, 4, 6])) {
return false;
}
return true;
}

View File

@@ -756,7 +756,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$value = null;
}
if (!in_array($field, $this->change)) {
$this->setAttr($field, isset($this->data[$field]) ? $this->data[$field] : $value);
$this->setAttr($field, !is_null($value) ? $value : (isset($this->data[$field]) ? $this->data[$field] : $value));
}
}
}

View File

@@ -83,7 +83,6 @@ class Request
protected $request = [];
protected $route = [];
protected $put;
protected $delete;
protected $session = [];
protected $file = [];
protected $cookie = [];
@@ -490,6 +489,7 @@ class Request
} elseif (!$this->method) {
if (isset($_POST[Config::get('var_method')])) {
$this->method = strtoupper($_POST[Config::get('var_method')]);
$this->{$this->method}($_POST);
} elseif (isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) {
$this->method = strtoupper($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']);
} else {
@@ -612,10 +612,9 @@ class Request
$vars = $this->post(false);
break;
case 'PUT':
$vars = $this->put(false);
break;
case 'DELETE':
$vars = $this->delete(false);
case 'PATCH':
$vars = $this->put(false);
break;
default:
$vars = [];
@@ -718,13 +717,20 @@ class Request
*/
public function delete($name = '', $default = null, $filter = null)
{
if (is_array($name)) {
return $this->delete = is_null($this->delete) ? $name : array_merge($this->delete, $name);
}
if (is_null($this->delete)) {
parse_str(file_get_contents('php://input'), $this->delete);
}
return $this->input($this->delete, $name, $default, $filter);
return $this->put($name, $default, $filter);
}
/**
* 设置获取获取PATCH参数
* @access public
* @param string|array $name 变量名
* @param mixed $default 默认值
* @param string|array $filter 过滤方法
* @return mixed
*/
public function patch($name = '', $default = null, $filter = null)
{
return $this->put($name, $default, $filter);
}
/**

View File

@@ -26,6 +26,7 @@ class Route
'POST' => [],
'PUT' => [],
'DELETE' => [],
'PATCH' => [],
'HEAD' => [],
'OPTIONS' => [],
'*' => [],
@@ -59,7 +60,7 @@ class Route
// 域名绑定
private static $bind = [];
// 当前分组
private static $group;
private static $group = '';
// 当前参数
private static $option = [];
@@ -242,7 +243,7 @@ class Route
self::$rules[$type][$rule] = ['rule' => $rule, 'route' => $route, 'var' => $vars, 'option' => $option, 'pattern' => $pattern];
if ('*' == $type) {
// 注册路由快捷方式
foreach (['GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS'] as $method) {
foreach (['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS'] as $method) {
self::$rules[$method][$rule] = true;
}
}
@@ -257,7 +258,11 @@ class Route
*/
public static function setGroup($name)
{
self::$group = $name;
if (self::$group) {
self::$group = self::$group . '/' . ltrim($name, '/');
} else {
self::$group = $name;
}
}
/**
@@ -291,9 +296,11 @@ class Route
if (!empty($name)) {
// 分组
if ($routes instanceof \Closure) {
$curentGroup = self::$group;
self::setGroup($name);
call_user_func_array($routes, []);
self::setGroup(null);
self::$group = $curentGroup;
self::$rules[$type][$name]['route'] = '';
self::$rules[$type][$name]['var'] = self::parseVar($name);
self::$rules[$type][$name]['option'] = $option;
@@ -317,7 +324,7 @@ class Route
self::$rules[$type][$name] = ['rule' => $item, 'route' => '', 'var' => [], 'option' => $option, 'pattern' => $pattern];
}
if ('*' == $type) {
foreach (['GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS'] as $method) {
foreach (['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS'] as $method) {
if (!isset(self::$rules[$method][$name])) {
self::$rules[$method][$name] = true;
} else {
@@ -408,6 +415,20 @@ class Route
self::rule($rule, $route, 'DELETE', $option, $pattern);
}
/**
* 注册PATCH路由
* @access public
* @param string $rule 路由规则
* @param string $route 路由地址
* @param array $option 路由参数
* @param array $pattern 变量规则
* @return void
*/
public static function patch($rule, $route = '', $option = [], $pattern = [])
{
self::rule($rule, $route, 'PATCH', $option, $pattern);
}
/**
* 注册资源路由
* @access public

View File

@@ -19,9 +19,6 @@ use think\Route;
class Url
{
// 生成URL地址的root
protected static $root;
/**
* URL生成 支持路由反射
* @param string $url URL表达式
@@ -116,7 +113,7 @@ class Url
// 检测域名
$domain = self::parseDomain($url, $domain);
// URL组装
$url = $domain . (self::$root ?: Request::instance()->root()) . '/' . ltrim($url, '/');
$url = $domain . Request::instance()->root() . '/' . ltrim($url, '/');
return $url;
}
@@ -319,11 +316,4 @@ class Url
{
Cache::rm('think_route_map');
}
// 指定当前生成URL地址的root
public static function root($root)
{
self::$root = $root;
Request::instance()->root($root);
}
}
}

View File

@@ -68,6 +68,7 @@ class Validate
'allowIp' => '不允许的IP访问',
'denyIp' => '禁止的IP访问',
'confirm' => ':attribute和字段 :rule 不一致',
'different' => ':attribute和字段 :rule 不能相同',
'egt' => ':attribute必须大于等于 :rule',
'gt' => ':attribute必须大于 :rule',
'elt' => ':attribute必须小于等于 :rule',
@@ -400,6 +401,19 @@ class Validate
return $this->getDataValue($data, $rule) == $value;
}
/**
* 验证是否和某个字段的值是否不同
* @access protected
* @param mixed $value 字段值
* @param mixed $rule 验证规则
* @param array $data 数据
* @return bool
*/
protected function different($value, $rule, $data)
{
return $this->getDataValue($data, $rule) != $value;
}
/**
* 验证是否大于等于某个值
* @access protected

View File

@@ -11,8 +11,9 @@
namespace think\controller;
use think\Response;
use think\App;
use think\Request;
use think\Response;
abstract class Rest
{
@@ -38,7 +39,7 @@ abstract class Rest
{
// 资源类型检测
$request = Request::instance();
$ext = $request->ext();
$ext = $request->ext();
if ('' == $ext) {
// 自动检测资源类型
$this->type = $request->type();
@@ -61,11 +62,10 @@ abstract class Rest
* REST 调用
* @access public
* @param string $method 方法名
* @param array $args 参数
* @return mixed
* @throws \Exception
*/
public function _empty($method, $args)
public function _empty($method)
{
if (method_exists($this, $method . '_' . $this->method . '_' . $this->type)) {
// RESTFul方法支持
@@ -76,7 +76,7 @@ abstract class Rest
$fun = $method . '_' . $this->method;
}
if (isset($fun)) {
return $this->$fun();
return App::invokeMethod([$this, $fun]);
} else {
// 抛出异常
throw new \Exception('error action :' . $method);

View File

@@ -357,7 +357,7 @@ class Query
protected function builder()
{
static $builder = [];
$driver = $this->driver;
$driver = $this->driver;
if (!isset($builder[$driver])) {
$class = '\\think\\db\\builder\\' . ucfirst($driver);
$builder[$driver] = new $class($this->connection);
@@ -660,7 +660,7 @@ class Query
}
if (count($join)) {
// 有设置第二个元素则把第二元素作为表前缀
$table = (string)current($join) . $table;
$table = (string) current($join) . $table;
} elseif (false === strpos($table, '.')) {
// 加上默认的表前缀
$table = $prefix . $table;
@@ -975,7 +975,7 @@ class Query
/** @var Paginator $class */
$class = false !== strpos($config['type'], '\\') ? $config['type'] : '\\think\\paginator\\driver\\' . ucwords($config['type']);
$page = isset($config['page']) ? (int)$config['page'] : call_user_func([
$page = isset($config['page']) ? (int) $config['page'] : call_user_func([
$class,
'getCurrentPage',
], $config['var_page']);
@@ -983,7 +983,7 @@ class Query
$page = $page < 1 ? 1 : $page;
$config['path'] = isset($config['path']) ? $config['path'] : call_user_func([$class, 'getCurrentPath']);
if (!$simple) {
$options = $this->getOptions();
$total = $this->count();
@@ -1326,7 +1326,7 @@ class Query
if (!isset($this->info[$guid])) {
$info = $this->connection->getFields($tableName);
$fields = array_keys($info);
$bind = $type = [];
$bind = $type = [];
foreach ($info as $key => $val) {
// 记录字段类型
$type[$key] = $val['type'];
@@ -1444,7 +1444,7 @@ class Query
$relation = $key;
$with[$key] = $key;
} elseif (is_string($relation) && strpos($relation, '.')) {
$with[$key] = $relation;
$with[$key] = $relation;
list($relation, $subRelation) = explode('.', $relation, 2);
}
@@ -1479,7 +1479,7 @@ class Query
if ($closure) {
// 执行闭包查询
call_user_func_array($closure, [& $this]);
call_user_func_array($closure, [ & $this]);
//指定获取关联的字段
//需要在 回调中 调方法 withField 方法,如
// $query->where(['id'=>1])->withField('id,name');
@@ -1599,13 +1599,15 @@ class Query
$options = $this->parseExpress();
// 生成SQL语句
$sql = $this->builder()->insert($data, $options, $replace);
// 获取参数绑定
$bind = $this->getBind();
if ($options['fetch_sql']) {
// 获取实际执行的SQL语句
return $this->connection->getRealSql($sql, $this->bind);
return $this->connection->getRealSql($sql, $bind);
}
$sequence = $sequence ?: (isset($options['sequence']) ? $options['sequence'] : null);
// 执行操作
return $this->execute($sql, $this->getBind(), $getLastInsID, $sequence);
return $this->execute($sql, $bind, $getLastInsID, $sequence);
}
/**
@@ -1636,12 +1638,14 @@ class Query
}
// 生成SQL语句
$sql = $this->builder()->insertAll($dataSet, $options);
// 获取参数绑定
$bind = $this->getBind();
if ($options['fetch_sql']) {
// 获取实际执行的SQL语句
return $this->connection->getRealSql($sql, $this->bind);
return $this->connection->getRealSql($sql, $bind);
} else {
// 执行操作
return $this->execute($sql, $this->getBind());
return $this->execute($sql, $bind);
}
}
@@ -1660,12 +1664,14 @@ class Query
// 生成SQL语句
$table = $this->parseSqlTable($table);
$sql = $this->builder()->selectInsert($fields, $table, $options);
// 获取参数绑定
$bind = $this->getBind();
if ($options['fetch_sql']) {
// 获取实际执行的SQL语句
return $this->connection->getRealSql($sql, $this->bind);
return $this->connection->getRealSql($sql, $bind);
} else {
// 执行操作
return $this->execute($sql, $this->getBind());
return $this->execute($sql, $bind);
}
}
@@ -1708,9 +1714,11 @@ class Query
}
// 生成UPDATE SQL语句
$sql = $this->builder()->update($data, $options);
// 获取参数绑定
$bind = $this->getBind();
if ($options['fetch_sql']) {
// 获取实际执行的SQL语句
return $this->connection->getRealSql($sql, $this->bind);
return $this->connection->getRealSql($sql, $bind);
} else {
// 检测缓存
if (isset($key) && Cache::get($key)) {
@@ -1718,7 +1726,7 @@ class Query
Cache::rm($key);
}
// 执行操作
return '' == $sql ? 0 : $this->execute($sql, $this->getBind());
return '' == $sql ? 0 : $this->execute($sql, $bind);
}
}
@@ -1736,7 +1744,7 @@ class Query
if ($data instanceof Query) {
return $data->select();
} elseif ($data instanceof \Closure) {
call_user_func_array($data, [& $this]);
call_user_func_array($data, [ & $this]);
$data = null;
}
// 分析查询表达式
@@ -1760,12 +1768,14 @@ class Query
if (!$resultSet) {
// 生成查询SQL
$sql = $this->builder()->select($options);
// 获取参数绑定
$bind = $this->getBind();
if ($options['fetch_sql']) {
// 获取实际执行的SQL语句
return $this->connection->getRealSql($sql, $this->bind);
return $this->connection->getRealSql($sql, $bind);
}
// 执行查询操作
$resultSet = $this->query($sql, $this->getBind(), $options['master'], $options['fetch_class']);
$resultSet = $this->query($sql, $bind, $options['master'], $options['fetch_class']);
if ($resultSet instanceof \PDOStatement) {
// 返回PDOStatement对象
@@ -1819,7 +1829,7 @@ class Query
if ($data instanceof Query) {
return $data->find();
} elseif ($data instanceof \Closure) {
call_user_func_array($data, [& $this]);
call_user_func_array($data, [ & $this]);
$data = null;
}
// 分析查询表达式
@@ -1845,12 +1855,14 @@ class Query
if (!$result) {
// 生成查询SQL
$sql = $this->builder()->select($options);
// 获取参数绑定
$bind = $this->getBind();
if ($options['fetch_sql']) {
// 获取实际执行的SQL语句
return $this->connection->getRealSql($sql, $this->bind);
return $this->connection->getRealSql($sql, $bind);
}
// 执行查询
$result = $this->query($sql, $this->getBind(), $options['master'], $options['fetch_class']);
$result = $this->query($sql, $bind, $options['master'], $options['fetch_class']);
if ($result instanceof \PDOStatement) {
// 返回PDOStatement对象
@@ -2015,10 +2027,11 @@ class Query
}
// 生成删除SQL语句
$sql = $this->builder()->delete($options);
// 获取参数绑定
$bind = $this->getBind();
if ($options['fetch_sql']) {
// 获取实际执行的SQL语句
return $this->connection->getRealSql($sql, $this->bind);
return $this->connection->getRealSql($sql, $bind);
}
// 检测缓存
@@ -2027,7 +2040,7 @@ class Query
Cache::rm($key);
}
// 执行操作
return $this->execute($sql, $this->getBind());
return $this->execute($sql, $bind);
}
/**
@@ -2112,10 +2125,10 @@ class Query
if (isset($options['page'])) {
// 根据页数计算limit
list($page, $listRows) = $options['page'];
$page = $page > 0 ? $page : 1;
$listRows = $listRows > 0 ? $listRows : (is_numeric($options['limit']) ? $options['limit'] : 20);
$offset = $listRows * ($page - 1);
$options['limit'] = $offset . ',' . $listRows;
$page = $page > 0 ? $page : 1;
$listRows = $listRows > 0 ? $listRows : (is_numeric($options['limit']) ? $options['limit'] : 20);
$offset = $listRows * ($page - 1);
$options['limit'] = $offset . ',' . $listRows;
}
$this->options = [];

View File

@@ -44,13 +44,13 @@ class Handle
'message' => $this->getMessage($exception),
'code' => $this->getCode($exception),
];
$log = "[{$data['code']}]{$data['message']}[{$data['file']}:{$data['line']}]";
$log = "[{$data['code']}]{$data['message']}[{$data['file']}:{$data['line']}]";
} else {
$data = [
'code' => $this->getCode($exception),
'message' => $this->getMessage($exception),
];
$log = "[{$data['code']}]{$data['message']}";
$log = "[{$data['code']}]{$data['message']}";
}
Log::record($log, 'error');
@@ -103,7 +103,7 @@ class Handle
$status = $e->getStatusCode();
$template = Config::get('http_exception_template');
if (!App::$debug && !empty($template[$status])) {
return Response::create($template[$status], 'view')->vars(['e' => $e]);
return Response::create($template[$status], 'view', $status)->assign(['e' => $e]);
} else {
return $this->convertExceptionToResponse($e);
}
@@ -155,9 +155,9 @@ class Handle
while (ob_get_level() > 1) {
ob_end_clean();
}
$data['echo'] = ob_get_clean();
ob_start();
extract($data);
include Config::get('exception_tmpl');

View File

@@ -36,18 +36,6 @@ class View extends Response
->fetch($data, $this->vars, $this->replace);
}
/**
* 视图变量赋值
* @access public
* @param array $vars 模板变量
* @return $this
*/
public function vars($vars = [])
{
$this->vars = $vars;
return $this;
}
/**
* 获取视图变量
* @access public
@@ -56,11 +44,11 @@ class View extends Response
*/
public function getVars($name = null)
{
if(is_null($name)){
if (is_null($name)) {
return $this->vars;
}else{
} else {
return isset($this->vars[$name]) ? $this->vars[$name] : null;
}
}
}
/**