1
0

response.class.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. protected $shutdown = false;
  14. public function __construct(){}
  15. public function __toString(){
  16. return $this->body;
  17. }
  18. public function clear(){
  19. if($this->open){
  20. $this->fire('clear');
  21. $this->body = '';
  22. }
  23. return $this;
  24. }
  25. public function clear_headers(){
  26. if($this->open){
  27. $this->fire('clear_headers');
  28. $this->headers = [];
  29. }
  30. return $this;
  31. }
  32. public function clear_header(string $name){
  33. foreach($this->headers as $key => $header){
  34. if($header[0] == $name){
  35. $this->fire('clear_header', $name);
  36. array_splice($this->headers, $key, 1);
  37. }
  38. }
  39. return $this;
  40. }
  41. public function write(string $chunk){
  42. $this->fire('write', $chunk);
  43. if($this->open){
  44. $this->body .= $chunk;
  45. }
  46. return $this;
  47. }
  48. public function json($json){
  49. if(is_array($json)){
  50. array_walk_recursive($json, function(&$item, $key){
  51. if(!mb_detect_encoding($item, 'utf-8', true)){
  52. $item = utf8_encode($item);
  53. }
  54. });
  55. }
  56. $this->fire('json', $json);
  57. $this->write(json_encode($json));
  58. if(json_last_error() != JSON_ERROR_NONE){
  59. throw new \Exception(json_last_error_msg());
  60. }
  61. return $this;
  62. }
  63. public function header(string $name, string $value){
  64. if($this->open){
  65. $this->fire('header', $name, $value);
  66. array_push(
  67. $this->headers,
  68. [
  69. $name,
  70. $value
  71. ]
  72. );
  73. }
  74. return $this;
  75. }
  76. public function redirect(string $url){
  77. $this->fire('redirect', $url);
  78. $this->header('Location', Router::url($url));
  79. return $this;
  80. }
  81. public function end(string $chunk=''){
  82. if($this->open){
  83. $this->write($chunk);
  84. $this->fire('end');
  85. $this->open = false;
  86. }
  87. return $this;
  88. }
  89. public function img(Image $img, string $type = null){
  90. if(!$type){
  91. $type = $img->type;
  92. }
  93. $this->fire('image', $img, $type);
  94. $this->clear_header('Content-Type')
  95. ->header('Content-Type', 'image/'.$type);
  96. if(!is_a($img, 'Image')){
  97. $img = new Image(100, 20);
  98. $img->text('Invalid Image',0,0,'black',12);
  99. }
  100. ob_start();
  101. $img();
  102. $this->write(ob_get_contents());
  103. ob_end_clean();
  104. return $this;
  105. }
  106. public function code(int $code=null){
  107. if(is_null($code)){
  108. return $this->code;
  109. }
  110. $this->fire('code', $code);
  111. $this->code = $code;
  112. return $this;
  113. }
  114. public function shutdown(){
  115. $this->shutdown = true;
  116. $this->fire('beforeshutdown');
  117. if($this->open){
  118. $this->end();
  119. }
  120. $this->fire('shutdown');
  121. http_response_code($this->code);
  122. foreach($this->headers as $k => $header){
  123. header("{$header[0]}: $header[1]");
  124. }
  125. echo $this->body;
  126. flush();
  127. $this->fire('aftershutdown');
  128. }
  129. public function open(){
  130. if(!$this->shutdown){
  131. $this->open = true;
  132. }
  133. return $this;
  134. }
  135. public static function locale(string $locale, string $domain, string $folder, string $codeset = 'UTF-8'){
  136. $path = realpath($folder);
  137. if($path !== false){
  138. bindtextdomain($domain, $path);
  139. textdomain($domain);
  140. bind_textdomain_codeset($domain, $codeset);
  141. $orig_locale = $locale;
  142. if(!file_exists("{$path}/$locale/LC_MESSAGES/{$domain}.mo")){
  143. $locale = \Locale::getPrimaryLanguage($locale);
  144. if(!file_exists("{$path}/$locale/LC_MESSAGES/{$domain}.mo")){
  145. $locale = \Locale::getDefault();
  146. if(!file_exists("{$path}/$locale/LC_MESSAGES/{$domain}.mo")){
  147. $dir = dir("{$path}/");
  148. do{
  149. $locale = $dir->read();
  150. if($locale === false){
  151. trigger_error("Unable to find fallback locale for {$orig_locale}", E_USER_WARNING);
  152. break;
  153. }
  154. }while(!file_exists("{$path}/$locale/LC_MESSAGES/{$domain}.mo"));
  155. }
  156. }
  157. }
  158. }else{
  159. trigger_error("Locale folder {$folder} missing", E_USER_WARNING);
  160. }
  161. \Locale::setDefault($locale);
  162. }
  163. }
  164. ?>