Skip to content

Commit

Permalink
Merge branch '5.1' of github.com:top-think/framework into 5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Oct 25, 2022
2 parents 450450a + 1ce73c7 commit ecf1a90
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 29 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ ThinkPHP5.1对底层架构做了进一步的改进,减少依赖,其主要特
+ 内置控制器扩展类
+ 模型自动验证

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

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


## 官方服务

+ [应用服务市场](https://market.topthink.com/)
+ [ThinkAPI——统一API服务](https://docs.topthink.com/think-api)

## 命名规范

`ThinkPHP5.1`遵循PSR-2命名规范和PSR-4自动加载规范。
Expand Down
2 changes: 1 addition & 1 deletion library/think/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
class App extends Container
{
const VERSION = '5.1.40 LTS';
const VERSION = '5.1.41 LTS';

/**
* 当前模块路径
Expand Down
50 changes: 50 additions & 0 deletions library/think/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,25 @@ protected function bindParams($reflect, $vars = [])
$type = key($vars) === 0 ? 1 : 0;
$params = $reflect->getParameters();

if (PHP_VERSION > 8.0) {
$args = $this->parseParamsForPHP8($params, $vars, $type);
} else {
$args = $this->parseParams($params, $vars, $type);
}

return $args;
}

/**
* 解析参数
* @access protected
* @param array $params 参数列表
* @param array $vars 参数数据
* @param int $type 参数类别
* @return array
*/
protected function parseParams($params, $vars, $type)
{
foreach ($params as $param) {
$name = $param->getName();
$lowerName = Loader::parseName($name);
Expand All @@ -480,7 +499,38 @@ protected function bindParams($reflect, $vars = [])
throw new InvalidArgumentException('method param miss:' . $name);
}
}
return $args;
}

/**
* 解析参数
* @access protected
* @param array $params 参数列表
* @param array $vars 参数数据
* @param int $type 参数类别
* @return array
*/
protected function parseParamsForPHP8($params, $vars, $type)
{
foreach ($params as $param) {
$name = $param->getName();
$lowerName = Loader::parseName($name);
$reflectionType = $param->getType();

if ($reflectionType && $reflectionType->isBuiltin() === false) {
$args[] = $this->getObjectParam($reflectionType->getName(), $vars);
} elseif (1 == $type && !empty($vars)) {
$args[] = array_shift($vars);
} elseif (0 == $type && array_key_exists($name, $vars)) {
$args[] = $vars[$name];
} elseif (0 == $type && array_key_exists($lowerName, $vars)) {
$args[] = $vars[$lowerName];
} elseif ($param->isDefaultValueAvailable()) {
$args[] = $param->getDefaultValue();
} else {
throw new InvalidArgumentException('method param miss:' . $name);
}
}
return $args;
}

Expand Down
43 changes: 26 additions & 17 deletions library/think/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,43 @@
* Class Model
* @package think
* @mixin Query
* @method $this scope(string|array $scope) static 查询范围
* @method $this where(mixed $field, string $op = null, mixed $condition = null) static 查询条件
* @method $this whereRaw(string $where, array $bind = []) static 表达式查询
* @method $this whereExp(string $field, string $condition, array $bind = []) static 字段表达式查询
* @method $this whereRaw(string $where, array $bind = [], string $logic = 'AND') static 表达式查询
* @method $this whereExp(string $field, string $condition, array $bind = [], string $logic = 'AND') static 字段表达式查询
* @method $this when(mixed $condition, mixed $query, mixed $otherwise = null) static 条件查询
* @method $this join(mixed $join, mixed $condition = null, string $type = 'INNER') static JOIN查询
* @method $this join(mixed $join, mixed $condition = null, string $type = 'INNER', array $bind = []) static JOIN查询
* @method $this view(mixed $join, mixed $field = null, mixed $on = null, string $type = 'INNER') static 视图查询
* @method $this with(mixed $with) static 关联预载入
* @method $this count(string $field) static Count统计查询
* @method $this min(string $field) static Min统计查询
* @method $this max(string $field) static Max统计查询
* @method $this with(mixed $with, callable $callback = null) static 关联预载入
* @method $this count(string $field = '*') static Count统计查询
* @method $this min(string $field, bool $force = true) static Min统计查询
* @method $this max(string $field, bool $force = true) static Max统计查询
* @method $this sum(string $field) static SUM统计查询
* @method $this avg(string $field) static Avg统计查询
* @method $this field(mixed $field, boolean $except = false) static 指定查询字段
* @method $this fieldRaw(string $field, array $bind = []) static 指定查询字段
* @method $this field(mixed $field, boolean $except = false, string $tableName = '', string $prefix = '', string $alias = '') static 指定查询字段
* @method $this fieldRaw(string $field) static 指定查询字段
* @method $this union(mixed $union, boolean $all = false) static UNION查询
* @method $this limit(mixed $offset, integer $length = null) static 查询LIMIT
* @method $this order(mixed $field, string $order = null) static 查询ORDER
* @method $this orderRaw(string $field, array $bind = []) static 查询ORDER
* @method $this cache(mixed $key = null , integer $expire = null) static 设置查询缓存
* @method mixed value(string $field) static 获取某个字段的值
* @method $this cache(mixed $key = null, integer|\DateTime $expire = null, string $tag = null) static 设置查询缓存
* @method mixed value(string $field, mixed $default = null) static 获取某个字段的值
* @method array column(string $field, string $key = '') static 获取某个列的值
* @method $this find(mixed $data = null) static 查询单个记录
* @method $this[] select(mixed $data = null) static 查询多个记录
* @method $this get(mixed $data = null,mixed $with =[],bool $cache= false) static 查询单个记录 支持关联预载入
* @method $this getOrFail(mixed $data = null,mixed $with =[],bool $cache= false) static 查询单个记录 不存在则抛出异常
* @method $this findOrEmpty(mixed $data = null,mixed $with =[],bool $cache= false) static 查询单个记录 不存在则返回空模型
* @method $this[] all(mixed $data = null,mixed $with =[],bool $cache= false) static 查询多个记录 支持关联预载入
* @method \think\Model withAttr(array $name,\Closure $closure) 动态定义获取器
* @method $this findOrFail(mixed $data = null) 查询单个记录
* @method Collection|$this[] select(mixed $data = null) static 查询多个记录
* @method $this get(mixed $data = null, mixed $with = [], bool $cache = false, bool $failException = false) static 查询单个记录 支持关联预载入
* @method $this getOrFail(mixed $data = null, mixed $with = [], bool $cache = false) static 查询单个记录 不存在则抛出异常
* @method $this findOrEmpty(mixed $data = null) static 查询单个记录 不存在则返回空模型
* @method Collection|$this[] all(mixed $data = null, mixed $with = [], bool $cache = false) static 查询多个记录 支持关联预载入
* @method $this withAttr(array $name, \Closure $closure = null) static 动态定义获取器
* @method $this withJoin(string|array $with, string $joinType = '') static
* @method $this withCount(string|array $relation, bool $subQuery = true) static 关联统计
* @method $this withSum(string|array $relation, string $field, bool $subQuery = true) static 关联SUM统计
* @method $this withMax(string|array $relation, string $field, bool $subQuery = true) static 关联MAX统计
* @method $this withMin(string|array $relation, string $field, bool $subQuery = true) static 关联Min统计
* @method $this withAvg(string|array $relation, string $field, bool $subQuery = true) static 关联Avg统计
* @method Paginator|$this paginate(int|array $listRows = null, int|bool $simple = false, array $config = []) static 分页
*/
abstract class Model implements \JsonSerializable, \ArrayAccess
{
Expand Down
2 changes: 1 addition & 1 deletion library/think/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -1800,7 +1800,7 @@ public function setHost($host)
public function host($strict = false)
{
if (!$this->host) {
$this->host = $this->server('HTTP_X_REAL_HOST') ?: $this->server('HTTP_HOST');
$this->host = $this->server('HTTP_X_REAL_HOST') ?: $this->server('HTTP_X_FORWARDED_HOST') ?: $this->server('HTTP_HOST');
}

return true === $strict && strpos($this->host, ':') ? strstr($this->host, ':', true) : $this->host;
Expand Down
2 changes: 2 additions & 0 deletions library/think/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@

use think\exception\RouteNotFoundException;
use think\route\AliasRule;
use think\route\Dispatch;
use think\route\dispatch\Url as UrlDispatch;
use think\route\Domain;
use think\route\Resource;
use think\route\Rule;
use think\route\RuleGroup;
use think\route\RuleItem;

Expand Down
2 changes: 1 addition & 1 deletion library/think/db/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -3464,7 +3464,7 @@ public function findOrFail($data = null)
}

/**
* 查找单条记录 如果不存在则抛出异常
* 查找单条记录 不存在则返回空模型
* @access public
* @param array|string|Query|\Closure $data
* @return array|\PDOStatement|string|Model
Expand Down
16 changes: 16 additions & 0 deletions library/think/model/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ public function load($relation)
return $this;
}

/**
* 绑定(一对一)关联属性到当前模型
* @access protected
* @param string $relation 关联名称
* @param array $attrs 绑定属性
* @return $this
*/
public function bindAttr($relation, array $attrs = [])
{
$this->each(function (Model $model) use ($relation, $attrs) {
$model->bindAttr($relation, $attrs);
});

return $this;
}

/**
* 设置需要隐藏的输出属性
* @access public
Expand Down
27 changes: 27 additions & 0 deletions library/think/model/concern/RelationShip.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use think\Collection;
use think\db\Query;
use think\Exception;
use think\Loader;
use think\Model;
use think\model\Relation;
Expand Down Expand Up @@ -115,6 +116,32 @@ public function setRelation($name, $value, $data = [])
return $this;
}

/**
* 绑定(一对一)关联属性到当前模型
* @access protected
* @param string $relation 关联名称
* @param array $attrs 绑定属性
* @return $this
* @throws Exception
*/
public function bindAttr($relation, array $attrs = [])
{
$relation = $this->getRelation($relation);

foreach ($attrs as $key => $attr) {
$key = is_numeric($key) ? $attr : $key;
$value = $this->getOrigin($key);

if (!is_null($value)) {
throw new Exception('bind attr has exists:' . $key);
}

$this->setAttr($key, $relation ? $relation->getAttr($attr) : null);
}

return $this;
}

/**
* 关联数据写入
* @access public
Expand Down
3 changes: 2 additions & 1 deletion library/think/model/relation/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use think\Model;
use think\model\Pivot;
use think\model\Relation;
use think\Paginator;

class BelongsToMany extends Relation
{
Expand Down Expand Up @@ -582,7 +583,7 @@ public function attach($data, $pivot = [])
* 判断是否存在关联数据
* @access public
* @param mixed $data 数据 可以使用关联模型对象 或者 关联对象的主键
* @return Pivot
* @return Pivot|false
* @throws Exception
*/
public function attached($data)
Expand Down
14 changes: 8 additions & 6 deletions library/think/model/relation/OneToOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,20 +282,22 @@ protected function match($model, $relation, &$result)
/**
* 绑定关联属性到父模型
* @access protected
* @param Model $model 关联模型对象
* @param Model $result 父模型对象
* @param Model $result 关联模型对象
* @param Model $model 父模型对象
* @return void
* @throws Exception
*/
protected function bindAttr($model, &$result)
{
foreach ($this->bindAttr as $key => $attr) {
$key = is_numeric($key) ? $attr : $key;
if (isset($result->$key)) {
$key = is_numeric($key) ? $attr : $key;
$value = $result->getOrigin($key);

if (!is_null($value)) {
throw new Exception('bind attr has exists:' . $key);
} else {
$result->setAttr($key, $model ? $model->$attr : null);
}

$result->setAttr($key, $model ? $model->getAttr($attr) : null);
}
}

Expand Down
2 changes: 1 addition & 1 deletion tpl/think_exception.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@
var err_line = $('.line-' + LINE, ol[0])[0];
err_line.className = err_line.className + ' line-error';

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

1 comment on commit ecf1a90

@ken678
Copy link

@ken678 ken678 commented on ecf1a90 Oct 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5.1.42的怎么版本还是5.1.41 LTS

Please sign in to comment.