Skip to content

Commit 079f4cc

Browse files
committed
Merge pull request #197 from DanielPlainview/feature-http-request-create-from-globals
Инициализация HttpRequest из super globals Conflicts: main/Flow/HttpRequest.class.php
1 parent f14ed4c commit 079f4cc

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

main/Flow/HttpRequest.class.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,45 @@ final class HttpRequest
4040
private $method = null;
4141

4242
private $url = null;
43+
44+
//for CurlHttpClient if you need to send raw CURLOPT_POSTFIELDS
45+
private $body = null;
4346

4447
/**
4548
* @return HttpRequest
4649
**/
4750
public static function create()
4851
{
49-
return new self;
52+
return new static();
53+
}
54+
55+
/**
56+
* @return HttpRequest
57+
**/
58+
public static function createFromGlobals()
59+
{
60+
$request =
61+
static::create()->
62+
setGet($_GET)->
63+
setPost($_POST)->
64+
setServer($_SERVER)->
65+
setCookie($_COOKIE)->
66+
setFiles($_FILES);
67+
68+
if (isset($_SESSION))
69+
$request->setSession($_SESSION);
70+
71+
foreach ($_SERVER as $name => $value)
72+
if (substr($name, 0, 5) === 'HTTP_')
73+
$request->setHeaderVar(substr($name, 5), $value);
74+
75+
if (
76+
$request->hasServerVar('CONTENT_TYPE')
77+
&& $request->getServerVar('CONTENT_TYPE') !== 'application/x-www-form-urlencoded'
78+
)
79+
$request->setBody(file_get_contents('php://input'));
80+
81+
return $request;
5082
}
5183

5284
public function &getGet()
@@ -138,7 +170,7 @@ public function hasServerVar($name)
138170
public function setServer(array $server)
139171
{
140172
$this->server = $server;
141-
173+
142174
return $this;
143175
}
144176

0 commit comments

Comments
 (0)