1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Knp\FriendlyContexts\Builder; |
4
|
|
|
|
5
|
|
|
use Http\Client\Common\Plugin; |
6
|
|
|
use Http\Message\CookieJar; |
7
|
|
|
use Http\Message\RequestFactory; |
8
|
|
|
use Knp\FriendlyContexts\Http\ClientFactory; |
9
|
|
|
use Knp\FriendlyContexts\Http\Security\SecurityExtensionInterface; |
10
|
|
|
|
11
|
|
|
class RequestBuilder |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var RequestFactory |
15
|
|
|
*/ |
16
|
|
|
private $requestFactory; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var ClientFactory |
20
|
|
|
*/ |
21
|
|
|
private $clientFactory; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var boolean |
25
|
|
|
*/ |
26
|
|
|
private $keepCookies; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var CookieJar |
30
|
|
|
*/ |
31
|
|
|
protected $cookieJar; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $method; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string[] |
40
|
|
|
*/ |
41
|
|
|
private $headers; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string[] |
45
|
|
|
*/ |
46
|
|
|
private $queries; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var string|array |
50
|
|
|
*/ |
51
|
|
|
private $body; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
private $options; |
57
|
|
|
|
58
|
|
|
private $postBody; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var SecurityExtensionInterface[] |
62
|
|
|
*/ |
63
|
|
|
private $securityExtensions; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var Plugin[] |
67
|
|
|
*/ |
68
|
|
|
private $plugins; |
69
|
|
|
|
70
|
|
|
private $uri; |
71
|
|
|
|
72
|
|
|
private $files; |
73
|
|
|
|
74
|
|
|
private static function getAcceptedMethods() |
75
|
|
|
{ |
76
|
|
|
return [ |
77
|
|
|
'GET', |
78
|
|
|
'PUT', |
79
|
|
|
'POST', |
80
|
|
|
'DELETE', |
81
|
|
|
'HEAD', |
82
|
|
|
'OPTIONS', |
83
|
|
|
'PATCH' |
84
|
|
|
]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function __construct(ClientFactory $clientFactory) |
88
|
|
|
{ |
89
|
|
|
$this->clientFactory = $clientFactory; |
|
|
|
|
90
|
|
|
$this->options = []; |
91
|
|
|
$this->securityExtensions = []; |
92
|
|
|
$this->files = []; |
93
|
|
|
$this->keepCookies = false; |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function build($uri = null, array $queries = null, array $headers = null, array $postBody = null, $body = null, array $options = []) |
97
|
|
|
{ |
98
|
|
|
$this->setUri($uri ?: $this->uri); |
99
|
|
|
$this->setQueries($queries ?: $this->queries); |
100
|
|
|
$this->setHeaders($headers ?: $this->headers); |
101
|
|
|
$this->setPostBody($postBody ?: $this->postBody); |
102
|
|
|
$this->setBody($body ?: $this->body); |
103
|
|
|
$this->setOptions($options ?: $this->options); |
104
|
|
|
|
105
|
|
|
if (null === $this->method) { |
106
|
|
|
throw new \RuntimeException('You can\'t build a request without any methods'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// BC Layer: to be remove in 1.0 |
110
|
|
|
foreach ($this->securityExtensions as $extension) { |
111
|
|
|
$extension->secure($this); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
// End of BC Layer |
114
|
|
|
|
115
|
|
|
$request = $this->requestFactory->createRequest( |
116
|
|
|
$this->method, |
117
|
|
|
$this->getUri(), |
118
|
|
|
$this->getHeaders() |
119
|
|
|
); |
120
|
|
|
// , |
|
|
|
|
121
|
|
|
// $this->getQueries(), |
122
|
|
|
// , |
123
|
|
|
// $this->getPostBody(), |
124
|
|
|
// $this->getBody(), |
125
|
|
|
// $this->getOptions() |
126
|
|
|
// ); |
127
|
|
|
// |
128
|
|
|
// if (null !== $this->cookies) { |
129
|
|
|
// foreach ($this->cookies as $name => $cookie) { |
130
|
|
|
// $request->addCookie($name, $cookie); |
131
|
|
|
// } |
132
|
|
|
// } |
133
|
|
|
// |
134
|
|
|
// foreach ($this->securityExtensions as $extension) { |
135
|
|
|
// $extension->secureRequest($request, $this); |
136
|
|
|
// } |
137
|
|
|
// |
138
|
|
|
// foreach ($this->files as $file) { |
139
|
|
|
// $request->addPostFile($file['name'], $file['path']); |
140
|
|
|
// } |
141
|
|
|
// |
142
|
|
|
$this->clean(); // Make the builder reusable |
143
|
|
|
|
144
|
|
|
return $request; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param Plugin $plugin |
149
|
|
|
* @return RequestBuilder |
150
|
|
|
*/ |
151
|
|
|
public function addPlugin(Plugin $plugin) |
152
|
|
|
{ |
153
|
|
|
$this->plugins[] = $plugin; |
154
|
|
|
|
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return Plugin[] |
160
|
|
|
*/ |
161
|
|
|
public function getPlugins() |
162
|
|
|
{ |
163
|
|
|
return $this->plugins; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function setMethod($method) |
167
|
|
|
{ |
168
|
|
|
$method = strtoupper($method); |
169
|
|
|
|
170
|
|
|
if (!in_array($method, self::getAcceptedMethods())) { |
171
|
|
|
throw new \InvalidArgumentException(sprintf( |
172
|
|
|
'The requets method "%s" is not a valid HTTP method (valid method are "%s")', |
173
|
|
|
$method, |
174
|
|
|
implode(', ', self::getAcceptedMethods()) |
175
|
|
|
)); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$this->method = $method; |
179
|
|
|
|
180
|
|
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function getMethod() |
184
|
|
|
{ |
185
|
|
|
return $this->method; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function setHeaders(array $headers = null) |
189
|
|
|
{ |
190
|
|
|
$this->headers = $headers; |
|
|
|
|
191
|
|
|
|
192
|
|
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function getHeaders() |
196
|
|
|
{ |
197
|
|
|
return $this->headers; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function setBody($body = null) |
201
|
|
|
{ |
202
|
|
|
$this->body = $body; |
203
|
|
|
|
204
|
|
|
return $this; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function getQueries() |
208
|
|
|
{ |
209
|
|
|
return $this->queries; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
public function setQueries(array $queries = null) |
213
|
|
|
{ |
214
|
|
|
$this->queries = $queries; |
|
|
|
|
215
|
|
|
|
216
|
|
|
return $this; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function getBody() |
220
|
|
|
{ |
221
|
|
|
return $this->body; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public function setOptions(array $options = null) |
225
|
|
|
{ |
226
|
|
|
$this->options = $options; |
|
|
|
|
227
|
|
|
|
228
|
|
|
return $this; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
public function getOptions() |
232
|
|
|
{ |
233
|
|
|
return $this->options; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
public function setPostBody(array $postBody = null) |
237
|
|
|
{ |
238
|
|
|
$this->postBody = $postBody; |
239
|
|
|
|
240
|
|
|
return $this; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
public function getPostBody() |
244
|
|
|
{ |
245
|
|
|
return $this->postBody; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
public function getCookies() |
249
|
|
|
{ |
250
|
|
|
return $this->cookieJar->getCookies(); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
public function setCookies(array $cookies = null) |
254
|
|
|
{ |
255
|
|
|
$this->cookieJar->setCookies($cookies); |
|
|
|
|
256
|
|
|
|
257
|
|
|
return $this; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @return SecurityExtensionInterface[] |
262
|
|
|
*/ |
263
|
|
|
public function getSecurityExtensions() |
264
|
|
|
{ |
265
|
|
|
return $this->securityExtensions; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
public function setUri($uri = null) |
269
|
|
|
{ |
270
|
|
|
$this->uri = substr($uri, 0, 1) === '/' ? substr($uri, 1) : $uri; |
271
|
|
|
|
272
|
|
|
return $this; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
public function getUri() |
276
|
|
|
{ |
277
|
|
|
return $this->uri; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
public function addFile($name, $path) |
281
|
|
|
{ |
282
|
|
|
$this->files[] = ['name' => $name, 'path' => $path]; |
283
|
|
|
|
284
|
|
|
return $this; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @param boolean $keepCookies |
289
|
|
|
* @return RequestBuilder |
290
|
|
|
*/ |
291
|
|
|
public function keepCookies($keepCookies=true) |
292
|
|
|
{ |
293
|
|
|
$this->keepCookies = $keepCookies; |
294
|
|
|
|
295
|
|
|
return $this; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
protected function clean() |
299
|
|
|
{ |
300
|
|
|
$this->uri = null; |
301
|
|
|
$this->method = null; |
302
|
|
|
$this->queries = null; |
|
|
|
|
303
|
|
|
$this->body = null; |
304
|
|
|
$this->postBody = null; |
305
|
|
|
$this->headers = null; |
|
|
|
|
306
|
|
|
$this->options = []; |
307
|
|
|
$this->securityExtensions = []; |
308
|
|
|
$this->files = []; |
309
|
|
|
$this->plugins = []; |
|
|
|
|
310
|
|
|
|
311
|
|
|
if (!$this->keepCookies) { |
312
|
|
|
$this->cookieJar = new CookieJar(); |
313
|
|
|
} |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
public function addSecurityExtension(SecurityExtensionInterface $extension) |
317
|
|
|
{ |
318
|
|
|
trigger_error("Deprecated. To be remove in 1.0. Use `addPlugin` instead.", E_USER_DEPRECATED); |
|
|
|
|
319
|
|
|
$this->securityExtensions[] = $extension; |
320
|
|
|
|
321
|
|
|
return $this; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
public function setSecurity(SecurityExtensionInterface $extension) |
325
|
|
|
{ |
326
|
|
|
trigger_error("Deprecated. To be remove in 1.0. Use `addPlugin` instead.", E_USER_DEPRECATED); |
|
|
|
|
327
|
|
|
$this->securityExtensions = [$extension]; |
328
|
|
|
} |
329
|
|
|
} |
330
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.