改进Request类

This commit is contained in:
thinkphp
2016-05-09 23:19:41 +08:00
parent 96a32debc2
commit c305b11d1b
4 changed files with 203 additions and 73 deletions

View File

@@ -22,6 +22,7 @@ use think\Input;
use think\Lang; use think\Lang;
use think\Loader; use think\Loader;
use think\Log; use think\Log;
use think\Request;
use think\Route; use think\Route;
use think\Session; use think\Session;
use think\Url; use think\Url;
@@ -358,3 +359,14 @@ function route($rule = '', $route = [], $type = '*', $option = [], $pattern = []
{ {
Route::register($rule, $route, $type, $option, $pattern); Route::register($rule, $route, $type, $option, $pattern);
} }
/**
* 获取Request参数
* @param string $name 方法
* @param mixed $param 参数
* @return mixed
*/
function request($name, $param = '')
{
return Request::instance()->$name($param);
}

View File

@@ -64,7 +64,7 @@ class App
} }
// 获取当前请求的调度信息 // 获取当前请求的调度信息
$dispatch = Request::dispatch(); $dispatch = Request::instance()->dispatch();
if (empty($dispatch)) { if (empty($dispatch)) {
// 未指定调度类型 则进行URL路由检测 // 未指定调度类型 则进行URL路由检测
$dispatch = self::route($config); $dispatch = self::route($config);
@@ -309,15 +309,15 @@ class App
public static function route(array $config) public static function route(array $config)
{ {
define('__INFO__', Request::pathinfo()); define('__INFO__', Request::instance()->pathinfo());
define('__EXT__', Request::ext()); define('__EXT__', Request::instance()->ext());
// 检测URL禁用后缀 // 检测URL禁用后缀
if ($config['url_deny_suffix'] && preg_match('/\.(' . $config['url_deny_suffix'] . ')$/i', __INFO__)) { if ($config['url_deny_suffix'] && preg_match('/\.(' . $config['url_deny_suffix'] . ')$/i', __INFO__)) {
throw new Exception('url suffix deny'); throw new Exception('url suffix deny');
} }
$_SERVER['PATH_INFO'] = Request::path(); $_SERVER['PATH_INFO'] = Request::instance()->path();
$depr = $config['pathinfo_depr']; $depr = $config['pathinfo_depr'];
$result = false; $result = false;
// 路由检测 // 路由检测
@@ -341,7 +341,7 @@ class App
//保证$_REQUEST正常取值 //保证$_REQUEST正常取值
$_REQUEST = array_merge($_POST, $_GET, $_COOKIE); $_REQUEST = array_merge($_POST, $_GET, $_COOKIE);
// 注册调度机制 // 注册调度机制
return Request::dispatch($result); return Request::instance()->dispatch($result);
} }
} }

View File

