19
19
UploadedFileFactoryInterface ,
20
20
ServerRequestInterface ,
21
21
StreamInterface ,
22
- UploadedFileInterface
22
+ UploadedFileInterface ,
23
+ UriFactoryInterface
23
24
};
24
25
use InvalidArgumentException ;
25
26
use UnexpectedValueException ;
@@ -55,29 +56,16 @@ class ServerRequestBuilder
55
56
56
57
private array $ server ;
57
58
58
- /** @var ServerRequestFactoryInterface|StreamFactoryInterface|UploadedFileFactoryInterface */
59
- private $ factory ;
60
-
61
- private string $ method = 'GET ' ;
62
-
63
- private string $ uri = '/ ' ;
64
-
65
- private array $ queryParams = [];
66
-
67
- private string $ protocolVer = '1.1 ' ;
59
+ private ServerRequestInterface $ request ;
68
60
69
- private array $ headers = [];
61
+ /** @var ServerRequestFactoryInterface|StreamFactoryInterface|UploadedFileFactoryInterface|UriFactoryInterface */
62
+ private $ factory ;
70
63
71
- private array $ cookieParams = [] ;
64
+ private ? StreamInterface $ body = null ;
72
65
73
66
/** @var null|array|object */
74
67
private $ parsedBody ;
75
68
76
- private ?StreamInterface $ body = null ;
77
-
78
- /** @var UploadedFileInterface[] */
79
- private array $ uploadedFiles = [];
80
-
81
69
public static function fromSapi (
82
70
array $ server ,
83
71
object $ factory ,
@@ -110,79 +98,53 @@ public function __construct(array $server, object $factory)
110
98
111
99
$ this ->server = $ server ;
112
100
$ this ->factory = $ factory ;
101
+ $ this ->request = $ factory ->createServerRequest ('GET ' , '/ ' , $ server );
113
102
}
114
103
115
104
public function build (): ServerRequestInterface
116
105
{
117
- $ request = $ this -> factory -> createServerRequest ( $ this ->method , $ this -> uri , $ this ->server )
118
- -> withProtocolVersion ( $ this ->protocolVer );
106
+ if ( empty ( $ this ->parsedBody ) && ! empty (( string ) $ this ->body )) {
107
+ $ parser = ( self :: $ preferredMediaParser )( $ this ->request );
119
108
120
- if (! empty ( $ this ->queryParams )) {
121
- $ request = $ request -> withQueryParams ( $ this ->queryParams );
109
+ $ this ->request = $ this -> request
110
+ -> withParsedBody ( $ parser -> parse (( string ) $ this ->body ) );
122
111
}
123
112
124
- if (! empty ($ this ->headers )) {
125
- $ request = $ this ->addHeadersToServerRequest ($ request );
126
- }
127
-
128
- if (! empty ($ this ->cookieParams )) {
129
- $ request = $ request ->withCookieParams ($ this ->cookieParams );
130
- }
131
-
132
- $ isBodyEmpty = (null === $ this ->body );
133
-
134
- if (empty ($ this ->parsedBody ) && ! $ isBodyEmpty ) {
135
- $ parser = (self ::$ preferredMediaParser )($ request );
136
- $ this ->parsedBody = $ parser ->parse ((string ) $ this ->body );
137
- }
138
-
139
- if (
140
- null === $ this ->parsedBody
141
- || is_array ($ this ->parsedBody )
142
- || is_object ($ this ->parsedBody )
143
- ) {
144
- $ request = $ request ->withParsedBody ($ this ->parsedBody );
145
- }
146
-
147
- if (! $ isBodyEmpty ) {
148
- $ request = $ request ->withBody ($ this ->body );
149
- }
150
-
151
- if (! empty ($ this ->uploadedFiles )) {
152
- $ request = $ request ->withUploadedFiles ($ this ->uploadedFiles );
153
- }
154
-
155
- return $ request ;
113
+ return $ this ->request ;
156
114
}
157
115
158
116
public function addMethod (): self
159
117
{
160
- $ this -> method = ( empty ($ this ->server ['REQUEST_METHOD ' ]))
161
- ? ' GET '
162
- : $ this -> server [ ' REQUEST_METHOD ' ];
118
+ if (! empty ($ this ->server ['REQUEST_METHOD ' ])) {
119
+ $ this -> request = $ this -> request -> withMethod ( $ this -> server [ ' REQUEST_METHOD ' ]);
120
+ }
163
121
164
122
return $ this ;
165
123
}
166
124
167
125
public function addUri (): self
168
126
{
169
- $ server = $ this ->server ;
170
-
171
- $ uriParts = parse_url ($ server ['REQUEST_URI ' ] ?? '' );
172
- [$ path , $ query , $ fragment ] = $ this ->extractUriComponents ($ server , $ uriParts );
127
+ $ uriParts = parse_url ($ this ->server ['REQUEST_URI ' ] ?? '' );
128
+ [$ path , $ query , $ fragment ] = $ this ->extractUriComponents ($ this ->server , $ uriParts );
173
129
174
130
$ baseUri = $ this ->getUriAuthorityWithScheme ();
175
131
176
132
if ($ path ) {
177
133
$ baseUri = rtrim ($ baseUri , '/ ' ) . '/ ' . ltrim ($ path , '/ ' );
178
134
}
179
135
180
- $ this -> uri = (
136
+ $ uri = (
181
137
($ baseUri ?: '/ ' )
182
138
. ($ query ? ('? ' . ltrim ($ query , '? ' )) : '' )
183
139
. ($ fragment ? "# {$ fragment }" : '' )
184
140
);
185
141
142
+ parse_str ($ query , $ queryParams );
143
+
144
+ $ this ->request = $ this ->request
145
+ ->withUri ($ this ->factory ->createUri ($ uri ))
146
+ ->withQueryParams ($ queryParams );
147
+
186
148
return $ this ;
187
149
}
188
150
@@ -193,22 +155,26 @@ public function addUri(): self
193
155
*/
194
156
public function addProtocolVersion (): self
195
157
{
158
+ $ protocolVer = '1.1 ' ;
159
+
196
160
if (
197
161
isset ($ this ->server ['SERVER_PROTOCOL ' ])
198
- && $ this ->server ['SERVER_PROTOCOL ' ] !== "HTTP/ {$ this -> protocolVer }"
162
+ && $ this ->server ['SERVER_PROTOCOL ' ] !== "HTTP/ {$ protocolVer }"
199
163
) {
200
- $ this -> protocolVer = strtr ((string ) $ this ->server ['SERVER_PROTOCOL ' ], ['HTTP/ ' => '' ]);
164
+ $ protocolVer = strtr ((string ) $ this ->server ['SERVER_PROTOCOL ' ], ['HTTP/ ' => '' ]);
201
165
202
- $ isNumeric = (int ) $ this -> protocolVer ;
166
+ $ isNumeric = (int ) $ protocolVer ;
203
167
204
168
if (! $ isNumeric ) {
205
169
throw new UnexpectedValueException (sprintf (
206
170
'Unrecognized protocol version "%s" ' ,
207
- $ this -> protocolVer
171
+ $ protocolVer
208
172
));
209
173
}
210
174
}
211
175
176
+ $ this ->request = $ this ->request ->withProtocolVersion ($ protocolVer );
177
+
212
178
return $ this ;
213
179
}
214
180
@@ -222,10 +188,27 @@ public function addHeaders(): self
222
188
$ str = strtolower (str_replace ('_ ' , '- ' , $ str ));
223
189
preg_match_all ($ pattern , $ str , $ normalizedHeaders , PREG_SET_ORDER );
224
190
225
- $ this ->headers = [
226
- 'original ' => $ originalHeaders ,
227
- 'normalized ' => $ normalizedHeaders ,
228
- ];
191
+ $ totalMatches = count ($ normalizedHeaders );
192
+
193
+ for ($ i = 0 ; $ i < $ totalMatches ; $ i ++) {
194
+ $ isRedirect = ! (empty ($ normalizedHeaders [$ i ][1 ]));
195
+ $ originalKey = $ originalHeaders [$ i ][2 ] . $ originalHeaders [$ i ][3 ];
196
+
197
+ // apache prefixes environment variables with `REDIRECT_` if they are
198
+ // added by rewrite rules
199
+ if ($ isRedirect ) {
200
+ if (isset ($ this ->server [$ originalKey ])) {
201
+ continue ;
202
+ }
203
+
204
+ $ originalKey = 'REDIRECT_ ' . $ originalKey ;
205
+ }
206
+
207
+ $ newKey = $ normalizedHeaders [$ i ][2 ] . $ normalizedHeaders [$ i ][3 ];
208
+
209
+ $ this ->request = $ this ->request
210
+ ->withHeader ($ newKey , $ this ->server [$ originalKey ]);
211
+ }
229
212
230
213
return $ this ;
231
214
}
@@ -236,7 +219,8 @@ public function addCookieParams(array $cookies): self
236
219
$ cookies = $ this ->parseCookieHeader ($ this ->server ['HTTP_COOKIE ' ]);
237
220
}
238
221
239
- $ this ->cookieParams = $ cookies ?: $ this ->cookieParams ;
222
+ $ this ->request = $ this ->request
223
+ ->withCookieParams ($ cookies ?: []);
240
224
241
225
return $ this ;
242
226
}
@@ -254,7 +238,11 @@ public function addParsedBody($parsedBody): self
254
238
);
255
239
}
256
240
241
+ $ this ->request = $ this ->request
242
+ ->withParsedBody ($ parsedBody );
243
+
257
244
$ this ->parsedBody = $ parsedBody ;
245
+
258
246
return $ this ;
259
247
}
260
248
@@ -274,6 +262,7 @@ public function addBody($body): self
274
262
}
275
263
276
264
if ($ body instanceof StreamInterface && (string ) $ body !== '' ) {
265
+ $ this ->request = $ this ->request ->withBody ($ body );
277
266
$ this ->body = $ body ;
278
267
}
279
268
@@ -292,7 +281,8 @@ public function addBody($body): self
292
281
*/
293
282
public function addUploadedFiles (array $ files ): self
294
283
{
295
- $ this ->uploadedFiles = self ::normalizeUploadedFiles ($ files , $ this ->factory );
284
+ $ this ->request = $ this ->request
285
+ ->withUploadedFiles ($ this ->normalizeUploadedFiles ($ files ));
296
286
297
287
return $ this ;
298
288
}
@@ -305,33 +295,32 @@ public function addUploadedFiles(array $files): self
305
295
* instances.
306
296
*
307
297
* @param array $files `$_FILES` struct.
308
- * @param UploadedFileFactoryInterface|StreamFactoryInterface $httpFactory
309
298
*
310
299
* @return UploadedFileInterface[]|UploadedFileInterface
311
300
*/
312
- private static function createUploadedFileFromSpec (array $ files, $ httpFactory )
301
+ private function createUploadedFileFromSpec (array $ files )
313
302
{
314
303
if (is_array ($ files ['tmp_name ' ])) {
315
304
$ normalizedFiles = [];
316
305
317
306
foreach ($ files ['tmp_name ' ] as $ key => $ file ) {
318
- $ normalizedFiles [$ key ] = self :: createUploadedFileFromSpec ([
307
+ $ normalizedFiles [$ key ] = $ this -> createUploadedFileFromSpec ([
319
308
'tmp_name ' => $ files ['tmp_name ' ][$ key ],
320
309
'size ' => $ files ['size ' ][$ key ],
321
310
'error ' => $ files ['error ' ][$ key ],
322
311
'name ' => $ files ['name ' ][$ key ],
323
312
'type ' => $ files ['type ' ][$ key ],
324
- ], $ httpFactory );
313
+ ]);
325
314
}
326
315
327
316
return $ normalizedFiles ;
328
317
}
329
318
330
319
$ stream = ($ files ['tmp_name ' ] instanceof StreamInterface)
331
320
? $ files ['tmp_name ' ]
332
- : $ httpFactory ->createStreamFromFile ($ files ['tmp_name ' ], 'r+ ' );
321
+ : $ this -> factory ->createStreamFromFile ($ files ['tmp_name ' ], 'r+ ' );
333
322
334
- return $ httpFactory ->createUploadedFile (
323
+ return $ this -> factory ->createUploadedFile (
335
324
$ stream ,
336
325
$ files ['size ' ],
337
326
(int ) $ files ['error ' ],
@@ -345,13 +334,12 @@ private static function createUploadedFileFromSpec(array $files, $httpFactory)
345
334
* arrays are normalized.
346
335
*
347
336
* @param array $files
348
- * @param UploadedFileFactoryInterface|StreamFactoryInterface $httpFactory
349
337
*
350
338
* @return UploadedFileInterface[]
351
339
*
352
340
* @throws InvalidArgumentException
353
341
*/
354
- private static function normalizeUploadedFiles (array $ files, $ httpFactory ): array
342
+ private function normalizeUploadedFiles (array $ files ): array
355
343
{
356
344
$ normalized = [];
357
345
@@ -363,8 +351,8 @@ private static function normalizeUploadedFiles(array $files, $httpFactory): arra
363
351
364
352
if (is_array ($ value )) {
365
353
$ normalized [$ key ] = (isset ($ value ['tmp_name ' ]))
366
- ? self :: createUploadedFileFromSpec ($ value, $ httpFactory )
367
- : self :: normalizeUploadedFiles ($ value, $ httpFactory );
354
+ ? $ this -> createUploadedFileFromSpec ($ value )
355
+ : $ this -> normalizeUploadedFiles ($ value );
368
356
continue ;
369
357
}
370
358
@@ -436,35 +424,6 @@ private function parseCookieHeader(string $cookieHeader): array
436
424
return $ cookies ;
437
425
}
438
426
439
- private function addHeadersToServerRequest (
440
- ServerRequestInterface $ request
441
- ): ServerRequestInterface {
442
- $ originalHeaders = $ this ->headers ['original ' ];
443
- $ normalizedHeaders = $ this ->headers ['normalized ' ];
444
- $ totalMatches = count ($ normalizedHeaders );
445
-
446
- for ($ i = 0 ; $ i < $ totalMatches ; $ i ++) {
447
- $ isRedirect = ! (empty ($ normalizedHeaders [$ i ][1 ]));
448
- $ originalKey = $ originalHeaders [$ i ][2 ] . $ originalHeaders [$ i ][3 ];
449
-
450
- // apache prefixes environment variables with `REDIRECT_` if they are
451
- // added by rewrite rules
452
- if ($ isRedirect ) {
453
- if (isset ($ this ->server [$ originalKey ])) {
454
- continue ;
455
- }
456
-
457
- $ originalKey = 'REDIRECT_ ' . $ originalKey ;
458
- }
459
-
460
- $ newKey = $ normalizedHeaders [$ i ][2 ] . $ normalizedHeaders [$ i ][3 ];
461
-
462
- $ request = $ request ->withHeader ($ newKey , $ this ->server [$ originalKey ]);
463
- }
464
-
465
- return $ request ;
466
- }
467
-
468
427
private function extractUriComponents (array $ server , array $ uriParts ): array
469
428
{
470
429
$ path = '' ;
@@ -485,8 +444,6 @@ private function extractUriComponents(array $server, array $uriParts): array
485
444
$ query = $ uriParts ['query ' ];
486
445
}
487
446
488
- parse_str ($ query , $ this ->queryParams );
489
-
490
447
$ fragment = (! empty ($ uriParts ['fragment ' ])) ? $ uriParts ['fragment ' ] : '' ;
491
448
return [$ path , $ query , $ fragment ];
492
449
}
0 commit comments