Commit 9806563f by Qiang Xue

Minor refactoring.

parent 3fc19008
...@@ -137,7 +137,6 @@ class Application extends Module ...@@ -137,7 +137,6 @@ class Application extends Module
// Allocating twice more than required to display memory exhausted error // Allocating twice more than required to display memory exhausted error
// in case of trying to allocate last 1 byte while all memory is taken. // in case of trying to allocate last 1 byte while all memory is taken.
$this->_memoryReserve = str_repeat('x', 1024 * 256); $this->_memoryReserve = str_repeat('x', 1024 * 256);
register_shutdown_function(array($this, 'end'), 0, false);
register_shutdown_function(array($this, 'handleFatalError')); register_shutdown_function(array($this, 'handleFatalError'));
} }
} }
...@@ -172,6 +171,7 @@ class Application extends Module ...@@ -172,6 +171,7 @@ class Application extends Module
$this->beforeRun(); $this->beforeRun();
$response = $this->getResponse(); $response = $this->getResponse();
$response->begin(); $response->begin();
register_shutdown_function(array($this, 'end'), 0, false);
$status = $this->processRequest(); $status = $this->processRequest();
$response->end(); $response->end();
$this->afterRun(); $this->afterRun();
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
namespace yii\base; namespace yii\base;
use yii\web\Response;
/** /**
* HttpException represents an exception caused by an improper request of the end-user. * HttpException represents an exception caused by an improper request of the end-user.
...@@ -43,8 +44,8 @@ class HttpException extends UserException ...@@ -43,8 +44,8 @@ class HttpException extends UserException
*/ */
public function getName() public function getName()
{ {
if (isset(\yii\web\Response::$statusTexts[$this->statusCode])) { if (isset(Response::$httpStatuses[$this->statusCode])) {
return \yii\web\Response::$statusTexts[$this->statusCode]; return Response::$httpStatuses[$this->statusCode];
} else { } else {
return 'Error'; return 'Error';
} }
......
...@@ -13,23 +13,29 @@ namespace yii\base; ...@@ -13,23 +13,29 @@ namespace yii\base;
*/ */
class Response extends Component class Response extends Component
{ {
/**
* @event Event an event raised when the application begins to generate the response.
*/
const EVENT_BEGIN_RESPONSE = 'beginResponse'; const EVENT_BEGIN_RESPONSE = 'beginResponse';
/**
* @event Event an event raised when the generation of the response finishes.
*/
const EVENT_END_RESPONSE = 'endResponse'; const EVENT_END_RESPONSE = 'endResponse';
/** /**
* Starts output buffering * Starts output buffering
*/ */
public function beginOutput() public function beginBuffer()
{ {
ob_start(); ob_start();
ob_implicit_flush(false); ob_implicit_flush(false);
} }
/** /**
* Returns contents of the output buffer and discards it * Returns contents of the output buffer and stops the buffer.
* @return string output buffer contents * @return string output buffer contents
*/ */
public function endOutput() public function endBuffer()
{ {
return ob_get_clean(); return ob_get_clean();
} }
...@@ -38,16 +44,16 @@ class Response extends Component ...@@ -38,16 +44,16 @@ class Response extends Component
* Returns contents of the output buffer * Returns contents of the output buffer
* @return string output buffer contents * @return string output buffer contents
*/ */
public function getOutput() public function getBuffer()
{ {
return ob_get_contents(); return ob_get_contents();
} }
/** /**
* Discards the output buffer * Discards the output buffer
* @param boolean $all if true recursively discards all output buffers used * @param boolean $all if true, it will discards all output buffers.
*/ */
public function cleanOutput($all = true) public function cleanBuffer($all = true)
{ {
if ($all) { if ($all) {
for ($level = ob_get_level(); $level > 0; --$level) { for ($level = ob_get_level(); $level > 0; --$level) {
...@@ -60,11 +66,25 @@ class Response extends Component ...@@ -60,11 +66,25 @@ class Response extends Component
} }
} }
/**
* Begins generating the response.
* This method is called at the beginning of [[Application::run()]].
* The default implementation will trigger the [[EVENT_BEGIN_RESPONSE]] event.
* If you overwrite this method, make sure you call the parent implementation so that
* the event can be triggered.
*/
public function begin() public function begin()
{ {
$this->trigger(self::EVENT_BEGIN_RESPONSE); $this->trigger(self::EVENT_BEGIN_RESPONSE);
} }
/**
* Ends generating the response.
* This method is called at the end of [[Application::run()]].
* The default implementation will trigger the [[EVENT_END_RESPONSE]] event.
* If you overwrite this method, make sure you call the parent implementation so that
* the event can be triggered.
*/
public function end() public function end()
{ {
$this->trigger(self::EVENT_END_RESPONSE); $this->trigger(self::EVENT_END_RESPONSE);
......
...@@ -46,11 +46,10 @@ class Response extends \yii\base\Response ...@@ -46,11 +46,10 @@ class Response extends \yii\base\Response
* @var string the version of the HTTP protocol to use * @var string the version of the HTTP protocol to use
*/ */
public $version = '1.0'; public $version = '1.0';
/** /**
* @var array list of HTTP status codes and the corresponding texts * @var array list of HTTP status codes and the corresponding texts
*/ */
public static $statusTexts = array( public static $httpStatuses = array(
100 => 'Continue', 100 => 'Continue',
101 => 'Switching Protocols', 101 => 'Switching Protocols',
102 => 'Processing', 102 => 'Processing',
...@@ -135,12 +134,12 @@ class Response extends \yii\base\Response ...@@ -135,12 +134,12 @@ class Response extends \yii\base\Response
public function begin() public function begin()
{ {
parent::begin(); parent::begin();
$this->beginOutput(); $this->beginBuffer();
} }
public function end() public function end()
{ {
$this->content .= $this->endOutput(); $this->content .= $this->endBuffer();
$this->send(); $this->send();
parent::end(); parent::end();
} }
...@@ -157,7 +156,7 @@ class Response extends \yii\base\Response ...@@ -157,7 +156,7 @@ class Response extends \yii\base\Response
throw new InvalidParamException("The HTTP status code is invalid: $value"); throw new InvalidParamException("The HTTP status code is invalid: $value");
} }
if ($text === null) { if ($text === null) {
$this->statusText = isset(self::$statusTexts[$this->_statusCode]) ? self::$statusTexts[$this->_statusCode] : ''; $this->statusText = isset(self::$httpStatuses[$this->_statusCode]) ? self::$httpStatuses[$this->_statusCode] : '';
} else { } else {
$this->statusText = $text; $this->statusText = $text;
} }
...@@ -178,15 +177,17 @@ class Response extends \yii\base\Response ...@@ -178,15 +177,17 @@ class Response extends \yii\base\Response
public function renderJson($data) public function renderJson($data)
{ {
$this->getHeaders()->set('content-type', 'application/json'); $this->getHeaders()->set('Content-Type', 'application/json');
$this->content = Json::encode($data); $this->content = Json::encode($data);
$this->send();
} }
public function renderJsonp($data, $callbackName) public function renderJsonp($data, $callbackName)
{ {
$this->getHeaders()->set('content-type', 'text/javascript'); $this->getHeaders()->set('Content-Type', 'text/javascript');
$data = Json::encode($data); $data = Json::encode($data);
$this->content = "$callbackName($data);"; $this->content = "$callbackName($data);";
$this->send();
} }
/** /**
...@@ -197,6 +198,17 @@ class Response extends \yii\base\Response ...@@ -197,6 +198,17 @@ class Response extends \yii\base\Response
{ {
$this->sendHeaders(); $this->sendHeaders();
$this->sendContent(); $this->sendContent();
if (function_exists('fastcgi_finish_request')) {
fastcgi_finish_request();
} else {
for ($level = ob_get_level(); $level > 0; --$level) {
if (!@ob_end_flush()) {
ob_clean();
}
}
flush();
}
} }
public function reset() public function reset()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment