修正setAccessible问题,支持php8.5

This commit is contained in:
2026-01-06 12:50:18 +08:00
parent d18044dbd7
commit a68ac1ce79
3 changed files with 60 additions and 38 deletions

View File

@@ -1,3 +1,5 @@
# 临时修复topthink/framework 的问题
![](https://www.thinkphp.cn/uploads/images/20230630/300c856765af4d8ae758c503185f8739.png)
# ThinkPHP 8

View File

@@ -191,7 +191,9 @@ class Event
if (empty($prefix) && $reflect->hasProperty('eventPrefix')) {
$reflectProperty = $reflect->getProperty('eventPrefix');
if(PHP_VERSION_ID < 80100) {
$reflectProperty->setAccessible(true);
}
$prefix = $reflectProperty->getValue($observer);
}

View File

@@ -8,7 +8,7 @@
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);
declare (strict_types=1);
namespace think\route;
@@ -30,6 +30,7 @@ abstract class Dispatch
{
/**
* 应用对象
*
* @var App
*/
protected $app;
@@ -40,6 +41,7 @@ abstract class Dispatch
/**
* 执行路由调度
*
* @access public
* @return Response
*/
@@ -51,15 +53,15 @@ abstract class Dispatch
protected function autoResponse($data): Response
{
if ($data instanceof Response) {
if($data instanceof Response) {
$response = $data;
} elseif ($data instanceof ResponseInterface) {
$response = Response::create((string) $data->getBody(), 'html', $data->getStatusCode());
} elseif($data instanceof ResponseInterface) {
$response = Response::create((string)$data->getBody(), 'html', $data->getStatusCode());
foreach ($data->getHeaders() as $header => $values) {
$response->header([$header => implode(", ", $values)]);
}
} elseif (!is_null($data)) {
} elseif(!is_null($data)) {
// 默认自动识别响应输出类型
$type = $this->request->isJson() ? 'json' : 'html';
$response = Response::create($data, $type);
@@ -76,6 +78,7 @@ abstract class Dispatch
/**
* 检查路由后置操作
*
* @access protected
* @return void
*/
@@ -84,8 +87,8 @@ abstract class Dispatch
$option = $this->option;
// 添加中间件
if (!empty($option['middleware'])) {
if (isset($option['without_middleware'])) {
if(!empty($option['middleware'])) {
if(isset($option['without_middleware'])) {
$middleware = !empty($option['without_middleware']) ? array_diff($option['middleware'], $option['without_middleware']) : [];
} else {
$middleware = $option['middleware'];
@@ -93,12 +96,12 @@ abstract class Dispatch
$this->app->middleware->import($middleware, 'route');
}
if (!empty($option['append'])) {
if(!empty($option['append'])) {
$this->param = array_merge($this->param, $option['append']);
}
// 绑定模型数据
if (!empty($option['model'])) {
if(!empty($option['model'])) {
$this->createBindModel($option['model'], $this->param);
}
@@ -109,13 +112,14 @@ abstract class Dispatch
$this->request->setRoute($this->param);
// 数据自动验证
if (isset($option['validate'])) {
if(isset($option['validate'])) {
$this->autoValidate($option['validate']);
}
}
/**
* 获取操作的绑定参数
*
* @access protected
* @return array
*/
@@ -131,8 +135,11 @@ abstract class Dispatch
/**
* 执行中间件调度
*
* @access public
*
* @param object $controller 控制器实例
*
* @return void
*/
protected function responseWithMiddlewarePipeline($instance, $action)
@@ -146,13 +153,13 @@ abstract class Dispatch
$suffix = $this->rule->config('action_suffix');
$action = $action . $suffix;
if (is_callable([$instance, $action])) {
if(is_callable([$instance, $action])) {
$vars = $this->getActionBindVars();
try {
$reflect = new ReflectionMethod($instance, $action);
// 严格获取当前操作方法名
$actionName = $reflect->getName();
if ($suffix) {
if($suffix) {
$actionName = substr($actionName, 0, -strlen($suffix));
}
@@ -175,26 +182,31 @@ abstract class Dispatch
/**
* 使用反射机制注册控制器中间件
*
* @access public
*
* @param object $controller 控制器实例
*
* @return void
*/
protected function registerControllerMiddleware($controller): void
{
$class = new ReflectionClass($controller);
if ($class->hasProperty('middleware')) {
if($class->hasProperty('middleware')) {
$reflectionProperty = $class->getProperty('middleware');
if(PHP_VERSION_ID < 80100) {
$reflectionProperty->setAccessible(true);
}
$middlewares = $reflectionProperty->getValue($controller);
$action = $this->request->action(true);
foreach ($middlewares as $key => $val) {
if (!is_int($key)) {
if(!is_int($key)) {
$middleware = $key;
$options = $val;
} elseif (isset($val['middleware'])) {
} elseif(isset($val['middleware'])) {
$middleware = $val['middleware'];
$options = $val['options'] ?? [];
} else {
@@ -202,15 +214,15 @@ abstract class Dispatch
$options = [];
}
if (isset($options['only']) && !in_array($action, $this->parseActions($options['only']))) {
if(isset($options['only']) && !in_array($action, $this->parseActions($options['only']))) {
continue;
} elseif (isset($options['except']) && in_array($action, $this->parseActions($options['except']))) {
} elseif(isset($options['except']) && in_array($action, $this->parseActions($options['except']))) {
continue;
}
if (is_string($middleware) && str_contains($middleware, ':')) {
if(is_string($middleware) && str_contains($middleware, ':')) {
$middleware = explode(':', $middleware);
if (count($middleware) > 1) {
if(count($middleware) > 1) {
$middleware = [$middleware[0], array_slice($middleware, 1)];
}
}
@@ -229,20 +241,23 @@ abstract class Dispatch
/**
* 路由绑定模型实例
*
* @access protected
*
* @param array $bindModel 绑定模型
* @param array $matches 路由变量
*
* @return void
*/
protected function createBindModel(array $bindModel, array $matches): void
{
foreach ($bindModel as $key => $val) {
if ($val instanceof \Closure) {
if($val instanceof \Closure) {
$result = $this->app->invokeFunction($val, $matches);
} else {
$fields = explode('&', $key);
if (is_array($val)) {
if(is_array($val)) {
[$model, $exception] = $val;
} else {
$model = $val;
@@ -253,7 +268,7 @@ abstract class Dispatch
$match = true;
foreach ($fields as $field) {
if (!isset($matches[$field])) {
if(!isset($matches[$field])) {
$match = false;
break;
} else {
@@ -261,12 +276,12 @@ abstract class Dispatch
}
}
if ($match) {
if($match) {
$result = $model::where($where)->failException($exception)->find();
}
}
if (!empty($result)) {
if(!empty($result)) {
// 注入容器
$this->app->instance($result::class, $result);
}
@@ -275,8 +290,11 @@ abstract class Dispatch
/**
* 验证数据
*
* @access protected
*
* @param array $option
*
* @return void
* @throws \think\exception\ValidateException
*/
@@ -284,7 +302,7 @@ abstract class Dispatch
{
[$validate, $scene, $message, $batch] = $option;
if (is_array($validate)) {
if(is_array($validate)) {
// 指定验证规则
$v = new Validate();
$v->rule($validate);
@@ -294,7 +312,7 @@ abstract class Dispatch
$v = new $class();
if (!empty($scene)) {
if(!empty($scene)) {
$v->scene($scene);
}
}