197 lines
4.0 KiB
PHP
Executable File
197 lines
4.0 KiB
PHP
Executable File
<?php
|
|
namespace RR;
|
|
|
|
/*
|
|
*
|
|
*
|
|
*/
|
|
class Response{
|
|
|
|
private static $_instance;
|
|
|
|
/*
|
|
*
|
|
*/
|
|
public $_headers = Array();
|
|
|
|
/*
|
|
*
|
|
*/
|
|
public $_body = Array();
|
|
|
|
/*
|
|
*
|
|
*/
|
|
public $_code = 200;
|
|
|
|
//SINGLETON==============================================
|
|
|
|
private function __construct(){
|
|
}
|
|
|
|
private static function newObj() : Response {
|
|
if (!isset( self::$_instance )) {
|
|
self::$_instance = new Response();
|
|
}
|
|
return self::$_instance;
|
|
}
|
|
|
|
public function getInstance() : Response {
|
|
if (!isset(self::$_instance)) {
|
|
return self::newObj();
|
|
}
|
|
return self::$_instance;
|
|
}
|
|
|
|
//=======================================================
|
|
|
|
/**
|
|
*
|
|
* @ctag Response::header($key, $value);
|
|
*
|
|
*/
|
|
public static function header($key, $value){
|
|
$istance = self::getInstance();
|
|
$istance->_headers[$key] = $value;
|
|
return $istance;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @ctag Response::bodyPrepend($value);
|
|
*
|
|
*/
|
|
public static function bodyPrepend($body = null){
|
|
$instance = self::getInstance();
|
|
if (null !== $body) {
|
|
$instance->_body = (string) $body . $instance->_body;
|
|
return $instance;
|
|
}
|
|
return $instance;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @ctag Response::body($value);
|
|
*
|
|
*/
|
|
public static function body($body = null){
|
|
$instance = self::getInstance();
|
|
if (null !== $body) {
|
|
$instance->_body = (string) $body;
|
|
return $instance;
|
|
}
|
|
return $instance;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @ctag Response::bodyAppend($value);
|
|
*
|
|
*/
|
|
public static function bodyAppend($body = null){
|
|
$instance = self::getInstance();
|
|
if (null !== $body) {
|
|
$instance->_body .= (string) $body;
|
|
return $instance;
|
|
}
|
|
return $instance;
|
|
}
|
|
|
|
private function sendHeaders(){
|
|
foreach ($this->_headers as $key => $value) {
|
|
header($key .': '. $value, true);
|
|
}
|
|
return $this;
|
|
}
|
|
|
|
private function sendBody(){
|
|
if(!empty($this->_body)){
|
|
echo (string) $this->_body;
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @ctag Response::bodySend();
|
|
*
|
|
*/
|
|
public static function send(){
|
|
$instance = self::getInstance();
|
|
$instance->sendHeaders();
|
|
$instance->sendBody();
|
|
http_response_code($instance->_code);
|
|
return $instance;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @ctag Response::json($object);
|
|
* @ctag Response::json($object, $prefix);
|
|
*
|
|
*/
|
|
public static function json($object, $jsonPrefix = null): Response{
|
|
$instance = self::getInstance();
|
|
|
|
$instance->body('');
|
|
|
|
$json = $object;
|
|
if(!is_string($object)){
|
|
$json = json_encode($object);
|
|
}
|
|
|
|
if (null !== $jsonPrefix) {
|
|
$instance->header('Content-Type', 'text/javascript');
|
|
$instance->body("$jsonPrefix($json);");
|
|
} else {
|
|
$instance->header('Content-Type', 'application/json');
|
|
$instance->body($json);
|
|
}
|
|
|
|
return $instance;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @ctag Response::redirect($url);
|
|
* @ctag Response::redirect($url, $code);
|
|
*
|
|
*/
|
|
public static function redirect($url, $code = 302){
|
|
$instance = self::getInstance();
|
|
|
|
if(is_ajax_request()){
|
|
return $instance->json(['location' => $url])->send();
|
|
}
|
|
|
|
## TODO
|
|
header("Location: $url");
|
|
die;
|
|
|
|
$instance->header('Location: ', $url)->send();
|
|
|
|
return $instance->send()->done();
|
|
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @ctag Response::back();
|
|
*
|
|
*/
|
|
public static function back(){
|
|
$instance = self::getInstance();
|
|
|
|
$instance->header('Location: ', $_SERVER['HTTP_REFERER']);
|
|
|
|
$instance->body('');
|
|
$instance->send();
|
|
}
|
|
|
|
public function done(){
|
|
die;
|
|
}
|
|
}
|