From 5618c367bfb2a6947c6aec2c274434d32ceffc62 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 20 Jun 2016 11:44:46 +0800 Subject: [PATCH] =?UTF-8?q?request=E7=B1=BB=E5=A2=9E=E5=8A=A0=E5=8A=A8?= =?UTF-8?q?=E6=80=81=E6=96=B9=E6=B3=95=E7=9A=84=E6=B3=A8=E5=85=A5hook?= =?UTF-8?q?=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Request.php | 83 +++++++++++++-------------------------- 1 file changed, 28 insertions(+), 55 deletions(-) diff --git a/library/think/Request.php b/library/think/Request.php index 83ea17e8..e1d64af9 100644 --- a/library/think/Request.php +++ b/library/think/Request.php @@ -14,6 +14,7 @@ namespace think; use think\Config; use think\File; use think\Session; +use think\Exception; class Request { @@ -110,6 +111,8 @@ class Request // 全局过滤规则 protected $filter; + // Hook扩展方法 + protected static $hook = []; /** * 架构函数 @@ -125,6 +128,31 @@ class Request } } + public function __call($method, $args) + { + if (array_key_exists($method, self::$hook)) { + call_user_func_array(self::$hook[$method], $args); + } else { + throw new Exception('method not exists:' . __CLASS__ . '->' . $method); + } + } + + /** + * Hook 方法注入 + * @access public + * @param string|array $method 方法名 + * @param mixed $callback callable + * @return void + */ + public static function hook($method, $callback = null) + { + if (is_array($method)) { + self::$hook = array_merge(self::$hook, $method); + } else { + self::$hook[$method] = $callback; + } + } + /** * 初始化 * @access public @@ -1184,61 +1212,6 @@ class Request } } - /** - * 获取请求的user agent信息 - * @access public - * @return string - */ - public function agent() - { - return $this->server('HTTP_USER_AGENT'); - } - - /** - * 获取请求的accept encoding - * @access public - * @return string - */ - public function encode() - { - return $this->server('HTTP_ACCEPT_ENCODING'); - } - - /** - * 获取请求的accept language - * @access public - * @return string - */ - public function language() - { - return $this->server('HTTP_ACCEPT_LANGUAGE'); - } - - /** - * 获取请求的cache control - * @access public - * @return string - */ - public function cache() - { - return $this->server('HTTP_CACHE_CONTROL'); - } - - /** - * 获取当前请求的content type - * @access public - * @return string - */ - public function getContentType() - { - if (isset($_SERVER["CONTENT_TYPE"])) { - return $_SERVER["CONTENT_TYPE"]; - } elseif (isset($_SERVER["HTTP_CONTENT_TYPE"])) { - return $_SERVER["HTTP_CONTENT_TYPE"]; - } - return null; - } - /** * 当前URL地址中的scheme参数 * @access public