Skip to content

Commit

Permalink
📦 Compatible with PHP8. Closed #192
Browse files Browse the repository at this point in the history
  • Loading branch information
wisp-x committed Apr 16, 2021
1 parent 1a4a317 commit 26840ed
Show file tree
Hide file tree
Showing 395 changed files with 33,759 additions and 8,584 deletions.
252 changes: 169 additions & 83 deletions composer.lock

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion thinkphp/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
12 changes: 5 additions & 7 deletions thinkphp/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.39 LTS';
const VERSION = '5.1.41 LTS';

/**
* 当前模块路径
Expand Down Expand Up @@ -548,12 +548,10 @@ public function config($name = '')
public function routeInit()
{
// 路由检测
$files = scandir($this->routePath);
foreach ($files as $file) {
if (strpos($file, '.php')) {
$filename = $this->routePath . $file;
// 导入路由配置
$rules = include $filename;
if (is_dir($this->routePath)) {
$files = glob($this->routePath . '*.php');
foreach ($files as $file) {
$rules = include $file;
if (is_array($rules)) {
$this->route->import($rules);
}
Expand Down
2 changes: 1 addition & 1 deletion thinkphp/library/think/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ public function where($field, $operator, $value = null)
*/
public function column($columnKey, $indexKey = null)
{
return array_column($this->items, $columnKey, $indexKey);
return array_column($this->toArray(), $columnKey, $indexKey);
}

