body; } public function clear(){ if($this->open){ $this->body = ''; } return $this; } public function clear_headers(){ if($this->open){ $this->headers = []; } return $this; } public function clear_header($name){ foreach($this->headers as $key => $header){ if($header[0] == $name){ array_splice($this->headers, $key, 1); } } return $this; } public function 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->write(json_encode($json)); if(json_last_error() != JSON_ERROR_NONE){ throw new Exception(json_last_error_msg()); } return $this; } public function header($name,$value){ if($this->open){ array_push( $this->headers, [ $name, $value ] ); } return $this; } public function redirect($url){ $this->header('Location',Router::url($url)); return $this; } public function end($chunk=''){ if($this->open){ $this->write($chunk); $this->open = false; } return $this; } public function img($img, $type = false){ if(!$type){ $type = $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($code=null){ if(is_null($code)){ return $this->code; } $this->code = $code; return $this; } public function shutdown(){ if($this->open){ $this->end(); } http_response_code($this->code); foreach($this->headers as $k => $header){ header("{$header[0]}: $header[1]"); } echo $this->body; flush(); } } ?>