response.class.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. class Response {
  3. public $output = '';
  4. public $body = '';
  5. private $code = 200;
  6. public $headers = [];
  7. protected $open = true;
  8. public function __construct(){}
  9. public function __toString(){
  10. return $this->body;
  11. }
  12. public function clear(){
  13. if($this->open){
  14. $this->fire('clear');
  15. $this->body = '';
  16. }
  17. return $this;
  18. }
  19. public function clear_headers(){
  20. if($this->open){
  21. $this->fire('clear_headers');
  22. $this->headers = [];
  23. }
  24. return $this;
  25. }
  26. public function clear_header(string $name){
  27. foreach($this->headers as $key => $header){
  28. if($header[0] == $name){
  29. $this->fire('clear_header', $name);
  30. array_splice($this->headers, $key, 1);
  31. }
  32. }
  33. return $this;
  34. }
  35. public function write(string $chunk){
  36. $this->fire('write', $chunk);
  37. if($this->open){
  38. $this->body .= $chunk;
  39. }
  40. return $this;
  41. }
  42. public function json($json){
  43. if(is_array($json)){
  44. array_walk_recursive($json, function(&$item, $key){
  45. if(!mb_detect_encoding($item, 'utf-8', true)){
  46. $item = utf8_encode($item);
  47. }
  48. });
  49. }
  50. $this->fire('json', $json);
  51. $this->write(json_encode($json));
  52. if(json_last_error() != JSON_ERROR_NONE){
  53. throw new Exception(json_last_error_msg());
  54. }
  55. return $this;
  56. }
  57. public function header(string $name, string $value){
  58. if($this->open){
  59. $this->fire('header', $name, $value);
  60. array_push(
  61. $this->headers,
  62. [
  63. $name,
  64. $value
  65. ]
  66. );
  67. }
  68. return $this;
  69. }
  70. public function redirect(string $url){
  71. $this->fire('redirect', $url);
  72. $this->header('Location',Router::url($url));
  73. return $this;
  74. }
  75. public function end(string $chunk=''){
  76. if($this->open){
  77. $this->write($chunk);
  78. $this->fire('end');
  79. $this->open = false;
  80. }
  81. return $this;
  82. }
  83. public function img(Image $img, string $type = null){
  84. if(!$type){
  85. $type = $img->type;
  86. }
  87. $this->fire('image', $img, $type);
  88. $this->clear_header('Content-Type')
  89. ->header('Content-Type', 'image/'.$type);
  90. if(!is_a($img, 'Image')){
  91. $img = new Image(100, 20);
  92. $img->text('Invalid Image',0,0,'black',12);
  93. }
  94. ob_start();
  95. $img();
  96. $this->write(ob_get_contents());
  97. ob_end_clean();
  98. return $this;
  99. }
  100. public function code(int $code=null){
  101. if(is_null($code)){
  102. return $this->code;
  103. }
  104. $this->fire('code', $code);
  105. $this->code = $code;
  106. return $this;
  107. }
  108. public function shutdown(){
  109. $this->fire('beforeshutdown');
  110. if($this->open){
  111. $this->end();
  112. }
  113. $this->fire('shutdown');
  114. http_response_code($this->code);
  115. foreach($this->headers as $k => $header){
  116. header("{$header[0]}: $header[1]");
  117. }
  118. echo $this->body;
  119. flush();
  120. $this->fire('aftershutdown');
  121. }
  122. }
  123. ?>