修正Query类 改进Response类 改进异常输出

This commit is contained in:
thinkphp
2016-05-16 16:39:17 +08:00
parent 30a6bd7e8a
commit a4a14b3bf1
9 changed files with 76 additions and 42 deletions

View File

@@ -21,8 +21,10 @@ class Response
protected $data;
// 是否exit
protected $isExit = false;
// contentType
protected $contentType = [
// 当前的contentType
protected $contentType;
// 可用的输出类型
protected static $contentTypes = [
'json' => 'application/json',
'xml' => 'text/xml',
'html' => 'text/html',
@@ -57,12 +59,36 @@ class Response
{
$type = strtolower($type ?: (IS_AJAX ? 'json' : 'html'));
if (!isset(self::$instance[$type])) {
self::$instance[$type] = new static($options);
self::$instance[$type]->type($type, $options);
$class = '\\think\\response\\' . ucfirst($type);
$response = class_exists($class) ? new $class($options) : new static($options);
if (isset(self::$contentTypes[$type])) {
$response->contentType(self::$contentTypes[$type]);
}
self::$instance[$type] = $response;
}
return self::$instance[$type];
}
/**
* 输出类型设置
* @access public
* @param string $type 输出内容的格式类型
* @param array $options 参数
* @return $this
*/
public function type($type, $options = [])
{
$type = strtolower($type);
if (isset(self::$instance[$type])) {
return self::$instance[$type];
} else {
return self::create($type, $options);
}
}
/**
* 发送数据到客户端
* @access public
@@ -73,6 +99,10 @@ class Response
{
$data = $data ?: $this->data;
if (isset($this->contentType)) {
$this->contentType($this->contentType);
}
if (is_callable($this->transform)) {
$data = call_user_func_array($this->transform, [$data]);
}
@@ -146,26 +176,6 @@ class Response
return $this;
}
/**
* 输出类型设置
* @access public
* @param string $type 输出内容的格式类型
* @param array $options 参数
* @return $this
*/
public function type($type, $options = [])
{
$type = strtolower($type);
if (!isset(self::$instance[$type])) {
$class = '\\think\\response\\' . ucfirst($type);
self::$instance[$type] = class_exists($class) ? new $class($options) : $this;
}
if (isset($this->contentType[$type])) {
self::$instance[$type]->contentType($this->contentType[$type]);
}
return self::$instance[$type];
}
/**
* 输出是否exit设置
* @access public