@@ -16,40 +16,52 @@ use think\Input;
class Request class Request
{ {
/**
* @var object 对象实例
*/
protected static $instance;
/** /**
* @var string 基础URL * @var string 基础URL
*/ */
protected static $baseUrl; protected $baseUrl;
/** /**
* @var string 根目录 * @var string 根目录
*/ */
protected static $root; protected $root;
/** /**
* @var string pathinfo * @var string pathinfo
*/ */
protected static $pathinfo; protected $pathinfo;
/** /**
* @var string pathinfo不含后缀 * @var string pathinfo不含后缀
*/ */
protected static $path; protected $path;
/** /**
* @var array 路由 * @var array 路由
*/ */
protected static $route = []; protected $route = [];
/** /**
* @var array 调度信息 * @var array 调度信息
*/ */
protected static $dispatch = []; protected $dispatch = [];
protected $get = [];
protected $post = [];
protected $put = [];
protected $delete = [];
protected $file = [];
protected $cookie = [];
protected $server = [];
/** /**
* @var array 资源类型 * @var array 资源类型
*/ */
protected static $mime = [ protected $mime = [
'html' => 'text/html,application/xhtml+xml,*/*', 'html' => 'text/html,application/xhtml+xml,*/*',
'xml' => 'application/xml,text/xml,application/x-xml', 'xml' => 'application/xml,text/xml,application/x-xml',
'json' => 'application/json,text/x-json,application/jsonrequest,text/json', 'json' => 'application/json,text/x-json,application/jsonrequest,text/json',
@@ -66,18 +78,111 @@ class Request
'csv' => 'text/csv', 'csv' => 'text/csv',
]; ];
/**
* 架构函数
* @access public
* @param array $options 参数
*/
public function __construct($options = [])
{
foreach ($options as $name => $item) {
if (property_exists($this, $name)) {
$this->$name = $item;
}
}
}
/**
* 初始化
* @access public
* @param array $options 参数
* @return object
*/
public static function instance($options = [])
{
if (is_null(self::$instance)) {
self::$instance = new static($options);
}
return self::$instance;
}
/**
* 创建一个URL请求
* @access public
* @param string $uri URL地址
* @param string $method 请求类型
* @param array $params 请求参数
* @param array $cookie
* @param array $files
* @param array $server
* @return object
*/
public static function create($uri, $method = 'GET', $params = [], $cookie = [], $files = [], $server = [])
{
$server['PATH_INFO'] = '';
$server['REQUEST_METHOD'] = strtoupper($method);
$info = parse_url($uri);
if (isset($info['host'])) {
$server['SERVER_NAME'] = $info['host'];
$server['HTTP_HOST'] = $info['host'];
}
if (isset($info['scheme'])) {
if ('https' === $info['scheme']) {
$server['HTTPS'] = 'on';
$server['SERVER_PORT'] = 443;
} else {
unset($server['HTTPS']);
$server['SERVER_PORT'] = 80;
}
}
if (isset($info['port'])) {
$server['SERVER_PORT'] = $info['port'];
$server['HTTP_HOST'] = $server['HTTP_HOST'] . ':' . $info['port'];
}
if (isset($info['user'])) {
$server['PHP_AUTH_USER'] = $info['user'];
}
if (isset($info['pass'])) {
$server['PHP_AUTH_PW'] = $info['pass'];
}
if (!isset($info['path'])) {
$info['path'] = '/';
}
$options = [];
$options[strtolower($method)] = $params;
$queryString = '';
if (isset($info['query'])) {
parse_str(html_entity_decode($info['query']), $query);
if (isset($options['get'])) {
$options['get'] = array_replace($query, $options['get']);
$queryString = http_build_query($query, '', '&');
} else {
$options['get'] = $query;
$queryString = $info['query'];
}
} elseif (isset($options['get'])) {
$queryString = http_build_query($options['get'], '', '&');
}
$server['REQUEST_URI'] = $info['path'] . ('' !== $queryString ? '?' . $queryString : '');
$server['QUERY_STRING'] = $queryString;
$options['cookie'] = $cookie;
$options['file'] = $files;
$options['server'] = $server;
return new self($options);
}
/** /**
* 获取当前URL * 获取当前URL
* @access public * @access public
* @param string $url URL地址 * @param string $url URL地址
* @return string * @return string
*/ */
public static function url($url = '') public function url($url = '')
{ {
if (!empty($url)) { if (!empty($url)) {
self::$url = $url; $this->url = $url;
} else { } else {
return self::$url ?: $_SERVER[Config::get('url_request_uri')]; return $this->url ?: $_SERVER[Config::get('url_request_uri')];
} }
} }
@@ -87,12 +192,12 @@ class Request
* @param string $url URL地址 * @param string $url URL地址
* @return string * @return string
*/ */
public static function baseUrl($url = '') public function baseUrl($url = '')
{ {
if (!empty($url)) { if (!empty($url)) {
self::$baseUrl = $url; $this->baseUrl = $url;
} else { } else {
return self::$baseUrl ?: rtrim($_SERVER['SCRIPT_NAME'], '/'); return $this->baseUrl ?: rtrim($_SERVER['SCRIPT_NAME'], '/');
} }
} }
@@ -102,15 +207,15 @@ class Request
* @param string $url URL地址 * @param string $url URL地址
* @return string * @return string
*/ */
public static function root($url = '') public function root($url = '')
{ {
if (!empty($url)) { if (!empty($url)) {
self::$root = $url; $this->root = $url;
} elseif (self::$root) { } elseif ($this->root) {
return self::$root; return $this->root;
} else { } else {
$_root = rtrim(dirname(self::baseUrl()), '/'); $_root = rtrim(dirname($this->baseUrl()), '/');
return ('/' == $_root || '\\' == $_root) ? '' : $_root; return ('/' == $_root || '\\' == $_root) ? '' : $_root;
} }
} }
@@ -120,9 +225,9 @@ class Request
* @access public * @access public
* @return string * @return string
*/ */
public static function pathinfo() public function pathinfo()
{ {
if (is_null(self::$pathinfo)) { if (is_null($this->pathinfo)) {
if (isset($_GET[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')];
@@ -142,9 +247,9 @@ class Request
} }
} }
} }
self::$pathinfo = empty($_SERVER['PATH_INFO']) ? '/' : trim($_SERVER['PATH_INFO'], '/'); $this->pathinfo = empty($_SERVER['PATH_INFO']) ? '/' : trim($_SERVER['PATH_INFO'], '/');
} }
return self::$pathinfo; return $this->pathinfo;
} }
/** /**
@@ -152,13 +257,13 @@ class Request
* @access public * @access public
* @return string * @return string
*/ */
public static function path() public function path()
{ {
if (is_null(self::$path)) { if (is_null($this->path)) {
// 去除正常的URL后缀 // 去除正常的URL后缀
self::$path = preg_replace(Config::get('url_html_suffix') ? '/\.(' . trim(Config::get('url_html_suffix'), '.') . ')$/i' : '/\.' . self::ext() . '$/i', '', self::pathinfo()); $this->path = preg_replace(Config::get('url_html_suffix') ? '/\.(' . trim(Config::get('url_html_suffix'), '.') . ')$/i' : '/\.' . $this->ext() . '$/i', '', $this->pathinfo());
} }
return self::$path; return $this->path;
} }
/** /**
@@ -166,9 +271,9 @@ class Request
* @access public * @access public
* @return string * @return string
*/ */
public static function ext() public function ext()
{ {
return pathinfo(self::pathinfo(), PATHINFO_EXTENSION); return pathinfo($this->pathinfo(), PATHINFO_EXTENSION);
} }
/** /**
@@ -177,7 +282,7 @@ class Request
* @param bool $float 是否使用浮点类型 * @param bool $float 是否使用浮点类型
* @return integer|float * @return integer|float
*/ */
public static function time($float = false) public function time($float = false)
{ {
return $float ? $_SERVER['REQUEST_TIME_FLOAT'] : $_SERVER['REQUEST_TIME']; return $float ? $_SERVER['REQUEST_TIME_FLOAT'] : $_SERVER['REQUEST_TIME'];
} }
@@ -187,13 +292,13 @@ class Request
* @access public * @access public
* @return false|string * @return false|string
*/ */
public static function type() public function type()
{ {
if (!isset($_SERVER['HTTP_ACCEPT'])) { if (!isset($_SERVER['HTTP_ACCEPT'])) {
return false; return false;
} }
foreach (self::$mimeType as $key => $val) { foreach ($this->mimeType as $key => $val) {
$array = explode(',', $val); $array = explode(',', $val);
foreach ($array as $k => $v) { foreach ($array as $k => $v) {
if (stristr($_SERVER['HTTP_ACCEPT'], $v)) { if (stristr($_SERVER['HTTP_ACCEPT'], $v)) {
@@ -211,13 +316,13 @@ class Request
* @param string $val 资源类型 * @param string $val 资源类型
* @return void * @return void
*/ */
public static function mimeType($type, $val = '') public function mimeType($type, $val = '')
{ {
if (is_array($type)) { if (is_array($type)) {
self::$mimeType = array_merge(self::$mimeType, $type); $this->mimeType = array_merge($this->mimeType, $type);
} else { } else {
self::$mimeType[$type] = $val; $this->mimeType[$type] = $val;
} }
} }
@@ -226,7 +331,7 @@ class Request
* @access public * @access public
* @return string * @return string
*/ */
public static function method() public function method()
{ {
return IS_CLI ? 'GET' : $_SERVER['REQUEST_METHOD']; return IS_CLI ? 'GET' : $_SERVER['REQUEST_METHOD'];
} }
@@ -237,9 +342,10 @@ class Request
* @param string $name 变量名 * @param string $name 变量名
* @return mixed * @return mixed
*/ */
public static function param($name = '') public function param($name = '')
{ {
return Input::param($name); $method = $this->method();
return $this->$method($name);
} }
/** /**
@@ -248,9 +354,9 @@ class Request
* @param string $name 变量名 * @param string $name 变量名
* @return mixed * @return mixed
*/ */
public static function get($name = '') public function get($name = '')
{ {
return Input::get($name); return Input::data($this->get ?: $_GET, $name);
} }
/** /**
@@ -259,9 +365,9 @@ class Request
* @param string $name 变量名 * @param string $name 变量名
* @return mixed * @return mixed
*/ */
public static function post($name = '') public function post($name = '')
{ {
return Input::post($name); return Input::data($this->post ?: $_POST, $name);
} }
/** /**
@@ -270,9 +376,13 @@ class Request
* @param string $name 变量名 * @param string $name 变量名
* @return mixed * @return mixed
*/ */
public static function put($name = '') public function put($name = '')
{ {
return Input::put($name); static $_PUT = null;
if (is_null($_PUT)) {
parse_str(file_get_contents('php://input'), $_PUT);
}
return Input::data($this->put ?: $_PUT, $name);
} }
/** /**
@@ -281,9 +391,14 @@ class Request
* @param string $name 变量名 * @param string $name 变量名
* @return mixed * @return mixed
*/ */
public static function delete($name = '') public function delete($name = '')
{ {
return Input::delete($name); static $_DELETE = null;
if (is_null($_DELETE)) {
parse_str(file_get_contents('php://input'), $_DELETE);
$_DELETE = array_merge($_DELETE, $_GET);
}
return Input::data($this->delete ?: $_DELETE, $name);
} }
/** /**
@@ -292,9 +407,12 @@ class Request
* @param string $name 变量名 * @param string $name 变量名
* @return mixed * @return mixed
*/ */
public static function session($name = '') public function session($name = '')
{ {
return Input::session($name); if (PHP_SESSION_DISABLED == session_status()) {
session_start();
}
return Input::data($this->session ?: $_SESSION, $name);
} }
/** /**
@@ -303,9 +421,9 @@ class Request
* @param string $name 变量名 * @param string $name 变量名
* @return mixed * @return mixed
*/ */
public static function cookie($name = '') public function cookie($name = '')
{ {
return Input::cookie($name); return Input::data($this->cookie ?: $_COOKIE, $name);
} }
/** /**
@@ -314,9 +432,9 @@ class Request
* @param string $name 变量名 * @param string $name 变量名
* @return mixed * @return mixed
*/ */
public static function server($name = '') public function server($name = '')
{ {
return Input::server($name); return Input::data($this->server ?: $_SERVER, $name);
} }
/** /**
@@ -325,9 +443,9 @@ class Request
* @param string $name 名称 * @param string $name 名称
* @return null|array|\think\File * @return null|array|\think\File
*/ */
public static function file($name = '') public function file($name = '')
{ {
return Input::file($name); return Input::data($this->file ?: $_FILES, $name);
} }
/** /**
@@ -335,7 +453,7 @@ class Request
* @access public * @access public
* @return bool * @return bool
*/ */
public static function isSsl() public function isSsl()
{ {
if (isset($_SERVER['HTTPS']) && ('1' == $_SERVER['HTTPS'] || 'on' == strtolower($_SERVER['HTTPS']))) { if (isset($_SERVER['HTTPS']) && ('1' == $_SERVER['HTTPS'] || 'on' == strtolower($_SERVER['HTTPS']))) {
return true; return true;
@@ -350,7 +468,7 @@ class Request
* @access public * @access public
* @return bool * @return bool
*/ */
public static function isAjax() public function isAjax()
{ {
return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') ? true : false; return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') ? true : false;
} }
@@ -361,7 +479,7 @@ class Request
* @param boolean $adv 是否进行高级模式获取(有可能被伪装) * @param boolean $adv 是否进行高级模式获取(有可能被伪装)
* @return mixed * @return mixed
*/ */
public static function ip($type = 0, $adv = false) public function ip($type = 0, $adv = false)
{ {
$type = $type ? 1 : 0; $type = $type ? 1 : 0;
static $ip = null; static $ip = null;
@@ -397,7 +515,7 @@ class Request
* @access public * @access public
* @return string * @return string
*/ */
public static function scheme() public function scheme()
{ {
return $_SERVER['REQUEST_SCHEME']; return $_SERVER['REQUEST_SCHEME'];
} }
@@ -407,7 +525,7 @@ class Request
* @access public * @access public
* @return string * @return string
*/ */
public static function query() public function query()
{ {
return $_SERVER['QUERY_STRING']; return $_SERVER['QUERY_STRING'];
} }
@@ -417,7 +535,7 @@ class Request
* @access public * @access public
* @return string * @return string
*/ */
public static function host() public function host()
{ {
return $_SERVER['SERVER_NAME']; return $_SERVER['SERVER_NAME'];
} }
@@ -427,7 +545,7 @@ class Request
* @access public * @access public
* @return integer * @return integer
*/ */
public static function port() public function port()
{ {
return $_SERVER['SERVER_PORT']; return $_SERVER['SERVER_PORT'];
} }
@@ -438,12 +556,12 @@ class Request
* @param array $route 路由名称 * @param array $route 路由名称
* @return array * @return array
*/ */
public static function route($route = []) public function route($route = [])
{ {
if (!empty($route)) { if (!empty($route)) {
self::$route = $route; $this->route = $route;
} else { } else {
return self::$route; return $this->route;
} }
} }
@@ -453,12 +571,12 @@ class Request
* @param array $dispatch 调度信息 * @param array $dispatch 调度信息
* @return array * @return array
*/ */
public static function dispatch($dispatch = []) public function dispatch($dispatch = [])
{ {
if (!empty($dispatch)) { if (!empty($dispatch)) {
self::$dispatch = $dispatch; $this->dispatch = $dispatch;
} }
return self::$dispatch; return $this->dispatch;
} }
} }

View File

@@ -418,7 +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(['rule' => $key, 'route' => $route, 'pattern' => $pattern, 'option' => $option]); Request::instance()->route(['rule' => $key, 'route' => $route, 'pattern' => $pattern, 'option' => $option]);
return $result; return $result;
} }
} }
@@ -431,7 +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(['rule' => $rule, 'route' => $route, 'pattern' => $pattern, 'option' => $option]); Request::instance()->route(['rule' => $rule, 'route' => $route, 'pattern' => $pattern, 'option' => $option]);
return $result; return $result;
} }
} }