Skip to content

Commit ecf1a90

Browse files
committed
Merge branch '5.1' of github.com:top-think/framework into 5.1
2 parents 450450a + 1ce73c7 commit ecf1a90

File tree

12 files changed

+142
-29
lines changed

12 files changed

+142
-29
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ ThinkPHP5.1对底层架构做了进一步的改进,减少依赖,其主要特
3535
+ 内置控制器扩展类
3636
+ 模型自动验证
3737

38-
> ThinkPHP5.1的运行环境要求PHP5.6+。
38+
> ThinkPHP5.1的运行环境要求PHP5.6+ 兼容PHP8.0
3939
4040

4141
## 安装
@@ -70,6 +70,12 @@ composer update topthink/framework
7070
+ [完全开发手册](https://www.kancloud.cn/manual/thinkphp5_1/content)
7171
+ [升级指导](https://www.kancloud.cn/manual/thinkphp5_1/354155)
7272

73+
74+
## 官方服务
75+
76+
+ [应用服务市场](https://market.topthink.com/)
77+
+ [ThinkAPI——统一API服务](https://docs.topthink.com/think-api)
78+
7379
## 命名规范
7480

7581
`ThinkPHP5.1`遵循PSR-2命名规范和PSR-4自动加载规范。

library/think/App.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121
class App extends Container
2222
{
23-
const VERSION = '5.1.40 LTS';
23+
const VERSION = '5.1.41 LTS';
2424

2525
/**
2626
* 当前模块路径

library/think/Container.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,25 @@ protected function bindParams($reflect, $vars = [])
461461
$type = key($vars) === 0 ? 1 : 0;
462462
$params = $reflect->getParameters();
463463

464+
if (PHP_VERSION > 8.0) {
465+
$args = $this->parseParamsForPHP8($params, $vars, $type);
466+
} else {
467+
$args = $this->parseParams($params, $vars, $type);
468+
}
469+
470+
return $args;
471+
}
472+
473+
/**
474+
* 解析参数
475+
* @access protected
476+
* @param array $params 参数列表
477+
* @param array $vars 参数数据
478+
* @param int $type 参数类别
479+
* @return array
480+
*/
481+
protected function parseParams($params, $vars, $type)
482+
{
464483
foreach ($params as $param) {
465484
$name = $param->getName();
466485
$lowerName = Loader::parseName($name);
@@ -480,7 +499,38 @@ protected function bindParams($reflect, $vars = [])
480499
throw new InvalidArgumentException('method param miss:' . $name);
481500
}
482501
}
502+
return $args;
503+
}
504+
505+
/**
506+
* 解析参数
507+
* @access protected
508+
* @param array $params 参数列表
509+
* @param array $vars 参数数据
510+
* @param int $type 参数类别
511+
* @return array
512+
*/
513+
protected function parseParamsForPHP8($params, $vars, $type)
514+
{
515+
foreach ($params as $param) {
516+
$name = $param->getName();
517+
$lowerName = Loader::parseName($name);
518+
$reflectionType = $param->getType();
483519

520+
if ($reflectionType && $reflectionType->isBuiltin() === false) {
521+
$args[] = $this->getObjectParam($reflectionType->getName(), $vars);
522+
} elseif (1 == $type && !empty($vars)) {
523+
$args[] = array_shift($vars);
524+
} elseif (0 == $type && array_key_exists($name, $vars)) {
525+
$args[] = $vars[$name];
526+
} elseif (0 == $type && array_key_exists($lowerName, $vars)) {
527+
$args[] = $vars[$lowerName];
528+
} elseif ($param->isDefaultValueAvailable()) {
529+
$args[] = $param->getDefaultValue();
530+
} else {
531+
throw new InvalidArgumentException('method param miss:' . $name);
532+
}
533+
}
484534
return $args;
485535
}
486536

library/think/Model.php

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,43 @@
1818
* Class Model
1919
* @package think
2020
* @mixin Query
21+
* @method $this scope(string|array $scope) static 查询范围
2122
* @method $this where(mixed $field, string $op = null, mixed $condition = null) static 查询条件
22-
* @method $this whereRaw(string $where, array $bind = []) static 表达式查询
23-
* @method $this whereExp(string $field, string $condition, array $bind = []) static 字段表达式查询
23+
* @method $this whereRaw(string $where, array $bind = [], string $logic = 'AND') static 表达式查询
24+
* @method $this whereExp(string $field, string $condition, array $bind = [], string $logic = 'AND') static 字段表达式查询
2425
* @method $this when(mixed $condition, mixed $query, mixed $otherwise = null) static 条件查询
25-
* @method $this join(mixed $join, mixed $condition = null, string $type = 'INNER') static JOIN查询
26+
* @method $this join(mixed $join, mixed $condition = null, string $type = 'INNER', array $bind = []) static JOIN查询
2627
* @method $this view(mixed $join, mixed $field = null, mixed $on = null, string $type = 'INNER') static 视图查询
27-
* @method $this with(mixed $with) static 关联预载入
28-
* @method $this count(string $field) static Count统计查询
29-
* @method $this min(string $field) static Min统计查询
30-
* @method $this max(string $field) static Max统计查询
28+
* @method $this with(mixed $with, callable $callback = null) static 关联预载入
29+
* @method $this count(string $field = '*') static Count统计查询
30+
* @method $this min(string $field, bool $force = true) static Min统计查询
31+
* @method $this max(string $field, bool $force = true) static Max统计查询
3132
* @method $this sum(string $field) static SUM统计查询
3233
* @method $this avg(string $field) static Avg统计查询
33-
* @method $this field(mixed $field, boolean $except = false) static 指定查询字段
34-
* @method $this fieldRaw(string $field, array $bind = []) static 指定查询字段
34+
* @method $this field(mixed $field, boolean $except = false, string $tableName = '', string $prefix = '', string $alias = '') static 指定查询字段
35+
* @method $this fieldRaw(string $field) static 指定查询字段
3536
* @method $this union(mixed $union, boolean $all = false) static UNION查询
3637
* @method $this limit(mixed $offset, integer $length = null) static 查询LIMIT
3738
* @method $this order(mixed $field, string $order = null) static 查询ORDER
3839
* @method $this orderRaw(string $field, array $bind = []) static 查询ORDER
39-
* @method $this cache(mixed $key = null , integer $expire = null) static 设置查询缓存
40-
* @method mixed value(string $field) static 获取某个字段的值
40+
* @method $this cache(mixed $key = null, integer|\DateTime $expire = null, string $tag = null) static 设置查询缓存
41+
* @method mixed value(string $field, mixed $default = null) static 获取某个字段的值
4142
* @method array column(string $field, string $key = '') static 获取某个列的值
4243
* @method $this find(mixed $data = null) static 查询单个记录
43-
* @method $this[] select(mixed $data = null) static 查询多个记录
44-
* @method $this get(mixed $data = null,mixed $with =[],bool $cache= false) static 查询单个记录 支持关联预载入
45-
* @method $this getOrFail(mixed $data = null,mixed $with =[],bool $cache= false) static 查询单个记录 不存在则抛出异常
46-
* @method $this findOrEmpty(mixed $data = null,mixed $with =[],bool $cache= false) static 查询单个记录 不存在则返回空模型
47-
* @method $this[] all(mixed $data = null,mixed $with =[],bool $cache= false) static 查询多个记录 支持关联预载入
48-
* @method \think\Model withAttr(array $name,\Closure $closure) 动态定义获取器
44+
* @method $this findOrFail(mixed $data = null) 查询单个记录
45+
* @method Collection|$this[] select(mixed $data = null) static 查询多个记录
46+
* @method $this get(mixed $data = null, mixed $with = [], bool $cache = false, bool $failException = false) static 查询单个记录 支持关联预载入
47+
* @method $this getOrFail(mixed $data = null, mixed $with = [], bool $cache = false) static 查询单个记录 不存在则抛出异常
48+
* @method $this findOrEmpty(mixed $data = null) static 查询单个记录 不存在则返回空模型
49+
* @method Collection|$this[] all(mixed $data = null, mixed $with = [], bool $cache = false) static 查询多个记录 支持关联预载入
50+
* @method $this withAttr(array $name, \Closure $closure = null) static 动态定义获取器
51+
* @method $this withJoin(string|array $with, string $joinType = '') static
52+
* @method $this withCount(string|array $relation, bool $subQuery = true) static 关联统计
53+
* @method $this withSum(string|array $relation, string $field, bool $subQuery = true) static 关联SUM统计
54+
* @method $this withMax(string|array $relation, string $field, bool $subQuery = true) static 关联MAX统计
55+
* @method $this withMin(string|array $relation, string $field, bool $subQuery = true) static 关联Min统计
56+
* @method $this withAvg(string|array $relation, string $field, bool $subQuery = true) static 关联Avg统计
57+
* @method Paginator|$this paginate(int|array $listRows = null, int|bool $simple = false, array $config = []) static 分页
4958
*/
5059
abstract class Model implements \JsonSerializable, \ArrayAccess
5160
{

library/think/Request.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1800,7 +1800,7 @@ public function setHost($host)
18001800
public function host($strict = false)
18011801
{
18021802
if (!$this->host) {
1803-
$this->host = $this->server('HTTP_X_REAL_HOST') ?: $this->server('HTTP_HOST');
1803+
$this->host = $this->server('HTTP_X_REAL_HOST') ?: $this->server('HTTP_X_FORWARDED_HOST') ?: $this->server('HTTP_HOST');
18041804
}
18051805

18061806
return true === $strict && strpos($this->host, ':') ? strstr($this->host, ':', true) : $this->host;

library/think/Route.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313

1414
use think\exception\RouteNotFoundException;
1515
use think\route\AliasRule;
16+
use think\route\Dispatch;
1617
use think\route\dispatch\Url as UrlDispatch;
1718
use think\route\Domain;
1819
use think\route\Resource;
20+
use think\route\Rule;
1921
use think\route\RuleGroup;
2022
use think\route\RuleItem;
2123

library/think/db/Query.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3464,7 +3464,7 @@ public function findOrFail($data = null)
34643464
}
34653465

34663466
/**
3467-
* 查找单条记录 如果不存在则抛出异常
3467+
* 查找单条记录 不存在则返回空模型
34683468
* @access public
34693469
* @param array|string|Query|\Closure $data
34703470
* @return array|\PDOStatement|string|Model

library/think/model/Collection.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,22 @@ public function load($relation)
3232
return $this;
3333
}
3434

35+
/**
36+
* 绑定(一对一)关联属性到当前模型
37+
* @access protected
38+
* @param string $relation 关联名称
39+
* @param array $attrs 绑定属性
40+
* @return $this
41+
*/
42+
public function bindAttr($relation, array $attrs = [])
43+
{
44+
$this->each(function (Model $model) use ($relation, $attrs) {
45+
$model->bindAttr($relation, $attrs);
46+
});
47+
48+
return $this;
49+
}
50+
3551
/**
3652
* 设置需要隐藏的输出属性
3753
* @access public

library/think/model/concern/RelationShip.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use think\Collection;
1515
use think\db\Query;
16+
use think\Exception;
1617
use think\Loader;
1718
use think\Model;
1819
use think\model\Relation;
@@ -115,6 +116,32 @@ public function setRelation($name, $value, $data = [])
115116
return $this;
116117
}
117118

119+
/**
120+
* 绑定(一对一)关联属性到当前模型
121+
* @access protected
122+
* @param string $relation 关联名称
123+
* @param array $attrs 绑定属性
124+
* @return $this
125+
* @throws Exception
126+
*/
127+
public function bindAttr($relation, array $attrs = [])
128+
{
129+
$relation = $this->getRelation($relation);
130+
131+
foreach ($attrs as $key => $attr) {
132+
$key = is_numeric($key) ? $attr : $key;
133+
$value = $this->getOrigin($key);
134+
135+
if (!is_null($value)) {
136+
throw new Exception('bind attr has exists:' . $key);
137+
}
138+
139+
$this->setAttr($key, $relation ? $relation->getAttr($attr) : null);
140+
}
141+
142+
return $this;
143+
}
144+
118145
/**
119146
* 关联数据写入
120147
* @access public

library/think/model/relation/BelongsToMany.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use think\Model;
2020
use think\model\Pivot;
2121
use think\model\Relation;
22+
use think\Paginator;
2223

2324
class BelongsToMany extends Relation
2425
{
@@ -582,7 +583,7 @@ public function attach($data, $pivot = [])
582583
* 判断是否存在关联数据
583584
* @access public
584585
* @param mixed $data 数据 可以使用关联模型对象 或者 关联对象的主键
585-
* @return Pivot
586+
* @return Pivot|false
586587
* @throws Exception
587588
*/
588589
public function attached($data)

library/think/model/relation/OneToOne.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -282,20 +282,22 @@ protected function match($model, $relation, &$result)
282282
/**
283283
* 绑定关联属性到父模型
284284
* @access protected
285-
* @param Model $model 关联模型对象
286-
* @param Model $result 父模型对象
285+
* @param Model $result 关联模型对象
286+
* @param Model $model 父模型对象
287287
* @return void
288288
* @throws Exception
289289
*/
290290
protected function bindAttr($model, &$result)
291291
{
292292
foreach ($this->bindAttr as $key => $attr) {
293-
$key = is_numeric($key) ? $attr : $key;
294-
if (isset($result->$key)) {
293+
$key = is_numeric($key) ? $attr : $key;
294+
$value = $result->getOrigin($key);
295+
296+
if (!is_null($value)) {
295297
throw new Exception('bind attr has exists:' . $key);
296-
} else {
297-
$result->setAttr($key, $model ? $model->$attr : null);
298298
}
299+
300+
$result->setAttr($key, $model ? $model->getAttr($attr) : null);
299301
}
300302
}
301303

tpl/think_exception.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@
489489
var err_line = $('.line-' + LINE, ol[0])[0];
490490
err_line.className = err_line.className + ' line-error';
491491

492-
$.getScript('//cdn.bootcss.com/prettify/r298/prettify.min.js', function(){
492+
$.getScript('//cdn.bootcdn.net/ajax/libs/prettify/r298/prettify.min.js', function(){
493493
prettyPrint();
494494
495495
// 解决Firefox浏览器一个很诡异的问题

0 commit comments

Comments
 (0)