/**
Expand Down
50 changes: 50 additions & 0 deletions thinkphp/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
63 changes: 36 additions & 27 deletions thinkphp/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 Query where(mixed $field, string $op = null, mixed $condition = null) static 查询条件
* @method Query whereRaw(string $where, array $bind = []) static 表达式查询
* @method Query whereExp(string $field, string $condition, array $bind = []) static 字段表达式查询
* @method Query when(mixed $condition, mixed $query, mixed $otherwise = null) static 条件查询
* @method Query join(mixed $join, mixed $condition = null, string $type = 'INNER') static JOIN查询
* @method Query view(mixed $join, mixed $field = null, mixed $on = null, string $type = 'INNER') static 视图查询
* @method Query with(mixed $with) static 关联预载入
* @method Query count(string $field) static Count统计查询
* @method Query min(string $field) static Min统计查询
* @method Query max(string $field) static Max统计查询
* @method Query sum(string $field) static SUM统计查询
* @method Query avg(string $field) static Avg统计查询
* @method Query field(mixed $field, boolean $except = false) static 指定查询字段
* @method Query fieldRaw(string $field, array $bind = []) static 指定查询字段
* @method Query union(mixed $union, boolean $all = false) static UNION查询
* @method Query limit(mixed $offset, integer $length = null) static 查询LIMIT
* @method Query order(mixed $field, string $order = null) static 查询ORDER
* @method Query orderRaw(string $field, array $bind = []) static 查询ORDER
* @method Query cache(mixed $key = null , integer $expire = null) static 设置查询缓存
* @method mixed value(string $field) static 获取某个字段的值
* @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 = [], 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', array $bind = []) static JOIN查询
* @method $this view(mixed $join, mixed $field = null, mixed $on = null, string $type = 'INNER') static 视图查询
* @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, 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|\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 mixed find(mixed $data = null) static 查询单个记录
* @method mixed select(mixed $data = null) static 查询多个记录
* @method mixed get(mixed $data = null,mixed $with =[],bool $cache= false) static 查询单个记录 支持关联预载入
* @method mixed getOrFail(mixed $data = null,mixed $with =[],bool $cache= false) static 查询单个记录 不存在则抛出异常
* @method mixed findOrEmpty(mixed $data = null,mixed $with =[],bool $cache= false) static 查询单个记录 不存在则返回空模型
* @method mixed all(mixed $data = null,mixed $with =[],bool $cache= false) static 查询多个记录 支持关联预载入
* @method \think\Model withAttr(array $name,\Closure $closure) 动态定义获取器
* @method $this find(mixed $data = null) static 查询单个记录
* @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() static 分页
*/
abstract class Model implements \JsonSerializable, \ArrayAccess
{
Expand Down
2 changes: 1 addition & 1 deletion thinkphp/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 thinkphp/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: 2 additions & 0 deletions thinkphp/library/think/Validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ public function check($data, $rules = [], $scene = '')
foreach ($this->append as $key => $rule) {
if (!isset($rules[$key])) {
$rules[$key] = $rule;
unset($this->append[$key]);
}
}

Expand Down Expand Up @@ -520,6 +521,7 @@ protected function checkItem($field, $value, $rules, $data, $title = '', $msg =
if (isset($this->append[$field])) {
// 追加额外的验证规则
$rules = array_unique(array_merge($rules, $this->append[$field]), SORT_REGULAR);
unset($this->append[$field]);
}

$i = 0;
Expand Down
2 changes: 1 addition & 1 deletion thinkphp/library/think/cache/driver/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function __construct($options = [])
*/
public function has($name)
{
return $this->handler->exists($this->getCacheKey($name));
return $this->handler->exists($this->getCacheKey($name)) ? true : false;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion thinkphp/library/think/db/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ protected function parseIn(Query $query, $key, $exp, $value, $field, $bindType)
*/
protected function parseClosure(Query $query, $call, $show = true)
{
$newQuery = $query->newQuery()->setConnection($this->connection);
$newQuery = $query->newQuery()->removeOption();
$call($newQuery);

return $newQuery->buildSql($show);
Expand Down
4 changes: 2 additions & 2 deletions thinkphp/library/think/db/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -1764,7 +1764,7 @@ public function page($page, $listRows = null)
* var_page:分页变量,
* list_rows:每页数量
* type:分页类名
* @return \think\Paginator
* @return $this[]|\think\Paginator
* @throws DbException
*/
public function paginate($listRows = null, $simple = false, $config = [])
Expand Down 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
8 changes: 7 additions & 1 deletion thinkphp/library/think/db/builder/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,13 @@ public function parseKey(Query $query, $key, $strict = false)

$key = trim($key);

if (strpos($key, '->') && false === strpos($key, '(')) {
if(strpos($key, '->>') && false === strpos($key, '(')){
// JSON字段支持
list($field, $name) = explode('->>', $key, 2);

return $this->parseKey($query, $field, true) . '->>\'$' . (strpos($name, '[') === 0 ? '' : '.') . str_replace('->>', '.', $name) . '\'';
}
elseif (strpos($key, '->') && false === strpos($key, '(')) {
// JSON字段支持
list($field, $name) = explode('->', $key, 2);

Expand Down
16 changes: 8 additions & 8 deletions thinkphp/library/think/db/connector/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ public function getFields($tableName)
$info[$val['field']] = [
'name' => $val['field'],
'type' => $val['type'],
'notnull' => (bool) ('' === $val['null']), // not null is empty, null is yes
'notnull' => 'NO' == $val['null'],
'default' => $val['default'],
'primary' => (strtolower($val['key']) == 'pri'),
'autoinc' => (strtolower($val['extra']) == 'auto_increment'),
'primary' => strtolower($val['key']) == 'pri',
'autoinc' => strtolower($val['extra']) == 'auto_increment',
];
}
}
Expand Down Expand Up @@ -187,7 +187,7 @@ public function startTransXa($xid)
return false;
}

$this->execute("XA START '$xid'");
$this->linkID->exec("XA START '$xid'");
}

/**
Expand All @@ -199,8 +199,8 @@ public function startTransXa($xid)
public function prepareXa($xid)
{
$this->initConnect(true);
$this->execute("XA END '$xid'");
$this->execute("XA PREPARE '$xid'");
$this->linkID->exec("XA END '$xid'");
$this->linkID->exec("XA PREPARE '$xid'");
}

/**
Expand All @@ -212,7 +212,7 @@ public function prepareXa($xid)
public function commitXa($xid)
{
$this->initConnect(true);
$this->execute("XA COMMIT '$xid'");
$this->linkID->exec("XA COMMIT '$xid'");
}

/**
Expand All @@ -224,6 +224,6 @@ public function commitXa($xid)
public function rollbackXa($xid)
{
$this->initConnect(true);
$this->execute("XA ROLLBACK '$xid'");
$this->linkID->exec("XA ROLLBACK '$xid'");
}
}
2 changes: 1 addition & 1 deletion thinkphp/library/think/facade/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* @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 \think\route\RuleGroup group(string|array $name, mixed $route, string $method = '*', array $option = [], array $pattern = []) static 注册路由分组
* @method \think\route\RuleGroup group(string|array $name, array|\Closure $route, array $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 注册路由
* @method \think\route\RuleItem post(string $rule, mixed $route, array $option = [], array $pattern = []) static 注册路由
Expand Down
16 changes: 16 additions & 0 deletions thinkphp/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
2 changes: 1 addition & 1 deletion thinkphp/library/think/model/concern/Conversion.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function appendRelationAttr($attr, $append)
if (isset($this->data[$key])) {
throw new Exception('bind attr has exists:' . $key);
} else {
$this->data[$key] = $model->$attr;
$this->data[$key] = $model->getAttr($attr);
}
}
}
Expand Down
Loading

0 comments on commit 26840ed

Please sign in to comment.