修正setAccessible问题,支持php8.5
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
# 临时修复topthink/framework 的问题
|
||||||
|
|
||||||

|

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