123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- namespace Juju\Http;
- require_once(realpath(dirname(__DIR__).'/events.trait.php'));
- require_once(realpath(dirname(__DIR__).'/App/router.class.php'));
- use \Juju\{App\Router, Events};
- class Response {
- use Events;
- public $output = '';
- public $body = '';
- private $code = 200;
- public $headers = [];
- protected $open = true;
- protected $shutdown = false;
- public function __construct(){}
- public function __toString(){
- return $this->body;
- }
- public function clear(){
- if($this->open){
- $this->fire('clear');
- $this->body = '';
- }
- return $this;
- }
- public function clear_headers(){
- if($this->open){
- $this->fire('clear_headers');
- $this->headers = [];
- }
- return $this;
- }
- public function clear_header(string $name){
- foreach($this->headers as $key => $header){
- if($header[0] == $name){
- $this->fire('clear_header', $name);
- array_splice($this->headers, $key, 1);
- }
- }
- return $this;
- }
- public function write(string $chunk){
- $this->fire('write', $chunk);
- if($this->open){
- $this->body .= $chunk;
- }
- return $this;
- }
- public function json($json){
- if(is_array($json)){
- array_walk_recursive($json, function(&$item, $key){
- if(!mb_detect_encoding($item, 'utf-8', true)){
- $item = utf8_encode($item);
- }
- });
- }
- $this->fire('json', $json);
- $this->write(json_encode($json));
- if(json_last_error() != JSON_ERROR_NONE){
- throw new \Exception(json_last_error_msg());
- }
- return $this;
- }
- public function header(string $name, string $value = null){
- if(is_null($value)){
- $headers = array_filter($this->headers,function($header) use($name){
- return $header[0] == $name;
- });
- return end($headers)[1];
- }else{
- if($this->open){
- if($this->fire('header', $name, $value) !== false){
- $this->headers[] = [
- $name,
- $value
- ];
- }
- }
- }
- return $this;
- }
- public function redirect(string $url){
- $this->fire('redirect', $url);
- $this->header('Location', Router::url($url));
- return $this;
- }
- public function end(string $chunk=''){
- if($this->open){
- $this->write($chunk);
- $this->fire('end');
- $this->open = false;
- }
- return $this;
- }
- public function img(Image $img, string $type = null){
- if(!$type){
- $type = $img->type;
- }
- $this->fire('image', $img, $type);
- $this->clear_header('Content-Type')
- ->header('Content-Type', 'image/'.$type);
- if(!is_a($img, 'Image')){
- $img = new Image(100, 20);
- $img->text('Invalid Image',0,0,'black',12);
- }
- ob_start();
- $img();
- $this->write(ob_get_contents());
- ob_end_clean();
- return $this;
- }
- public function code(int $code=null){
- if(is_null($code)){
- return $this->code;
- }
- $this->fire('code', $code);
- $this->code = $code;
- return $this;
- }
- public function shutdown(){
- if(!$this->shutdown){
- $this->shutdown = true;
- $this->fire('beforeshutdown');
- if($this->open){
- $this->end();
- }
- $this->fire('shutdown');
- http_response_code($this->code);
- foreach($this->headers as $k => $header){
- header("{$header[0]}: $header[1]");
- }
- echo $this->body;
- flush();
- $this->fire('aftershutdown');
- }
- return $this;
- }
- public function open(){
- if(!$this->shutdown){
- $this->open = true;
- }
- return $this;
- }
- public static function locale(string $locale, string $domain, string $folder, string $codeset = 'UTF-8'){
- $path = realpath($folder);
- if($path !== false){
- bindtextdomain($domain, $path);
- textdomain($domain);
- bind_textdomain_codeset($domain, $codeset);
- $orig_locale = $locale;
- if(!file_exists("{$path}/$locale/LC_MESSAGES/{$domain}.mo")){
- $locale = \Locale::getPrimaryLanguage($locale);
- if(!file_exists("{$path}/$locale/LC_MESSAGES/{$domain}.mo")){
- $locale = \Locale::getDefault();
- if(!file_exists("{$path}/$locale/LC_MESSAGES/{$domain}.mo")){
- $dir = dir("{$path}/");
- do{
- $locale = $dir->read();
- if($locale === false){
- trigger_error("Unable to find fallback locale for {$orig_locale}", E_USER_WARNING);
- break;
- }
- }while(!file_exists("{$path}/$locale/LC_MESSAGES/{$domain}.mo"));
- }
- }
- }
- }else{
- trigger_error("Locale folder {$folder} missing", E_USER_WARNING);
- }
- \Locale::setDefault($locale);
- }
- }
- ?>
|