diff --git a/composer.json b/composer.json index 8a2e7a1c4..66eb2c456 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "require": { "php": ">=5.6.0", "endroid/qr-code": "^1.9", - "topthink/framework": "5.1.*", + "topthink/framework": "5.1.16", "zoujingli/ip2region": "dev-master", "aliyuncs/oss-sdk-php": "^2.2", "zoujingli/weopen-developer": "dev-master", diff --git a/thinkphp/library/think/App.php b/thinkphp/library/think/App.php index e5b75d2ba..4c2e19d10 100644 --- a/thinkphp/library/think/App.php +++ b/thinkphp/library/think/App.php @@ -20,7 +20,7 @@ use think\route\Dispatch; */ class App extends Container { - const VERSION = '5.1.17'; + const VERSION = '5.1.16'; /** * 当前模块路径 @@ -333,6 +333,7 @@ class App extends Container // 对容器中的对象实例进行配置更新 $this->containerConfigUpdate($module); } + } protected function containerConfigUpdate($module) @@ -426,7 +427,30 @@ class App extends Container } $this->middleware->add(function (Request $request, $next) use ($dispatch, $data) { - return is_null($data) ? $dispatch->run() : $data; + if (is_null($data)) { + try { + // 执行调度 + $data = $dispatch->run(); + } catch (HttpResponseException $exception) { + $data = $exception->getResponse(); + } + } + + // 输出数据到客户端 + if ($data instanceof Response) { + $response = $data; + } elseif (!is_null($data)) { + // 默认自动识别响应输出类型 + $isAjax = $request->isAjax(); + $type = $isAjax ? $this->config('app.default_ajax_return') : $this->config('app.default_return_type'); + + $response = Response::create($data, $type); + } else { + $data = ob_get_clean(); + $status = empty($data) ? 204 : 200; + $response = Response::create($data, '', $status); + } + return $response; }); $response = $this->middleware->dispatch($this->request); diff --git a/thinkphp/library/think/Controller.php b/thinkphp/library/think/Controller.php index 19fc8df6f..0220aee4a 100644 --- a/thinkphp/library/think/Controller.php +++ b/thinkphp/library/think/Controller.php @@ -43,17 +43,11 @@ class Controller protected $batchValidate = false; /** - * 前置操作方法列表(即将废弃) + * 前置操作方法列表 * @var array $beforeActionList */ protected $beforeActionList = []; - /** - * 控制器中间件 - * @var array - */ - protected $middleware = []; - /** * 构造方法 * @access public @@ -67,24 +61,7 @@ class Controller // 控制器初始化 $this->initialize(); - // 控制器中间件 - if ($this->middleware) { - foreach ($this->middleware as $key => $val) { - if (!is_int($key)) { - if (isset($val['only']) && !in_array($this->request->action(), $val['only'])) { - continue; - } elseif (isset($val['except']) && in_array($this->request->action(), $val['except'])) { - continue; - } else { - $val = $key; - } - } - - $this->app['middleware']->controller($val); - } - } - - // 前置操作方法 即将废弃 + // 前置操作方法 foreach ((array) $this->beforeActionList as $method => $options) { is_numeric($method) ? $this->beforeAction($options) : diff --git a/thinkphp/library/think/Log.php b/thinkphp/library/think/Log.php index b63233cd6..5cc98b008 100644 --- a/thinkphp/library/think/Log.php +++ b/thinkphp/library/think/Log.php @@ -117,7 +117,7 @@ class Log implements LoggerInterface return; } - if (is_string($msg) && !empty($context)) { + if (is_string($msg)) { $replace = []; foreach ($context as $key => $val) { $replace['{' . $key . '}'] = $val; diff --git a/thinkphp/library/think/Middleware.php b/thinkphp/library/think/Middleware.php index 4814fdb99..229ceaa43 100644 --- a/thinkphp/library/think/Middleware.php +++ b/thinkphp/library/think/Middleware.php @@ -39,95 +39,62 @@ class Middleware $this->config = array_merge($this->config, $config); } - /** - * 导入中间件 - * @access public - * @param array $middlewares - * @param string $type 中间件类型 - */ - public function import(array $middlewares = [], $type = 'route') + public function import(array $middlewares = []) { foreach ($middlewares as $middleware) { - $this->add($middleware, $type); + $this->add($middleware); } } /** - * 注册中间件 - * @access public - * @param mixed $middleware - * @param string $type 中间件类型 + * {@inheritdoc} */ - public function add($middleware, $type = 'route') + public function add($middleware) { if (is_null($middleware)) { return; } - $middleware = $this->buildMiddleware($middleware, $type); + $middleware = $this->buildMiddleware($middleware); if ($middleware) { - $this->queue[$type][] = $middleware; + $this->queue[] = $middleware; } } /** - * 注册控制器中间件 - * @access public - * @param mixed $middleware + * {@inheritdoc} */ - public function controller($middleware) - { - return $this->add($middleware, 'controller'); - } - - /** - * 移除中间件 - * @access public - * @param mixed $middleware - * @param string $type 中间件类型 - */ - public function unshift($middleware, $type = 'route') + public function unshift($middleware) { if (is_null($middleware)) { return; } - $middleware = $this->buildMiddleware($middleware, $type); + $middleware = $this->buildMiddleware($middleware); if ($middleware) { - array_unshift($this->queue[$type], $middleware); + array_unshift($this->queue, $middleware); } } /** - * 获取注册的中间件 - * @access public - * @param string $type 中间件类型 + * {@inheritdoc} */ - public function all($type = 'route') + public function all() { - return $this->queue[$type] ?: []; + return $this->queue; } /** - * 中间件调度 - * @access public - * @param Request $request - * @param string $type 中间件类型 + * {@inheritdoc} */ - public function dispatch(Request $request, $type = 'route') + public function dispatch(Request $request) { - return call_user_func($this->resolve($type), $request); + return call_user_func($this->resolve(), $request); } - /** - * 解析中间件 - * @access protected - * @param mixed $middleware - * @param string $type 中间件类型 - */ - protected function buildMiddleware($middleware, $type = 'route') + protected function buildMiddleware($middleware) { if (is_array($middleware)) { list($middleware, $param) = $middleware; @@ -150,7 +117,7 @@ class Middleware } if (is_array($middleware)) { - return $this->import($middleware, $type); + return $this->import($middleware); } if (strpos($middleware, ':')) { @@ -160,11 +127,10 @@ class Middleware return [[$this->app->make($middleware), 'handle'], isset($param) ? $param : null]; } - protected function resolve($type = 'route') + protected function resolve() { - return function (Request $request) use ($type) { - - $middleware = array_shift($this->queue[$type]); + return function (Request $request) { + $middleware = array_shift($this->queue); if (null === $middleware) { throw new InvalidArgumentException('The queue was exhausted, with no response returned'); @@ -173,7 +139,7 @@ class Middleware list($call, $param) = $middleware; try { - $response = call_user_func_array($call, [$request, $this->resolve($type), $param]); + $response = call_user_func_array($call, [$request, $this->resolve(), $param]); } catch (HttpResponseException $exception) { $response = $exception->getResponse(); } diff --git a/thinkphp/library/think/Model.php b/thinkphp/library/think/Model.php index 98d29eb4c..828894d00 100644 --- a/thinkphp/library/think/Model.php +++ b/thinkphp/library/think/Model.php @@ -236,7 +236,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess // 设置当前模型 确保查询返回模型对象 $query = Db::connect($this->connection, false, $this->query); $query->model($this) - ->json($this->json, $this->jsonAssoc) + ->json($this->json) ->setJsonFieldType($this->jsonType); if (isset(static::$readMaster['*']) || isset(static::$readMaster[static::class])) { @@ -284,7 +284,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $query = $this->buildQuery(); // 软删除 - if (property_exists($this, 'withTrashed') && !$this->withTrashed) { + if (method_exists($this, 'withNoTrashed')) { $this->withNoTrashed($query); } @@ -750,6 +750,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess */ public function saveAll($dataSet, $replace = true) { + $result = []; + $db = $this->db(false); $db->startTrans(); @@ -760,8 +762,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $auto = true; } - $result = []; - foreach ($dataSet as $key => $data) { if ($this->exists || (!empty($auto) && isset($data[$pk]))) { $result[$key] = self::update($data, [], $this->field); diff --git a/thinkphp/library/think/Request.php b/thinkphp/library/think/Request.php index 93fbb95b7..2be157af4 100644 --- a/thinkphp/library/think/Request.php +++ b/thinkphp/library/think/Request.php @@ -11,9 +11,6 @@ namespace think; -use think\facade\Cookie; -use think\facade\Session; - class Request { /** @@ -313,6 +310,7 @@ class Request { $request = new static($config->pull('app')); + $request->cookie = $app['cookie']->get(); $request->server = $_SERVER; $request->env = $app['env']->get(); @@ -763,19 +761,18 @@ class Request /** * 当前的请求类型 * @access public - * @param bool $origin 是否获取原始请求类型 + * @param bool $method true 获取原始请求类型 * @return string */ - public function method($origin = false) + public function method($method = false) { - if ($origin) { + if (true === $method) { // 获取原始请求类型 return $this->isCli() ? 'GET' : $this->server('REQUEST_METHOD'); } elseif (!$this->method) { if (isset($_POST[$this->config['var_method']])) { - $this->method = strtoupper($_POST[$this->config['var_method']]); - $method = strtolower($this->method); - $this->{$method} = $_POST; + $this->method = strtoupper($_POST[$this->config['var_method']]); + $this->{$this->method}($_POST); } elseif ($this->server('HTTP_X_HTTP_METHOD_OVERRIDE')) { $this->method = strtoupper($this->server('HTTP_X_HTTP_METHOD_OVERRIDE')); } else { @@ -913,7 +910,6 @@ class Request // 获取包含文件上传信息的数组 $file = $this->file(); $data = is_array($file) ? array_merge($this->param, $file) : $this->param; - return $this->input($data, '', $default, $filter); } @@ -1064,16 +1060,14 @@ class Request public function session($name = '', $default = null) { if (empty($this->session)) { - $this->session = Session::get(); + $this->session = facade\Session::get(); } if ('' === $name) { return $this->session; } - $data = $this->getData($this->session, $name); - - return is_null($data) ? $default : $data; + return isset($this->session[$name]) ? $this->session[$name] : $default; } /** @@ -1086,12 +1080,8 @@ class Request */ public function cookie($name = '', $default = null, $filter = '') { - if (empty($this->cookie)) { - $this->cookie = Cookie::get(); - } - if (!empty($name)) { - $data = Cookie::has($name) ? Cookie::get($name) : $default; + $data = isset($this->cookie[$name]) ? $this->cookie[$name] : $default; } else { $data = $this->cookie; } @@ -1282,10 +1272,14 @@ class Request list($name, $type) = explode('/', $name); } - $data = $this->getData($data, $name); - - if (is_null($data)) { - return $default; + // 按.拆分成多维数组进行判断 + foreach (explode('.', $name) as $val) { + if (isset($data[$val])) { + $data = $data[$val]; + } else { + // 无输入数据,返回默认值 + return $default; + } } if (is_object($data)) { @@ -1311,26 +1305,6 @@ class Request return $data; } - /** - * 获取数据 - * @access public - * @param array $data 数据源 - * @param string|false $name 字段名 - * @return mixed - */ - protected function getData(array $data, $name) - { - foreach (explode('.', $name) as $val) { - if (isset($data[$val])) { - $data = $data[$val]; - } else { - return; - } - } - - return $data; - } - /** * 设置或获取当前的过滤规则 * @access public diff --git a/thinkphp/library/think/Route.php b/thinkphp/library/think/Route.php index cb0a3748e..29e0d33e6 100644 --- a/thinkphp/library/think/Route.php +++ b/thinkphp/library/think/Route.php @@ -390,33 +390,6 @@ class Route return $this->app['rule_name']->get($name, $domain); } - /** - * 读取路由 - * @access public - * @param string $rule 路由规则 - * @param string $domain 域名 - * @return array - */ - public function getRule($rule, $domain = null) - { - if (is_null($domain)) { - $domain = $this->domain; - } - - return $this->app['rule_name']->getRule($rule, $domain); - } - - /** - * 读取路由 - * @access public - * @param string $domain 域名 - * @return array - */ - public function getRuleList($domain = null) - { - return $this->app['rule_name']->getRuleList($domain); - } - /** * 批量导入路由标识 * @access public diff --git a/thinkphp/library/think/Session.php b/thinkphp/library/think/Session.php index e700f0e4c..89e339b24 100644 --- a/thinkphp/library/think/Session.php +++ b/thinkphp/library/think/Session.php @@ -478,21 +478,16 @@ class Session public function has($name, $prefix = null) { empty($this->init) && $this->boot(); - $prefix = !is_null($prefix) ? $prefix : $this->prefix; - $value = $prefix ? (!empty($_SESSION[$prefix]) ? $_SESSION[$prefix] : []) : $_SESSION; - $name = explode('.', $name); + if (strpos($name, '.')) { + // 支持数组 + list($name1, $name2) = explode('.', $name); - foreach ($name as $val) { - if (!isset($value[$val])) { - return false; - } else { - $value = $value[$val]; - } + return $prefix ? isset($_SESSION[$prefix][$name1][$name2]) : isset($_SESSION[$name1][$name2]); + } else { + return $prefix ? isset($_SESSION[$prefix][$name]) : isset($_SESSION[$name]); } - - return true; } /** diff --git a/thinkphp/library/think/Validate.php b/thinkphp/library/think/Validate.php index de46956cf..bddbc69d7 100644 --- a/thinkphp/library/think/Validate.php +++ b/thinkphp/library/think/Validate.php @@ -112,6 +112,23 @@ class Validate */ protected $currentScene = null; + /** + * 内置正则验证规则 + * @var array + */ + protected $regex = [ + 'alpha' => '/^[A-Za-z]+$/', + 'alphaNum' => '/^[A-Za-z0-9]+$/', + 'alphaDash' => '/^[A-Za-z0-9\-\_]+$/', + 'chs' => '/^[\x{4e00}-\x{9fa5}]+$/u', + 'chsAlpha' => '/^[\x{4e00}-\x{9fa5}a-zA-Z]+$/u', + 'chsAlphaNum' => '/^[\x{4e00}-\x{9fa5}a-zA-Z0-9]+$/u', + 'chsDash' => '/^[\x{4e00}-\x{9fa5}a-zA-Z0-9\_\-]+$/u', + 'mobile' => '/^1[3-9][0-9]\d{8}$/', + 'idCard' => '/(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)/', + 'zip' => '/\d{6}/', + ]; + /** * Filter_var 规则 * @var array @@ -125,21 +142,6 @@ class Validate 'float' => FILTER_VALIDATE_FLOAT, ]; - /** - * 内置正则验证规则 - * @var array - */ - protected $regex = [ - 'alphaDash' => '/^[A-Za-z0-9\-\_]+$/', - 'chs' => '/^[\x{4e00}-\x{9fa5}]+$/u', - 'chsAlpha' => '/^[\x{4e00}-\x{9fa5}a-zA-Z]+$/u', - 'chsAlphaNum' => '/^[\x{4e00}-\x{9fa5}a-zA-Z0-9]+$/u', - 'chsDash' => '/^[\x{4e00}-\x{9fa5}a-zA-Z0-9\_\-]+$/u', - 'mobile' => '/^1[3-9][0-9]\d{8}$/', - 'idCard' => '/(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)/', - 'zip' => '/\d{6}/', - ]; - /** * 验证场景定义 * @var array @@ -749,9 +751,6 @@ class Validate case 'number': $result = ctype_digit((string) $value); break; - case 'alphaNum': - $result = ctype_alnum($value); - break; case 'array': // 是否为数组 $result = is_array($value); @@ -769,10 +768,6 @@ class Validate if (isset(self::$type[$rule])) { // 注册的验证规则 $result = call_user_func_array(self::$type[$rule], [$value]); - } elseif (function_exists('ctype_' . $rule)) { - // ctype验证规则 - $ctypeFun = 'ctype_' . $rule; - $result = $ctypeFun($value); } elseif (isset($this->filter[$rule])) { // Filter_var验证规则 $result = $this->filter($value, $this->filter[$rule]); diff --git a/thinkphp/library/think/db/Connection.php b/thinkphp/library/think/db/Connection.php index 72722362b..67a4958f7 100644 --- a/thinkphp/library/think/db/Connection.php +++ b/thinkphp/library/think/db/Connection.php @@ -1340,8 +1340,11 @@ abstract class Connection if (empty($options['fetch_sql']) && !empty($options['cache'])) { // 判断查询缓存 - $cache = $options['cache']; - $result = $this->getCacheData($query, $cache, null, $guid); + $cache = $options['cache']; + + $guid = is_string($cache['key']) ? $cache['key'] : $this->getCacheKey($query, $field); + + $result = Container::get('cache')->get($guid); if (false !== $result) { return $result; diff --git a/thinkphp/library/think/db/Query.php b/thinkphp/library/think/db/Query.php index b156524a0..23476a32f 100644 --- a/thinkphp/library/think/db/Query.php +++ b/thinkphp/library/think/db/Query.php @@ -87,7 +87,7 @@ class Query * 读取主库的表 * @var array */ - protected static $readMaster = []; + private static $readMaster = []; /** * 日期查询表达式 @@ -635,11 +635,11 @@ class Query * COUNT查询 * @access public * @param string $field 字段名 - * @return float|string + * @return integer|string */ public function count($field = '*') { - if (!empty($this->options['group'])) { + if (isset($this->options['group'])) { // 支持GROUP $options = $this->getOptions(); $subSql = $this->options($options)->field('count(' . $field . ') AS think_count')->bind($this->bind)->buildSql(); @@ -660,7 +660,7 @@ class Query * SUM查询 * @access public * @param string $field 字段名 - * @return float + * @return float|int */ public function sum($field) { @@ -695,7 +695,7 @@ class Query * AVG查询 * @access public * @param string $field 字段名 - * @return float + * @return float|int */ public function avg($field) { @@ -2124,13 +2124,11 @@ class Query * 设置JSON字段信息 * @access public * @param array $json JSON字段 - * @param bool $assoc 是否取出数组 * @return $this */ - public function json(array $json = [], $assoc = false) + public function json(array $json = []) { - $this->options['json'] = $json; - $this->options['json_assoc'] = $assoc; + $this->options['json'] = $json; return $this; } @@ -2249,32 +2247,6 @@ class Query return $this->parseWhereExp($logic, $field, strtolower($op) . ' time', $range, [], true); } - /** - * 查询当前时间在两个时间字段范围 - * @access public - * @param string $startField 开始时间字段 - * @param string $endField 结束时间字段 - * @return $this - */ - public function whereBetweenTimeField($startField, $endField) - { - return $this->whereTime($startField, '<=', time()) - ->whereTime($endField, '>=', time()); - } - - /** - * 查询当前时间不在两个时间字段范围 - * @access public - * @param string $startField 开始时间字段 - * @param string $endField 结束时间字段 - * @return $this - */ - public function whereNotBetweenTimeField($startField, $endField) - { - return $this->whereTime($startField, '>', time()) - ->whereTime($endField, '<', time(), 'OR'); - } - /** * 查询日期或者时间范围 * @access public @@ -2925,7 +2897,7 @@ class Query protected function resultToModel(&$result, $options = [], $resultSet = false) { if (!empty($options['json'])) { - $this->jsonResult($result, $options['json'], $options['json_assoc']); + $this->jsonResult($result, $options['json']); } $condition = (!$resultSet && isset($options['where']['AND'])) ? $options['where']['AND'] : null; diff --git a/thinkphp/library/think/db/connector/Sqlsrv.php b/thinkphp/library/think/db/connector/Sqlsrv.php index adefbb35c..4b5417097 100644 --- a/thinkphp/library/think/db/connector/Sqlsrv.php +++ b/thinkphp/library/think/db/connector/Sqlsrv.php @@ -125,100 +125,6 @@ class Sqlsrv extends Connection return $info; } - /** - * 得到某个列的数组 - * @access public - * @param Query $query 查询对象 - * @param string $field 字段名 多个字段用逗号分隔 - * @param string $key 索引 - * @return array - */ - public function column(Query $query, $field, $key = '') - { - $options = $query->getOptions(); - - if (empty($options['fetch_sql']) && !empty($options['cache'])) { - // 判断查询缓存 - $cache = $options['cache']; - - $guid = is_string($cache['key']) ? $cache['key'] : $this->getCacheKey($query, $field); - - $result = Container::get('cache')->get($guid); - - if (false !== $result) { - return $result; - } - } - - if (isset($options['field'])) { - $query->removeOption('field'); - } - - if (is_null($field)) { - $field = '*'; - } elseif ($key && '*' != $field) { - $field = $key . ',' . $field; - } - - if (is_string($field)) { - $field = array_map('trim', explode(',', $field)); - } - - $query->setOption('field', $field); - - // 生成查询SQL - $sql = $this->builder->select($query); - - $bind = $query->getBind(); - - if (!empty($options['fetch_sql'])) { - // 获取实际执行的SQL语句 - return $this->getRealSql($sql, $bind); - } - - // 执行查询操作 - $pdo = $this->query($sql, $bind, $options['master'], true); - - if (1 == $pdo->columnCount()) { - $result = $pdo->fetchAll(PDO::FETCH_COLUMN); - } else { - $resultSet = $pdo->fetchAll(PDO::FETCH_ASSOC); - - if ('*' == $field && $key) { - $result = array_column($resultSet, null, $key); - } elseif ($resultSet) { - $fields = array_keys($resultSet[0]); - $count = count($fields); - $key1 = array_shift($fields); - $key2 = $fields ? array_shift($fields) : ''; - $key = $key ?: $key1; - - if (strpos($key, '.')) { - list($alias, $key) = explode('.', $key); - } - - if (3 == $count) { - $column = $key2; - } elseif ($count < 3) { - $column = $key1; - } else { - $column = null; - } - - $result = array_column($resultSet, $column, $key); - } else { - $result = []; - } - } - - if (isset($cache) && isset($guid)) { - // 缓存数据 - $this->cacheData($guid, $result, $cache); - } - - return $result; - } - /** * SQL性能分析 * @access protected diff --git a/thinkphp/library/think/facade/Route.php b/thinkphp/library/think/facade/Route.php index c9f843d90..3e4dcaaee 100644 --- a/thinkphp/library/think/facade/Route.php +++ b/thinkphp/library/think/facade/Route.php @@ -26,7 +26,7 @@ use think\Facade; * @method void setName(string $name) static 批量导入路由标识 * @method void import(array $rules, string $type = '*') static 导入配置文件的路由规则 * @method \think\route\RuleItem rule(string $rule, mixed $route, string $method = '*', array $option = [], array $pattern = []) static 注册路由规则 - * @method void rules(array $rules, string $method = '*', array $option = [], array $pattern = []) static 批量注册路由规则 + * @method void rules(string $rules, string $method = '*', array $option = [], array $pattern = []) static 批量注册路由规则 * @method \think\route\RuleGroup group(string|array $name, mixed $route, string $method = '*', array $option = [], array $pattern = []) static 注册路由分组 * @method \think\route\RuleItem any(string $rule, mixed $route, array $option = [], array $pattern = []) static 注册路由 * @method \think\route\RuleItem get(string $rule, mixed $route, array $option = [], array $pattern = []) static 注册路由 diff --git a/thinkphp/library/think/log/driver/File.php b/thinkphp/library/think/log/driver/File.php index dfd963c28..ff3225907 100644 --- a/thinkphp/library/think/log/driver/File.php +++ b/thinkphp/library/think/log/driver/File.php @@ -72,7 +72,7 @@ class File $info[$type][] = $this->config['json'] ? $msg : '[ ' . $type . ' ] ' . $msg; } - if (!$this->config['json'] && (true === $this->config['apart_level'] || in_array($type, $this->config['apart_level']))) { + if (!$this->config['json'] && in_array($type, $this->config['apart_level'])) { // 独立记录的日志级别 $filename = $this->getApartLevelFile($path, $type); diff --git a/thinkphp/library/think/model/concern/Attribute.php b/thinkphp/library/think/model/concern/Attribute.php index c65c557bb..2ff0a6eab 100644 --- a/thinkphp/library/think/model/concern/Attribute.php +++ b/thinkphp/library/think/model/concern/Attribute.php @@ -36,12 +36,6 @@ trait Attribute */ protected $json = []; - /** - * JSON数据取出是否需要转换为数组 - * @var bool - */ - protected $jsonAssoc = false; - /** * JSON数据表字段类型 * @var array diff --git a/thinkphp/library/think/model/concern/SoftDelete.php b/thinkphp/library/think/model/concern/SoftDelete.php index c35a2a8ba..fa052945a 100644 --- a/thinkphp/library/think/model/concern/SoftDelete.php +++ b/thinkphp/library/think/model/concern/SoftDelete.php @@ -10,12 +10,6 @@ use think\db\Query; trait SoftDelete { - /** - * 是否包含软删除数据 - * @var bool - */ - protected $withTrashed = false; - /** * 判断当前实例是否被软删除 * @access public @@ -41,19 +35,7 @@ trait SoftDelete { $model = new static(); - return $model->withTrashedData(true)->db(false); - } - - /** - * 是否包含软删除数据 - * @access protected - * @param bool $withTrashed 是否包含软删除数据 - * @return $this - */ - protected function withTrashedData($withTrashed) - { - $this->withTrashed = $withTrashed; - return $this; + return $model->db(false); } /** @@ -105,7 +87,7 @@ trait SoftDelete $result = $this->isUpdate()->withEvent(false)->save(); - $this->withEvent(true); + $this->withEvent = true; } else { // 读取更新条件 $where = $this->getWhere(); diff --git a/thinkphp/library/think/model/relation/BelongsToMany.php b/thinkphp/library/think/model/relation/BelongsToMany.php index 02e666695..a6ff4ef47 100644 --- a/thinkphp/library/think/model/relation/BelongsToMany.php +++ b/thinkphp/library/think/model/relation/BelongsToMany.php @@ -563,7 +563,7 @@ class BelongsToMany extends Relation $pivot[] = [$this->localKey, '=', $this->parent->$pk]; if (isset($id)) { - $pivot[] = [$this->foreignKey, is_array($id) ? 'in' : '=', $id]; + $pivot[] = is_array($id) ? [$this->foreignKey, 'in', $id] : [$this->foreignKey, '=', $id]; } $result = $this->pivot->where($pivot)->delete(); diff --git a/thinkphp/library/think/model/relation/MorphTo.php b/thinkphp/library/think/model/relation/MorphTo.php index bb7c4d0b3..17315c88c 100644 --- a/thinkphp/library/think/model/relation/MorphTo.php +++ b/thinkphp/library/think/model/relation/MorphTo.php @@ -198,14 +198,14 @@ class MorphTo extends Relation if ($key == $result->$morphType) { // 关联模型 if (!isset($data[$result->$morphKey])) { - $relationModel = null; + throw new Exception('relation data not exists :' . $this->model); } else { $relationModel = $data[$result->$morphKey]; $relationModel->setParent(clone $result); $relationModel->isUpdate(true); - } - $result->setRelation($attr, $relationModel); + $result->setRelation($attr, $relationModel); + } } } } diff --git a/thinkphp/library/think/route/Dispatch.php b/thinkphp/library/think/route/Dispatch.php index 7f1982cdb..27a56d461 100644 --- a/thinkphp/library/think/route/Dispatch.php +++ b/thinkphp/library/think/route/Dispatch.php @@ -162,29 +162,7 @@ abstract class Dispatch $this->autoValidate($option['validate']); } - $data = $this->exec(); - - return $this->autoResponse($data); - } - - protected function autoResponse($data) - { - if ($data instanceof Response) { - $response = $data; - } elseif (!is_null($data)) { - // 默认自动识别响应输出类型 - $isAjax = $this->request->isAjax(); - $type = $isAjax ? $this->rule->getConfig('default_ajax_return') : $this->rule->getConfig('default_return_type'); - - $response = Response::create($data, $type); - } else { - $data = ob_get_clean(); - $data = false === $data ? '' : $data; - $status = '' === $data ? 204 : 200; - $response = Response::create($data, '', $status); - } - - return $response; + return $this->exec(); } /** diff --git a/thinkphp/library/think/route/Rule.php b/thinkphp/library/think/route/Rule.php index 75fb7a9f5..bdb7f1f26 100644 --- a/thinkphp/library/think/route/Rule.php +++ b/thinkphp/library/think/route/Rule.php @@ -171,16 +171,6 @@ abstract class Rule return $this->parent; } - /** - * 获取路由所在域名 - * @access public - * @return string - */ - public function getDomain() - { - return $this->parent->getDomain(); - } - /** * 获取变量规则定义 * @access public diff --git a/thinkphp/library/think/route/RuleGroup.php b/thinkphp/library/think/route/RuleGroup.php index 6b837bdcb..fc1ed6127 100644 --- a/thinkphp/library/think/route/RuleGroup.php +++ b/thinkphp/library/think/route/RuleGroup.php @@ -312,50 +312,33 @@ class RuleGroup extends Rule } } - if (empty($regex)) { - return false; - } - try { - $result = preg_match('/^(?:' . implode('|', $regex) . ')/u', $url, $match); + if (!empty($regex) && preg_match('/^(?:' . implode('|', $regex) . ')/u', $url, $match)) { + $var = []; + foreach ($match as $key => $val) { + if (is_string($key) && '' !== $val) { + list($name, $pos) = explode('_THINK_', $key); + + $var[$name] = $val; + } + } + + if (!isset($pos)) { + foreach ($regex as $key => $item) { + if (0 === strpos(str_replace(['\/', '\-', '\\' . $depr], ['/', '-', $depr], $item), $match[0])) { + $pos = $key; + break; + } + } + } + + return $items[$pos]->checkRule($request, $url, $var); + } + + return false; } catch (\Exception $e) { throw new Exception('route pattern error'); } - - if ($result) { - $var = []; - foreach ($match as $key => $val) { - if (is_string($key) && '' !== $val) { - list($name, $pos) = explode('_THINK_', $key); - - $var[$name] = $val; - } - } - - if (!isset($pos)) { - foreach ($regex as $key => $item) { - if (0 === strpos(str_replace(['\/', '\-', '\\' . $depr], ['/', '-', $depr], $item), $match[0])) { - $pos = $key; - break; - } - } - } - - $rule = $items[$pos]->getRule(); - $array = $this->router->getRule($rule); - - foreach ($array as $item) { - if (in_array($item->getMethod(), ['*', strtolower($request->method())])) { - $result = $item->checkRule($request, $url, $var); - - if (false !== $result) { - return $result; - } - } - } - } - - return false; } /** @@ -431,11 +414,6 @@ class RuleGroup extends Rule $method = strtolower($method); - if ('/' === $rule || '' === $rule) { - // 首页自动完整匹配 - $rule .= '$'; - } - // 创建路由规则实例 $ruleItem = new RuleItem($this->router, $this, $name, $rule, $route, $method, $option, $pattern); diff --git a/thinkphp/library/think/route/RuleItem.php b/thinkphp/library/think/route/RuleItem.php index 91c97ad43..31b584087 100644 --- a/thinkphp/library/think/route/RuleItem.php +++ b/thinkphp/library/think/route/RuleItem.php @@ -128,7 +128,6 @@ class RuleItem extends Rule $value = [$this->rule, $vars, $this->parent->getDomain(), $suffix, $this->method]; Container::get('rule_name')->set($name, $value, $first); - Container::get('rule_name')->setRule($this->rule, $this); } } @@ -243,7 +242,7 @@ class RuleItem extends Rule } if (false === strpos($rule, '<')) { - if (0 === strcasecmp($rule, $url) || (!$completeMatch && 0 === strncasecmp($rule . $depr, $url . $depr, strlen($rule . $depr)))) { + if (0 === strcasecmp($rule, $url) || (!$completeMatch && 0 === strncasecmp($rule, $url, strlen($rule)))) { return $var; } return false; diff --git a/thinkphp/library/think/route/RuleName.php b/thinkphp/library/think/route/RuleName.php index c73c36394..11fc19429 100644 --- a/thinkphp/library/think/route/RuleName.php +++ b/thinkphp/library/think/route/RuleName.php @@ -14,7 +14,6 @@ namespace think\route; class RuleName { protected $item = []; - protected $rule = []; /** * 注册路由标识 @@ -33,62 +32,6 @@ class RuleName } } - /** - * 注册路由规则 - * @access public - * @param string $rule 路由规则 - * @param RuleItem $route 路由 - * @return void - */ - public function setRule($rule, $route) - { - $this->rule[$route->getDomain()][$rule][$route->getRoute()] = $route; - } - - /** - * 根据路由规则获取路由对象(列表) - * @access public - * @param string $name 路由标识 - * @param string $domain 域名 - * @return array - */ - public function getRule($rule, $domain = null) - { - return isset($this->rule[$domain][$rule]) ? $this->rule[$domain][$rule] : []; - } - - /** - * 获取全部路由列表 - * @access public - * @param string $domain 域名 - * @return array - */ - public function getRuleList($domain = null) - { - $list = []; - - foreach ($this->rule as $ruleDomain => $rules) { - foreach ($rules as $rule => $items) { - foreach ($items as $item) { - $val = []; - - foreach (['method', 'rule', 'name', 'route', 'pattern', 'option'] as $param) { - $call = 'get' . $param; - $val[$param] = $item->$call(); - } - - $list[$ruleDomain][] = $val; - } - } - } - - if ($domain) { - return isset($list[$domain]) ? $list[$domain] : []; - } - - return $list; - } - /** * 导入路由标识 * @access public diff --git a/thinkphp/library/think/route/dispatch/Module.php b/thinkphp/library/think/route/dispatch/Module.php index 4224b362f..9f1db055b 100644 --- a/thinkphp/library/think/route/dispatch/Module.php +++ b/thinkphp/library/think/route/dispatch/Module.php @@ -15,7 +15,6 @@ use ReflectionMethod; use think\exception\ClassNotFoundException; use think\exception\HttpException; use think\Loader; -use think\Request; use think\route\Dispatch; class Module extends Dispatch @@ -85,8 +84,8 @@ class Module extends Dispatch // 监听module_init $this->app['hook']->listen('module_init'); + // 实例化控制器 try { - // 实例化控制器 $instance = $this->app->controller($this->controller, $this->rule->getConfig('url_controller_layer'), $this->rule->getConfig('controller_suffix'), @@ -95,42 +94,36 @@ class Module extends Dispatch throw new HttpException(404, 'controller not exists:' . $e->getClass()); } - $this->app['middleware']->controller(function (Request $request, $next) use ($instance) { - // 获取当前操作名 - $action = $this->actionName . $this->rule->getConfig('action_suffix'); + // 获取当前操作名 + $action = $this->actionName . $this->rule->getConfig('action_suffix'); - if (is_callable([$instance, $action])) { - // 执行操作方法 - $call = [$instance, $action]; + if (is_callable([$instance, $action])) { + // 执行操作方法 + $call = [$instance, $action]; - // 严格获取当前操作方法名 - $reflect = new ReflectionMethod($instance, $action); - $methodName = $reflect->getName(); - $suffix = $this->rule->getConfig('action_suffix'); - $actionName = $suffix ? substr($methodName, 0, -strlen($suffix)) : $methodName; - $this->request->setAction($actionName); + // 严格获取当前操作方法名 + $reflect = new ReflectionMethod($instance, $action); + $methodName = $reflect->getName(); + $suffix = $this->rule->getConfig('action_suffix'); + $actionName = $suffix ? substr($methodName, 0, -strlen($suffix)) : $methodName; + $this->request->setAction($actionName); - // 自动获取请求变量 - $vars = $this->rule->getConfig('url_param_type') - ? $this->request->route() - : $this->request->param(); - } elseif (is_callable([$instance, '_empty'])) { - // 空操作 - $call = [$instance, '_empty']; - $vars = [$this->actionName]; - $reflect = new ReflectionMethod($instance, '_empty'); - } else { - // 操作不存在 - throw new HttpException(404, 'method not exists:' . get_class($instance) . '->' . $action . '()'); - } + // 自动获取请求变量 + $vars = $this->rule->getConfig('url_param_type') + ? $this->request->route() + : $this->request->param(); + } elseif (is_callable([$instance, '_empty'])) { + // 空操作 + $call = [$instance, '_empty']; + $vars = [$this->actionName]; + $reflect = new ReflectionMethod($instance, '_empty'); + } else { + // 操作不存在 + throw new HttpException(404, 'method not exists:' . get_class($instance) . '->' . $action . '()'); + } - $this->app['hook']->listen('action_begin', $call); + $this->app['hook']->listen('action_begin', $call); - $data = $this->app->invokeReflectMethod($instance, $reflect, $vars); - - return $this->autoResponse($data); - }); - - return $this->app['middleware']->dispatch($this->request, 'controller'); + return $this->app->invokeReflectMethod($instance, $reflect, $vars); } } diff --git a/thinkphp/tpl/default_index.tpl b/thinkphp/tpl/default_index.tpl index e5c1363ac..740b19283 100644 --- a/thinkphp/tpl/default_index.tpl +++ b/thinkphp/tpl/default_index.tpl @@ -5,6 +5,6 @@ class Index{$suffix} { public function index() { - return '
ThinkPHP V5.1
12载初心不改(2006-2018) - 你值得信赖的PHP框架
ThinkPHP V5.1
12载初心不改(2006-2018) - 你值得信赖的PHP框架