Kaynağa Gözat

Adding suport for Typed Returns and changins some return methods

ahwelp 2 yıl önce
ebeveyn
işleme
a8089e483b
1 değiştirilmiş dosya ile 28 ekleme ve 12 silme
  1. 28 12
      src/RR/Response.php

+ 28 - 12
src/RR/Response.php

@@ -29,14 +29,14 @@ class Response{
     private function __construct(){
     }
 
-    private static function newObj(){
+    private static function newObj() : Response {
         if (!isset( self::$_instance )) {
             self::$_instance = new Response();
         }
         return self::$_instance;
     }
 
-    public function getInstance(){
+    public function getInstance() : Response {
         if (!isset(self::$_instance)) {
             return self::newObj();
         }
@@ -44,11 +44,11 @@ class Response{
     }
 
     //=======================================================
-    
+
     /**
      *
      * @ctag Response::header($key, $value);
-     * 
+     *
      */
     public static function header($key, $value){
         $istance = self::getInstance();
@@ -100,13 +100,16 @@ class Response{
 
     private function sendHeaders(){
         foreach ($this->_headers as $key => $value) {
-            header($key .': '. $value, false);
+            header($key .': '. $value, true);
         }
         return $this;
     }
 
     private function sendBody(){
-        echo (string) $this->_body;
+        if(!empty($this->_body)){
+            echo (string) $this->_body;
+        }
+
         return $this;
     }
 
@@ -119,6 +122,7 @@ class Response{
         $instance = self::getInstance();
         $instance->sendHeaders();
         $instance->sendBody();
+        http_response_code($instance->_code);
         return $instance;
     }
 
@@ -128,12 +132,16 @@ class Response{
      * @ctag Response::json($object, $prefix);
      *
      */
-    public static function json($object, $jsonPrefix = null){
+    public static function json($object, $jsonPrefix = null): Response{
         $instance = self::getInstance();
 
         $instance->body('');
 
-        $json = json_encode($object);
+        $json = $object;
+        if(!is_string($object)){
+            $json = json_encode($object);
+        }
+
         if (null !== $jsonPrefix) {
             $instance->header('Content-Type', 'text/javascript');
             $instance->body("$jsonPrefix($json);");
@@ -141,6 +149,7 @@ class Response{
             $instance->header('Content-Type', 'application/json');
             $instance->body($json);
         }
+
         return $instance;
     }
 
@@ -153,11 +162,18 @@ class Response{
     public static function redirect($url, $code = 302){
         $instance = self::getInstance();
 
-        $instance->_code = $code;
-        $instance->header('Location', $url);
+        if(is_ajax_request()){
+            return $instance->json(['location' => $url])->send();
+        }
+
+        ## TODO
+        header("Location: $url");
+        die;
+
+        $instance->header('Location: ', $url)->send();
+
+        return $instance->send()->done();
 
-        $instance->body('');
-        $instance->send();
     }
 
     /**