From 573725ade78db0d9091b9ad2fa36d0f61628511a Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 28 Mar 2012 16:15:21 -0500 Subject: [PATCH] use symfony http foundation where applicable. --- laravel/core.php | 29 +++++++-- laravel/request.php | 60 ++++++++++++------ laravel/uri.php | 147 +++----------------------------------------- 3 files changed, 73 insertions(+), 163 deletions(-) diff --git a/laravel/core.php b/laravel/core.php index c3949ab4..8cff1b7c 100644 --- a/laravel/core.php +++ b/laravel/core.php @@ -92,14 +92,18 @@ | Register The Symfony Components |-------------------------------------------------------------------------- | -| Laravel's "Artisan" CLI makes use of the Symfony Console component to -| build a wonderful CLI environment that is both robust and testable. -| We'll register the component's namespace here. +| Laravel makes use of the Symfony components where the situation is +| applicable and it is possible to do so. This allows us to focus +| on the parts of the framework that are unique and not re-do +| plumbing code that others have written. | */ Autoloader::namespaces(array( - 'Symfony\Component\Console' => path('base').'vendor/Symfony/Component/Console', + 'Symfony\Component\Console' + => path('base').'vendor/Symfony/Component/Console', + 'Symfony\Component\HttpFoundation' + => path('base').'vendor/Symfony/Component/HttpFoundation', )); /* @@ -157,4 +161,19 @@ foreach ($bundles as $bundle => $config) { Bundle::register($bundle, $config); -} \ No newline at end of file +} + +/* +|-------------------------------------------------------------------------- +| Register The Laravel Bundles +|-------------------------------------------------------------------------- +| +| Finally we will register all of the bundles that have been defined for +| the application. None of them will be started, yet but will be setup +| so that they may be started by the develop at any time. +| +*/ + +use Symfony\Component\HttpFoundation\Request as FoundationRequest; + +Request::$request = FoundationRequest::createFromGlobals(); \ No newline at end of file diff --git a/laravel/request.php b/laravel/request.php index 59d0ec7e..0b527fef 100644 --- a/laravel/request.php +++ b/laravel/request.php @@ -2,6 +2,13 @@ class Request { + /** + * The Symfony HttpFoundation Request instance. + * + * @var HttpFoundation\Request + */ + public static $request; + /** * All of the route instances handling the request. * @@ -71,20 +78,7 @@ public static function spoofed() */ public static function ip($default = '0.0.0.0') { - if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) - { - return $_SERVER['HTTP_X_FORWARDED_FOR']; - } - elseif (isset($_SERVER['HTTP_CLIENT_IP'])) - { - return $_SERVER['HTTP_CLIENT_IP']; - } - elseif (isset($_SERVER['REMOTE_ADDR'])) - { - return $_SERVER['REMOTE_ADDR']; - } - - return value($default); + return value(static::$request->getClientIp(), $default); } /** @@ -97,6 +91,26 @@ public static function protocol() return array_get($_SERVER, 'SERVER_PROTOCOL', 'HTTP/1.1'); } + /** + * Get the list of acceptable content types for the request. + * + * @return array + */ + public static function accept() + { + return static::$request->getAcceptableContentTypes(); + } + + /** + * Determine if the request accepts a given content type. + * + * @return bool + */ + public static function accepts($type) + { + return in_array($type, static::accept()); + } + /** * Determine if the current request is using HTTPS. * @@ -104,7 +118,7 @@ public static function protocol() */ public static function secure() { - return isset($_SERVER['HTTPS']) and strtolower($_SERVER['HTTPS']) !== 'off'; + return static::$request->isSecure(); } /** @@ -126,9 +140,7 @@ public static function forged() */ public static function ajax() { - if ( ! isset($_SERVER['HTTP_X_REQUESTED_WITH'])) return false; - - return strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest'; + return static::$request->isXmlHttpRequest(); } /** @@ -182,4 +194,16 @@ public static function route() return static::$route; } + /** + * 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::$request, $method), $parameters); + } + } \ No newline at end of file diff --git a/laravel/uri.php b/laravel/uri.php index d342f565..47820eeb 100644 --- a/laravel/uri.php +++ b/laravel/uri.php @@ -16,16 +16,6 @@ class URI { */ public static $segments = array(); - /** - * The server variables to check for the URI. - * - * @var array - */ - protected static $attempt = array( - 'PATH_INFO', 'REQUEST_URI', - 'PHP_SELF', 'REDIRECT_URL' - ); - /** * Get the full URI including the query string. * @@ -33,7 +23,7 @@ class URI { */ public static function full() { - return static::current().static::query(); + return Request::getUri(); } /** @@ -45,45 +35,14 @@ public static function current() { if ( ! is_null(static::$uri)) return static::$uri; - // To get the URI, we'll first call the detect method which will spin - // through each of the server variables that we check for the URI in - // and use the first one we encounter for the URI. - static::$uri = static::detect(); + // We'll simply get the path info from the Symfony Request instance and then + // format to meet our needs in the router. If the URI is root, we'll give + // back a single slash, otherwise we'll strip the slashes. + $uri = static::format(Request::getPathInfo()); - // If you ever encounter this error, please inform the nerdy Laravel - // dev team with information about your server. We want to support - // Laravel an as many servers as we possibly can! - if (is_null(static::$uri)) - { - throw new \Exception("Could not detect request URI."); - } + static::segments($uri); - static::segments(static::$uri); - - return static::$uri; - } - - /** - * Detect the URI from the server variables. - * - * @return string|null - */ - protected static function detect() - { - foreach (static::$attempt as $variable) - { - // Each variable we search for the URI has its own parser function - // which is responsible for doing any formatting before the value - // is fed into the main formatting function. - $method = "parse_{$variable}"; - - if (isset($_SERVER[$variable])) - { - $uri = static::$method($_SERVER[$variable]); - - return static::format($uri); - } - } + return static::$uri = $uri; } /** @@ -94,21 +53,6 @@ protected static function detect() */ protected static function format($uri) { - // First we want to remove the application's base URL from the URI if it is - // in the string. It is possible for some of the parsed server variables to - // include the entire document root in the string. - $uri = static::remove_base($uri); - - $index = '/'.Config::get('application.index'); - - // Next we'll remove the index file from the URI if it is there and then - // finally trim down the URI. If the URI is left with spaces, we'll use - // a single slash for the root URI. - if ($index !== '/') - { - $uri = static::remove($uri, $index); - } - return trim($uri, '/') ?: '/'; } @@ -138,61 +82,6 @@ public static function is($pattern, $uri = null) return preg_match('#'.$pattern.'#', $uri); } - /** - * Parse the PATH_INFO server variable. - * - * @param string $value - * @return string - */ - protected static function parse_path_info($value) - { - return $value; - } - - /** - * Parse the REQUEST_URI server variable. - * - * @param string $value - * @return string - */ - protected static function parse_request_uri($value) - { - return parse_url($value, PHP_URL_PATH); - } - - /** - * Parse the PHP_SELF server variable. - * - * @param string $value - * @return string - */ - protected static function parse_php_self($value) - { - return $value; - } - - /** - * Parse the REDIRECT_URL server variable. - * - * @param string $value - * @return string - */ - protected static function parse_redirect_url($value) - { - return $value; - } - - /** - * Remove the base URL off of the request URI. - * - * @param string $uri - * @return string - */ - protected static function remove_base($uri) - { - return static::remove($uri, parse_url(URL::base(), PHP_URL_PATH)); - } - /** * Get a specific segment of the request URI via an one-based index. * @@ -228,26 +117,4 @@ protected static function segments($uri) static::$segments = array_diff($segments, array('')); } - /** - * Remove a given value from the URI. - * - * @param string $uri - * @param string $value - * @return string - */ - protected static function remove($uri, $value) - { - return (strpos($uri, $value) === 0) ? substr($uri, strlen($value)) : $uri; - } - - /** - * Get the query string for the current request. - * - * @return string - */ - protected static function query() - { - return (count((array) $_GET) > 0) ? '?'.http_build_query($_GET) : ''; - } - } \ No newline at end of file