request.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php namespace Laravel; use Closure;
  2. class Request {
  3. /**
  4. * All of the route instances handling the request.
  5. *
  6. * @var array
  7. */
  8. public static $route;
  9. /**
  10. * The JSON payload for applications using Backbone.js or similar.
  11. *
  12. * @var object
  13. */
  14. public static $json;
  15. /**
  16. * The Symfony HttpFoundation Request instance.
  17. *
  18. * @var HttpFoundation\Request
  19. */
  20. public static $foundation;
  21. /**
  22. * The request data key that is used to indicate a spoofed request method.
  23. *
  24. * @var string
  25. */
  26. const spoofer = '_method';
  27. /**
  28. * Get the URI for the current request.
  29. *
  30. * @return string
  31. */
  32. public static function uri()
  33. {
  34. return URI::current();
  35. }
  36. /**
  37. * Get the request method.
  38. *
  39. * @return string
  40. */
  41. public static function method()
  42. {
  43. $method = static::foundation()->getMethod();
  44. return ($method == 'HEAD') ? 'GET' : $method;
  45. }
  46. /**
  47. * Get a header from the request.
  48. *
  49. * <code>
  50. * // Get a header from the request
  51. * $referer = Request::header('referer');
  52. * </code>
  53. *
  54. * @param string $key
  55. * @param mixed $default
  56. * @return mixed
  57. */
  58. public static function header($key, $default = null)
  59. {
  60. return array_get(static::foundation()->headers->all(), $key, $default);
  61. }
  62. /**
  63. * Get all of the HTTP request headers.
  64. *
  65. * @return array
  66. */
  67. public static function headers()
  68. {
  69. return static::foundation()->headers->all();
  70. }
  71. /**
  72. * Get the JSON payload for the request.
  73. *
  74. * @return object
  75. */
  76. public static function json()
  77. {
  78. if ( ! is_null(static::$json)) return static::$json;
  79. return static::$json = json_decode(static::foundation()->getContent());
  80. }
  81. /**
  82. * Get an item from the $_SERVER array.
  83. *
  84. * @param string $key
  85. * @param mixed $default
  86. * @return string
  87. */
  88. public static function server($key = null, $default = null)
  89. {
  90. return array_get(static::foundation()->server->all(), $key, $default);
  91. }
  92. /**
  93. * Determine if the request method is being spoofed by a hidden Form element.
  94. *
  95. * @return bool
  96. */
  97. public static function spoofed()
  98. {
  99. return ! is_null(static::foundation()->get(Request::spoofer));
  100. }
  101. /**
  102. * Get the requestor's IP address.
  103. *
  104. * @param mixed $default
  105. * @return string
  106. */
  107. public static function ip($default = '0.0.0.0')
  108. {
  109. return value(static::foundation()->getClientIp(), $default);
  110. }
  111. /**
  112. * Get the list of acceptable content types for the request.
  113. *
  114. * @return array
  115. */
  116. public static function accept()
  117. {
  118. return static::foundation()->getAcceptableContentTypes();
  119. }
  120. /**
  121. * Determine if the request accepts a given content type.
  122. *
  123. * @return bool
  124. */
  125. public static function accepts($type)
  126. {
  127. return in_array($type, static::accept());
  128. }
  129. /**
  130. * Get the languages accepted by the client's browser.
  131. *
  132. * @return array
  133. */
  134. public static function languages()
  135. {
  136. return static::foundation()->getLanguages();
  137. }
  138. /**
  139. * Determine if the current request is using HTTPS.
  140. *
  141. * @return bool
  142. */
  143. public static function secure()
  144. {
  145. return static::foundation()->isSecure() and Config::get('application.ssl');
  146. }
  147. /**
  148. * Determine if the request has been forged.
  149. *
  150. * The session CSRF token will be compared to the CSRF token in the request input.
  151. *
  152. * @return bool
  153. */
  154. public static function forged()
  155. {
  156. return Input::get(Session::csrf_token) !== Session::token();
  157. }
  158. /**
  159. * Determine if the current request is an AJAX request.
  160. *
  161. * @return bool
  162. */
  163. public static function ajax()
  164. {
  165. return static::foundation()->isXmlHttpRequest();
  166. }
  167. /**
  168. * Get the HTTP referrer for the request.
  169. *
  170. * @return string
  171. */
  172. public static function referrer()
  173. {
  174. return static::foundation()->headers->get('referer');
  175. }
  176. /**
  177. * Determine if the current request is via the command line.
  178. *
  179. * @return bool
  180. */
  181. public static function cli()
  182. {
  183. return defined('STDIN');
  184. }
  185. /**
  186. * Get the Laravel environment for the current request.
  187. *
  188. * @return string|null
  189. */
  190. public static function env()
  191. {
  192. return static::foundation()->server->get('LARAVEL_ENV');
  193. }
  194. /**
  195. * Set the Laravel environment for the current request.
  196. *
  197. * @param string $env
  198. * @return void
  199. */
  200. public static function set_env($env)
  201. {
  202. static::foundation()->server->set('LARAVEL_ENV', $env);
  203. }
  204. /**
  205. * Determine the current request environment.
  206. *
  207. * @param string $env
  208. * @return bool
  209. */
  210. public static function is_env($env)
  211. {
  212. return static::env() === $env;
  213. }
  214. /**
  215. * Detect the current environment from an environment configuration.
  216. *
  217. * @param array $environments
  218. * @param string $uri
  219. * @return string|null
  220. */
  221. public static function detect_env(array $environments, $uri)
  222. {
  223. foreach ($environments as $environment => $patterns)
  224. {
  225. // Essentially we just want to loop through each environment pattern
  226. // and determine if the current URI matches the pattern and if so
  227. // we will simply return the environment for that URI pattern.
  228. foreach ($patterns as $pattern)
  229. {
  230. if (Str::is($pattern, $uri))
  231. {
  232. return $environment;
  233. }
  234. }
  235. }
  236. }
  237. /**
  238. * Get the main route handling the request.
  239. *
  240. * @return Route
  241. */
  242. public static function route()
  243. {
  244. return static::$route;
  245. }
  246. /**
  247. * Get the Symfony HttpFoundation Request instance.
  248. *
  249. * @return HttpFoundation\Request
  250. */
  251. public static function foundation()
  252. {
  253. return static::$foundation;
  254. }
  255. /**
  256. * Pass any other methods to the Symfony request.
  257. *
  258. * @param string $method
  259. * @param array $parameters
  260. * @return mixed
  261. */
  262. public static function __callStatic($method, $parameters)
  263. {
  264. return call_user_func_array(array(static::foundation(), $method), $parameters);
  265. }
  266. }