123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- <?php namespace Laravel;
- class Response {
-
- public $content;
-
- public $status;
-
- public $headers = array();
-
- protected $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 of($name, $data = array())
- {
- return new static(View::of($name, $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 function render()
- {
- return ($this->content instanceof View) ? $this->content->render() : (string) $this->content;
- }
-
- public function send()
- {
- if ( ! headers_sent()) $this->headers();
- echo $this->render();
- }
-
- public function headers()
- {
- if ( ! isset($this->headers['Content-Type']))
- {
- $encoding = Config::$items['application']['encoding'];
- $this->header('Content-Type', "text/html; charset={$encoding}");
- }
- header(Request::protocol().' '.$this->status.' '.$this->statuses[$this->status]);
- foreach ($this->headers as $name => $value)
- {
- header($name.': '.$value, true);
- }
- }
-
- public function header($name, $value)
- {
- $this->headers[$name] = $value;
- return $this;
- }
-
- public function status($status)
- {
- $this->status = $status;
- return $this;
- }
-
- public static function __callStatic($method, $parameters)
- {
- if (strpos($method, 'of_') === 0)
- {
- return static::with(substr($method, 3), Arr::get($parameters, 0, array()));
- }
- }
- }
|