Files
spa-api/laravel/request.php

267 lines
5.0 KiB
PHP
Raw Normal View History

2012-02-12 14:48:36 -06:00
<?php namespace Laravel; use Closure;
2011-06-08 23:45:08 -05:00
2011-08-30 22:35:32 -05:00
class Request {
2011-08-23 21:04:40 -05:00
/**
2012-03-28 22:06:56 -05:00
* All of the route instances handling the request.
*
2012-03-28 22:06:56 -05:00
* @var array
*/
2012-03-28 22:06:56 -05:00
public static $route;
/**
2012-03-28 22:06:56 -05:00
* The Symfony HttpFoundation Request instance.
*
2012-03-28 22:06:56 -05:00
* @var HttpFoundation\Request
*/
2012-03-28 22:06:56 -05:00
public static $foundation;
2011-10-10 21:34:15 -05:00
2011-09-21 21:46:16 -05:00
/**
* The request data key that is used to indicate a spoofed request method.
*
* @var string
*/
2012-03-28 22:58:49 -05:00
const spoofer = '_method';
2011-09-16 19:59:20 -05:00
2011-06-08 23:45:08 -05:00
/**
2011-10-29 23:30:53 -05:00
* Get the URI for the current request.
2011-08-26 21:42:04 -05:00
*
* @return string
2011-08-15 22:29:11 -05:00
*/
2011-10-29 22:31:50 -05:00
public static function uri()
2011-08-15 22:29:11 -05:00
{
2011-11-15 11:30:34 +00:00
return URI::current();
}
2011-06-08 23:45:08 -05:00
/**
* Get the request method.
*
* @return string
*/
2011-10-29 22:31:50 -05:00
public static function method()
2011-06-08 23:45:08 -05:00
{
2012-03-28 22:58:49 -05:00
$method = static::foundation()->getMethod();
2012-02-23 08:43:55 -06:00
2012-03-28 22:58:49 -05:00
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();
2011-09-20 21:28:19 -05:00
}
/**
* Get an item from the $_SERVER array.
*
* @param string $key
* @param mixed $default
* @return string
*/
2011-10-29 22:31:50 -05:00
public static function server($key = null, $default = null)
2011-09-20 21:28:19 -05:00
{
2012-03-28 22:58:49 -05:00
return array_get(static::foundation()->server->all(), $key, $default);
2011-07-08 12:38:54 -07:00
}
/**
* Determine if the request method is being spoofed by a hidden Form element.
*
* @return bool
*/
2011-10-29 22:31:50 -05:00
public static function spoofed()
2011-07-08 12:38:54 -07:00
{
2012-03-28 22:58:49 -05:00
return ! is_null(static::foundation()->get(Request::spoofer));
2011-06-08 23:45:08 -05:00
}
/**
* Get the requestor's IP address.
*
2011-09-20 21:28:19 -05:00
* @param mixed $default
2011-06-08 23:45:08 -05:00
* @return string
*/
2011-10-29 22:31:50 -05:00
public static function ip($default = '0.0.0.0')
2011-06-08 23:45:08 -05:00
{
return value(static::foundation()->getClientIp(), $default);
2011-06-08 23:45:08 -05:00
}
/**
* Get the list of acceptable content types for the request.
*
* @return array
*/
public static function accept()
{
return static::foundation()->getAcceptableContentTypes();
}
/**
* Determine if the request accepts a given content type.
*
* @return bool
*/
public static function accepts($type)
{
return in_array($type, static::accept());
}
/**
* Get the languages accepted by the client's browser.
*
* @return array
*/
public static function languages()
{
return static::foundation()->getLanguages();
}
2011-06-08 23:45:08 -05:00
/**
2011-09-20 21:28:19 -05:00
* Determine if the current request is using HTTPS.
2011-06-08 23:45:08 -05:00
*
* @return bool
2011-06-08 23:45:08 -05:00
*/
2011-10-29 22:31:50 -05:00
public static function secure()
2011-06-08 23:45:08 -05:00
{
return static::foundation()->isSecure() and Config::get('application.ssl');
2011-06-08 23:45:08 -05:00
}
2011-11-02 21:27:43 -05:00
/**
* 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()
{
2012-01-16 13:59:24 -06:00
return Input::get(Session::csrf_token) !== Session::token();
2011-11-02 21:27:43 -05:00
}
2011-06-08 23:45:08 -05:00
/**
2011-09-20 21:28:19 -05:00
* Determine if the current request is an AJAX request.
2011-06-08 23:45:08 -05:00
*
* @return bool
*/
2011-10-29 22:31:50 -05:00
public static function ajax()
2011-06-08 23:45:08 -05:00
{
return static::foundation()->isXmlHttpRequest();
2011-06-08 23:45:08 -05:00
}
2012-03-26 15:30:01 -05:00
/**
* Get the HTTP referrer for the request.
*
* @return string
*/
public static function referrer()
{
2012-03-28 22:58:49 -05:00
return static::foundation()->headers->get('referer');
2012-03-26 15:30:01 -05:00
}
2012-01-16 13:59:24 -06:00
/**
* Determine if the current request is via the command line.
*
* @return bool
*/
public static function cli()
{
return defined('STDIN');
}
/**
* Get the Laravel environment for the current request.
*
* @return string|null
*/
public static function env()
{
2012-03-28 22:58:49 -05:00
return static::foundation()->server->get('LARAVEL_ENV');
}
/**
* Determine the current request environment.
*
* @param string $env
* @return bool
*/
public static function is_env($env)
{
return static::env() === $env;
}
/**
* Detect the current environment from an environment configuration.
*
* @param array $environments
* @param string $uri
* @return string|null
*/
public static function detect_env(array $environments, $uri)
{
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
// we'll simply return the environment for that URI pattern.
foreach ($patterns as $pattern)
{
if (Str::is($pattern, $uri))
{
return $environment;
}
}
}
}
/**
2012-02-12 14:48:36 -06:00
* Get the main route handling the request.
*
* @return Route
*/
2011-10-29 22:31:50 -05:00
public static function route()
{
2011-10-29 22:31:50 -05:00
return static::$route;
}
2011-08-23 21:04:40 -05:00
/**
* Get the Symfony HttpFoundation Request instance.
*
* @return HttpFoundation\Request
*/
public static function foundation()
{
return static::$foundation;
}
/**
* Pass any other methods to the Symfony request.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public static function __callStatic($method, $parameters)
{
return call_user_func_array(array(static::foundation(), $method), $parameters);
}
2011-11-23 10:32:17 -06:00
}