Response类增加getCode方法和getContent方法 send方法data参数去掉 App类的run方法返回response对象

This commit is contained in:
thinkphp
2016-06-13 16:45:39 +08:00
parent a9cfe2a801
commit 4689c86e0a
4 changed files with 49 additions and 15 deletions

View File

@@ -123,12 +123,12 @@ class App
// 输出数据到客户端
if ($data instanceof Response) {
return $data->send();
return $data;
} elseif (!is_null($data)) {
// 默认自动识别响应输出类型
$isAjax = $request->isAjax();
$type = $isAjax ? Config::get('default_ajax_return') : Config::get('default_return_type');
return Response::create($data, $type)->send();
return Response::create($data, $type);
}
}

View File

@@ -20,8 +20,10 @@ class Response
protected static $instance = [];
// 输出数据的转换方法
protected $transform;
// 输出数据
// 原始数据
protected $data;
// 输出数据
protected $content;
// 输出类型
protected $type;
// 当前的contentType
@@ -98,26 +100,21 @@ class Response
/**
* 发送数据到客户端
* @access public
* @param mixed $data 数据
* @return mixed
* @throws \InvalidArgumentException
*/
public function send($data = null)
public function send()
{
$data = !is_null($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]);
}
defined('RESPONSE_TYPE') or define('RESPONSE_TYPE', $this->type);
// 处理输出数据
$data = $this->output($data);
$data = $this->getTransformData();
// 输出数据赋值
$this->content = $data;
// 监听response_data
Hook::listen('response_data', $data, $this);
@@ -147,6 +144,22 @@ class Response
return $data;
}
/**
* 处理数据
* @access protected
* @param mixed $data 要处理的数据
* @return mixed
*/
protected function getTransformData()
{
if (is_callable($this->transform)) {
$data = call_user_func_array($this->transform, [$this->data]);
}else{
$data = $this->data;
}
return $this->output($data);
}
/**
* 处理数据
* @access protected
@@ -319,7 +332,7 @@ class Response
}
/**
* 获取数据
* 获取原始数据
* @return mixed
*/
public function getData()
@@ -327,6 +340,15 @@ class Response
return $this->data;
}
/**
* 获取输出数据
* @return mixed
*/
public function getContent()
{
return $this->content;
}
/**
* 获取输出类型
* @return string
@@ -335,4 +357,13 @@ class Response
{
return $this->type;
}
/**
* 获取状态码
* @return integer
*/
public function getCode()
{
return $this->header['status'];
}
}

View File

@@ -64,5 +64,8 @@ if (isset($mode['tags'])) {
// 是否自动运行
if (APP_AUTO_RUN) {
App::run();
$response = App::run();
if($response instanceof Response){
$response->send();
}
}

View File

@@ -50,7 +50,7 @@ class appTest extends \PHPUnit_Framework_TestCase
{
Config::set('root_namespace', ['/path/']);
App::run(Request::instance());
App::run(Request::instance())->send();
$expectOutputString = '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p> ThinkPHP V5<br/><span style="font-size:30px">十年磨一剑 - 为API开发设计的高性能框架</span></p><span style="font-size:22px;">[ V5.0 版本由 <a href="http://www.qiniu.com" target="qiniu">七牛云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_bd568ce7058a1091"></thinkad>';
$this->expectOutputString($expectOutputString);