response.php 8.9 KB

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