Response.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace RR;
  3. /*
  4. *
  5. *
  6. */
  7. class Response{
  8. private static $_instance;
  9. /*
  10. *
  11. */
  12. public $_headers = Array();
  13. /*
  14. *
  15. */
  16. public $_body = Array();
  17. /*
  18. *
  19. */
  20. public $_code = 200;
  21. //SINGLETON==============================================
  22. private function __construct(){
  23. }
  24. private static function newObj(){
  25. if (!isset( self::$_instance )) {
  26. self::$_instance = new Response();
  27. }
  28. return self::$_instance;
  29. }
  30. public function getInstance(){
  31. if (!isset(self::$_instance)) {
  32. return self::newObj();
  33. }
  34. return self::$_instance;
  35. }
  36. //=======================================================
  37. public static function header($key, $value){
  38. $istance = self::getInstance();
  39. $istance->_headers[$key] = $value;
  40. return $istance;
  41. }
  42. public static function body_prepend($body = null){
  43. $instance = self::getInstance();
  44. if (null !== $body) {
  45. $instance->_body = (string) $body . $instance->_body;
  46. return $instance;
  47. }
  48. return $instance;
  49. }
  50. public static function body($body = null){
  51. $instance = self::getInstance();
  52. if (null !== $body) {
  53. $instance->_body = (string) $body;
  54. return $instance;
  55. }
  56. return $instance;
  57. }
  58. public static function body_append($body = null){
  59. $instance = self::getInstance();
  60. if (null !== $body) {
  61. $instance->_body .= (string) $body;
  62. return $instance;
  63. }
  64. return $instance;
  65. }
  66. private function send_headers(){
  67. foreach ($this->_headers as $key => $value) {
  68. header($key .': '. $value, false);
  69. }
  70. return $this;
  71. }
  72. private function send_body(){
  73. echo (string) $this->_body;
  74. return $this;
  75. }
  76. public static function send(){
  77. $instance = self::getInstance();
  78. $instance->send_headers();
  79. $instance->send_body();
  80. return $instance;
  81. }
  82. public static function json($object, $jsonp_prefix = null){
  83. $instance = self::getInstance();
  84. $instance->body('');
  85. $json = json_encode($object);
  86. if (null !== $jsonp_prefix) {
  87. $instance->header('Content-Type', 'text/javascript');
  88. $instance->body("$jsonp_prefix($json);");
  89. } else {
  90. $instance->header('Content-Type', 'application/json');
  91. $instance->body($json);
  92. }
  93. return $instance;
  94. }
  95. public static function redirect($url, $code = 302){
  96. $instance = self::getInstance();
  97. $instance->_code = $code;
  98. $instance->header('Location', $url);
  99. $instance->body('');
  100. $instance->send();
  101. }
  102. public static function back(){
  103. $instance = self::getInstance();
  104. $instance->header('Location: ', $_SERVER['HTTP_REFERER']);
  105. $instance->body('');
  106. $instance->send();
  107. }
  108. function done(){
  109. die;
  110. }
  111. }