|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright (c) 2011-2015, Celestino Diaz <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
8
|
|
|
* in the Software without restriction, including without limitation the rights |
|
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
11
|
|
|
* furnished to do so, subject to the following conditions: |
|
12
|
|
|
* |
|
13
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
14
|
|
|
* all copies or substantial portions of the Software. |
|
15
|
|
|
* |
|
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
22
|
|
|
* THE SOFTWARE. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace Brickoo\Component\Http; |
|
26
|
|
|
|
|
27
|
|
|
use Brickoo\Component\Http\Aggregator\HttpRequestUriAggregator; |
|
28
|
|
|
use Brickoo\Component\Http\Header\Aggregator\HeaderFieldClassMap; |
|
29
|
|
|
use Brickoo\Component\Http\Header\Aggregator\HeaderFieldsAggregator; |
|
30
|
|
|
use Brickoo\Component\Http\Header\Aggregator\Strategy\PhpHeaderFieldsAggregatorStrategy; |
|
31
|
|
|
use Brickoo\Component\Http\Exception\MissingBuilderDependencyException; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Class HttpRequestBuilder |
|
35
|
|
|
* Build a http request object with dependencies. |
|
36
|
|
|
* @package Brickoo\Component\Http |
|
37
|
|
|
* @author Celestino Diaz <[email protected]> |
|
38
|
|
|
*/ |
|
39
|
|
|
class HttpRequestBuilder { |
|
40
|
|
|
|
|
41
|
|
|
/** @var array */ |
|
42
|
|
|
private $serverVariables; |
|
43
|
|
|
|
|
44
|
|
|
/** @var \Brickoo\Component\Http\HttpMethod */ |
|
45
|
|
|
private $method; |
|
46
|
|
|
|
|
47
|
|
|
/** @var \Brickoo\Component\Http\HttpVersion */ |
|
48
|
|
|
private $version; |
|
49
|
|
|
|
|
50
|
|
|
/** @var \Brickoo\Component\Http\Uri */ |
|
51
|
|
|
private $uri; |
|
52
|
|
|
|
|
53
|
|
|
/** @var \Brickoo\Component\Http\HttpMessage */ |
|
54
|
|
|
private $message; |
|
55
|
|
|
|
|
56
|
|
|
/** @var \Brickoo\Component\Http\HttpMessageHeader */ |
|
57
|
|
|
private $messageHeader; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Class constructor |
|
61
|
|
|
* @param array $serverVariables the PHP $_SERVER variables |
|
62
|
|
|
*/ |
|
63
|
2 |
|
public function __construct(array $serverVariables) { |
|
64
|
2 |
|
$this->serverVariables = $serverVariables; |
|
65
|
2 |
|
$this->method = null; |
|
66
|
2 |
|
$this->version = null; |
|
67
|
2 |
|
$this->uri = null; |
|
68
|
2 |
|
$this->message = null; |
|
69
|
2 |
|
$this->messageHeader = null; |
|
70
|
2 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Build the http request object with |
|
74
|
|
|
* prepared dependencies. |
|
75
|
|
|
* @return \Brickoo\Component\Http\HttpRequest |
|
76
|
|
|
*/ |
|
77
|
2 |
|
public function build() { |
|
78
|
2 |
|
return new HttpRequest( |
|
79
|
2 |
|
$this->method, |
|
80
|
2 |
|
$this->version, |
|
81
|
2 |
|
$this->uri, |
|
82
|
2 |
|
$this->message |
|
83
|
2 |
|
); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Build the request method. |
|
88
|
|
|
* @param null|\Brickoo\Component\Http\HttpMethod $requestMethod |
|
89
|
|
|
* @return \Brickoo\Component\Http\HttpRequestBuilder |
|
90
|
|
|
*/ |
|
91
|
2 |
|
public function buildRequestMethod(HttpMethod $requestMethod = null) { |
|
92
|
2 |
|
if ($requestMethod === null) { |
|
93
|
1 |
|
$requestMethod = new HttpMethod( |
|
94
|
1 |
|
$this->getServerVariable("REQUEST_METHOD", HttpMethod::GET) |
|
95
|
1 |
|
); |
|
96
|
1 |
|
} |
|
97
|
2 |
|
$this->method = $requestMethod; |
|
98
|
2 |
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Build the request protocol version. |
|
103
|
|
|
* @param null|\Brickoo\Component\Http\HttpVersion $protocolVersion |
|
104
|
|
|
* @return \Brickoo\Component\Http\HttpRequestBuilder |
|
105
|
|
|
*/ |
|
106
|
2 |
|
public function buildProtocolVersion(HttpVersion $protocolVersion = null) { |
|
107
|
2 |
|
if ($protocolVersion === null) { |
|
108
|
1 |
|
$protocolVersion = new HttpVersion( |
|
109
|
1 |
|
$this->getServerVariable("SERVER_PROTOCOL", HttpVersion::HTTP_1_0) |
|
110
|
1 |
|
); |
|
111
|
1 |
|
} |
|
112
|
2 |
|
$this->version = $protocolVersion; |
|
113
|
2 |
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Build the uri dependency. |
|
118
|
|
|
* @param null|\Brickoo\Component\Http\Uri $uri |
|
119
|
|
|
* @throws \Brickoo\Component\Http\Exception\MissingBuilderDependencyException |
|
120
|
|
|
* @return \Brickoo\Component\Http\HttpRequestBuilder |
|
121
|
|
|
*/ |
|
122
|
3 |
|
public function buildUri(Uri $uri = null) { |
|
123
|
3 |
|
if ($uri === null) { |
|
124
|
2 |
|
$this->checkMessageHeaderDependency(); |
|
125
|
|
|
|
|
126
|
1 |
|
$uri = (new UriFactory)->create( |
|
127
|
1 |
|
new HttpRequestUriAggregator( |
|
128
|
1 |
|
$this->messageHeader, |
|
129
|
1 |
|
$this->serverVariables |
|
130
|
1 |
|
) |
|
131
|
1 |
|
); |
|
132
|
1 |
|
} |
|
133
|
2 |
|
$this->uri = $uri; |
|
134
|
2 |
|
return $this; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Build the request message dependency. |
|
139
|
|
|
* @param null|\Brickoo\Component\Http\HttpMessage $message |
|
140
|
|
|
* @throws \Brickoo\Component\Http\Exception\MissingBuilderDependencyException |
|
141
|
|
|
* @return \Brickoo\Component\Http\HttpRequestBuilder |
|
142
|
|
|
*/ |
|
143
|
3 |
|
public function buildMessage(HttpMessage $message = null) { |
|
144
|
3 |
|
if ($message === null) { |
|
145
|
2 |
|
$this->checkMessageHeaderDependency(); |
|
146
|
|
|
|
|
147
|
1 |
|
$message = new HttpMessage( |
|
148
|
1 |
|
$this->messageHeader, |
|
149
|
1 |
|
new HttpMessageBody(file_get_contents("php://input")) |
|
150
|
1 |
|
); |
|
151
|
1 |
|
} |
|
152
|
2 |
|
$this->message = $message; |
|
153
|
2 |
|
return $this; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Build the request message header dependency. |
|
158
|
|
|
* @param null|\Brickoo\Component\Http\HttpMessageHeader $messageHeader |
|
159
|
|
|
* @return \Brickoo\Component\Http\HttpRequestBuilder |
|
160
|
|
|
*/ |
|
161
|
2 |
|
public function buildMessageHeader(HttpMessageHeader $messageHeader = null) { |
|
162
|
2 |
|
if ($messageHeader === null) { |
|
163
|
1 |
|
$messageHeader = new HttpMessageHeader( |
|
164
|
1 |
|
(new HeaderFieldsAggregator( |
|
165
|
1 |
|
new HeaderFieldClassMap(), |
|
166
|
1 |
|
new PhpHeaderFieldsAggregatorStrategy($this->serverVariables) |
|
167
|
1 |
|
))->getHeaderFields() |
|
168
|
1 |
|
); |
|
169
|
1 |
|
} |
|
170
|
2 |
|
$this->messageHeader = $messageHeader; |
|
171
|
2 |
|
return $this; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Return the server variable value. |
|
176
|
|
|
* @param string $variableName |
|
177
|
|
|
* @param null|string $defaultValue |
|
178
|
|
|
* @return null|string |
|
179
|
|
|
*/ |
|
180
|
1 |
|
private function getServerVariable($variableName, $defaultValue = null) { |
|
181
|
1 |
|
if (isset($this->serverVariables[$variableName])) { |
|
182
|
1 |
|
return $this->serverVariables[$variableName]; |
|
183
|
|
|
} |
|
184
|
1 |
|
return $defaultValue; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Check if the message header dependency is set. |
|
189
|
|
|
* @throws MissingBuilderDependencyException |
|
190
|
|
|
* @return void |
|
191
|
|
|
*/ |
|
192
|
3 |
|
private function checkMessageHeaderDependency() { |
|
193
|
3 |
|
if (!$this->messageHeader instanceof HttpMessageHeader) { |
|
194
|
2 |
|
throw new MissingBuilderDependencyException("HttpMessageHeader"); |
|
195
|
|
|
} |
|
196
|
1 |
|
} |
|
197
|
|
|
|
|
198
|
|
|
} |
|
199
|
|
|
|