123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- <?php namespace Laravel;
- class Response {
-
- public $content;
-
- public $status = 200;
-
- public $headers = array();
-
- public static $statuses = array(
- 100 => 'Continue',
- 101 => 'Switching Protocols',
- 200 => 'OK',
- 201 => 'Created',
- 202 => 'Accepted',
- 203 => 'Non-Authoritative Information',
- 204 => 'No Content',
- 205 => 'Reset Content',
- 206 => 'Partial Content',
- 207 => 'Multi-Status',
- 300 => 'Multiple Choices',
- 301 => 'Moved Permanently',
- 302 => 'Found',
- 303 => 'See Other',
- 304 => 'Not Modified',
- 305 => 'Use Proxy',
- 307 => 'Temporary Redirect',
- 400 => 'Bad Request',
- 401 => 'Unauthorized',
- 402 => 'Payment Required',
- 403 => 'Forbidden',
- 404 => 'Not Found',
- 405 => 'Method Not Allowed',
- 406 => 'Not Acceptable',
- 407 => 'Proxy Authentication Required',
- 408 => 'Request Timeout',
- 409 => 'Conflict',
- 410 => 'Gone',
- 411 => 'Length Required',
- 412 => 'Precondition Failed',
- 413 => 'Request Entity Too Large',
- 414 => 'Request-URI Too Long',
- 415 => 'Unsupported Media Type',
- 416 => 'Requested Range Not Satisfiable',
- 417 => 'Expectation Failed',
- 422 => 'Unprocessable Entity',
- 423 => 'Locked',
- 424 => 'Failed Dependency',
- 500 => 'Internal Server Error',
- 501 => 'Not Implemented',
- 502 => 'Bad Gateway',
- 503 => 'Service Unavailable',
- 504 => 'Gateway Timeout',
- 505 => 'HTTP Version Not Supported',
- 507 => 'Insufficient Storage',
- 509 => 'Bandwidth Limit Exceeded'
- );
-
- public function __construct($content, $status = 200, $headers = array())
- {
- $this->status = $status;
- $this->content = $content;
- $this->headers = $headers;
- }
-
- public static function make($content, $status = 200, $headers = array())
- {
- return new static($content, $status, $headers);
- }
-
- public static function view($view, $data = array())
- {
- return new static(View::make($view, $data));
- }
-
- public static function error($code, $data = array())
- {
- return new static(View::make('error.'.$code, $data), $code);
- }
-
- public static function download($path, $name = null, $headers = array())
- {
- if (is_null($name)) $name = basename($path);
- $headers = array_merge(array(
- 'content-description' => 'File Transfer',
- 'content-type' => File::mime(File::extension($path)),
- 'content-disposition' => 'attachment; filename="'.$name.'"',
- 'content-transfer-encoding' => 'binary',
- 'expires' => 0,
- 'cache-control' => 'must-revalidate, post-check=0, pre-check=0',
- 'pragma' => 'public',
- 'content-length' => File::size($path),
- ), $headers);
- return new static(File::get($path), 200, $headers);
- }
-
- public static function prepare($response)
- {
- if ( ! $response instanceof Response)
- {
- $response = new static($response);
- }
-
-
-
-
-
-
-
- $response->render();
- return $response;
- }
-
- public function render()
- {
- if (is_object($this->content) and method_exists($this->content, '__toString'))
- {
- $this->content = $this->content->__toString();
- }
- else
- {
- $this->content = (string) $this->content;
- }
- return $this->content;
- }
-
- public function send()
- {
- if ( ! headers_sent()) $this->send_headers();
- echo (string) $this->content;
- }
-
- public function send_headers()
- {
-
-
-
-
-
-
-
- if (isset($_SERVER['FCGI_SERVER_VERSION']))
- {
- header('Status: '.$this->status.' '.$this->message());
- }
- else
- {
- header(Request::protocol().' '.$this->status.' '.$this->message());
- }
-
-
-
- if ( ! isset($this->headers['content-type']))
- {
- $encoding = Config::get('application.encoding');
- $this->header('content-type', 'text/html; charset='.$encoding);
- }
-
-
-
- foreach ($this->headers as $name => $value)
- {
- header("{$name}: {$value}", true);
- }
- }
-
- public function message()
- {
- return static::$statuses[$this->status];
- }
-
- public function header($name, $value)
- {
- $this->headers[strtolower($name)] = $value;
- return $this;
- }
-
- public function status($status)
- {
- $this->status = $status;
- return $this;
- }
- }
|