Adding suport for Typed Returns and changins some return methods

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