1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CapMousse\ReactRestify\Http; |
4
|
|
|
|
5
|
|
|
use React\Http\Request as ReactHttpRequest; |
6
|
|
|
use Evenement\EventEmitter; |
7
|
|
|
|
8
|
|
|
class Request extends EventEmitter |
9
|
|
|
{ |
10
|
|
|
/** @var React\Http\Request */ |
11
|
|
|
public $httpRequest; |
12
|
|
|
|
13
|
|
|
/** @var mixed */ |
14
|
|
|
private $content; |
15
|
|
|
|
16
|
|
|
/** @var array */ |
17
|
|
|
private $data = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param ReactHttpRequest $httpRequest |
21
|
|
|
*/ |
22
|
|
|
public function __construct(ReactHttpRequest $httpRequest) |
23
|
|
|
{ |
24
|
|
|
$this->httpRequest = $httpRequest; |
|
|
|
|
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Set the raw data of the request |
29
|
|
|
* @param mixed $content |
30
|
|
|
*/ |
31
|
|
|
public function setContent($content) |
32
|
|
|
{ |
33
|
|
|
$this->content = $content; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Set the raw data of the request |
38
|
|
|
* @param mixed $content |
|
|
|
|
39
|
|
|
*/ |
40
|
|
|
public function getContent() |
41
|
|
|
{ |
42
|
|
|
return $this->content; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getHeaders() |
46
|
|
|
{ |
47
|
|
|
$headers = array_change_key_case($this->httpRequest->getHeaders(), CASE_LOWER); |
48
|
|
|
$headers = array_map(function ($value) { |
49
|
|
|
return strtolower($value); |
50
|
|
|
}, $headers); |
51
|
|
|
|
52
|
|
|
return $headers; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function parseData() |
56
|
|
|
{ |
57
|
|
|
$headers = $this->getHeaders(); |
58
|
|
|
|
59
|
|
|
if (!in_array($this->httpRequest->getMethod(), ['PUT', 'POST'])) { |
60
|
|
|
return $this->emit('end'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->httpRequest->on('data', function($data) use ($headers, &$dataResult) { |
64
|
|
|
$dataResult .= $data; |
65
|
|
|
|
66
|
|
|
if (isset($headers["Content-Length"])) { |
67
|
|
|
if (strlen($dataResult) == $headers["Content-Length"]) { |
68
|
|
|
$this->httpRequest->close(); |
69
|
|
|
} |
70
|
|
|
} else { |
71
|
|
|
$this->httpRequest->close(); |
72
|
|
|
} |
73
|
|
|
}); |
74
|
|
|
|
75
|
|
|
$this->httpRequest->on('end', function() use ($headers, &$dataResult) { |
76
|
|
|
$error = null; |
|
|
|
|
77
|
|
|
$data = []; |
78
|
|
|
|
79
|
|
|
if ($dataResult === null) return $this->emit('end'); |
80
|
|
|
|
81
|
|
|
if (isset($headers['content-type']) && $headers['content-type'] == 'application/json') { |
82
|
|
|
$jsonData = json_decode($dataResult, true); |
83
|
|
|
|
84
|
|
|
if ($jsonData === null) { |
85
|
|
|
$this->emit('error', [json_last_error_msg()]); |
86
|
|
|
} else { |
87
|
|
|
$data = $jsonData; |
88
|
|
|
} |
89
|
|
|
} else { |
90
|
|
|
parse_str($dataResult, $data); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$this->setContent($dataResult); |
94
|
|
|
$this->setData($data); |
95
|
|
|
$this->emit('end'); |
96
|
|
|
}); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Set the data array |
101
|
|
|
* @param array $data array of data |
102
|
|
|
*/ |
103
|
|
|
public function setData($data) |
104
|
|
|
{ |
105
|
|
|
$this->data = array_merge($data, $this->data); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get the data array |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
|
|
public function getData() |
113
|
|
|
{ |
114
|
|
|
return $this->data; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function __get($name) |
118
|
|
|
{ |
119
|
|
|
return isset($this->data[$name]) ? $this->data[$name] : false; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function __set($name, $value) |
123
|
|
|
{ |
124
|
|
|
$this->data[$name] = $value; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..