diff --git a/library/think/App.php b/library/think/App.php index bb4dfae1..6b911fdb 100644 --- a/library/think/App.php +++ b/library/think/App.php @@ -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); } } diff --git a/library/think/Response.php b/library/think/Response.php index c9c479b3..0024e0af 100644 --- a/library/think/Response.php +++ b/library/think/Response.php @@ -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']; + } } diff --git a/start.php b/start.php index be2b2ba9..036716bc 100644 --- a/start.php +++ b/start.php @@ -64,5 +64,8 @@ if (isset($mode['tags'])) { // 是否自动运行 if (APP_AUTO_RUN) { - App::run(); + $response = App::run(); + if($response instanceof Response){ + $response->send(); + } } diff --git a/tests/thinkphp/library/think/appTest.php b/tests/thinkphp/library/think/appTest.php index d959ff83..0f9173ac 100644 --- a/tests/thinkphp/library/think/appTest.php +++ b/tests/thinkphp/library/think/appTest.php @@ -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 = '