response.class.php 2.8 KB

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