response.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php namespace Laravel;
  2. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  3. use Symfony\Component\HttpFoundation\Response as FoundationResponse;
  4. class Response {
  5. /**
  6. * The content of the response.
  7. *
  8. * @var mixed
  9. */
  10. public $content;
  11. /**
  12. * The Symfony HttpFoundation Response instance.
  13. *
  14. * @var HttpFoundation\Response
  15. */
  16. public $foundation;
  17. /**
  18. * Create a new response instance.
  19. *
  20. * @param mixed $content
  21. * @param int $status
  22. * @param array $headers
  23. * @return void
  24. */
  25. public function __construct($content, $status = 200, $headers = array())
  26. {
  27. $this->content = $content;
  28. $this->foundation = new FoundationResponse('', $status, $headers);
  29. }
  30. /**
  31. * Create a new response instance.
  32. *
  33. * <code>
  34. * // Create a response instance with string content
  35. * return Response::make(json_encode($user));
  36. *
  37. * // Create a response instance with a given status
  38. * return Response::make('Not Found', 404);
  39. *
  40. * // Create a response with some custom headers
  41. * return Response::make(json_encode($user), 200, array('header' => 'value'));
  42. * </code>
  43. *
  44. * @param mixed $content
  45. * @param int $status
  46. * @param array $headers
  47. * @return Response
  48. */
  49. public static function make($content, $status = 200, $headers = array())
  50. {
  51. return new static($content, $status, $headers);
  52. }
  53. /**
  54. * Create a new response instance containing a view.
  55. *
  56. * <code>
  57. * // Create a response instance with a view
  58. * return Response::view('home.index');
  59. *
  60. * // Create a response instance with a view and data
  61. * return Response::view('home.index', array('name' => 'Taylor'));
  62. * </code>
  63. *
  64. * @param string $view
  65. * @param array $data
  66. * @return Response
  67. */
  68. public static function view($view, $data = array())
  69. {
  70. return new static(View::make($view, $data));
  71. }
  72. /**
  73. * Create a new error response instance.
  74. *
  75. * The response status code will be set using the specified code.
  76. *
  77. * The specified error should match a view in your views/error directory.
  78. *
  79. * <code>
  80. * // Create a 404 response
  81. * return Response::error('404');
  82. *
  83. * // Create a 404 response with data
  84. * return Response::error('404', array('message' => 'Not Found'));
  85. * </code>
  86. *
  87. * @param int $code
  88. * @param array $data
  89. * @return Response
  90. */
  91. public static function error($code, $data = array())
  92. {
  93. return new static(View::make('error.'.$code, $data), $code);
  94. }
  95. /**
  96. * Create a new download response instance.
  97. *
  98. * <code>
  99. * // Create a download response to a given file
  100. * return Response::download('path/to/file.jpg');
  101. *
  102. * // Create a download response with a given file name
  103. * return Response::download('path/to/file.jpg', 'your_file.jpg');
  104. * </code>
  105. *
  106. * @param string $path
  107. * @param string $name
  108. * @param array $headers
  109. * @return Response
  110. */
  111. public static function download($path, $name = null, $headers = array())
  112. {
  113. if (is_null($name)) $name = basename($path);
  114. // We'll set some sensible default headers, but merge the array given to
  115. // us so that the developer has the chance to override any of these
  116. // default headers with header values of their own liking.
  117. $headers = array_merge(array(
  118. 'Content-Description' => 'File Transfer',
  119. 'Content-Type' => File::mime(File::extension($path)),
  120. 'Content-Transfer-Encoding' => 'binary',
  121. 'Expires' => 0,
  122. 'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
  123. 'Pragma' => 'public',
  124. 'Content-Length' => File::size($path),
  125. ), $headers);
  126. // Once we create the response, we need to set the content disposition
  127. // header on the response based on the file's name. We'll pass this
  128. // off to the HttpFoundation and let it create the header text.
  129. $response = new static(File::get($path), 200, $headers);
  130. $d = $response->disposition($name);
  131. return $response->header('Content-Disposition', $d);
  132. }
  133. /**
  134. * Create the proper Content-Disposition header.
  135. *
  136. * @param string $file
  137. * @return string
  138. */
  139. public function disposition($file)
  140. {
  141. $type = ResponseHeaderBag::DISPOSITION_ATTACHMENT;
  142. return $this->foundation->headers->makeDisposition($type, $file);
  143. }
  144. /**
  145. * Prepare a response from the given value.
  146. *
  147. * @param mixed $response
  148. * @return Response
  149. */
  150. public static function prepare($response)
  151. {
  152. // We will need to force the response to be a string before closing
  153. // the session since the developer may be utilizing the session
  154. // within the view, and we can't age it until rendering.
  155. if ( ! $response instanceof Response)
  156. {
  157. $response = new static($response);
  158. }
  159. $response->render();
  160. return $response;
  161. }
  162. /**
  163. * Convert the content of the Response to a string and return it.
  164. *
  165. * @return string
  166. */
  167. public function render()
  168. {
  169. // If the content is a stringable object, we'll go ahead and call
  170. // to toString method so that we can get the string content of
  171. // the content object. Otherwise we'll just cast to string.
  172. if (str_object($this->content))
  173. {
  174. $this->content = $this->content->__toString();
  175. }
  176. else
  177. {
  178. $this->content = (string) $this->content;
  179. }
  180. // Once we obtain the string content, we can set the content on
  181. // the HttpFoundation's Response instance in preparation for
  182. // sending it back to client browser when all is finished.
  183. $this->foundation->setContent($this->content);
  184. return $this->content;
  185. }
  186. /**
  187. * Send the headers and content of the response to the browser.
  188. *
  189. * @return void
  190. */
  191. public function send()
  192. {
  193. $this->cookies();
  194. $this->foundation->prepare(Request::foundation());
  195. $this->foundation->send();
  196. }
  197. /**
  198. * Send all of the response headers to the browser.
  199. *
  200. * @return void
  201. */
  202. public function send_headers()
  203. {
  204. $this->foundation->prepare(Request::foundation());
  205. $this->foundation->sendHeaders();
  206. }
  207. /**
  208. * Set the cookies on the HttpFoundation Response.
  209. *
  210. * @return void
  211. */
  212. protected function cookies()
  213. {
  214. $ref = new \ReflectionClass('Symfony\Component\HttpFoundation\Cookie');
  215. // All of the cookies for the response are actually stored on the
  216. // Cookie class until we're ready to send the response back to
  217. // the browser. This allows our cookies to be set easily.
  218. foreach (Cookie::$jar as $name => $cookie)
  219. {
  220. $config = array_values($cookie);
  221. $this->headers()->setCookie($ref->newInstanceArgs($config));
  222. }
  223. }
  224. /**
  225. * Add a header to the array of response headers.
  226. *
  227. * @param string $name
  228. * @param string $value
  229. * @return Response
  230. */
  231. public function header($name, $value)
  232. {
  233. $this->foundation->headers->set($name, $value);
  234. return $this;
  235. }
  236. /**
  237. * Get the HttpFoundation Response headers.
  238. *
  239. * @return ResponseParameterBag
  240. */
  241. public function headers()
  242. {
  243. return $this->foundation->headers;
  244. }
  245. /**
  246. * Get / set the response status code.
  247. *
  248. * @param int $status
  249. * @return mixed
  250. */
  251. public function status($status = null)
  252. {
  253. if (is_null($status))
  254. {
  255. return $this->foundation->getStatusCode();
  256. }
  257. else
  258. {
  259. $this->foundation->setStatusCode($status);
  260. return $this;
  261. }
  262. }
  263. }