request.php 5.26 KB
Newer Older
1
<?php namespace Laravel;
2

Taylor Otwell committed
3
class Request {
Taylor Otwell committed
4 5

	/**
Taylor Otwell committed
6
	 * All of the route instances handling the request.
7
	 *
Taylor Otwell committed
8
	 * @var array
9
	 */
Taylor Otwell committed
10
	public static $route;
11 12

	/**
Taylor Otwell committed
13
	 * The Symfony HttpFoundation Request instance.
14
	 *
Taylor Otwell committed
15
	 * @var HttpFoundation\Request
16
	 */
Taylor Otwell committed
17
	public static $foundation;
Taylor Otwell committed
18 19

	/**
Taylor Otwell committed
20
	 * The request data key that is used to indicate a spoofed request method.
21 22 23
	 *
	 * @var string
	 */
Taylor Otwell committed
24
	const spoofer = '_method';
25 26

	/**
Taylor Otwell committed
27
	 * Get the URI for the current request.
28
	 *
29
	 * @return string
Taylor Otwell committed
30
	 */
31
	public static function uri()
Taylor Otwell committed
32
	{
Phill Sparks committed
33
		return URI::current();
34 35 36
	}

	/**
37 38 39 40
	 * Get the request method.
	 *
	 * @return string
	 */
41
	public static function method()
42
	{
Taylor Otwell committed
43
		$method = static::foundation()->getMethod();
44

Taylor Otwell committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
		return ($method == 'HEAD') ? 'GET' : $method;
	}

	/**
	 * Get a header from the request.
	 *
	 * <code>
	 *		// Get a header from the request
	 *		$referer = Request::header('referer');
	 * </code>
	 *
	 * @param  string  $key
	 * @param  mixed   $default
	 * @return mixed
	 */
	public static function header($key, $default = null)
	{
		return array_get(static::foundation()->headers->all(), $key, $default);
	}

	/**
	 * Get all of the HTTP request headers.
	 *
	 * @return array
	 */
	public static function headers()
	{
		return static::foundation()->headers->all();
73 74 75 76 77 78 79 80 81
	}

	/**
	 * Get an item from the $_SERVER array.
	 *
	 * @param  string  $key
	 * @param  mixed   $default
	 * @return string
	 */
82
	public static function server($key = null, $default = null)
83
	{
84
		return array_get(static::foundation()->server->all(), strtoupper($key), $default);
85 86 87 88 89 90 91
	}

	/**
	 * Determine if the request method is being spoofed by a hidden Form element.
	 *
	 * @return bool
	 */
92
	public static function spoofed()
93
	{
Taylor Otwell committed
94
		return ! is_null(static::foundation()->get(Request::spoofer));
95 96 97 98 99
	}

	/**
	 * Get the requestor's IP address.
	 *
100
	 * @param  mixed   $default
101 102
	 * @return string
	 */
103
	public static function ip($default = '0.0.0.0')
104
	{
105 106
		$client_ip = static::foundation()->getClientIp();
		return $client_ip === NULL ? $default : $client_ip;
107 108 109
	}

	/**
110 111 112 113 114 115
	 * Get the list of acceptable content types for the request.
	 *
	 * @return array
	 */
	public static function accept()
	{
116
		return static::foundation()->getAcceptableContentTypes();
117 118 119 120 121 122 123 124 125 126 127 128 129
	}

	/**
	 * Determine if the request accepts a given content type.
	 *
	 * @return bool
	 */
	public static function accepts($type)
	{
		return in_array($type, static::accept());
	}

	/**
130 131 132 133 134 135 136 137 138 139
	 * Get the languages accepted by the client's browser.
	 *
	 * @return array
	 */
	public static function languages()
	{
		return static::foundation()->getLanguages();
	}

	/**
140
	 * Determine if the current request is using HTTPS.
141
	 *
142
	 * @return bool
143
	 */
144
	public static function secure()
145
	{
146
		return static::foundation()->isSecure() and Config::get('application.ssl');
147 148 149
	}

	/**
Taylor Otwell committed
150 151 152 153 154 155 156 157
	 * Determine if the request has been forged.
	 *
	 * The session CSRF token will be compared to the CSRF token in the request input.
	 *
	 * @return bool
	 */
	public static function forged()
	{
158
		return Input::get(Session::csrf_token) !== Session::token();
Taylor Otwell committed
159 160 161
	}

	/**
162
	 * Determine if the current request is an AJAX request.
163 164 165
	 *
	 * @return bool
	 */
166
	public static function ajax()
167
	{
168
		return static::foundation()->isXmlHttpRequest();
169 170
	}

171
	/**
172 173 174 175 176 177
	 * Get the HTTP referrer for the request.
	 *
	 * @return string
	 */
	public static function referrer()
	{
Taylor Otwell committed
178
		return static::foundation()->headers->get('referer');
179 180 181
	}

	/**
182 183 184 185 186 187 188 189 190 191
	 * Determine if the current request is via the command line.
	 *
	 * @return bool
	 */
	public static function cli()
	{
		return defined('STDIN');
	}

	/**
192 193 194 195 196 197
	 * Get the Laravel environment for the current request.
	 *
	 * @return string|null
	 */
	public static function env()
	{
Taylor Otwell committed
198
		return static::foundation()->server->get('LARAVEL_ENV');
199 200 201
	}

	/**
202 203 204 205 206 207 208 209 210 211 212
	 * Set the Laravel environment for the current request.
	 *
	 * @param  string  $env
	 * @return void
	 */
	public static function set_env($env)
	{
		static::foundation()->server->set('LARAVEL_ENV', $env);
	}

	/**
213 214 215 216 217 218 219 220 221 222 223
	 * Determine the current request environment.
	 *
	 * @param  string  $env
	 * @return bool
	 */
	public static function is_env($env)
	{
		return static::env() === $env;
	}

	/**
224 225 226
	 * Detect the current environment from an environment configuration.
	 *
	 * @param  array        $environments
227
	 * @param  string       $uri
228 229
	 * @return string|null
	 */
230
	public static function detect_env(array $environments, $uri)
231 232 233 234 235
	{
		foreach ($environments as $environment => $patterns)
		{
			// Essentially we just want to loop through each environment pattern
			// and determine if the current URI matches the pattern and if so
236
			// we will simply return the environment for that URI pattern.
237 238
			foreach ($patterns as $pattern)
			{
239
				if (Str::is($pattern, $uri))
240 241 242 243 244 245 246 247
				{
					return $environment;
				}
			}
		}
	}

	/**
248
	 * Get the main route handling the request.
249
	 *
250
	 * @return Route
251
	 */
252
	public static function route()
253
	{
254
		return static::$route;
255
	}
Taylor Otwell committed
256

257
	/**
258 259 260 261 262 263 264 265 266 267
	 * Get the Symfony HttpFoundation Request instance.
	 *
	 * @return HttpFoundation\Request
	 */
	public static function foundation()
	{
		return static::$foundation;
	}

	/**
268 269 270 271 272 273 274 275
	 * Pass any other methods to the Symfony request.
	 *
	 * @param  string  $method
	 * @param  array   $parameters
	 * @return mixed
	 */
	public static function __callStatic($method, $parameters)
	{
276
		return call_user_func_array(array(static::foundation(), $method), $parameters);
277 278
	}

279
}