改进App类

This commit is contained in:
thinkphp
2016-05-07 18:20:04 +08:00
parent f495332abc
commit 3b3751f3d5
3 changed files with 29 additions and 74 deletions

View File

@@ -18,9 +18,6 @@ namespace think;
class App class App
{ {
// 应用调度机制
private static $dispatch = [];
/** /**
* 执行应用程序 * 执行应用程序
* @access public * @access public
@@ -66,36 +63,36 @@ class App
} }
} }
if (empty(self::$dispatch['type'])) { // 获取当前请求的调度信息
$dispatch = Request::dispatch();
if (empty($dispatch)) {
// 未指定调度类型 则进行URL路由检测 // 未指定调度类型 则进行URL路由检测
self::route($config); $dispatch = self::route($config);
} }
// 记录路由信息 // 记录路由信息
APP_DEBUG && Log::record('[ ROUTE ] ' . var_export(self::$dispatch, true), 'info'); APP_DEBUG && Log::record('[ ROUTE ] ' . var_export($dispatch, true), 'info');
// 监听app_begin // 监听app_begin
APP_HOOK && Hook::listen('app_begin', self::$dispatch); APP_HOOK && Hook::listen('app_begin', $dispatch);
switch ($dispatch['type']) {
// 根据类型调度
switch (self::$dispatch['type']) {
case 'redirect': case 'redirect':
// 执行重定向跳转 // 执行重定向跳转
header('Location: ' . self::$dispatch['url'], true, self::$dispatch['status']); header('Location: ' . $dispatch['url'], true, $dispatch['status']);
break; break;
case 'module': case 'module':
// 模块/控制器/操作 // 模块/控制器/操作
$data = self::module(self::$dispatch['module'], $config); $data = self::module($dispatch['module'], $config);
break; break;
case 'controller': case 'controller':
// 执行控制器操作 // 执行控制器操作
$data = Loader::action(self::$dispatch['controller'], self::$dispatch['params']); $data = Loader::action($dispatch['controller'], $dispatch['params']);
break; break;
case 'method': case 'method':
// 执行回调方法 // 执行回调方法
$data = self::invokeMethod(self::$dispatch['method'], self::$dispatch['params']); $data = self::invokeMethod($dispatch['method'], $dispatch['params']);
break; break;
case 'function': case 'function':
// 规则闭包 // 规则闭包
$data = self::invokeFunction(self::$dispatch['function'], self::$dispatch['params']); $data = self::invokeFunction($dispatch['function'], $dispatch['params']);
break; break;
default: default:
throw new Exception('dispatch type not support', 10008); throw new Exception('dispatch type not support', 10008);
@@ -303,32 +300,6 @@ class App
} }
} }
// 分析 PATH_INFO
private static function parsePathinfo(array $config)
{
if (isset($_GET[$config['var_pathinfo']])) {
// 判断URL里面是否有兼容模式参数
$_SERVER['PATH_INFO'] = $_GET[$config['var_pathinfo']];
unset($_GET[$config['var_pathinfo']]);
} elseif (IS_CLI) {
// CLI模式下 index.php module/controller/action/params/...
$_SERVER['PATH_INFO'] = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : '';
}
// 监听path_info
APP_HOOK && Hook::listen('path_info');
// 分析PATHINFO信息
if (!isset($_SERVER['PATH_INFO'])) {
foreach ($config['pathinfo_fetch'] as $type) {
if (!empty($_SERVER[$type])) {
$_SERVER['PATH_INFO'] = (0 === strpos($_SERVER[$type], $_SERVER['SCRIPT_NAME'])) ?
substr($_SERVER[$type], strlen($_SERVER['SCRIPT_NAME'])) : $_SERVER[$type];
break;
}
}
}
}
/** /**
* URL路由检测根据PATH_INFO) * URL路由检测根据PATH_INFO)
* @access public * @access public
@@ -337,28 +308,18 @@ class App
*/ */
public static function route(array $config) public static function route(array $config)
{ {
// 解析PATH_INFO
self::parsePathinfo($config);
if (empty($_SERVER['PATH_INFO'])) { define('__INFO__', Request::pathinfo());
$_SERVER['PATH_INFO'] = ''; define('__EXT__', Request::ext());
define('__INFO__', '');
define('__EXT__', ''); // 检测URL禁用后缀
} else { if ($config['url_deny_suffix'] && preg_match('/\.(' . $config['url_deny_suffix'] . ')$/i', __INFO__)) {
$_SERVER['PATH_INFO'] = trim($_SERVER['PATH_INFO'], '/'); throw new Exception('url suffix deny');
define('__INFO__', $_SERVER['PATH_INFO']);
// URL后缀
define('__EXT__', strtolower(pathinfo($_SERVER['PATH_INFO'], PATHINFO_EXTENSION)));
// 检测URL禁用后缀
if ($config['url_deny_suffix'] && preg_match('/\.(' . $config['url_deny_suffix'] . ')$/i', __INFO__)) {
throw new Exception('url suffix deny');
}
// 去除正常的URL后缀
$_SERVER['PATH_INFO'] = preg_replace($config['url_html_suffix'] ? '/\.(' . trim($config['url_html_suffix'], '.') . ')$/i' : '/\.' . __EXT__ . '$/i', '', __INFO__);
} }
$depr = $config['pathinfo_depr']; $_SERVER['PATH_INFO'] = Request::path();
$result = false; $depr = $config['pathinfo_depr'];
$result = false;
// 路由检测 // 路由检测
if (APP_ROUTE_ON && !empty($config['url_route_on'])) { if (APP_ROUTE_ON && !empty($config['url_route_on'])) {
// 开启路由 // 开启路由
@@ -380,12 +341,7 @@ class App
//保证$_REQUEST正常取值 //保证$_REQUEST正常取值
$_REQUEST = array_merge($_POST, $_GET, $_COOKIE); $_REQUEST = array_merge($_POST, $_GET, $_COOKIE);
// 注册调度机制 // 注册调度机制
self::dispatch($result); return Request::dispatch($result);
} }
// 指定应用调度
public static function dispatch($dispatch)
{
self::$dispatch = $dispatch;
}
} }

View File

@@ -108,7 +108,7 @@ class Request
public static function pathinfo() public static function pathinfo()
{ {
if (is_null(self::$pathinfo)) { if (is_null(self::$pathinfo)) {
if (Config::get('var_pathinfo')) { if (isset($_GET[Config::get('var_pathinfo')])) {
// 判断URL里面是否有兼容模式参数 // 判断URL里面是否有兼容模式参数
$_SERVER['PATH_INFO'] = $_GET[Config::get('var_pathinfo')]; $_SERVER['PATH_INFO'] = $_GET[Config::get('var_pathinfo')];
unset($_GET[Config::get('var_pathinfo')]); unset($_GET[Config::get('var_pathinfo')]);
@@ -442,9 +442,8 @@ class Request
{ {
if (!empty($type)) { if (!empty($type)) {
self::$dispatch = $dispatch; self::$dispatch = $dispatch;
} else {
return self::$dispatch;
} }
return self::$dispatch;
} }
} }

View File

@@ -11,6 +11,8 @@
namespace think; namespace think;
use think\Request;
class Route class Route
{ {
// 路由规则 // 路由规则
@@ -362,11 +364,6 @@ class Route
$url = str_replace($depr, '/', $url); $url = str_replace($depr, '/', $url);
} }
// 优先检测是否存在PATH_INFO
if (empty($url)) {
$url = '/';
}
if (isset(self::$map[$url])) { if (isset(self::$map[$url])) {
// URL映射 // URL映射
return self::parseUrl(self::$map[$url], $depr); return self::parseUrl(self::$map[$url], $depr);
@@ -421,6 +418,7 @@ class Route
} }
$result = self::checkRule($key, $route, $url1, $pattern, $option); $result = self::checkRule($key, $route, $url1, $pattern, $option);
if (false !== $result) { if (false !== $result) {
Request::route($route);
return $result; return $result;
} }
} }
@@ -433,6 +431,7 @@ class Route
// 规则路由 // 规则路由
$result = self::checkRule($rule, $route, $url, $pattern, $option); $result = self::checkRule($rule, $route, $url, $pattern, $option);
if (false !== $result) { if (false !== $result) {
Request::route($route);
return $result; return $result;
} }
} }
@@ -575,6 +574,7 @@ class Route
// 地址格式 [模块/控制器/操作?]参数1=值1&参数2=值2... // 地址格式 [模块/控制器/操作?]参数1=值1&参数2=值2...
private static function parseRoute($url, $reverse = false) private static function parseRoute($url, $reverse = false)
{ {
$url = trim($url, '/');
$var = []; $var = [];
if (false !== strpos($url, '?')) { if (false !== strpos($url, '?')) {
// [模块/控制器/操作?]参数1=值1&参数2=值2... // [模块/控制器/操作?]参数1=值1&参数2=值2...