From 7f385217b978274be667e98e876ef67ebd89a195 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 11 May 2016 14:04:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9Bresponse=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helper.php | 10 +- library/think/App.php | 2 +- library/think/Error.php | 2 +- library/think/Response.php | 149 ++++++++---------- library/think/controller/Rest.php | 6 +- library/traits/controller/Jump.php | 8 +- tests/thinkphp/library/think/responseTest.php | 36 +++-- 7 files changed, 94 insertions(+), 119 deletions(-) diff --git a/helper.php b/helper.php index 726e3ae0..4806a0e5 100644 --- a/helper.php +++ b/helper.php @@ -371,12 +371,10 @@ function request() } /** - * 设置Response输出 - * @param string $name 方法 - * @param mixed $param 参数 - * @return mixed + * 获取当前的Response对象实例 + * @return \think\Response */ -function response($name, $param = '') +function response() { - return Response::$name($param); + return Response::instance(); } diff --git a/library/think/App.php b/library/think/App.php index 407980d9..1d747be6 100644 --- a/library/think/App.php +++ b/library/think/App.php @@ -111,7 +111,7 @@ class App // 监听app_end APP_HOOK && Hook::listen('app_end', $data); // 自动响应输出 - return Response::send($data, '', Config::get('response_return')); + return Response::instance()->send($data, '', Config::get('response_return')); } } } diff --git a/library/think/Error.php b/library/think/Error.php index eea83677..9b760e13 100644 --- a/library/think/Error.php +++ b/library/think/Error.php @@ -154,7 +154,7 @@ class Error // 异常信息输出监听 APP_HOOK && Hook::listen('error_output', $data); // 输出异常内容 - Response::send($data, $type, Config::get('response_return')); + Response::instance()->send($data, $type, Config::get('response_return')); } else { //ob_end_clean(); extract($data); diff --git a/library/think/Response.php b/library/think/Response.php index 45eaf327..55e450ae 100644 --- a/library/think/Response.php +++ b/library/think/Response.php @@ -16,17 +16,18 @@ use think\Url; class Response { + protected static $instance; // 输出数据的转换方法 - protected static $transform = null; + protected $transform = null; // 输出数据 - protected static $data = ''; + protected $data = ''; // 是否exit - protected static $isExit = false; + protected $isExit = false; // 输出类型 - protected static $type = ''; + protected $type = ''; // contentType - protected static $contentType = [ + protected $contentType = [ 'json' => 'application/json', 'xml' => 'text/xml', 'html' => 'text/html', @@ -34,56 +35,30 @@ class Response 'script' => 'application/javascript', 'text' => 'text/plain', ]; - // HTTP status - protected static $code = [ - // Informational 1xx - 100 => 'Continue', - 101 => 'Switching Protocols', - // Success 2xx - 200 => 'OK', - 201 => 'Created', - 202 => 'Accepted', - 203 => 'Non-Authoritative Information', - 204 => 'No Content', - 205 => 'Reset Content', - 206 => 'Partial Content', - // Redirection 3xx - 300 => 'Multiple Choices', - 301 => 'Moved Permanently', - 302 => 'Moved Temporarily ', // 1.1 - 303 => 'See Other', - 304 => 'Not Modified', - 305 => 'Use Proxy', - // 306 is deprecated but reserved - 307 => 'Temporary Redirect', - // Client Error 4xx - 400 => 'Bad Request', - 401 => 'Unauthorized', - 402 => 'Payment Required', - 403 => 'Forbidden', - 404 => 'Not Found', - 405 => 'Method Not Allowed', - 406 => 'Not Acceptable', - 407 => 'Proxy Authentication Required', - 408 => 'Request Timeout', - 409 => 'Conflict', - 410 => 'Gone', - 411 => 'Length Required', - 412 => 'Precondition Failed', - 413 => 'Request Entity Too Large', - 414 => 'Request-URI Too Long', - 415 => 'Unsupported Media Type', - 416 => 'Requested Range Not Satisfiable', - 417 => 'Expectation Failed', - // Server Error 5xx - 500 => 'Internal Server Error', - 501 => 'Not Implemented', - 502 => 'Bad Gateway', - 503 => 'Service Unavailable', - 504 => 'Gateway Timeout', - 505 => 'HTTP Version Not Supported', - 509 => 'Bandwidth Limit Exceeded', - ]; + + /** + * 架构函数 + * @access public + * @param array $options 参数 + */ + public function __construct($type = '') + { + $this->type = $type; + } + + /** + * 初始化 + * @access public + * @param string $type 输出类型 + * @return \think\Request + */ + public static function instance($type = '') + { + if (is_null(self::$instance)) { + self::$instance = new static($type); + } + return self::$instance; + } /** * 发送数据到客户端 @@ -93,20 +68,20 @@ class Response * @param bool $return 是否返回数据 * @return mixed */ - public static function send($data = [], $type = '', $return = false) + public function send($data = [], $type = '', $return = false) { if ('' == $type) { - $type = self::$type ?: (IS_AJAX ? Config::get('default_ajax_return') : Config::get('default_return_type')); + $type = $this->type ?: (IS_AJAX ? Config::get('default_ajax_return') : Config::get('default_return_type')); } $type = strtolower($type); - $data = $data ?: self::$data; + $data = $data ?: $this->data; - if (!headers_sent() && isset(self::$contentType[$type])) { - header('Content-Type:' . self::$contentType[$type] . '; charset=utf-8'); + if (!headers_sent() && isset($this->contentType[$type])) { + header('Content-Type:' . $this->contentType[$type] . '; charset=utf-8'); } - if (is_callable(self::$transform)) { - $data = call_user_func_array(self::$transform, [$data]); + if (is_callable($this->transform)) { + $data = call_user_func_array($this->transform, [$data]); } else { switch ($type) { case 'json': @@ -128,7 +103,7 @@ class Response } echo $data; - self::isExit() && exit(); + $this->isExit() && exit(); } /** @@ -137,9 +112,10 @@ class Response * @param mixed $callback 调用的转换方法 * @return $this */ - public static function transform($callback) + public function transform($callback) { - self::$transform = $callback; + $this->transform = $callback; + return $this; } /** @@ -148,34 +124,37 @@ class Response * @param mixed $data 输出数据 * @return $this */ - public static function data($data) + public function data($data) { - self::$data = $data; + $this->data = $data; + return $this; } /** * 输出类型设置 * @access public * @param string $type 输出内容的格式类型 - * @return mixed + * @return $this */ - public static function type($type) + public function type($type) { - self::$type = $type; + $this->type = $type; + return $this; } /** * 输出是否exit设置 * @access public * @param bool $exit 是否退出 - * @return mixed + * @return $this */ - public static function isExit($exit = null) + public function isExit($exit = null) { if (is_null($exit)) { - return self::$isExit; + return $this->isExit; } - self::$isExit = (boolean) $exit; + $this->isExit = (boolean) $exit; + return $this; } /** @@ -186,7 +165,7 @@ class Response * @param string $msg 提示信息 * @return mixed */ - public static function result($data, $code = 0, $msg = '', $type = '') + public function result($data, $code = 0, $msg = '', $type = '') { $result = [ 'code' => $code, @@ -194,7 +173,7 @@ class Response 'time' => NOW_TIME, 'data' => $data, ]; - self::$type = $type; + $this->type = $type; return $result; } @@ -205,7 +184,7 @@ class Response * @param array|int $params 其它URL参数或http code * @return void */ - public static function redirect($url, $params = []) + public function redirect($url, $params = []) { $http_response_code = 301; if (is_int($params) && in_array($params, [301, 302])) { @@ -221,24 +200,22 @@ class Response * @access public * @param string $name 参数名 * @param string $value 参数值 - * @return void + * @return $this */ - public static function header($name, $value) + public function header($name, $value) { header($name . ':' . $value); + return $this; } /** * 发送HTTP状态 * @param integer $code 状态码 - * @return void + * @return $this */ - public static function code($code) + public function code($code) { - if (isset(self::$statusCode[$code])) { - header('HTTP/1.1 ' . $code . ' ' . self::$statusCode[$code]); - // 确保FastCGI模式下正常 - header('Status:' . $code . ' ' . self::$statusCode[$code]); - } + http_response_code($code); + return $this; } } diff --git a/library/think/controller/Rest.php b/library/think/controller/Rest.php index 33372e1b..1608ff94 100644 --- a/library/think/controller/Rest.php +++ b/library/think/controller/Rest.php @@ -92,11 +92,7 @@ abstract class Rest */ protected function response($data, $type = '', $code = 200) { - http_response_code($code); - Response::data($data); - if ($type) { - Response::type($type); - } + return Response::instance()->data($data)->type($type)->code($code); } /** diff --git a/library/traits/controller/Jump.php b/library/traits/controller/Jump.php index c6a3a243..cf54966e 100644 --- a/library/traits/controller/Jump.php +++ b/library/traits/controller/Jump.php @@ -50,7 +50,7 @@ trait Jump $result = View::instance(Config::get('template'), Config::get('view_replace_str')) ->fetch(Config::get('dispatch_success_tmpl'), $result); } - Response::send($result, $type); + Response::instance()->send($result, $type); } /** @@ -83,7 +83,7 @@ trait Jump $result = View::instance(Config::get('template'), Config::get('view_replace_str')) ->fetch(Config::get('dispatch_error_tmpl'), $result); } - Response::send($result, $type); + Response::instance()->send($result, $type); } /** @@ -97,7 +97,7 @@ trait Jump */ public function result($data, $code = 0, $msg = '', $type = '') { - return Response::result($data, $code, $msg, $type); + return Response::instance()->result($data, $code, $msg, $type); } /** @@ -109,7 +109,7 @@ trait Jump */ public function redirect($url, $params = []) { - Response::redirect($url, $params); + Response::instance()->redirect($url, $params); } } diff --git a/tests/thinkphp/library/think/responseTest.php b/tests/thinkphp/library/think/responseTest.php index f2b5d6a6..2bba1845 100644 --- a/tests/thinkphp/library/think/responseTest.php +++ b/tests/thinkphp/library/think/responseTest.php @@ -70,7 +70,7 @@ class responseTest extends \PHPUnit_Framework_TestCase { Config::set('default_ajax_return', $this->default_ajax_return); Config::set('default_return_type', $this->default_return_type); - Response::type(Config::get('default_return_type')); // 会影响其他测试 + Response::instance()->type(Config::get('default_return_type')); // 会影响其他测试 } /** @@ -83,23 +83,24 @@ class responseTest extends \PHPUnit_Framework_TestCase $dataArr["key"] = "value"; //$dataArr->key = "val"; - $result = Response::send($dataArr, "", true); + $response = Response::instance(); + $result = $response->send($dataArr, "", true); $this->assertArrayHasKey("key", $result); - $result = Response::send($dataArr, "json", true); + $result = $response->send($dataArr, "json", true); $this->assertEquals('{"key":"value"}', $result); $handler = "callback"; $_GET[Config::get('var_jsonp_handler')] = $handler; - $result = Response::send($dataArr, "jsonp", true); + $result = $response->send($dataArr, "jsonp", true); $this->assertEquals('callback({"key":"value"});', $result); - Response::transform(function () { + $response->transform(function () { return "callbackreturndata"; }); - $result = Response::send($dataArr, "", true); + $result = $response->send($dataArr, "", true); $this->assertEquals("callbackreturndata", $result); $_GET[Config::get('var_jsonp_handler')] = ""; } @@ -110,15 +111,16 @@ class responseTest extends \PHPUnit_Framework_TestCase */ public function testtransform() { - Response::transform(function () { + $response = Response::instance(); + $response->transform(function () { return "callbackreturndata"; }); $dataArr = []; - $result = Response::send($dataArr, "", true); + $result = $response->send($dataArr, "", true); $this->assertEquals("callbackreturndata", $result); - Response::transform(null); + $response->transform(null); } /** @@ -128,7 +130,7 @@ class responseTest extends \PHPUnit_Framework_TestCase public function testType() { $type = "json"; - Response::type($type); + Response::instance()->type($type); } /** @@ -138,8 +140,9 @@ class responseTest extends \PHPUnit_Framework_TestCase public function testData() { $data = "data"; - Response::data($data); - Response::data(null); + $response = Response::instance(); + $response->data($data); + $response->data(null); } /** @@ -149,11 +152,12 @@ class responseTest extends \PHPUnit_Framework_TestCase public function testIsExit() { $isExit = true; - Response::isExit($isExit); + $response = Response::instance(); + $response->isExit($isExit); - $result = Response::isExit(); + $result = $response->isExit(); $this->assertTrue($isExit, $result); - Response::isExit(false); + $response->isExit(false); } /** @@ -166,7 +170,7 @@ class responseTest extends \PHPUnit_Framework_TestCase $code = "1001"; $msg = "the msg"; $type = "json"; - $result = Response::result($data, $code, $msg, $type); + $result = Response::instance()->result($data, $code, $msg, $type); $this->assertEquals($code, $result["code"]); $this->assertEquals($msg, $result["msg"]);