response.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php namespace Laravel;
  2. use Symfony\Component\HttpFoundation\Response as FoundationResponse;
  3. class Response {
  4. /**
  5. * The content of the response.
  6. *
  7. * @var mixed
  8. */
  9. public $content;
  10. /**
  11. * The Symfony HttpFoundation Response instance.
  12. *
  13. * @var HttpFoundation\Response
  14. */
  15. public $foundation;
  16. /**
  17. * Create a new response instance.
  18. *
  19. * @param mixed $content
  20. * @param int $status
  21. * @param array $headers
  22. * @return void
  23. */
  24. public function __construct($content, $status = 200, $headers = array())
  25. {
  26. $this->content = $content;
  27. $this->foundation = new FoundationResponse('', $status, $headers);
  28. }
  29. /**
  30. * Create a new response instance.
  31. *
  32. * <code>
  33. * // Create a response instance with string content
  34. * return Response::make(json_encode($user));
  35. *
  36. * // Create a response instance with a given status
  37. * return Response::make('Not Found', 404);
  38. *
  39. * // Create a response with some custom headers
  40. * return Response::make(json_encode($user), 200, array('header' => 'value'));
  41. * </code>
  42. *
  43. * @param mixed $content
  44. * @param int $status
  45. * @param array $headers
  46. * @return Response
  47. */
  48. public static function make($content, $status = 200, $headers = array())
  49. {
  50. return new static($content, $status, $headers);
  51. }
  52. /**
  53. * Create a new response instance containing a view.
  54. *
  55. * <code>
  56. * // Create a response instance with a view
  57. * return Response::view('home.index');
  58. *
  59. * // Create a response instance with a view and data
  60. * return Response::view('home.index', array('name' => 'Taylor'));
  61. * </code>
  62. *
  63. * @param string $view
  64. * @param array $data
  65. * @return Response
  66. */
  67. public static function view($view, $data = array())
  68. {
  69. return new static(View::make($view, $data));
  70. }
  71. /**
  72. * Create a new error response instance.
  73. *
  74. * The response status code will be set using the specified code.
  75. *
  76. * The specified error should match a view in your views/error directory.
  77. *
  78. * <code>
  79. * // Create a 404 response
  80. * return Response::error('404');
  81. *
  82. * // Create a 404 response with data
  83. * return Response::error('404', array('message' => 'Not Found'));
  84. * </code>
  85. *
  86. * @param int $code
  87. * @param array $data
  88. * @return Response
  89. */
  90. public static function error($code, $data = array())
  91. {
  92. return new static(View::make('error.'.$code, $data), $code);
  93. }
  94. /**
  95. * Create a new download response instance.
  96. *
  97. * <code>
  98. * // Create a download response to a given file
  99. * return Response::download('path/to/file.jpg');
  100. *
  101. * // Create a download response with a given file name
  102. * return Response::download('path/to/file.jpg', 'your_file.jpg');
  103. * </code>
  104. *
  105. * @param string $path
  106. * @param string $name
  107. * @param array $headers
  108. * @return Response
  109. */
  110. public static function download($path, $name = null, $headers = array())
  111. {
  112. if (is_null($name)) $name = basename($path);
  113. $headers = array_merge(array(
  114. 'Content-Description' => 'File Transfer',
  115. 'Content-Type' => File::mime(File::extension($path)),
  116. 'Content-Disposition' => 'attachment; filename="'.$name.'"',
  117. 'Content-Transfer-Encoding' => 'binary',
  118. 'Expires' => 0,
  119. 'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
  120. 'Pragma' => 'public',
  121. 'Content-Length' => File::size($path),
  122. ), $headers);
  123. return new static(File::get($path), 200, $headers);
  124. }
  125. /**
  126. * Prepare a response from the given value.
  127. *
  128. * If the value is not a response, it will be converted into a response
  129. * instance and the content will be cast to a string.
  130. *
  131. * @param mixed $response
  132. * @return Response
  133. */
  134. public static function prepare($response)
  135. {
  136. // We'll need to force the response to be a string before closing
  137. // the session, since the developer may be utilizing the session
  138. // within the view, and we can't age it until rendering.
  139. if ( ! $response instanceof Response)
  140. {
  141. $response = new static($response);
  142. }
  143. $response->render();
  144. return $response;
  145. }
  146. /**
  147. * Convert the content of the Response to a string and return it.
  148. *
  149. * @return string
  150. */
  151. public function render()
  152. {
  153. // If the content is a stringable object, we'll go ahead and call
  154. // to toString method so that we can get the string content of
  155. // the content object. Otherwise we'll just cast to string.
  156. if (str_object($this->content))
  157. {
  158. $this->content = $this->content->__toString();
  159. }
  160. else
  161. {
  162. $this->content = (string) $this->content;
  163. }
  164. // Once we have the string content, we can set the content on
  165. // the HttpFoundation Response instance in preparation for
  166. // sending it back to client browser when all is done.
  167. $this->foundation->setContent($this->content);
  168. return $this->content;
  169. }
  170. /**
  171. * Send the headers and content of the response to the browser.
  172. *
  173. * @return void
  174. */
  175. public function send()
  176. {
  177. $this->foundation->prepare(Request::$foundation);
  178. $this->foundation->send();
  179. }
  180. /**
  181. * Send all of the response headers to the browser.
  182. *
  183. * @return void
  184. */
  185. public function send_headers()
  186. {
  187. $this->foundation->prepare(Request::$foundation);
  188. $this->foundation->sendHeaders();
  189. }
  190. /**
  191. * Add a header to the array of response headers.
  192. *
  193. * @param string $name
  194. * @param string $value
  195. * @return Response
  196. */
  197. public function header($name, $value)
  198. {
  199. $this->foundation->headers->set($name, $value);
  200. return $this;
  201. }
  202. /**
  203. * Set the response status code.
  204. *
  205. * @param int $status
  206. * @return Response
  207. */
  208. public function status($status)
  209. {
  210. $this->foundation->setStatusCode($status);
  211. return $this;
  212. }
  213. }