Skip to content

Commit 510b2d2

Browse files
committed
bug symfony#15249 [HttpFoundation] [PSR-7] Allow to use resources as content body and to return resources from string content (dunglas)
This PR was squashed before being merged into the 2.3 branch (closes symfony#15249). Discussion ---------- [HttpFoundation] [PSR-7] Allow to use resources as content body and to return resources from string content | Q | A | ------------- | --- | Bug fix? | yes | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | not yet * Allows to fix tests of https://github.com/symfony/psr-http-message-bridge with PHP 5.6. * Ease the transition to PSR-7 (in PSR-7, almost everything is stream - symfony#15186) Maybe should I open it against 2.8 but it can be considered a bug fix at least for the part "returning a string as a resource". Commits ------- 059964d [HttpFoundation] [PSR-7] Allow to use resources as content body and to return resources from string content
2 parents d6af4ad + 059964d commit 510b2d2

File tree

2 files changed

+57
-16
lines changed

2 files changed

+57
-16
lines changed

src/Symfony/Component/HttpFoundation/Request.php

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,13 @@ class Request
199199
/**
200200
* Constructor.
201201
*
202-
* @param array $query The GET parameters
203-
* @param array $request The POST parameters
204-
* @param array $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
205-
* @param array $cookies The COOKIE parameters
206-
* @param array $files The FILES parameters
207-
* @param array $server The SERVER parameters
208-
* @param string $content The raw body data
202+
* @param array $query The GET parameters
203+
* @param array $request The POST parameters
204+
* @param array $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
205+
* @param array $cookies The COOKIE parameters
206+
* @param array $files The FILES parameters
207+
* @param array $server The SERVER parameters
208+
* @param string|resource $content The raw body data
209209
*
210210
* @api
211211
*/
@@ -219,13 +219,13 @@ public function __construct(array $query = array(), array $request = array(), ar
219219
*
220220
* This method also re-initializes all properties.
221221
*
222-
* @param array $query The GET parameters
223-
* @param array $request The POST parameters
224-
* @param array $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
225-
* @param array $cookies The COOKIE parameters
226-
* @param array $files The FILES parameters
227-
* @param array $server The SERVER parameters
228-
* @param string $content The raw body data
222+
* @param array $query The GET parameters
223+
* @param array $request The POST parameters
224+
* @param array $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
225+
* @param array $cookies The COOKIE parameters
226+
* @param array $files The FILES parameters
227+
* @param array $server The SERVER parameters
228+
* @param string|resource $content The raw body data
229229
*
230230
* @api
231231
*/
@@ -1465,16 +1465,38 @@ public function isMethodSafe()
14651465
*/
14661466
public function getContent($asResource = false)
14671467
{
1468-
if (PHP_VERSION_ID < 50600 && (false === $this->content || (true === $asResource && null !== $this->content))) {
1468+
$currentContentIsResource = is_resource($this->content);
1469+
if (PHP_VERSION_ID < 50600 && false === $this->content) {
14691470
throw new \LogicException('getContent() can only be called once when using the resource return type and PHP below 5.6.');
14701471
}
14711472

14721473
if (true === $asResource) {
1474+
if ($currentContentIsResource) {
1475+
rewind($this->content);
1476+
1477+
return $this->content;
1478+
}
1479+
1480+
// Content passed in parameter (test)
1481+
if (is_string($this->content)) {
1482+
$resource = fopen('php://temp','r+');
1483+
fwrite($resource, $this->content);
1484+
rewind($resource);
1485+
1486+
return $resource;
1487+
}
1488+
14731489
$this->content = false;
14741490

14751491
return fopen('php://input', 'rb');
14761492
}
14771493

1494+
if ($currentContentIsResource) {
1495+
rewind($this->content);
1496+
1497+
return stream_get_contents($this->content);
1498+
}
1499+
14781500
if (null === $this->content) {
14791501
$this->content = file_get_contents('php://input');
14801502
}

src/Symfony/Component/HttpFoundation/Tests/RequestTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,26 @@ public function testGetContentReturnsResource()
923923
$this->assertTrue(feof($retval));
924924
}
925925

926+
public function testGetContentReturnsResourceWhenContentSetInConstructor()
927+
{
928+
$req = new Request(array(), array(), array(), array(), array(), array(), 'MyContent');
929+
$resource = $req->getContent(true);
930+
931+
$this->assertTrue(is_resource($resource));
932+
$this->assertEquals('MyContent', stream_get_contents($resource));
933+
}
934+
935+
public function testContentAsResource()
936+
{
937+
$resource = fopen('php://memory','r+');
938+
fwrite($resource, 'My other content');
939+
rewind($resource);
940+
941+
$req = new Request(array(), array(), array(), array(), array(), array(), $resource);
942+
$this->assertEquals('My other content', stream_get_contents($req->getContent(true)));
943+
$this->assertEquals('My other content', $req->getContent());
944+
}
945+
926946
/**
927947
* @expectedException \LogicException
928948
* @dataProvider getContentCantBeCalledTwiceWithResourcesProvider
@@ -967,7 +987,6 @@ public function getContentCantBeCalledTwiceWithResourcesProvider()
967987
return array(
968988
'Resource then fetch' => array(true, false),
969989
'Resource then resource' => array(true, true),
970-
'Fetch then resource' => array(false, true),
971990
);
972991
}
973992

0 commit comments

Comments
 (0)