response.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php namespace Laravel;
  2. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  3. use Symfony\Component\HttpFoundation\LaravelResponse 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 JSON response.
  74. *
  75. * <code>
  76. * // Create a response instance with JSON
  77. * return Response::json($data, 200, array('header' => 'value'));
  78. * </code>
  79. *
  80. * @param mixed $data
  81. * @param int $status
  82. * @param array $headers
  83. * @return Response
  84. */
  85. public static function json($data, $status = 200, $headers = array())
  86. {
  87. $headers['Content-Type'] = 'application/json; charset=utf-8';
  88. return new static(json_encode($data), $status, $headers);
  89. }
  90. /**
  91. * Create a new response of JSON'd Eloquent models.
  92. *
  93. * <code>
  94. * // Create a new response instance with Eloquent models
  95. * return Response::eloquent($data, 200, array('header' => 'value'));
  96. * </code>
  97. *
  98. * @param Eloquent|array $data
  99. * @param int $status
  100. * @param array $headers
  101. * @return Response
  102. */
  103. public static function eloquent($data, $status = 200, $headers = array())
  104. {
  105. $headers['Content-Type'] = 'application/json; charset=utf-8';
  106. return new static(eloquent_to_json($data), $status, $headers);
  107. }
  108. /**
  109. * Create a new error response instance.
  110. *
  111. * The response status code will be set using the specified code.
  112. *
  113. * The specified error should match a view in your views/error directory.
  114. *
  115. * <code>
  116. * // Create a 404 response
  117. * return Response::error('404');
  118. *
  119. * // Create a 404 response with data
  120. * return Response::error('404', array('message' => 'Not Found'));
  121. * </code>
  122. *
  123. * @param int $code
  124. * @param array $data
  125. * @return Response
  126. */
  127. public static function error($code, $data = array())
  128. {
  129. return new static(View::make('error.'.$code, $data), $code);
  130. }
  131. /**
  132. * Create a new download response instance.
  133. *
  134. * <code>
  135. * // Create a download response to a given file
  136. * return Response::download('path/to/file.jpg');
  137. *
  138. * // Create a download response with a given file name
  139. * return Response::download('path/to/file.jpg', 'your_file.jpg');
  140. * </code>
  141. *
  142. * @param string $path
  143. * @param string $name
  144. * @param array $headers
  145. * @return Response
  146. */
  147. public static function download($path, $name = null, $headers = array())
  148. {
  149. if (is_null($name)) $name = basename($path);
  150. // We'll set some sensible default headers, but merge the array given to
  151. // us so that the developer has the chance to override any of these
  152. // default headers with header values of their own liking.
  153. $headers = array_merge(array(
  154. 'Content-Description' => 'File Transfer',
  155. 'Content-Type' => File::mime(File::extension($path)),
  156. 'Content-Transfer-Encoding' => 'binary',
  157. 'Expires' => 0,
  158. 'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
  159. 'Pragma' => 'public',
  160. 'Content-Length' => File::size($path),
  161. ), $headers);
  162. // Once we create the response, we need to set the content disposition
  163. // header on the response based on the file's name. We'll pass this
  164. // off to the HttpFoundation and let it create the header text.
  165. $response = new static(File::get($path), 200, $headers);
  166. $d = $response->disposition($name);
  167. return $response->header('Content-Disposition', $d);
  168. }
  169. /**
  170. * Create the proper Content-Disposition header.
  171. *
  172. * @param string $file
  173. * @return string
  174. */
  175. public function disposition($file)
  176. {
  177. $type = ResponseHeaderBag::DISPOSITION_ATTACHMENT;
  178. return $this->foundation->headers->makeDisposition($type, $file);
  179. }
  180. /**
  181. * Prepare a response from the given value.
  182. *
  183. * @param mixed $response
  184. * @return Response
  185. */
  186. public static function prepare($response)
  187. {
  188. // We will need to force the response to be a string before closing
  189. // the session since the developer may be utilizing the session
  190. // within the view, and we can't age it until rendering.
  191. if ( ! $response instanceof Response)
  192. {
  193. $response = new static($response);
  194. }
  195. return $response;
  196. }
  197. /**
  198. * Send the headers and content of the response to the browser.
  199. *
  200. * @return void
  201. */
  202. public function send()
  203. {
  204. $this->cookies();
  205. $this->foundation->prepare(Request::foundation());
  206. $this->foundation->send();
  207. }
  208. /**
  209. * Convert the content of the Response to a string and return it.
  210. *
  211. * @return string
  212. */
  213. public function render()
  214. {
  215. // If the content is a stringable object, we'll go ahead and call
  216. // the toString method so that we can get the string content of
  217. // the content object. Otherwise we'll just cast to string.
  218. if (str_object($this->content))
  219. {
  220. $this->content = $this->content->__toString();
  221. }
  222. else
  223. {
  224. $this->content = (string) $this->content;
  225. }
  226. // Once we obtain the string content, we can set the content on
  227. // the HttpFoundation's Response instance in preparation for
  228. // sending it back to client browser when all is finished.
  229. $this->foundation->setContent($this->content);
  230. return $this->content;
  231. }
  232. /**
  233. * Send all of the response headers to the browser.
  234. *
  235. * @return void
  236. */
  237. public function send_headers()
  238. {
  239. $this->foundation->prepare(Request::foundation());
  240. $this->foundation->sendHeaders();
  241. }
  242. /**
  243. * Set the cookies on the HttpFoundation Response.
  244. *
  245. * @return void
  246. */
  247. protected function cookies()
  248. {
  249. $ref = new \ReflectionClass('Symfony\Component\HttpFoundation\Cookie');
  250. // All of the cookies for the response are actually stored on the
  251. // Cookie class until we're ready to send the response back to
  252. // the browser. This allows our cookies to be set easily.
  253. foreach (Cookie::$jar as $name => $cookie)
  254. {
  255. $config = array_values($cookie);
  256. $this->headers()->setCookie($ref->newInstanceArgs($config));
  257. }
  258. }
  259. /**
  260. * Add a header to the array of response headers.
  261. *
  262. * @param string $name
  263. * @param string $value
  264. * @return Response
  265. */
  266. public function header($name, $value)
  267. {
  268. $this->foundation->headers->set($name, $value);
  269. return $this;
  270. }
  271. /**
  272. * Get the HttpFoundation Response headers.
  273. *
  274. * @return ResponseParameterBag
  275. */
  276. public function headers()
  277. {
  278. return $this->foundation->headers;
  279. }
  280. /**
  281. * Get / set the response status code.
  282. *
  283. * @param int $status
  284. * @return mixed
  285. */
  286. public function status($status = null)
  287. {
  288. if (is_null($status))
  289. {
  290. return $this->foundation->getStatusCode();
  291. }
  292. else
  293. {
  294. $this->foundation->setStatusCode($status);
  295. return $this;
  296. }
  297. }
  298. /**
  299. * Render the response when cast to string
  300. *
  301. * @return string
  302. */
  303. public function __toString()
  304. {
  305. return $this->render();
  306. }
  307. }