Prechádzať zdrojové kódy

response with headers

alexlcdee 7 rokov pred
rodič
commit
759fb1d1f9
2 zmenil súbory, kde vykonal 53 pridanie a 7 odobranie
  1. 28 0
      src/web/Header.php
  2. 25 7
      src/web/Response.php

+ 28 - 0
src/web/Header.php

@@ -0,0 +1,28 @@
+<?php
+
+namespace yiins\web;
+
+
+class Header
+{
+    /**
+     * @var string
+     */
+    private $name;
+    /**
+     * @var string
+     */
+    private $value;
+
+    public function __construct(string $name, string $value)
+    {
+        $this->name = $name;
+        $this->value = $value;
+    }
+
+    public function __toString()
+    {
+        return $this->name . ': ' . $this->value;
+    }
+
+}

+ 25 - 7
src/web/Response.php

@@ -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);
+        }
+    }
+
 }