Skip to content

Commit 3d10a6c

Browse files
feature symfony#66 Add support for streamed Symfony request (Ekman)
This PR was squashed before being merged into the 1.2-dev branch. Discussion ---------- Add support for streamed Symfony request As a continuation of #3 and symfony#50 . Add support for converting a `ServerRequestInterface` with a large body to a Symfony request. I'm not all too familiar with Symfony components, but I saw that a Symfony request supports `string|resource|null` as its body. Since calling `detach()` on a `StreamInterface` will make the stream unsuable I added a `$streamed` argument to the function in order to not make break any existing code, and to make it a conscious decision of the caller to stream the response. I'm happy to make any necessary changes if needed. Commits ------- df26630 Add support for streamed Symfony request
2 parents 5aa8ca9 + df26630 commit 3d10a6c

7 files changed

+27
-27
lines changed

Factory/DiactorosFactory.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ public function createRequest(Request $symfonyRequest)
8484
/**
8585
* Converts Symfony uploaded files array to the PSR one.
8686
*
87-
* @param array $uploadedFiles
88-
*
8987
* @return array
9088
*/
9189
private function getFiles(array $uploadedFiles)
@@ -110,8 +108,6 @@ private function getFiles(array $uploadedFiles)
110108
/**
111109
* Creates a PSR-7 UploadedFile instance from a Symfony one.
112110
*
113-
* @param UploadedFile $symfonyUploadedFile
114-
*
115111
* @return UploadedFileInterface
116112
*/
117113
private function createUploadedFile(UploadedFile $symfonyUploadedFile)

Factory/HttpFoundationFactory.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function __construct(int $responseBufferMaxLength = 16372)
4343
/**
4444
* {@inheritdoc}
4545
*/
46-
public function createRequest(ServerRequestInterface $psrRequest)
46+
public function createRequest(ServerRequestInterface $psrRequest, bool $streamed = false)
4747
{
4848
$server = [];
4949
$uri = $psrRequest->getUri();
@@ -69,7 +69,7 @@ public function createRequest(ServerRequestInterface $psrRequest)
6969
$psrRequest->getCookieParams(),
7070
$this->getFiles($psrRequest->getUploadedFiles()),
7171
$server,
72-
$psrRequest->getBody()->__toString()
72+
$streamed ? $psrRequest->getBody()->detach() : $psrRequest->getBody()->__toString()
7373
);
7474
$request->headers->replace($psrRequest->getHeaders());
7575

@@ -79,8 +79,6 @@ public function createRequest(ServerRequestInterface $psrRequest)
7979
/**
8080
* Converts to the input array to $_FILES structure.
8181
*
82-
* @param array $uploadedFiles
83-
*
8482
* @return array
8583
*/
8684
private function getFiles(array $uploadedFiles)
@@ -101,8 +99,6 @@ private function getFiles(array $uploadedFiles)
10199
/**
102100
* Creates Symfony UploadedFile instance from PSR-7 ones.
103101
*
104-
* @param UploadedFileInterface $psrUploadedFile
105-
*
106102
* @return UploadedFile
107103
*/
108104
private function createUploadedFile(UploadedFileInterface $psrUploadedFile)
@@ -183,13 +179,11 @@ public function createResponse(ResponseInterface $psrResponse, bool $streamed =
183179
*
184180
* Some snippets have been taken from the Guzzle project: https://github.com/guzzle/guzzle/blob/5.3/src/Cookie/SetCookie.php#L34
185181
*
186-
* @param string $cookie
187-
*
188182
* @return Cookie
189183
*
190184
* @throws \InvalidArgumentException
191185
*/
192-
private function createCookie($cookie)
186+
private function createCookie(string $cookie)
193187
{
194188
foreach (explode(';', $cookie) as $part) {
195189
$part = trim($part);

Factory/PsrHttpFactory.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ public function createRequest(Request $symfonyRequest)
7878
/**
7979
* Converts Symfony uploaded files array to the PSR one.
8080
*
81-
* @param array $uploadedFiles
82-
*
8381
* @return array
8482
*/
8583
private function getFiles(array $uploadedFiles)
@@ -104,8 +102,6 @@ private function getFiles(array $uploadedFiles)
104102
/**
105103
* Creates a PSR-7 UploadedFile instance from a Symfony one.
106104
*
107-
* @param UploadedFile $symfonyUploadedFile
108-
*
109105
* @return UploadedFileInterface
110106
*/
111107
private function createUploadedFile(UploadedFile $symfonyUploadedFile)

HttpFoundationFactoryInterface.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,14 @@ interface HttpFoundationFactoryInterface
2626
/**
2727
* Creates a Symfony Request instance from a PSR-7 one.
2828
*
29-
* @param ServerRequestInterface $psrRequest
30-
*
3129
* @return Request
3230
*/
33-
public function createRequest(ServerRequestInterface $psrRequest);
31+
public function createRequest(ServerRequestInterface $psrRequest, bool $streamed = false);
3432

3533
/**
3634
* Creates a Symfony Response instance from a PSR-7 one.
3735
*
38-
* @param ResponseInterface $psrResponse
39-
*
4036
* @return Response
4137
*/
42-
public function createResponse(ResponseInterface $psrResponse);
38+
public function createResponse(ResponseInterface $psrResponse, bool $streamed = false);
4339
}

HttpMessageFactoryInterface.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,13 @@ interface HttpMessageFactoryInterface
2626
/**
2727
* Creates a PSR-7 Request instance from a Symfony one.
2828
*
29-
* @param Request $symfonyRequest
30-
*
3129
* @return ServerRequestInterface
3230
*/
3331
public function createRequest(Request $symfonyRequest);
3432

3533
/**
3634
* Creates a PSR-7 Response instance from a Symfony one.
3735
*
38-
* @param Response $symfonyResponse
39-
*
4036
* @return ResponseInterface
4137
*/
4238
public function createResponse(Response $symfonyResponse);

Tests/Factory/HttpFoundationFactoryTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,27 @@ public function testCreateRequest()
8585
$this->assertEquals(['a', 'b'], $symfonyRequest->headers->all('X-data'));
8686
}
8787

88+
public function testCreateRequestWithStreamedBody()
89+
{
90+
$serverRequest = new ServerRequest(
91+
'1.1',
92+
[],
93+
new Stream('The body'),
94+
'/',
95+
'GET',
96+
null,
97+
[],
98+
[],
99+
[],
100+
[],
101+
null,
102+
[]
103+
);
104+
105+
$symfonyRequest = $this->factory->createRequest($serverRequest, true);
106+
$this->assertEquals('The body', $symfonyRequest->getContent());
107+
}
108+
88109
public function testCreateRequestWithNullParsedBody()
89110
{
90111
$serverRequest = new ServerRequest(

Tests/Fixtures/Stream.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function close()
3737

3838
public function detach()
3939
{
40+
return fopen('data://text/plain,'.$this->stringContent, 'r');
4041
}
4142

4243
public function getSize()

0 commit comments

Comments
 (0)