1
0

response.class.php 2.7 KB

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