response.class.php 2.9 KB

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