From 1e8e3798aabb4113f357583bade2efafa03ac7bc Mon Sep 17 00:00:00 2001 From: Anyon Date: Mon, 29 Oct 2018 10:13:41 +0800 Subject: [PATCH] =?UTF-8?q?[=E6=9B=B4=E6=96=B0]ComposerUpdate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- thinkphp/README.md | 4 +- thinkphp/library/think/App.php | 2 +- thinkphp/library/think/Controller.php | 59 ++++++------ thinkphp/library/think/Lang.php | 2 +- thinkphp/library/think/Log.php | 24 ++--- thinkphp/library/think/Url.php | 6 +- thinkphp/library/think/Validate.php | 38 +++++++- thinkphp/library/think/db/Connection.php | 6 +- thinkphp/library/think/db/Query.php | 14 +-- thinkphp/library/think/model/Relation.php | 2 +- .../library/think/model/concern/Attribute.php | 3 +- .../think/model/concern/RelationShip.php | 7 +- .../think/model/relation/BelongsTo.php | 49 ++++++++-- .../think/model/relation/BelongsToMany.php | 94 +++++++++++++++---- .../library/think/model/relation/HasMany.php | 32 ++++--- .../think/model/relation/HasManyThrough.php | 3 +- .../library/think/model/relation/HasOne.php | 39 +++++++- .../think/model/relation/MorphMany.php | 43 +++++---- .../library/think/model/relation/MorphTo.php | 3 +- .../library/think/model/relation/OneToOne.php | 14 --- .../library/think/route/dispatch/Module.php | 5 + vendor/autoload.php | 2 +- vendor/composer/autoload_real.php | 14 +-- vendor/composer/autoload_static.php | 8 +- vendor/composer/installed.json | 12 +-- 25 files changed, 328 insertions(+), 157 deletions(-) diff --git a/thinkphp/README.md b/thinkphp/README.md index 92731cb23..a30e8f0eb 100644 --- a/thinkphp/README.md +++ b/thinkphp/README.md @@ -1,6 +1,6 @@ ![](http://www.thinkphp.cn/Uploads/editor/2016-06-23/576b4732a6e04.png) -ThinkPHP 5.1 —— 12载初心,你值得信赖的PHP框架 +ThinkPHP 5.1(LTS) —— 12载初心,你值得信赖的PHP框架 =============== [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/top-think/framework/badges/quality-score.png?b=5.1)](https://scrutinizer-ci.com/g/top-think/framework/?branch=5.1) @@ -27,6 +27,7 @@ ThinkPHP5.1对底层架构做了进一步的改进,减少依赖,其主要特 + 中间件支持(`V5.1.6+`) + 支持`Swoole`/`Workerman`运行(`V5.1.18+`) +官方已经正式宣布`5.1.27`版本为LTS版本。 ### 废除的功能: @@ -36,6 +37,7 @@ ThinkPHP5.1对底层架构做了进一步的改进,减少依赖,其主要特 > ThinkPHP5.1的运行环境要求PHP5.6+。 + ## 安装 使用composer安装 diff --git a/thinkphp/library/think/App.php b/thinkphp/library/think/App.php index d888e3c97..254f53a99 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.27 LTS'; + const VERSION = '5.1.28 LTS'; /** * 当前模块路径 diff --git a/thinkphp/library/think/Controller.php b/thinkphp/library/think/Controller.php index e0966eb27..a57da9e35 100644 --- a/thinkphp/library/think/Controller.php +++ b/thinkphp/library/think/Controller.php @@ -67,35 +67,6 @@ class Controller // 控制器初始化 $this->initialize(); - // 控制器中间件 - if ($this->middleware) { - foreach ($this->middleware as $key => $val) { - if (!is_int($key)) { - $only = $except = null; - - if (isset($val['only'])) { - $only = array_map(function ($item) { - return strtolower($item); - }, $val['only']); - } elseif (isset($val['except'])) { - $except = array_map(function ($item) { - return strtolower($item); - }, $val['except']); - } - - if (isset($only) && !in_array($this->request->action(), $only)) { - continue; - } elseif (isset($except) && in_array($this->request->action(), $except)) { - continue; - } else { - $val = $key; - } - } - - $this->app['middleware']->controller($val); - } - } - // 前置操作方法 即将废弃 foreach ((array) $this->beforeActionList as $method => $options) { is_numeric($method) ? @@ -108,6 +79,36 @@ class Controller protected function initialize() {} + // 注册控制器中间件 + public function registerMiddleware() + { + foreach ($this->middleware as $key => $val) { + if (!is_int($key)) { + $only = $except = null; + + if (isset($val['only'])) { + $only = array_map(function ($item) { + return strtolower($item); + }, $val['only']); + } elseif (isset($val['except'])) { + $except = array_map(function ($item) { + return strtolower($item); + }, $val['except']); + } + + if (isset($only) && !in_array($this->request->action(), $only)) { + continue; + } elseif (isset($except) && in_array($this->request->action(), $except)) { + continue; + } else { + $val = $key; + } + } + + $this->app['middleware']->controller($val); + } + } + /** * 前置操作 * @access protected diff --git a/thinkphp/library/think/Lang.php b/thinkphp/library/think/Lang.php index 5d8d9d99e..be7979f89 100644 --- a/thinkphp/library/think/Lang.php +++ b/thinkphp/library/think/Lang.php @@ -161,7 +161,7 @@ class Lang $range = $range ?: $this->range; // 空参数返回所有定义 - if (empty($name)) { + if (is_null($name)) { return $this->lang[$range]; } diff --git a/thinkphp/library/think/Log.php b/thinkphp/library/think/Log.php index 70c7bb9a5..1a3749626 100644 --- a/thinkphp/library/think/Log.php +++ b/thinkphp/library/think/Log.php @@ -205,26 +205,18 @@ class Log implements LoggerInterface return false; } - if (empty($this->config['level'])) { - // 获取全部日志 - $log = $this->log; - if (!$this->app->isDebug() && isset($log['debug'])) { - unset($log['debug']); + $log = []; + + foreach ($this->log as $level => $info) { + if (!$this->app->isDebug() && 'debug' == $level) { + continue; } - foreach ($log as $level => $info) { + if (empty($this->config['level']) || in_array($level, $this->config['level'])) { + $log[$level] = $info; + $this->app['hook']->listen('log_level', [$level, $info]); } - } else { - // 记录允许级别 - $log = []; - foreach ($this->config['level'] as $level) { - if (isset($this->log[$level])) { - $log[$level] = $this->log[$level]; - // 监听log_level - $this->app['hook']->listen('log_level', [$level, $log[$level]]); - } - } } $result = $this->driver->save($log, true); diff --git a/thinkphp/library/think/Url.php b/thinkphp/library/think/Url.php index 6707bf289..9e41b8819 100644 --- a/thinkphp/library/think/Url.php +++ b/thinkphp/library/think/Url.php @@ -348,12 +348,16 @@ class Url public function getRuleUrl($rule, &$vars = [], $allowDomain = '') { foreach ($rule as $item) { - list($url, $pattern, $domain, $suffix) = $item; + list($url, $pattern, $domain, $suffix, $method) = $item; if (is_string($allowDomain) && $domain != $allowDomain) { continue; } + if ($this->app['request']->port() != 80) { + $domain .= ':' . $this->app['request']->port(); + } + if (empty($pattern)) { return [rtrim($url, '?/-'), $domain, $suffix]; } diff --git a/thinkphp/library/think/Validate.php b/thinkphp/library/think/Validate.php index de46956cf..ebc072932 100644 --- a/thinkphp/library/think/Validate.php +++ b/thinkphp/library/think/Validate.php @@ -87,6 +87,8 @@ class Validate 'min' => 'min size of :attribute must be :rule', 'after' => ':attribute cannot be less than :rule', 'before' => ':attribute cannot exceed :rule', + 'afterWith' => ':attribute cannot be less than :rule', + 'beforeWith' => ':attribute cannot exceed :rule', 'expire' => ':attribute not within :rule', 'allowIp' => 'access IP is not allowed', 'denyIp' => 'access IP denied', @@ -422,7 +424,7 @@ class Validate // 字段验证 if ($rule instanceof \Closure) { - $result = call_user_func_array($rule, [$value, $data]); + $result = call_user_func_array($rule, [$value, $data, $title, $this]); } elseif ($rule instanceof ValidateRule) { // 验证因子 $result = $this->checkItem($key, $value, $rule->getRule(), $data, $rule->getTitle() ?: $title, $rule->getMsg()); @@ -1241,9 +1243,10 @@ class Validate * @access public * @param mixed $value 字段值 * @param mixed $rule 验证规则 + * @param array $data 数据 * @return bool */ - public function after($value, $rule) + public function after($value, $rule, $data) { return strtotime($value) >= strtotime($rule); } @@ -1253,13 +1256,42 @@ class Validate * @access public * @param mixed $value 字段值 * @param mixed $rule 验证规则 + * @param array $data 数据 * @return bool */ - public function before($value, $rule) + public function before($value, $rule, $data) { return strtotime($value) <= strtotime($rule); } + /** + * 验证日期字段 + * @access protected + * @param mixed $value 字段值 + * @param mixed $rule 验证规则 + * @param array $data 数据 + * @return bool + */ + protected function afterWith($value, $rule, $data) + { + $rule = $this->getDataValue($data, $rule); + return !is_null($rule) && strtotime($value) >= strtotime($rule); + } + + /** + * 验证日期字段 + * @access protected + * @param mixed $value 字段值 + * @param mixed $rule 验证规则 + * @param array $data 数据 + * @return bool + */ + protected function beforeWith($value, $rule, $data) + { + $rule = $this->getDataValue($data, $rule); + return !is_null($rule) && strtotime($value) <= strtotime($rule); + } + /** * 验证有效期 * @access public diff --git a/thinkphp/library/think/db/Connection.php b/thinkphp/library/think/db/Connection.php index d2bad3c0a..6c337bc2b 100644 --- a/thinkphp/library/think/db/Connection.php +++ b/thinkphp/library/think/db/Connection.php @@ -1314,7 +1314,11 @@ abstract class Connection */ public function aggregate(Query $query, $aggregate, $field) { - $field = $aggregate . '(' . $this->builder->parseKey($query, $field, true) . ') AS tp_' . strtolower($aggregate); + if (0 === stripos($field, 'DISTINCT ')) { + list($distinct, $field) = explode(' ', $field); + } + + $field = $aggregate . '(' . (!empty($distinct) ? 'DISTINCT ' : '') . $this->builder->parseKey($query, $field, true) . ') AS tp_' . strtolower($aggregate); return $this->value($query, $field, 0); } diff --git a/thinkphp/library/think/db/Query.php b/thinkphp/library/think/db/Query.php index cea5a4c8d..d48b95c6f 100644 --- a/thinkphp/library/think/db/Query.php +++ b/thinkphp/library/think/db/Query.php @@ -2650,7 +2650,8 @@ class Query } foreach ($relations as $key => $relation) { - $closure = null; + $closure = $aggregateField = null; + if ($relation instanceof \Closure) { $closure = $relation; $relation = $key; @@ -2659,14 +2660,15 @@ class Query $relation = $key; } - if (!isset($aggregateField)) { + $relation = Loader::parseName($relation, 1, false); + + $count = $this->model->$relation()->getRelationCountQuery($closure, $aggregate, $field, $aggregateField); + + if (empty($aggregateField)) { $aggregateField = Loader::parseName($relation) . '_' . $aggregate; } - $relation = Loader::parseName($relation, 1, false); - $count = '(' . $this->model->$relation()->getRelationCountQuery($closure, $aggregate, $field) . ')'; - - $this->field([$count => $aggregateField]); + $this->field(['(' . $count . ')' => $aggregateField]); } } diff --git a/thinkphp/library/think/model/Relation.php b/thinkphp/library/think/model/Relation.php index b1434856a..c2b9adc7b 100644 --- a/thinkphp/library/think/model/Relation.php +++ b/thinkphp/library/think/model/Relation.php @@ -158,7 +158,7 @@ abstract class Relation $result = call_user_func_array([$this->query->getModel(), $method], $args); - return $result === $this->query ? $this : $result; + return $result === $this->query && !in_array(strtolower($method), ['fetchsql', 'fetchpdo']) ? $this : $result; } else { throw new Exception('method not exists:' . __CLASS__ . '->' . $method); } diff --git a/thinkphp/library/think/model/concern/Attribute.php b/thinkphp/library/think/model/concern/Attribute.php index 76342b9f3..02f62b14f 100644 --- a/thinkphp/library/think/model/concern/Attribute.php +++ b/thinkphp/library/think/model/concern/Attribute.php @@ -327,6 +327,8 @@ trait Attribute if (method_exists($this, $method)) { $value = $this->$method($value, array_merge($this->data, $data)); + + $this->set[$name] = true; } elseif (isset($this->type[$name])) { // 类型转换 $value = $this->writeTransform($value, $this->type[$name]); @@ -335,7 +337,6 @@ trait Attribute // 设置数据对象属性 $this->data[$name] = $value; - $this->set[$name] = true; } /** diff --git a/thinkphp/library/think/model/concern/RelationShip.php b/thinkphp/library/think/model/concern/RelationShip.php index f4c091b60..38ad5d20f 100644 --- a/thinkphp/library/think/model/concern/RelationShip.php +++ b/thinkphp/library/think/model/concern/RelationShip.php @@ -309,7 +309,7 @@ trait RelationShip public function relationCount(&$result, $relations, $aggregate = 'sum', $field = '*') { foreach ($relations as $key => $relation) { - $closure = null; + $closure = $name = null; if ($relation instanceof \Closure) { $closure = $relation; @@ -320,9 +320,10 @@ trait RelationShip } $relation = Loader::parseName($relation, 1, false); - $count = $this->$relation()->relationCount($result, $closure, $aggregate, $field); - if (!isset($name)) { + $count = $this->$relation()->relationCount($result, $closure, $aggregate, $field, $name); + + if (empty($name)) { $name = Loader::parseName($relation) . '_' . $aggregate; } diff --git a/thinkphp/library/think/model/relation/BelongsTo.php b/thinkphp/library/think/model/relation/BelongsTo.php index a1d2b23c8..298297898 100644 --- a/thinkphp/library/think/model/relation/BelongsTo.php +++ b/thinkphp/library/think/model/relation/BelongsTo.php @@ -74,12 +74,17 @@ class BelongsTo extends OneToOne * @param \Closure $closure 闭包 * @param string $aggregate 聚合查询方法 * @param string $field 字段 + * @param string $aggregateAlias 聚合字段别名 * @return string */ - public function getRelationCountQuery($closure, $aggregate = 'count', $field = '*') + public function getRelationCountQuery($closure, $aggregate = 'count', $field = '*', &$aggregateAlias = '') { if ($closure) { - $closure($this->query); + $return = $closure($this->query); + + if ($return && is_string($return)) { + $aggregateAlias = $return; + } } return $this->query @@ -88,6 +93,37 @@ class BelongsTo extends OneToOne ->$aggregate($field); } + /** + * 关联统计 + * @access public + * @param Model $result 数据对象 + * @param \Closure $closure 闭包 + * @param string $aggregate 聚合查询方法 + * @param string $field 字段 + * @param string $name 统计字段别名 + * @return integer + */ + public function relationCount($result, $closure, $aggregate = 'count', $field = '*', &$name = '') + { + $foreignKey = $this->foreignKey; + + if (!isset($result->$foreignKey)) { + return 0; + } + + if ($closure) { + $return = $closure($this->query); + + if ($resturn && is_string($return)) { + $name = $return; + } + } + + return $this->query + ->where($this->localKey, '=', $result->$foreignKey) + ->$aggregate($field); + } + /** * 根据关联条件查询当前模型 * @access public @@ -228,10 +264,7 @@ class BelongsTo extends OneToOne */ public function associate($model) { - $foreignKey = $this->foreignKey; - $pk = $model->getPk(); - - $this->parent->setAttr($foreignKey, $model->$pk); + $this->parent->setAttr($this->foreignKey, $model->getKey()); $this->parent->save(); return $this->parent->setRelation($this->relation, $model); @@ -244,9 +277,7 @@ class BelongsTo extends OneToOne */ public function dissociate() { - $foreignKey = $this->foreignKey; - - $this->parent->setAttr($foreignKey, null); + $this->parent->setAttr($this->foreignKey, null); $this->parent->save(); return $this->parent->setRelation($this->relation, null); diff --git a/thinkphp/library/think/model/relation/BelongsToMany.php b/thinkphp/library/think/model/relation/BelongsToMany.php index d90430a8e..a66b0d01e 100644 --- a/thinkphp/library/think/model/relation/BelongsToMany.php +++ b/thinkphp/library/think/model/relation/BelongsToMany.php @@ -25,6 +25,8 @@ class BelongsToMany extends Relation protected $middle; // 中间表模型名称 protected $pivotName; + // 中间表数据名称 + protected $pivotDataName = 'pivot'; // 中间表模型对象 protected $pivot; @@ -67,6 +69,18 @@ class BelongsToMany extends Relation return $this; } + /** + * 设置中间表数据名称 + * @access public + * @param string $name + * @return $this + */ + public function pivotDataName($name) + { + $this->pivotDataName = $name; + return $this; + } + /** * 获取中间表更新条件 * @param $data @@ -121,7 +135,7 @@ class BelongsToMany extends Relation } } - $model->setRelation('pivot', $this->newPivot($pivot, true)); + $model->setRelation($this->pivotDataName, $this->newPivot($pivot, true)); } } @@ -350,21 +364,30 @@ class BelongsToMany extends Relation * @param \Closure $closure 闭包 * @param string $aggregate 聚合查询方法 * @param string $field 字段 + * @param string $name 统计字段别名 * @return integer */ - public function relationCount($result, $closure, $aggregate = 'count', $field = '*') + public function relationCount($result, $closure, $aggregate = 'count', $field = '*', &$name = '') { - $pk = $result->getPk(); - $count = 0; + $pk = $result->getPk(); - if (isset($result->$pk)) { - $pk = $result->$pk; - $count = $this->belongsToManyQuery($this->foreignKey, $this->localKey, [ - ['pivot.' . $this->localKey, '=', $pk], - ])->$aggregate($field); + if (!isset($result->$pk)) { + return 0; } - return $count; + $pk = $result->$pk; + + if ($closure) { + $return = $closure($this->query); + + if ($return && is_string($return)) { + $name = $return; + } + } + + return $this->belongsToManyQuery($this->foreignKey, $this->localKey, [ + ['pivot.' . $this->localKey, '=', $pk], + ])->$aggregate($field); } /** @@ -373,10 +396,19 @@ class BelongsToMany extends Relation * @param \Closure $closure 闭包 * @param string $aggregate 聚合查询方法 * @param string $field 字段 - * @return string + * @param string $aggregateAlias 聚合字段别名 + * @return array */ - public function getRelationCountQuery($closure, $aggregate = 'count', $field = '*') + public function getRelationCountQuery($closure, $aggregate = 'count', $field = '*', &$aggregateAlias = '') { + if ($closure) { + $return = $closure($this->query); + + if ($return && is_string($return)) { + $aggregateAlias = $return; + } + } + return $this->belongsToManyQuery($this->foreignKey, $this->localKey, [ [ 'pivot.' . $this->localKey, 'exp', $this->query->raw('=' . $this->parent->getTable() . '.' . $this->parent->getPk()), @@ -396,7 +428,11 @@ class BelongsToMany extends Relation protected function eagerlyManyToMany($where, $relation, $subRelation = '', $closure = null) { // 预载入关联查询 支持嵌套预载入 - $list = $this->belongsToManyQuery($this->foreignKey, $this->localKey, $where, $closure) + if ($closure) { + $closure($this->query); + } + + $list = $this->belongsToManyQuery($this->foreignKey, $this->localKey, $where) ->with($subRelation) ->select(); @@ -414,7 +450,7 @@ class BelongsToMany extends Relation } } - $set->setRelation('pivot', $this->newPivot($pivot, true)); + $set->setRelation($this->pivotDataName, $this->newPivot($pivot, true)); $data[$pivot[$this->localKey]][] = $set; } @@ -428,15 +464,10 @@ class BelongsToMany extends Relation * @param string $foreignKey 关联模型关联键 * @param string $localKey 当前模型关联键 * @param array $condition 关联查询条件 - * @param \Closure $closure 闭包 * @return Query */ - protected function belongsToManyQuery($foreignKey, $localKey, $condition = [], $closure = null) + protected function belongsToManyQuery($foreignKey, $localKey, $condition = []) { - if ($closure) { - $closure($this->query); - } - // 关联查询封装 $tableName = $this->query->getTable(); $table = $this->pivot->getTable(); @@ -543,6 +574,29 @@ class BelongsToMany extends Relation } } + /** + * 判断是否存在关联数据 + * @access public + * @param mixed $data 数据 可以使用关联模型对象 或者 关联对象的主键 + * @return Pivot + * @throws Exception + */ + public function attached($data) + { + if ($data instanceof Model) { + $id = $data->getKey(); + } else { + $id = $data; + } + + $pivot = $this->pivot + ->where($this->localKey, $this->parent->getKey()) + ->where($this->foreignKey, $id) + ->find(); + + return $pivot ?: false; + } + /** * 解除关联的一个中间表数据 * @access public diff --git a/thinkphp/library/think/model/relation/HasMany.php b/thinkphp/library/think/model/relation/HasMany.php index 2038a9606..7ea783d26 100644 --- a/thinkphp/library/think/model/relation/HasMany.php +++ b/thinkphp/library/think/model/relation/HasMany.php @@ -152,22 +152,27 @@ class HasMany extends Relation * @param \Closure $closure 闭包 * @param string $aggregate 聚合查询方法 * @param string $field 字段 + * @param string $name 统计字段别名 * @return integer */ - public function relationCount($result, $closure, $aggregate = 'count', $field = '*') + public function relationCount($result, $closure, $aggregate = 'count', $field = '*', &$name = '') { $localKey = $this->localKey; - $count = 0; - if (isset($result->$localKey)) { - if ($closure) { - $closure($this->query); - } - - $count = $this->query->where($this->foreignKey, '=', $result->$localKey)->$aggregate($field); + if (!isset($result->$localKey)) { + return 0; } - return $count; + if ($closure) { + $return = $closure($this->query); + if ($resturn && is_string($return)) { + $name = $return; + } + } + + return $this->query + ->where($this->foreignKey, '=', $result->$localKey) + ->$aggregate($field); } /** @@ -176,12 +181,17 @@ class HasMany extends Relation * @param \Closure $closure 闭包 * @param string $aggregate 聚合查询方法 * @param string $field 字段 + * @param string $aggregateAlias 聚合字段别名 * @return string */ - public function getRelationCountQuery($closure, $aggregate = 'count', $field = '*') + public function getRelationCountQuery($closure, $aggregate = 'count', $field = '*', &$aggregateAlias = '') { if ($closure) { - $closure($this->query); + $return = $closure($this->query); + + if ($return && is_string($return)) { + $aggregateAlias = $return; + } } return $this->query diff --git a/thinkphp/library/think/model/relation/HasManyThrough.php b/thinkphp/library/think/model/relation/HasManyThrough.php index 411f3fccf..7c7acaa07 100644 --- a/thinkphp/library/think/model/relation/HasManyThrough.php +++ b/thinkphp/library/think/model/relation/HasManyThrough.php @@ -120,9 +120,10 @@ class HasManyThrough extends Relation * @param \Closure $closure 闭包 * @param string $aggregate 聚合查询方法 * @param string $field 字段 + * @param string $name 统计字段别名 * @return integer */ - public function relationCount($result, $closure, $aggregate = 'count', $field = '*') + public function relationCount($result, $closure, $aggregate = 'count', $field = '*', &$name = '') {} /** diff --git a/thinkphp/library/think/model/relation/HasOne.php b/thinkphp/library/think/model/relation/HasOne.php index 1ccb6adc6..27e4eca9d 100644 --- a/thinkphp/library/think/model/relation/HasOne.php +++ b/thinkphp/library/think/model/relation/HasOne.php @@ -74,12 +74,17 @@ class HasOne extends OneToOne * @param \Closure $closure 闭包 * @param string $aggregate 聚合查询方法 * @param string $field 字段 + * @param string $aggregateAlias 聚合字段别名 * @return string */ - public function getRelationCountQuery($closure, $aggregate = 'count', $field = '*') + public function getRelationCountQuery($closure, $aggregate = 'count', $field = '*', &$aggregateAlias = '') { if ($closure) { - $closure($this->query); + $return = $closure($this->query); + + if ($return && is_string($return)) { + $aggregateAlias = $return; + } } return $this->query @@ -88,6 +93,36 @@ class HasOne extends OneToOne ->$aggregate($field); } + /** + * 关联统计 + * @access public + * @param Model $result 数据对象 + * @param \Closure $closure 闭包 + * @param string $aggregate 聚合查询方法 + * @param string $field 字段 + * @param string $name 统计字段别名 + * @return integer + */ + public function relationCount($result, $closure, $aggregate = 'count', $field = '*', &$name = '') + { + $localKey = $this->localKey; + + if (!isset($result->$localKey)) { + return 0; + } + + if ($closure) { + $return = $closure($this->query); + if ($resturn && is_string($return)) { + $name = $return; + } + } + + return $this->query + ->where($this->foreignKey, '=', $result->$localKey) + ->$aggregate($field); + } + /** * 根据关联条件查询当前模型 * @access public diff --git a/thinkphp/library/think/model/relation/MorphMany.php b/thinkphp/library/think/model/relation/MorphMany.php index 7c083dd79..e1ca0912f 100644 --- a/thinkphp/library/think/model/relation/MorphMany.php +++ b/thinkphp/library/think/model/relation/MorphMany.php @@ -186,27 +186,31 @@ class MorphMany extends Relation * @param \Closure $closure 闭包 * @param string $aggregate 聚合查询方法 * @param string $field 字段 + * @param string $name 统计字段别名 * @return integer */ - public function relationCount($result, $closure, $aggregate = 'count', $field = '*') + public function relationCount($result, $closure, $aggregate = 'count', $field = '*', &$name = '') { - $pk = $result->getPk(); - $count = 0; + $pk = $result->getPk(); - if (isset($result->$pk)) { - if ($closure) { - $closure($this->query); - } - - $count = $this->query - ->where([ - [$this->morphKey, '=', $result->$pk], - [$this->morphType, '=', $this->type], - ]) - ->$aggregate($field); + if (!isset($result->$pk)) { + return 0; } - return $count; + if ($closure) { + $return = $closure($this->query); + + if ($return && is_string($return)) { + $name = $return; + } + } + + return $this->query + ->where([ + [$this->morphKey, '=', $result->$pk], + [$this->morphType, '=', $this->type], + ]) + ->$aggregate($field); } /** @@ -215,12 +219,17 @@ class MorphMany extends Relation * @param \Closure $closure 闭包 * @param string $aggregate 聚合查询方法 * @param string $field 字段 + * @param string $aggregateAlias 聚合字段别名 * @return string */ - public function getRelationCountQuery($closure, $aggregate = 'count', $field = '*') + public function getRelationCountQuery($closure, $aggregate = 'count', $field = '*', &$aggregateAlias = '') { if ($closure) { - $closure($this->query); + $return = $closure($this->query); + + if ($return && is_string($return)) { + $aggregateAlias = $return; + } } return $this->query diff --git a/thinkphp/library/think/model/relation/MorphTo.php b/thinkphp/library/think/model/relation/MorphTo.php index bb7c4d0b3..6da6a0252 100644 --- a/thinkphp/library/think/model/relation/MorphTo.php +++ b/thinkphp/library/think/model/relation/MorphTo.php @@ -238,9 +238,10 @@ class MorphTo extends Relation * @param \Closure $closure 闭包 * @param string $aggregate 聚合查询方法 * @param string $field 字段 + * @param string $name 统计字段别名 * @return integer */ - public function relationCount($result, $closure, $aggregate = 'count', $field = '*') + public function relationCount($result, $closure, $aggregate = 'count', $field = '*', &$name = '') {} /** diff --git a/thinkphp/library/think/model/relation/OneToOne.php b/thinkphp/library/think/model/relation/OneToOne.php index dbffcb774..59fce77fb 100644 --- a/thinkphp/library/think/model/relation/OneToOne.php +++ b/thinkphp/library/think/model/relation/OneToOne.php @@ -236,20 +236,6 @@ abstract class OneToOne extends Relation return $this->bindAttr; } - /** - * 关联统计 - * @access public - * @param Model $result 数据对象 - * @param \Closure $closure 闭包 - * @param string $aggregate 聚合查询方法 - * @param string $field 字段 - * @return integer - */ - public function relationCount($result, $closure, $aggregate = 'count', $field = '*') - { - throw new Exception('relation not support: ' . $aggregate); - } - /** * 一对一 关联模型预查询拼装 * @access public diff --git a/thinkphp/library/think/route/dispatch/Module.php b/thinkphp/library/think/route/dispatch/Module.php index bfffec0ff..396ce11dd 100644 --- a/thinkphp/library/think/route/dispatch/Module.php +++ b/thinkphp/library/think/route/dispatch/Module.php @@ -12,6 +12,7 @@ namespace think\route\dispatch; use ReflectionMethod; +use think\Controller; use think\exception\ClassNotFoundException; use think\exception\HttpException; use think\Loader; @@ -91,6 +92,10 @@ class Module extends Dispatch $this->rule->getConfig('url_controller_layer'), $this->rule->getConfig('controller_suffix'), $this->rule->getConfig('empty_controller')); + + if ($instance instanceof Controller) { + $instance->registerMiddleware(); + } } catch (ClassNotFoundException $e) { throw new HttpException(404, 'controller not exists:' . $e->getClass()); } diff --git a/vendor/autoload.php b/vendor/autoload.php index 628adbbcd..68f41a6f2 100644 --- a/vendor/autoload.php +++ b/vendor/autoload.php @@ -4,4 +4,4 @@ require_once __DIR__ . '/composer/autoload_real.php'; -return ComposerAutoloaderInite4f62b825d40d3e077bb16351eabf2a4::getLoader(); +return ComposerAutoloaderInite9f61c7f17c1efc184e83ad53ffe6a75::getLoader(); diff --git a/vendor/composer/autoload_real.php b/vendor/composer/autoload_real.php index df2d928c5..6064efa3a 100644 --- a/vendor/composer/autoload_real.php +++ b/vendor/composer/autoload_real.php @@ -2,7 +2,7 @@ // autoload_real.php @generated by Composer -class ComposerAutoloaderInite4f62b825d40d3e077bb16351eabf2a4 +class ComposerAutoloaderInite9f61c7f17c1efc184e83ad53ffe6a75 { private static $loader; @@ -19,15 +19,15 @@ class ComposerAutoloaderInite4f62b825d40d3e077bb16351eabf2a4 return self::$loader; } - spl_autoload_register(array('ComposerAutoloaderInite4f62b825d40d3e077bb16351eabf2a4', 'loadClassLoader'), true, true); + spl_autoload_register(array('ComposerAutoloaderInite9f61c7f17c1efc184e83ad53ffe6a75', 'loadClassLoader'), true, true); self::$loader = $loader = new \Composer\Autoload\ClassLoader(); - spl_autoload_unregister(array('ComposerAutoloaderInite4f62b825d40d3e077bb16351eabf2a4', 'loadClassLoader')); + spl_autoload_unregister(array('ComposerAutoloaderInite9f61c7f17c1efc184e83ad53ffe6a75', 'loadClassLoader')); $useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded()); if ($useStaticLoader) { require_once __DIR__ . '/autoload_static.php'; - call_user_func(\Composer\Autoload\ComposerStaticInite4f62b825d40d3e077bb16351eabf2a4::getInitializer($loader)); + call_user_func(\Composer\Autoload\ComposerStaticInite9f61c7f17c1efc184e83ad53ffe6a75::getInitializer($loader)); } else { $map = require __DIR__ . '/autoload_namespaces.php'; foreach ($map as $namespace => $path) { @@ -48,19 +48,19 @@ class ComposerAutoloaderInite4f62b825d40d3e077bb16351eabf2a4 $loader->register(true); if ($useStaticLoader) { - $includeFiles = Composer\Autoload\ComposerStaticInite4f62b825d40d3e077bb16351eabf2a4::$files; + $includeFiles = Composer\Autoload\ComposerStaticInite9f61c7f17c1efc184e83ad53ffe6a75::$files; } else { $includeFiles = require __DIR__ . '/autoload_files.php'; } foreach ($includeFiles as $fileIdentifier => $file) { - composerRequiree4f62b825d40d3e077bb16351eabf2a4($fileIdentifier, $file); + composerRequiree9f61c7f17c1efc184e83ad53ffe6a75($fileIdentifier, $file); } return $loader; } } -function composerRequiree4f62b825d40d3e077bb16351eabf2a4($fileIdentifier, $file) +function composerRequiree9f61c7f17c1efc184e83ad53ffe6a75($fileIdentifier, $file) { if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) { require $file; diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index 0a10923c7..bfb323958 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -4,7 +4,7 @@ namespace Composer\Autoload; -class ComposerStaticInite4f62b825d40d3e077bb16351eabf2a4 +class ComposerStaticInite9f61c7f17c1efc184e83ad53ffe6a75 { public static $files = array ( '1cfd2761b63b0a29ed23657ea394cb2d' => __DIR__ . '/..' . '/topthink/think-captcha/src/helper.php', @@ -303,9 +303,9 @@ class ComposerStaticInite4f62b825d40d3e077bb16351eabf2a4 public static function getInitializer(ClassLoader $loader) { return \Closure::bind(function () use ($loader) { - $loader->prefixLengthsPsr4 = ComposerStaticInite4f62b825d40d3e077bb16351eabf2a4::$prefixLengthsPsr4; - $loader->prefixDirsPsr4 = ComposerStaticInite4f62b825d40d3e077bb16351eabf2a4::$prefixDirsPsr4; - $loader->classMap = ComposerStaticInite4f62b825d40d3e077bb16351eabf2a4::$classMap; + $loader->prefixLengthsPsr4 = ComposerStaticInite9f61c7f17c1efc184e83ad53ffe6a75::$prefixLengthsPsr4; + $loader->prefixDirsPsr4 = ComposerStaticInite9f61c7f17c1efc184e83ad53ffe6a75::$prefixDirsPsr4; + $loader->classMap = ComposerStaticInite9f61c7f17c1efc184e83ad53ffe6a75::$classMap; }, null, ClassLoader::class); } diff --git a/vendor/composer/installed.json b/vendor/composer/installed.json index f7049b38a..eb240193a 100644 --- a/vendor/composer/installed.json +++ b/vendor/composer/installed.json @@ -50,17 +50,17 @@ }, { "name": "topthink/framework", - "version": "v5.1.27", - "version_normalized": "5.1.27.0", + "version": "v5.1.28", + "version_normalized": "5.1.28.0", "source": { "type": "git", "url": "https://github.com/top-think/framework.git", - "reference": "f607b8fd51cc25e4da7e2f4ef0d5e64f390d36bd" + "reference": "8102ec96136a66f926bae89faea540b91687de37" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/top-think/framework/zipball/f607b8fd51cc25e4da7e2f4ef0d5e64f390d36bd", - "reference": "f607b8fd51cc25e4da7e2f4ef0d5e64f390d36bd", + "url": "https://api.github.com/repos/top-think/framework/zipball/8102ec96136a66f926bae89faea540b91687de37", + "reference": "8102ec96136a66f926bae89faea540b91687de37", "shasum": "", "mirrors": [ { @@ -82,7 +82,7 @@ "sebastian/phpcpd": "2.*", "squizlabs/php_codesniffer": "2.*" }, - "time": "2018-10-22T02:10:08+00:00", + "time": "2018-10-28T12:24:29+00:00", "type": "think-framework", "installation-source": "dist", "notification-url": "https://packagist.org/downloads/",