改进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
{
// 应用调度机制
private static $dispatch = [];
/**
* 执行应用程序
* @access public
@@ -66,36 +63,36 @@ class App
}
}
if (empty(self::$dispatch['type'])) {
// 获取当前请求的调度信息
$dispatch = Request::dispatch();
if (empty($dispatch)) {
// 未指定调度类型 则进行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_HOOK && Hook::listen('app_begin', self::$dispatch);
// 根据类型调度
switch (self::$dispatch['type']) {
APP_HOOK && Hook::listen('app_begin', $dispatch);
switch ($dispatch['type']) {
case 'redirect':
// 执行重定向跳转
header('Location: ' . self::$dispatch['url'], true, self::$dispatch['status']);
header('Location: ' . $dispatch['url'], true, $dispatch['status']);
break;
case 'module':
// 模块/控制器/操作
$data = self::module(self::$dispatch['module'], $config);
$data = self::module($dispatch['module'], $config);
break;
case 'controller':
// 执行控制器操作
$data = Loader::action(self::$dispatch['controller'], self::$dispatch['params']);
$data = Loader::action($dispatch['controller'], $dispatch['params']);
break;
case 'method':
// 执行回调方法
$data = self::invokeMethod(self::$dispatch['method'], self::$dispatch['params']);
$data = self::invokeMethod($dispatch['method'], $dispatch['params']);
break;
case 'function':
// 规则闭包
$data = self::invokeFunction(self::$dispatch['function'], self::$dispatch['params']);
$data = self::invokeFunction($dispatch['function'], $dispatch['params']);
break;
default:
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)
* @access public
@@ -337,28 +308,18 @@ class App
*/
public static function route(array $config)
{
// 解析PATH_INFO
self::parsePathinfo($config);
if (empty($_SERVER['PATH_INFO'])) {
$_SERVER['PATH_INFO'] = '';
define('__INFO__', '');
define('__EXT__', '');
} else {
$_SERVER['PATH_INFO'] = trim($_SERVER['PATH_INFO'], '/');
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__);
define('__INFO__', Request::pathinfo());
define('__EXT__', Request::ext());
// 检测URL禁用后缀
if ($config['url_deny_suffix'] && preg_match('/\.(' . $config['url_deny_suffix'] . ')$/i', __INFO__)) {
throw new Exception('url suffix deny');
}
$depr = $config['pathinfo_depr'];
$result = false;
$_SERVER['PATH_INFO'] = Request::path();
$depr = $config['pathinfo_depr'];
$result = false;
// 路由检测
if (APP_ROUTE_ON && !empty($config['url_route_on'])) {
// 开启路由
@@ -380,12 +341,7 @@ class App
//保证$_REQUEST正常取值
$_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()
{
if (is_null(self::$pathinfo)) {
if (Config::get('var_pathinfo')) {
if (isset($_GET[Config::get('var_pathinfo')])) {
// 判断URL里面是否有兼容模式参数
$_SERVER['PATH_INFO'] = $_GET[Config::get('var_pathinfo')];
unset($_GET[Config::get('var_pathinfo')]);
@@ -442,9 +442,8 @@ class Request
{
if (!empty($type)) {
self::$dispatch = $dispatch;
} else {
return self::$dispatch;
}
return self::$dispatch;
}
}

View File

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