改进Request类支持静态调用

This commit is contained in:
thinkphp
2016-05-26 13:06:11 +08:00
parent 8ccb3f1162
commit d6c74d3081

View File

@@ -195,7 +195,7 @@ class Request
* @param string $url URL地址 * @param string $url URL地址
* @return string * @return string
*/ */
public function domain($domain = '') protected function domain($domain = '')
{ {
if (!empty($domain)) { if (!empty($domain)) {
$this->domain = $domain; $this->domain = $domain;
@@ -213,7 +213,7 @@ class Request
* @param bool $domain 是否需要域名 * @param bool $domain 是否需要域名
* @return string * @return string
*/ */
public function url($url = '') protected function url($url = '')
{ {
if (is_string($url) && !empty($url)) { if (is_string($url) && !empty($url)) {
$this->url = $url; $this->url = $url;
@@ -234,7 +234,7 @@ class Request
* @param string $url URL地址 * @param string $url URL地址
* @return string * @return string
*/ */
public function baseUrl($url = '') protected function baseUrl($url = '')
{ {
if (is_string($url) && !empty($url)) { if (is_string($url) && !empty($url)) {
$this->baseUrl = $url; $this->baseUrl = $url;
@@ -252,7 +252,7 @@ class Request
* @param string $file 当前执行的文件 * @param string $file 当前执行的文件
* @return string * @return string
*/ */
public function baseFile($file = '') protected function baseFile($file = '')
{ {
if (is_string($file) && !empty($file)) { if (is_string($file) && !empty($file)) {
$this->baseFile = $file; $this->baseFile = $file;
@@ -284,7 +284,7 @@ class Request
* @param string $url URL地址 * @param string $url URL地址
* @return string * @return string
*/ */
public function root($url = '') protected function root($url = '')
{ {
if (is_string($url) && !empty($url)) { if (is_string($url) && !empty($url)) {
$this->root = $url; $this->root = $url;
@@ -304,7 +304,7 @@ class Request
* @access public * @access public
* @return string * @return string
*/ */
public function pathinfo() protected function pathinfo()
{ {
if (is_null($this->pathinfo)) { if (is_null($this->pathinfo)) {
if (isset($_GET[Config::get('var_pathinfo')])) { if (isset($_GET[Config::get('var_pathinfo')])) {
@@ -336,7 +336,7 @@ class Request
* @access public * @access public
* @return string * @return string
*/ */
public function path() protected function path()
{ {
if (is_null($this->path)) { if (is_null($this->path)) {
// 去除正常的URL后缀 // 去除正常的URL后缀
@@ -350,7 +350,7 @@ class Request
* @access public * @access public
* @return string * @return string
*/ */
public function ext() protected function ext()
{ {
return pathinfo($this->pathinfo(), PATHINFO_EXTENSION); return pathinfo($this->pathinfo(), PATHINFO_EXTENSION);
} }
@@ -361,7 +361,7 @@ class Request
* @param bool $float 是否使用浮点类型 * @param bool $float 是否使用浮点类型
* @return integer|float * @return integer|float
*/ */
public function time($float = false) protected function time($float = false)
{ {
return $float ? $_SERVER['REQUEST_TIME_FLOAT'] : $_SERVER['REQUEST_TIME']; return $float ? $_SERVER['REQUEST_TIME_FLOAT'] : $_SERVER['REQUEST_TIME'];
} }
@@ -371,7 +371,7 @@ class Request
* @access public * @access public
* @return false|string * @return false|string
*/ */
public function type() protected function type()
{ {
$accept = isset($this->server['HTTP_ACCEPT']) ? $this->server['HTTP_ACCEPT'] : $_SERVER['HTTP_ACCEPT']; $accept = isset($this->server['HTTP_ACCEPT']) ? $this->server['HTTP_ACCEPT'] : $_SERVER['HTTP_ACCEPT'];
if (empty($accept)) { if (empty($accept)) {
@@ -396,7 +396,7 @@ class Request
* @param string $val 资源类型 * @param string $val 资源类型
* @return void * @return void
*/ */
public function mimeType($type, $val = '') protected function mimeType($type, $val = '')
{ {
if (is_array($type)) { if (is_array($type)) {
$this->mimeType = array_merge($this->mimeType, $type); $this->mimeType = array_merge($this->mimeType, $type);
@@ -411,7 +411,7 @@ class Request
* @param string $method 请求类型 * @param string $method 请求类型
* @return string * @return string
*/ */
public function method($method = '') protected function method($method = '')
{ {
if ($method) { if ($method) {
$this->method = $method; $this->method = $method;
@@ -427,7 +427,7 @@ class Request
* @access public * @access public
* @return bool * @return bool
*/ */
public function isGet() protected function isGet()
{ {
return $this->method() == 'GET'; return $this->method() == 'GET';
} }
@@ -437,7 +437,7 @@ class Request
* @access public * @access public
* @return bool * @return bool
*/ */
public function isPost() protected function isPost()
{ {
return $this->method() == 'POST'; return $this->method() == 'POST';
} }
@@ -447,7 +447,7 @@ class Request
* @access public * @access public
* @return bool * @return bool
*/ */
public function isPut() protected function isPut()
{ {
return $this->method() == 'PUT'; return $this->method() == 'PUT';
} }
@@ -457,7 +457,7 @@ class Request
* @access public * @access public
* @return bool * @return bool
*/ */
public function isDelete() protected function isDelete()
{ {
return $this->method() == 'DELETE'; return $this->method() == 'DELETE';
} }
@@ -467,7 +467,7 @@ class Request
* @access public * @access public
* @return bool * @return bool
*/ */
public function isCli() protected function isCli()
{ {
return PHP_SAPI == 'cli'; return PHP_SAPI == 'cli';
} }
@@ -477,7 +477,7 @@ class Request
* @access public * @access public
* @return bool * @return bool
*/ */
public function isCgi() protected function isCgi()
{ {
return strpos(PHP_SAPI, 'cgi') === 0; return strpos(PHP_SAPI, 'cgi') === 0;
} }
@@ -489,7 +489,7 @@ class Request
* @param mixed $default 默认值 * @param mixed $default 默认值
* @return mixed * @return mixed
*/ */
public function param($name = '', $default = null) protected function param($name = '', $default = null)
{ {
if (empty($this->param)) { if (empty($this->param)) {
$method = $this->method(); $method = $this->method();
@@ -524,7 +524,7 @@ class Request
* @param bool $checkEmpty 是否检测空值 * @param bool $checkEmpty 是否检测空值
* @return mixed * @return mixed
*/ */
public function has($name, $checkEmpty = false) protected function has($name, $checkEmpty = false)
{ {
if (empty($this->param)) { if (empty($this->param)) {
$param = $this->param(); $param = $this->param();
@@ -544,7 +544,7 @@ class Request
* @param string|array $name 变量名 * @param string|array $name 变量名
* @return mixed * @return mixed
*/ */
public function only($name) protected function only($name)
{ {
$param = $this->param(); $param = $this->param();
if (is_string($name)) { if (is_string($name)) {
@@ -565,7 +565,7 @@ class Request
* @param string|array $name 变量名 * @param string|array $name 变量名
* @return mixed * @return mixed
*/ */
public function except($name) protected function except($name)
{ {
$param = $this->param(); $param = $this->param();
if (is_string($name)) { if (is_string($name)) {
@@ -585,7 +585,7 @@ class Request
* @param string $name 变量名 * @param string $name 变量名
* @return mixed * @return mixed
*/ */
public function session($name = '') protected function session($name = '')
{ {
if (PHP_SESSION_DISABLED == session_status()) { if (PHP_SESSION_DISABLED == session_status()) {
session_start(); session_start();
@@ -599,7 +599,7 @@ class Request
* @param string $name 变量名 * @param string $name 变量名
* @return mixed * @return mixed
*/ */
public function cookie($name = '') protected function cookie($name = '')
{ {
return Input::data($this->cookie ?: $_COOKIE, $name); return Input::data($this->cookie ?: $_COOKIE, $name);
} }
@@ -610,7 +610,7 @@ class Request
* @param string $name 变量名 * @param string $name 变量名
* @return mixed * @return mixed
*/ */
public function server($name = '') protected function server($name = '')
{ {
return Input::data($this->server ?: $_SERVER, $name); return Input::data($this->server ?: $_SERVER, $name);
} }
@@ -621,7 +621,7 @@ class Request
* @param string $name 名称 * @param string $name 名称
* @return null|array|\think\File * @return null|array|\think\File
*/ */
public function file($name = '') protected function file($name = '')
{ {
return Input::file($name, $this->file ?: $_FILES); return Input::file($name, $this->file ?: $_FILES);
} }
@@ -631,7 +631,7 @@ class Request
* @access public * @access public
* @return bool * @return bool
*/ */
public function isSsl() protected function isSsl()
{ {
$server = array_merge($_SERVER, $this->server); $server = array_merge($_SERVER, $this->server);
if (isset($server['HTTPS']) && ('1' == $server['HTTPS'] || 'on' == strtolower($server['HTTPS']))) { if (isset($server['HTTPS']) && ('1' == $server['HTTPS'] || 'on' == strtolower($server['HTTPS']))) {
@@ -645,22 +645,32 @@ class Request
} }
/** /**
* 当前是否ajax请求 * 当前是否Ajax请求
* @access public * @access public
* @return bool * @return bool
*/ */
public function isAjax() protected 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;
} }
/**
* 当前是否Pjax请求
* @access public
* @return bool
*/
protected function isPjax()
{
return (isset($_SERVER['HTTP_X_PJAX']) && $_SERVER['HTTP_X_PJAX']) ? true : false;
}
/** /**
* 获取客户端IP地址 * 获取客户端IP地址
* @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字 * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
* @param boolean $adv 是否进行高级模式获取(有可能被伪装) * @param boolean $adv 是否进行高级模式获取(有可能被伪装)
* @return mixed * @return mixed
*/ */
public function ip($type = 0, $adv = false) protected function ip($type = 0, $adv = false)
{ {
$type = $type ? 1 : 0; $type = $type ? 1 : 0;
static $ip = null; static $ip = null;
@@ -696,7 +706,7 @@ class Request
* @access public * @access public
* @return string * @return string
*/ */
public function scheme() protected function scheme()
{ {
return $this->isSsl() ? 'https' : 'http'; return $this->isSsl() ? 'https' : 'http';
} }
@@ -706,7 +716,7 @@ class Request
* @access public * @access public
* @return string * @return string
*/ */
public function query() protected function query()
{ {
return $this->server('QUERY_STRING'); return $this->server('QUERY_STRING');
} }
@@ -716,7 +726,7 @@ class Request
* @access public * @access public
* @return string * @return string
*/ */
public function host() protected function host()
{ {
return $this->server('HTTP_HOST'); return $this->server('HTTP_HOST');
} }
@@ -726,7 +736,7 @@ class Request
* @access public * @access public
* @return integer * @return integer
*/ */
public function port() protected function port()
{ {
return $this->server('SERVER_PORT'); return $this->server('SERVER_PORT');
} }
@@ -736,7 +746,7 @@ class Request
* @access public * @access public
* @return integer * @return integer
*/ */
public function protocol() protected function protocol()
{ {
return $this->server('SERVER_PROTOCOL'); return $this->server('SERVER_PROTOCOL');
} }
@@ -746,7 +756,7 @@ class Request
* @access public * @access public
* @return integer * @return integer
*/ */
public function remotePort() protected function remotePort()
{ {
return $this->server('REMOTE_PORT'); return $this->server('REMOTE_PORT');
} }
@@ -757,7 +767,7 @@ class Request
* @param array $route 路由名称 * @param array $route 路由名称
* @return array * @return array
*/ */
public function route($route = []) protected function route($route = [])
{ {
if (!empty($route)) { if (!empty($route)) {
$this->route = $route; $this->route = $route;
@@ -772,7 +782,7 @@ class Request
* @param array $dispatch 调度信息 * @param array $dispatch 调度信息
* @return array * @return array
*/ */
public function dispatch($dispatch = []) protected function dispatch($dispatch = [])
{ {
if (!empty($dispatch)) { if (!empty($dispatch)) {
$this->dispatch = $dispatch; $this->dispatch = $dispatch;
@@ -780,4 +790,18 @@ class Request
return $this->dispatch; return $this->dispatch;
} }
public static function __callStatic($method, $params)
{
if (is_null(self::$instance)) {
self::instance();
}
return call_user_func_array([self::$instance, $method], $params);
}
public function __call($method, $params)
{
if (method_exists($this, $method)) {
return call_user_func_array([$this, $method], $params);
}
}
} }