response.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php namespace Laravel;
  2. class Response_Factory {
  3. /**
  4. * The view factory instance.
  5. *
  6. * @var View_Factory
  7. */
  8. private $view;
  9. /**
  10. * Create a new response factory instance.
  11. *
  12. * @param File $file
  13. * @return void
  14. */
  15. public function __construct(View_Factory $view)
  16. {
  17. $this->view = $view;
  18. }
  19. /**
  20. * Create a new response instance.
  21. *
  22. * @param mixed $content
  23. * @param int $status
  24. * @return Response
  25. */
  26. public function make($content, $status = 200)
  27. {
  28. return new Response($content, $status);
  29. }
  30. /**
  31. * Create a new response instance containing a view.
  32. *
  33. * @param string $view
  34. * @param array $data
  35. * @return Response
  36. */
  37. public function view($view, $data = array())
  38. {
  39. return new Response($this->view->make($view, $data));
  40. }
  41. /**
  42. * Create a new error response instance.
  43. *
  44. * The response status code will be set using the specified code.
  45. *
  46. * Note: The specified error code should correspond to a view in your views/error directory.
  47. *
  48. * @param int $code
  49. * @param array $data
  50. * @return void
  51. */
  52. public function error($code, $data = array())
  53. {
  54. $data['homepage'] = IoC::resolve('laravel.config')->get('application.url');
  55. return new Response($this->view->make('error/'.$code, $data), $code);
  56. }
  57. }
  58. class Response {
  59. /**
  60. * The content of the response.
  61. *
  62. * @var mixed
  63. */
  64. public $content;
  65. /**
  66. * The HTTP status code of the response.
  67. *
  68. * @var int
  69. */
  70. public $status;
  71. /**
  72. * The response headers.
  73. *
  74. * @var array
  75. */
  76. public $headers = array();
  77. /**
  78. * HTTP status codes.
  79. *
  80. * @var array
  81. */
  82. private $statuses = array(
  83. 100 => 'Continue',
  84. 101 => 'Switching Protocols',
  85. 200 => 'OK',
  86. 201 => 'Created',
  87. 202 => 'Accepted',
  88. 203 => 'Non-Authoritative Information',
  89. 204 => 'No Content',
  90. 205 => 'Reset Content',
  91. 206 => 'Partial Content',
  92. 207 => 'Multi-Status',
  93. 300 => 'Multiple Choices',
  94. 301 => 'Moved Permanently',
  95. 302 => 'Found',
  96. 303 => 'See Other',
  97. 304 => 'Not Modified',
  98. 305 => 'Use Proxy',
  99. 307 => 'Temporary Redirect',
  100. 400 => 'Bad Request',
  101. 401 => 'Unauthorized',
  102. 402 => 'Payment Required',
  103. 403 => 'Forbidden',
  104. 404 => 'Not Found',
  105. 405 => 'Method Not Allowed',
  106. 406 => 'Not Acceptable',
  107. 407 => 'Proxy Authentication Required',
  108. 408 => 'Request Timeout',
  109. 409 => 'Conflict',
  110. 410 => 'Gone',
  111. 411 => 'Length Required',
  112. 412 => 'Precondition Failed',
  113. 413 => 'Request Entity Too Large',
  114. 414 => 'Request-URI Too Long',
  115. 415 => 'Unsupported Media Type',
  116. 416 => 'Requested Range Not Satisfiable',
  117. 417 => 'Expectation Failed',
  118. 422 => 'Unprocessable Entity',
  119. 423 => 'Locked',
  120. 424 => 'Failed Dependency',
  121. 500 => 'Internal Server Error',
  122. 501 => 'Not Implemented',
  123. 502 => 'Bad Gateway',
  124. 503 => 'Service Unavailable',
  125. 504 => 'Gateway Timeout',
  126. 505 => 'HTTP Version Not Supported',
  127. 507 => 'Insufficient Storage',
  128. 509 => 'Bandwidth Limit Exceeded'
  129. );
  130. /**
  131. * Create a new response instance.
  132. *
  133. * @param mixed $content
  134. * @param int $status
  135. * @return void
  136. */
  137. public function __construct($content, $status = 200)
  138. {
  139. $this->content = $content;
  140. $this->status = $status;
  141. }
  142. /**
  143. * Get the evaluated string contents of the response.
  144. *
  145. * @return string
  146. */
  147. public function render()
  148. {
  149. return ($this->content instanceof View) ? $this->content->render() : (string) $this->content;
  150. }
  151. /**
  152. * Send the response to the browser.
  153. *
  154. * All of the response header will be sent to the browser first, followed by the content
  155. * of the response instance, which will be evaluated and rendered by the render method.
  156. *
  157. * @return void
  158. */
  159. public function send()
  160. {
  161. if ( ! isset($this->headers['Content-Type'])) $this->header('Content-Type', 'text/html; charset=utf-8');
  162. if ( ! headers_sent()) $this->send_headers();
  163. echo $this->render();
  164. }
  165. /**
  166. * Send the response headers to the browser.
  167. *
  168. * @return void
  169. */
  170. public function send_headers()
  171. {
  172. $protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
  173. header($protocol.' '.$this->status.' '.$this->statuses[$this->status]);
  174. foreach ($this->headers as $name => $value)
  175. {
  176. header($name.': '.$value, true);
  177. }
  178. }
  179. /**
  180. * Add a header to the response.
  181. *
  182. * @param string $name
  183. * @param string $value
  184. * @return Response
  185. */
  186. public function header($name, $value)
  187. {
  188. $this->headers[$name] = $value;
  189. return $this;
  190. }
  191. /**
  192. * Set the response status code.
  193. *
  194. * @param int $status
  195. * @return Response
  196. */
  197. public function status($status)
  198. {
  199. $this->status = $status;
  200. return $this;
  201. }
  202. }