request.php 5.5 KB

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