|
|
@@ -18,6 +18,8 @@ class Response extends \CApplicationComponent
|
|
|
public $filters = [];
|
|
|
protected $_result;
|
|
|
protected $isSent = false;
|
|
|
+ private $_headers = [];
|
|
|
+ private $_statusCode = 200;
|
|
|
|
|
|
public function init()
|
|
|
{
|
|
|
@@ -31,7 +33,12 @@ class Response extends \CApplicationComponent
|
|
|
|
|
|
public function setStatusCode($value)
|
|
|
{
|
|
|
- header('HTTP/1.1 ' . $value);
|
|
|
+ $this->_statusCode = $value;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function setHeader(string $name, string $value)
|
|
|
+ {
|
|
|
+ $this->_headers[strtolower($name)] = new Header($name, $value);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -43,6 +50,7 @@ class Response extends \CApplicationComponent
|
|
|
return;
|
|
|
}
|
|
|
$this->prepare();
|
|
|
+ $this->sendHeaders();
|
|
|
$this->sendContent();
|
|
|
$this->isSent = true;
|
|
|
}
|
|
|
@@ -50,21 +58,23 @@ class Response extends \CApplicationComponent
|
|
|
protected function prepare()
|
|
|
{
|
|
|
if ($this->format === static::FORMAT_JSON) {
|
|
|
- header('Content-type: application/json; charset=utf-8');
|
|
|
+ $this->setHeader('Content-type', 'application/json; charset=utf-8');
|
|
|
$this->_result = \CJSON::encode($this->_result);
|
|
|
}
|
|
|
if ($this->format === static::FORMAT_XML) {
|
|
|
- header('Content-type: application/xml; charset=utf-8');
|
|
|
+ $this->setHeader('Content-type', 'application/xml; charset=utf-8');
|
|
|
}
|
|
|
if ($this->format === static::FORMAT_RAW) {
|
|
|
- header('Content-type: text/plain; charset=utf-8');
|
|
|
+ $this->setHeader('Content-type', 'text/plain; charset=utf-8');
|
|
|
}
|
|
|
|
|
|
- foreach ($this->filters as $filter) {
|
|
|
+ $this->_result = array_reduce($this->filters, function ($previousResult, $filter) {
|
|
|
if ($filter instanceof ResponseFilterInterface) {
|
|
|
- $this->_result = $filter->process($this->_result);
|
|
|
+ return $filter->process($previousResult);
|
|
|
}
|
|
|
- }
|
|
|
+
|
|
|
+ return $previousResult;
|
|
|
+ }, $this->_result);
|
|
|
}
|
|
|
|
|
|
protected function sendContent()
|
|
|
@@ -72,4 +82,12 @@ class Response extends \CApplicationComponent
|
|
|
echo trim($this->_result);
|
|
|
}
|
|
|
|
|
|
+ protected function sendHeaders()
|
|
|
+ {
|
|
|
+ header('HTTP/1.1 ' . $this->_statusCode);
|
|
|
+ foreach ($this->_headers as $header) {
|
|
|
+ header((string)$header);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|