response.php 8.3 KB

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