mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-01 12:42:48 +08:00
改进Response类
This commit is contained in:
@@ -11,80 +11,75 @@
|
||||
|
||||
namespace think;
|
||||
|
||||
use think\Hook;
|
||||
use think\response\Json;
|
||||
use think\response\Jsonp;
|
||||
use think\response\Redirect;
|
||||
use think\response\View;
|
||||
use think\response\Xml;
|
||||
|
||||
class Response
|
||||
{
|
||||
// 输出类型的实例化对象
|
||||
protected static $instance = [];
|
||||
// 输出数据的转换方法
|
||||
protected $transform;
|
||||
|
||||
// 原始数据
|
||||
protected $data;
|
||||
// 输出类型
|
||||
protected $type;
|
||||
|
||||
// 当前的contentType
|
||||
protected $contentType;
|
||||
// 可用的输出类型
|
||||
protected $contentTypes = [
|
||||
'json' => 'application/json',
|
||||
'xml' => 'text/xml',
|
||||
'html' => 'text/html',
|
||||
'jsonp' => 'application/javascript',
|
||||
'script' => 'application/javascript',
|
||||
'text' => 'text/plain',
|
||||
];
|
||||
protected $contentType = 'text/html';
|
||||
|
||||
// 字符集
|
||||
protected $charset = 'utf-8';
|
||||
|
||||
//状态
|
||||
protected $code = 200;
|
||||
|
||||
// 输出参数
|
||||
protected $options = [];
|
||||
// header参数
|
||||
protected $header = [];
|
||||
|
||||
protected $content = null;
|
||||
|
||||
/**
|
||||
* 架构函数
|
||||
* @access public
|
||||
* @param mixed $data 输出数据
|
||||
* @param string $type 输出类型
|
||||
* @param array $options 输出参数
|
||||
* @access public
|
||||
* @param mixed $data 输出数据
|
||||
* @param int $code
|
||||
* @param array $header
|
||||
* @param array $options 输出参数
|
||||
*/
|
||||
public function __construct($data = '', $type = '', $options = [])
|
||||
public function __construct($data = '', $code = 200, array $header = [], $options = [])
|
||||
{
|
||||
$this->data = $data;
|
||||
$this->type = empty($type) ? 'null' : strtolower($type);
|
||||
|
||||
if (isset($this->contentTypes[$this->type])) {
|
||||
$this->contentType($this->contentTypes[$this->type]);
|
||||
}
|
||||
|
||||
$this->data($data);
|
||||
$this->header = $header;
|
||||
$this->code = $code;
|
||||
if (!empty($options)) {
|
||||
$this->options = array_merge($this->options, $options);
|
||||
}
|
||||
// 方便获取某个类型的实例
|
||||
self::$instance[$this->type] = $this;
|
||||
$this->contentType($this->contentType, $this->charset);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建Response对象
|
||||
* @access public
|
||||
* @param mixed $data 输出数据
|
||||
* @param string $type 输出类型
|
||||
* @param array $options 输出参数
|
||||
* @return Response
|
||||
* @param mixed $data 输出数据
|
||||
* @param string $type 输出类型
|
||||
* @param int $code
|
||||
* @param array $header
|
||||
* @param array $options 输出参数
|
||||
* @return Response|Json|View|Xml|Redirect|Jsonp
|
||||
*/
|
||||
public static function create($data = '', $type = '', $options = [])
|
||||
public static function create($data = '', $type = '', $code = 200, array $header = [], $options = [])
|
||||
{
|
||||
$type = empty($type) ? 'null' : strtolower($type);
|
||||
|
||||
if (!isset(self::$instance[$type])) {
|
||||
$class = false !== strpos($type, '\\') ? $type : '\\think\\response\\' . ucfirst($type);
|
||||
if (class_exists($class)) {
|
||||
$response = new $class($data, $type, $options);
|
||||
} else {
|
||||
$response = new static($data, $type, $options);
|
||||
}
|
||||
self::$instance[$type] = $response;
|
||||
$class = false !== strpos($type, '\\') ? $type : '\\think\\response\\' . ucfirst($type);
|
||||
if (class_exists($class)) {
|
||||
$response = new $class($data, $code, $header, $options);
|
||||
} else {
|
||||
$response = new static($data, $code, $header, $options);
|
||||
}
|
||||
return self::$instance[$type];
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -95,11 +90,6 @@ class Response
|
||||
*/
|
||||
public function send()
|
||||
{
|
||||
if (isset($this->contentType)) {
|
||||
$this->contentType($this->contentType);
|
||||
}
|
||||
|
||||
defined('RESPONSE_TYPE') or define('RESPONSE_TYPE', $this->type);
|
||||
|
||||
// 处理输出数据
|
||||
$data = $this->getContent();
|
||||
@@ -110,26 +100,20 @@ class Response
|
||||
// 发送头部信息
|
||||
if (!headers_sent() && !empty($this->header)) {
|
||||
// 发送状态码
|
||||
if (isset($this->header['status'])) {
|
||||
http_response_code($this->header['status']);
|
||||
unset($this->header['status']);
|
||||
}
|
||||
http_response_code($this->code);
|
||||
|
||||
foreach ($this->header as $name => $val) {
|
||||
header($name . ':' . $val);
|
||||
}
|
||||
}
|
||||
if (is_scalar($data)) {
|
||||
echo $data;
|
||||
} elseif (!is_null($data)) {
|
||||
throw new \InvalidArgumentException('variable type error:' . gettype($data));
|
||||
}
|
||||
|
||||
echo $data;
|
||||
|
||||
if (function_exists('fastcgi_finish_request')) {
|
||||
// 提高页面响应
|
||||
fastcgi_finish_request();
|
||||
}
|
||||
return $data;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -143,18 +127,6 @@ class Response
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换控制器输出的数据
|
||||
* @access public
|
||||
* @param mixed $callback 调用的转换方法
|
||||
* @return $this
|
||||
*/
|
||||
public function transform($callback)
|
||||
{
|
||||
$this->transform = $callback;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出的参数
|
||||
* @access public
|
||||
@@ -179,11 +151,12 @@ class Response
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 设置响应头
|
||||
* @access public
|
||||
* @param string|array $name 参数名
|
||||
* @param string $value 参数值
|
||||
* @param string|array $name 参数名
|
||||
* @param string $value 参数值
|
||||
* @return $this
|
||||
*/
|
||||
public function header($name, $value = null)
|
||||
@@ -196,17 +169,6 @@ class Response
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送HTTP Location
|
||||
* @param string $url Location地址
|
||||
* @return $this
|
||||
*/
|
||||
public function location($url)
|
||||
{
|
||||
$this->header['Location'] = $url;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送HTTP状态
|
||||
* @param integer $code 状态码
|
||||
@@ -214,7 +176,7 @@ class Response
|
||||
*/
|
||||
public function code($code)
|
||||
{
|
||||
$this->header['status'] = $code;
|
||||
$this->code = $code;
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -242,12 +204,12 @@ class Response
|
||||
|
||||
/**
|
||||
* ETag
|
||||
* @param string $etag
|
||||
* @param string $eTag
|
||||
* @return $this
|
||||
*/
|
||||
public function eTag($etag)
|
||||
public function eTag($eTag)
|
||||
{
|
||||
$this->header['etag'] = $etag;
|
||||
$this->header['ETag'] = $eTag;
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -264,8 +226,8 @@ class Response
|
||||
|
||||
/**
|
||||
* 页面输出类型
|
||||
* @param string $contentType 输出类型
|
||||
* @param string $charset 输出编码
|
||||
* @param string $contentType 输出类型
|
||||
* @param string $charset 输出编码
|
||||
* @return $this
|
||||
*/
|
||||
public function contentType($contentType, $charset = 'utf-8')
|
||||
@@ -299,22 +261,19 @@ class Response
|
||||
*/
|
||||
public function getContent()
|
||||
{
|
||||
if (is_callable($this->transform)) {
|
||||
$data = call_user_func_array($this->transform, [$this->data]);
|
||||
} else {
|
||||
$data = $this->data;
|
||||
$content = $this->output($this->data);
|
||||
|
||||
if (null !== $content && !is_string($content) && !is_numeric($content) && !is_callable([
|
||||
$content,
|
||||
'__toString'
|
||||
])
|
||||
) {
|
||||
throw new \InvalidArgumentException(sprintf('variable type error: %s', gettype($content)));
|
||||
}
|
||||
return $this->output($data);
|
||||
|
||||
return (string)$content;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取输出类型
|
||||
* @return string
|
||||
*/
|
||||
public function getType()
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取状态码
|
||||
@@ -322,6 +281,6 @@ class Response
|
||||
*/
|
||||
public function getCode()
|
||||
{
|
||||
return isset($this->header['status']) ? $this->header['status'] : 200;
|
||||
return $this->code;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user