Response.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace KarmaFW\App;
  3. class Response
  4. {
  5. protected $headers = [];
  6. protected $content = '';
  7. protected $status = 200;
  8. protected $status_name = 'OK';
  9. protected $content_type = '';
  10. protected $headers_sent = false;
  11. /* public */ const http_status_codes = [
  12. 100 => 'Continue',
  13. 101 => 'Switching Protocols',
  14. 102 => 'Processing',
  15. 103 => 'Early Hints',
  16. 200 => 'OK',
  17. 201 => 'Created',
  18. 202 => 'Accepted',
  19. 203 => 'Non-Authoritative Information',
  20. 204 => 'No Content',
  21. 205 => 'Reset Content',
  22. 206 => 'Partial Content',
  23. 300 => 'Multiple Choices',
  24. 301 => 'Moved Permanently',
  25. 302 => 'Found',
  26. 303 => 'See Other',
  27. 304 => 'Not Modified',
  28. 400 => 'Bad Request',
  29. 401 => 'Unauthorized',
  30. 402 => 'Payment Required',
  31. 403 => 'Forbidden',
  32. 404 => 'Not Found',
  33. 405 => 'Method Not Allowed',
  34. 406 => 'Not Acceptable',
  35. 407 => 'Proxy Authentication Required',
  36. 408 => 'Request Time-out',
  37. 409 => 'Conflict',
  38. 410 => 'Gone',
  39. 411 => 'Length Required',
  40. 500 => 'Internal Server Error',
  41. 501 => 'Not Implemented',
  42. 502 => 'Bad Gateway ou Proxy Error',
  43. 503 => 'Service Unavailable',
  44. 504 => 'Gateway Time-out',
  45. 505 => 'HTTP Version not supported',
  46. ];
  47. public function __construct($content='', $content_type='text/html')
  48. {
  49. $this->content = $content;
  50. $this->content_type = $content_type;
  51. }
  52. public function getStatus()
  53. {
  54. return $this->status;
  55. }
  56. public function getStatusName()
  57. {
  58. return $this->status_name;
  59. }
  60. public function setStatus($status=200)
  61. {
  62. $this->status = $status;
  63. $status_name = ! empty(self::http_status_codes[$status]) ? self::http_status_codes[$status] : "Unknown status";
  64. $this->status_name = $status_name;
  65. }
  66. public function getContentType()
  67. {
  68. return $this->content_type;
  69. }
  70. public function setContentType($content_type)
  71. {
  72. $this->content_type = $content_type;
  73. }
  74. public function getBody()
  75. {
  76. return $this->getContent();
  77. }
  78. public function getContent()
  79. {
  80. return $this->content;
  81. }
  82. public function getContentLength()
  83. {
  84. return strlen($this->content);
  85. }
  86. public function setContent($content)
  87. {
  88. $this->content = $content;
  89. }
  90. public function append($content)
  91. {
  92. $this->content .= $content;
  93. }
  94. public function prepend($content)
  95. {
  96. $this->content = $content . $this->content;
  97. }
  98. public function getHeaders()
  99. {
  100. return $this->headers;
  101. }
  102. public function setheaders($headers)
  103. {
  104. //return $this->headers = $headers;
  105. $this->headers = [];
  106. foreach ($headers as $k => $v) {
  107. $this->addHeader($k, $v);
  108. }
  109. }
  110. public function addHeader($key, $value)
  111. {
  112. $key = ucwords(strtolower($key), " -\t\r\n\f\v");
  113. $this->headers[$key] = $value;
  114. }
  115. public function sendHeaders()
  116. {
  117. if ($this->headers_sent) {
  118. //error_log("Warning: headers already sent");
  119. return;
  120. } else if (headers_sent()) {
  121. error_log("Warning: headers already sent");
  122. $this->headers_sent = true;
  123. return;
  124. }
  125. if (! empty($this->status)) {
  126. $status_name = empty($this->status_name) ? "Unknown http status" : $this->status_name;
  127. header('HTTP/1.0 ' . $this->status . ' ' . $status_name);
  128. $this->headers['X-Status'] = $this->status . ' ' . $status_name;
  129. }
  130. if (empty($this->headers['Content-Type']) && ! empty($this->content_type)) {
  131. $this->headers['Content-Type'] = $this->content_type;
  132. }
  133. if (empty($this->headers['Content-Length'])) {
  134. $this->headers['Content-Length'] = $this->getContentLength();
  135. }
  136. foreach ($this->headers as $k => $v) {
  137. header($k . ": " . $v);
  138. }
  139. $this->headers_sent = true;
  140. }
  141. public function send()
  142. {
  143. $this->headers_sent = ($this->headers_sent || headers_sent());
  144. if (! $this->headers_sent) {
  145. $this->sendHeaders();
  146. }
  147. if (strlen($this->content) > 0) {
  148. echo $this->content;
  149. }
  150. }
  151. }