Skip to content

Commit 50218c2

Browse files
committed
Merge pull request #197 from DanielPlainview/feature-http-request-create-from-globals
Инициализация HttpRequest из super globals
2 parents 144c08a + f2a50b0 commit 50218c2

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

main/Flow/HttpRequest.class.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ final class HttpRequest
4646
* @var HttpUrl
4747
*/
4848
private $url = null;
49-
49+
5050
//for CurlHttpClient if you need to send raw CURLOPT_POSTFIELDS
5151
private $body = null;
5252

@@ -55,7 +55,36 @@ final class HttpRequest
5555
**/
5656
public static function create()
5757
{
58-
return new self;
58+
return new static();
59+
}
60+
61+
/**
62+
* @return HttpRequest
63+
**/
64+
public static function createFromGlobals()
65+
{
66+
$request =
67+
static::create()->
68+
setGet($_GET)->
69+
setPost($_POST)->
70+
setServer($_SERVER)->
71+
setCookie($_COOKIE)->
72+
setFiles($_FILES);
73+
74+
if (isset($_SESSION))
75+
$request->setSession($_SESSION);
76+
77+
foreach ($_SERVER as $name => $value)
78+
if (substr($name, 0, 5) === 'HTTP_')
79+
$request->setHeaderVar(substr($name, 5), $value);
80+
81+
if (
82+
$request->hasServerVar('CONTENT_TYPE')
83+
&& $request->getServerVar('CONTENT_TYPE') !== 'application/x-www-form-urlencoded'
84+
)
85+
$request->setBody(file_get_contents('php://input'));
86+
87+
return $request;
5988
}
6089

6190
public function &getGet()
@@ -147,7 +176,7 @@ public function hasServerVar($name)
147176
public function setServer(array $server)
148177
{
149178
$this->server = $server;
150-
179+
151180
return $this;
152181
}
153182

0 commit comments

Comments
 (0)