mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 07:12:47 +08:00
增加app_dispatch钩子
This commit is contained in:
@@ -108,6 +108,9 @@ class App
|
|||||||
|
|
||||||
// 获取应用调度信息
|
// 获取应用调度信息
|
||||||
$dispatch = self::$dispatch;
|
$dispatch = self::$dispatch;
|
||||||
|
// 监听 app_dispatch
|
||||||
|
Hook::listen('app_dispatch', $dispatch);
|
||||||
|
|
||||||
// 未设置调度信息则进行 URL 路由检测
|
// 未设置调度信息则进行 URL 路由检测
|
||||||
if (empty($dispatch)) {
|
if (empty($dispatch)) {
|
||||||
$dispatch = self::routeCheck($request, $config);
|
$dispatch = self::routeCheck($request, $config);
|
||||||
@@ -147,8 +150,8 @@ class App
|
|||||||
} elseif (!is_null($data)) {
|
} elseif (!is_null($data)) {
|
||||||
// 默认自动识别响应输出类型
|
// 默认自动识别响应输出类型
|
||||||
$type = $request->isAjax() ?
|
$type = $request->isAjax() ?
|
||||||
Config::get('default_ajax_return') :
|
Config::get('default_ajax_return') :
|
||||||
Config::get('default_return_type');
|
Config::get('default_return_type');
|
||||||
|
|
||||||
$response = Response::create($data, $type);
|
$response = Response::create($data, $type);
|
||||||
} else {
|
} else {
|
||||||
@@ -169,7 +172,9 @@ class App
|
|||||||
public static function initCommon()
|
public static function initCommon()
|
||||||
{
|
{
|
||||||
if (empty(self::$init)) {
|
if (empty(self::$init)) {
|
||||||
if (defined('APP_NAMESPACE')) self::$namespace = APP_NAMESPACE;
|
if (defined('APP_NAMESPACE')) {
|
||||||
|
self::$namespace = APP_NAMESPACE;
|
||||||
|
}
|
||||||
|
|
||||||
Loader::addNamespace(self::$namespace, APP_PATH);
|
Loader::addNamespace(self::$namespace, APP_PATH);
|
||||||
|
|
||||||
@@ -184,11 +189,16 @@ class App
|
|||||||
ini_set('display_errors', 'Off');
|
ini_set('display_errors', 'Off');
|
||||||
} elseif (!IS_CLI) {
|
} elseif (!IS_CLI) {
|
||||||
// 重新申请一块比较大的 buffer
|
// 重新申请一块比较大的 buffer
|
||||||
if (ob_get_level() > 0) $output = ob_get_clean();
|
if (ob_get_level() > 0) {
|
||||||
|
$output = ob_get_clean();
|
||||||
|
}
|
||||||
|
|
||||||
ob_start();
|
ob_start();
|
||||||
|
|
||||||
if (!empty($output)) echo $output;
|
if (!empty($output)) {
|
||||||
|
echo $output;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($config['root_namespace'])) {
|
if (!empty($config['root_namespace'])) {
|
||||||
@@ -342,9 +352,9 @@ class App
|
|||||||
*/
|
*/
|
||||||
public static function invokeClass($class, $vars = [])
|
public static function invokeClass($class, $vars = [])
|
||||||
{
|
{
|
||||||
$reflect = new \ReflectionClass($class);
|
$reflect = new \ReflectionClass($class);
|
||||||
$constructor = $reflect->getConstructor();
|
$constructor = $reflect->getConstructor();
|
||||||
$args = $constructor ? self::bindParams($constructor, $vars) : [];
|
$args = $constructor ? self::bindParams($constructor, $vars) : [];
|
||||||
|
|
||||||
return $reflect->newInstanceArgs($args);
|
return $reflect->newInstanceArgs($args);
|
||||||
}
|
}
|
||||||
@@ -361,15 +371,15 @@ class App
|
|||||||
// 自动获取请求变量
|
// 自动获取请求变量
|
||||||
if (empty($vars)) {
|
if (empty($vars)) {
|
||||||
$vars = Config::get('url_param_type') ?
|
$vars = Config::get('url_param_type') ?
|
||||||
Request::instance()->route() :
|
Request::instance()->route() :
|
||||||
Request::instance()->param();
|
Request::instance()->param();
|
||||||
}
|
}
|
||||||
|
|
||||||
$args = [];
|
$args = [];
|
||||||
if ($reflect->getNumberOfParameters() > 0) {
|
if ($reflect->getNumberOfParameters() > 0) {
|
||||||
// 判断数组类型 数字数组时按顺序绑定参数
|
// 判断数组类型 数字数组时按顺序绑定参数
|
||||||
reset($vars);
|
reset($vars);
|
||||||
$type = key($vars) === 0 ? 1 : 0;
|
$type = key($vars) === 0 ? 1 : 0;
|
||||||
|
|
||||||
foreach ($reflect->getParameters() as $param) {
|
foreach ($reflect->getParameters() as $param) {
|
||||||
$args[] = self::getParamValue($param, $vars, $type);
|
$args[] = self::getParamValue($param, $vars, $type);
|
||||||
@@ -408,8 +418,8 @@ class App
|
|||||||
}
|
}
|
||||||
|
|
||||||
$result = method_exists($className, 'instance') ?
|
$result = method_exists($className, 'instance') ?
|
||||||
$className::instance() :
|
$className::instance() :
|
||||||
new $className;
|
new $className;
|
||||||
}
|
}
|
||||||
} elseif (1 == $type && !empty($vars)) {
|
} elseif (1 == $type && !empty($vars)) {
|
||||||
$result = array_shift($vars);
|
$result = array_shift($vars);
|
||||||
@@ -435,11 +445,11 @@ class App
|
|||||||
protected static function exec($dispatch, $config)
|
protected static function exec($dispatch, $config)
|
||||||
{
|
{
|
||||||
switch ($dispatch['type']) {
|
switch ($dispatch['type']) {
|
||||||
case 'redirect': // 重定向跳转
|
case 'redirect': // 重定向跳转
|
||||||
$data = Response::create($dispatch['url'], 'redirect')
|
$data = Response::create($dispatch['url'], 'redirect')
|
||||||
->code($dispatch['status']);
|
->code($dispatch['status']);
|
||||||
break;
|
break;
|
||||||
case 'module': // 模块/控制器/操作
|
case 'module': // 模块/控制器/操作
|
||||||
$data = self::module(
|
$data = self::module(
|
||||||
$dispatch['module'],
|
$dispatch['module'],
|
||||||
$config,
|
$config,
|
||||||
@@ -455,14 +465,14 @@ class App
|
|||||||
$config['controller_suffix']
|
$config['controller_suffix']
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
case 'method': // 回调方法
|
case 'method': // 回调方法
|
||||||
$vars = array_merge(Request::instance()->param(), $dispatch['var']);
|
$vars = array_merge(Request::instance()->param(), $dispatch['var']);
|
||||||
$data = self::invokeMethod($dispatch['method'], $vars);
|
$data = self::invokeMethod($dispatch['method'], $vars);
|
||||||
break;
|
break;
|
||||||
case 'function': // 闭包
|
case 'function': // 闭包
|
||||||
$data = self::invokeFunction($dispatch['function']);
|
$data = self::invokeFunction($dispatch['function']);
|
||||||
break;
|
break;
|
||||||
case 'response': // Response 实例
|
case 'response': // Response 实例
|
||||||
$data = $dispatch['response'];
|
$data = $dispatch['response'];
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@@ -483,14 +493,16 @@ class App
|
|||||||
*/
|
*/
|
||||||
public static function module($result, $config, $convert = null)
|
public static function module($result, $config, $convert = null)
|
||||||
{
|
{
|
||||||
if (is_string($result)) $result = explode('/', $result);
|
if (is_string($result)) {
|
||||||
|
$result = explode('/', $result);
|
||||||
|
}
|
||||||
|
|
||||||
$request = Request::instance();
|
$request = Request::instance();
|
||||||
|
|
||||||
if ($config['app_multi_module']) {
|
if ($config['app_multi_module']) {
|
||||||
// 多模块部署
|
// 多模块部署
|
||||||
$module = strip_tags(strtolower($result[0] ?: $config['default_module']));
|
$module = strip_tags(strtolower($result[0] ?: $config['default_module']));
|
||||||
$bind = Route::getBind('module');
|
$bind = Route::getBind('module');
|
||||||
$available = false;
|
$available = false;
|
||||||
|
|
||||||
if ($bind) {
|
if ($bind) {
|
||||||
@@ -615,7 +627,7 @@ class App
|
|||||||
|
|
||||||
// 路由检测(根据路由定义返回不同的URL调度)
|
// 路由检测(根据路由定义返回不同的URL调度)
|
||||||
$result = Route::check($request, $path, $depr, $config['url_domain_deploy']);
|
$result = Route::check($request, $path, $depr, $config['url_domain_deploy']);
|
||||||
$must = !is_null(self::$routeMust) ? self::$routeMust : $config['url_route_must'];
|
$must = !is_null(self::$routeMust) ? self::$routeMust : $config['url_route_must'];
|
||||||
|
|
||||||
if ($must && false === $result) {
|
if ($must && false === $result) {
|
||||||
// 路由无效
|
// 路由无效
|
||||||
|
|||||||
Reference in New Issue
Block a user