mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-01 20:52:48 +08:00
改进response类
This commit is contained in:
10
helper.php
10
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();
|
||||
}
|
||||
|
||||
@@ -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'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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"]);
|
||||
|
||||
Reference in New Issue
Block a user