Response.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. namespace KarmaFW\App;
  3. use KarmaFW\App;
  4. // TODO: a remplacer par ou rendre compatible avec GuzzleHttp\Psr7\Response
  5. class Response extends \Exception
  6. {
  7. protected $headers = [];
  8. protected $body = '';
  9. protected $status = 200;
  10. protected $reasonPhrase = 'OK';
  11. protected $content_type = '';
  12. protected $headers_sent = false;
  13. protected $protocol = null;
  14. protected $template_path = null;
  15. protected $template_data = [];
  16. /* public */ const http_status_codes = [
  17. 100 => 'Continue',
  18. 101 => 'Switching Protocols',
  19. 102 => 'Processing',
  20. 103 => 'Early Hints',
  21. 200 => 'OK',
  22. 201 => 'Created',
  23. 202 => 'Accepted',
  24. 203 => 'Non-Authoritative Information',
  25. 204 => 'No Content',
  26. 205 => 'Reset Content',
  27. 206 => 'Partial Content',
  28. 300 => 'Multiple Choices',
  29. 301 => 'Moved Permanently',
  30. 302 => 'Found',
  31. 303 => 'See Other',
  32. 304 => 'Not Modified',
  33. 400 => 'Bad Request',
  34. 401 => 'Unauthorized',
  35. 402 => 'Payment Required',
  36. 403 => 'Forbidden',
  37. 404 => 'Not Found',
  38. 405 => 'Method Not Allowed',
  39. 406 => 'Not Acceptable',
  40. 407 => 'Proxy Authentication Required',
  41. 408 => 'Request Time-out',
  42. 409 => 'Conflict',
  43. 410 => 'Gone',
  44. 411 => 'Length Required',
  45. 500 => 'Internal Server Error',
  46. 501 => 'Not Implemented',
  47. 502 => 'Bad Gateway ou Proxy Error',
  48. 503 => 'Service Unavailable',
  49. 504 => 'Gateway Time-out',
  50. 505 => 'HTTP Version not supported',
  51. ];
  52. public function __construct($status=200, array $headers=[], $body=null, $version='1.1', $reason=null)
  53. {
  54. $this->body = $body;
  55. $this->setStatus($status);
  56. $this->setHeaders($headers);
  57. $this->protocol = $version;
  58. }
  59. public function getStatus()
  60. {
  61. return $this->status;
  62. }
  63. public function getProtocol()
  64. {
  65. return $this->protocol;
  66. }
  67. public function setProtocol($protocol)
  68. {
  69. $this->protocol = $protocol;
  70. return $this;
  71. }
  72. public function getReasonPhrase()
  73. {
  74. return $this->reasonPhrase;
  75. }
  76. public function setStatus($status=200)
  77. {
  78. $this->status = $status;
  79. $reasonPhrase = ! empty(self::http_status_codes[$status]) ? self::http_status_codes[$status] : "Unknown status";
  80. $this->reasonPhrase = $reasonPhrase;
  81. return $this;
  82. }
  83. public function getContentType()
  84. {
  85. return $this->content_type;
  86. }
  87. public function setContentType($content_type)
  88. {
  89. $this->content_type = $content_type;
  90. return $this;
  91. }
  92. public function getTemplatePath()
  93. {
  94. return $this->template_path;
  95. }
  96. public function setTemplatePath($template_path)
  97. {
  98. $this->template_path = $template_path;
  99. return $this;
  100. }
  101. public function getTemplateData()
  102. {
  103. return $this->template_data;
  104. }
  105. public function setTemplateData($template_data)
  106. {
  107. $this->template_data = $template_data;
  108. return $this;
  109. }
  110. public function getContent()
  111. {
  112. // DEPRECATED
  113. return $this->getBody();
  114. }
  115. public function getBody()
  116. {
  117. return $this->body;
  118. }
  119. public function getContentLength()
  120. {
  121. return strlen($this->body);
  122. }
  123. public function setBody($body)
  124. {
  125. $this->body = $body;
  126. return $this;
  127. }
  128. public function setContent($body)
  129. {
  130. // DEPRECATED
  131. return $this->setBody($body);
  132. }
  133. public function setHtml($body, $status=200, $content_type='text/html')
  134. {
  135. return $this->setBody($body)
  136. ->setContentType($content_type)
  137. ->setStatus($status);
  138. }
  139. public function setJson($body, $status=200, $content_type='application/json')
  140. {
  141. return $this->setBody($body)
  142. ->setContentType($content_type)
  143. ->setStatus($status);
  144. }
  145. public function setCsv($body, $status=200, $content_type='text/csv')
  146. {
  147. return $this->setBody($body)
  148. ->setContentType($content_type)
  149. ->setStatus($status);
  150. }
  151. public function append($body)
  152. {
  153. $this->body .= $body;
  154. return $this;
  155. }
  156. public function prepend($body)
  157. {
  158. $this->body = $body . $this->body;
  159. return $this;
  160. }
  161. public function getHeaders()
  162. {
  163. return $this->headers;
  164. }
  165. public function setHeaders($headers)
  166. {
  167. //return $this->headers = $headers;
  168. $this->headers = [];
  169. foreach ($headers as $k => $v) {
  170. if (is_numeric($k)) {
  171. if (strpos($v, ':') === false) {
  172. continue;
  173. }
  174. $parts = explode(':', $v);
  175. $k = trim($parts[0]);
  176. $v = trim($parts[1]);
  177. }
  178. $this->addHeader($k, $v);
  179. }
  180. return $this;
  181. }
  182. public function addHeader($key, $value)
  183. {
  184. $key = ucwords(strtolower($key), " -\t\r\n\f\v");
  185. $this->headers[$key] = $value;
  186. return $this;
  187. }
  188. public function sendHeaders()
  189. {
  190. if (App::isCli()) {
  191. return;
  192. }
  193. if ($this->headers_sent) {
  194. //error_log("Warning: headers already sent");
  195. return;
  196. } else if (headers_sent()) {
  197. error_log("Warning: headers already sent");
  198. $this->headers_sent = true;
  199. return;
  200. }
  201. if (! empty($this->status)) {
  202. $reasonPhrase = empty($this->reasonPhrase) ? "Unknown http status" : trim($this->reasonPhrase);
  203. $this->headers['X-Status'] = $this->status . ' ' . $reasonPhrase;
  204. header('HTTP/1.0 ' . $this->status . ' ' . $reasonPhrase);
  205. }
  206. if (empty($this->headers['Content-Type']) && ! empty($this->content_type)) {
  207. $this->headers['Content-Type'] = $this->content_type;
  208. }
  209. if (empty($this->headers['Content-Length'])) {
  210. $this->headers['Content-Length'] = $this->getContentLength();
  211. }
  212. foreach ($this->headers as $k => $v) {
  213. header($k . ": " . $v);
  214. }
  215. $this->headers_sent = true;
  216. return $this;
  217. }
  218. public function send()
  219. {
  220. $this->headers_sent = ($this->headers_sent || headers_sent());
  221. if (! $this->headers_sent) {
  222. $this->sendHeaders();
  223. }
  224. if (strlen($this->body) > 0) {
  225. echo $this->body;
  226. }
  227. return $this;
  228. }
  229. }