From f2a50b0b19181c77ef168f7adfec3ea2550ea70c Mon Sep 17 00:00:00 2001 From: Nikita Konstantinov Date: Sat, 5 Oct 2013 23:10:49 +0400 Subject: [PATCH] Add HttpRequest::createFromGlobals() --- main/Flow/HttpRequest.class.php | 35 ++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/main/Flow/HttpRequest.class.php b/main/Flow/HttpRequest.class.php index 44c044a6cb..6fefa0b644 100644 --- a/main/Flow/HttpRequest.class.php +++ b/main/Flow/HttpRequest.class.php @@ -46,7 +46,7 @@ final class HttpRequest * @var HttpUrl */ private $url = null; - + //for CurlHttpClient if you need to send raw CURLOPT_POSTFIELDS private $body = null; @@ -55,7 +55,36 @@ final class HttpRequest **/ public static function create() { - return new self; + return new static(); + } + + /** + * @return HttpRequest + **/ + public static function createFromGlobals() + { + $request = + static::create()-> + setGet($_GET)-> + setPost($_POST)-> + setServer($_SERVER)-> + setCookie($_COOKIE)-> + setFiles($_FILES); + + if (isset($_SESSION)) + $request->setSession($_SESSION); + + foreach ($_SERVER as $name => $value) + if (substr($name, 0, 5) === 'HTTP_') + $request->setHeaderVar(substr($name, 5), $value); + + if ( + $request->hasServerVar('CONTENT_TYPE') + && $request->getServerVar('CONTENT_TYPE') !== 'application/x-www-form-urlencoded' + ) + $request->setBody(file_get_contents('php://input')); + + return $request; } public function &getGet() @@ -147,7 +176,7 @@ public function hasServerVar($name) public function setServer(array $server) { $this->server = $server; - + return $this; }