From 85e5496d9dbdd8de91a875841670004dd32edec0 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 6 Apr 2010 17:44:52 +0200 Subject: [PATCH] [RequestHandler] added Request::createFromUri() --- .../Components/RequestHandler/Request.php | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/Symfony/Components/RequestHandler/Request.php b/src/Symfony/Components/RequestHandler/Request.php index 2e0734de0a..fcf9320ab7 100644 --- a/src/Symfony/Components/RequestHandler/Request.php +++ b/src/Symfony/Components/RequestHandler/Request.php @@ -92,6 +92,57 @@ class Request implements RequestInterface $this->format = null; } + /** + * Creates a Request based on a given URI and configuration. + * + * @param string $uri The URI + * @param string $method The HTTP method + * @param array $parameters The request (GET) or query (POST) parameters + * @param array $cookies The request cookies ($_COOKIE) + * @param array $files The request files ($_FILES) + * @param array $server The server parameters ($_SERVER) + * + */ + static public function createFromUri($uri, $method = 'get', $parameters = array(), $cookies = array(), $files = array(), $server = array()) + { + if (in_array($method, array('post', 'put', 'delete'))) + { + $request = $parameters; + $query = array(); + } + else + { + $request = array(); + $query = $parameters; + } + + $queryString = false !== ($pos = strpos($uri, '?')) ? html_entity_decode(substr($uri, $pos + 1)) : ''; + parse_str($queryString, $qs); + if (is_array($qs)) + { + $query = array_replace($qs, $query); + } + + $server = array_replace(array( + 'HTTP_HOST' => 'localhost', + 'SERVER_NAME' => 'localhost', + 'SERVER_PORT' => 80, + 'HTTP_USER_AGENT' => 'SymfonyClient/1.0', + 'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', + 'HTTP_ACCEPT_LANGUAGE' => 'en-us,en;q=0.5', + 'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7', + 'REMOTE_ADDR' => '127.0.0.1', + 'REQUEST_METHOD' => strtoupper($method), + 'PATH_INFO' => '', + 'REQUEST_URI' => $uri, + 'SCRIPT_NAME' => '', + 'SCRIPT_FILENAME' => '', + 'QUERY_STRING' => $queryString, + ), $server); + + return new self($request, $query, array(), $cookies, $files, $server); + } + /** * Clones a request and overrides some of its parameters. *