PageRenderTime 25ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/laravel/laravel/response.php

https://bitbucket.org/xbdvh/hoa-tri-todolist
PHP | 374 lines | 129 code | 43 blank | 202 comment | 5 complexity | d5fcb453f4352fbbd9bfac17b6b8aa0a MD5 | raw file
  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. // If the Content-Disposition header has already been set by the
  186. // merge above, then do not override it with out generated one.
  187. if (!isset($headers['Content-Disposition'])) {
  188. $d = $response->disposition($name);
  189. $response = $response->header('Content-Disposition', $d);
  190. }
  191. return $response;
  192. }
  193. /**
  194. * Create the proper Content-Disposition header.
  195. *
  196. * @param string $file
  197. * @return string
  198. */
  199. public function disposition($file)
  200. {
  201. $type = ResponseHeaderBag::DISPOSITION_ATTACHMENT;
  202. return $this->foundation->headers->makeDisposition($type, $file);
  203. }
  204. /**
  205. * Prepare a response from the given value.
  206. *
  207. * @param mixed $response
  208. * @return Response
  209. */
  210. public static function prepare($response)
  211. {
  212. // We will need to force the response to be a string before closing
  213. // the session since the developer may be utilizing the session
  214. // within the view, and we can't age it until rendering.
  215. if ( ! $response instanceof Response)
  216. {
  217. $response = new static($response);
  218. }
  219. return $response;
  220. }
  221. /**
  222. * Send the headers and content of the response to the browser.
  223. *
  224. * @return void
  225. */
  226. public function send()
  227. {
  228. $this->cookies();
  229. $this->foundation->prepare(Request::foundation());
  230. $this->foundation->send();
  231. }
  232. /**
  233. * Convert the content of the Response to a string and return it.
  234. *
  235. * @return string
  236. */
  237. public function render()
  238. {
  239. // If the content is a stringable object, we'll go ahead and call
  240. // the toString method so that we can get the string content of
  241. // the content object. Otherwise we'll just cast to string.
  242. if (str_object($this->content))
  243. {
  244. $this->content = $this->content->__toString();
  245. }
  246. else
  247. {
  248. $this->content = (string) $this->content;
  249. }
  250. // Once we obtain the string content, we can set the content on
  251. // the HttpFoundation's Response instance in preparation for
  252. // sending it back to client browser when all is finished.
  253. $this->foundation->setContent($this->content);
  254. return $this->content;
  255. }
  256. /**
  257. * Send all of the response headers to the browser.
  258. *
  259. * @return void
  260. */
  261. public function send_headers()
  262. {
  263. $this->foundation->prepare(Request::foundation());
  264. $this->foundation->sendHeaders();
  265. }
  266. /**
  267. * Set the cookies on the HttpFoundation Response.
  268. *
  269. * @return void
  270. */
  271. protected function cookies()
  272. {
  273. $ref = new \ReflectionClass('Symfony\Component\HttpFoundation\Cookie');
  274. // All of the cookies for the response are actually stored on the
  275. // Cookie class until we're ready to send the response back to
  276. // the browser. This allows our cookies to be set easily.
  277. foreach (Cookie::$jar as $name => $cookie)
  278. {
  279. $config = array_values($cookie);
  280. $this->headers()->setCookie($ref->newInstanceArgs($config));
  281. }
  282. }
  283. /**
  284. * Add a header to the array of response headers.
  285. *
  286. * @param string $name
  287. * @param string $value
  288. * @return Response
  289. */
  290. public function header($name, $value)
  291. {
  292. $this->foundation->headers->set($name, $value);
  293. return $this;
  294. }
  295. /**
  296. * Get the HttpFoundation Response headers.
  297. *
  298. * @return ResponseParameterBag
  299. */
  300. public function headers()
  301. {
  302. return $this->foundation->headers;
  303. }
  304. /**
  305. * Get / set the response status code.
  306. *
  307. * @param int $status
  308. * @return mixed
  309. */
  310. public function status($status = null)
  311. {
  312. if (is_null($status))
  313. {
  314. return $this->foundation->getStatusCode();
  315. }
  316. else
  317. {
  318. $this->foundation->setStatusCode($status);
  319. return $this;
  320. }
  321. }
  322. /**
  323. * Render the response when cast to string
  324. *
  325. * @return string
  326. */
  327. public function __toString()
  328. {
  329. return $this->render();
  330. }
  331. }