[HttpFoundation] [PSR-7] Allow to use resources as content body and to return resources from string content
This commit is contained in:
parent
5ec123fc60
commit
059964daf3
@ -205,7 +205,7 @@ class Request
|
|||||||
* @param array $cookies The COOKIE parameters
|
* @param array $cookies The COOKIE parameters
|
||||||
* @param array $files The FILES parameters
|
* @param array $files The FILES parameters
|
||||||
* @param array $server The SERVER parameters
|
* @param array $server The SERVER parameters
|
||||||
* @param string $content The raw body data
|
* @param string|resource $content The raw body data
|
||||||
*
|
*
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
@ -225,7 +225,7 @@ class Request
|
|||||||
* @param array $cookies The COOKIE parameters
|
* @param array $cookies The COOKIE parameters
|
||||||
* @param array $files The FILES parameters
|
* @param array $files The FILES parameters
|
||||||
* @param array $server The SERVER parameters
|
* @param array $server The SERVER parameters
|
||||||
* @param string $content The raw body data
|
* @param string|resource $content The raw body data
|
||||||
*
|
*
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
@ -1465,16 +1465,38 @@ class Request
|
|||||||
*/
|
*/
|
||||||
public function getContent($asResource = false)
|
public function getContent($asResource = false)
|
||||||
{
|
{
|
||||||
if (PHP_VERSION_ID < 50600 && (false === $this->content || (true === $asResource && null !== $this->content))) {
|
$currentContentIsResource = is_resource($this->content);
|
||||||
|
if (PHP_VERSION_ID < 50600 && false === $this->content) {
|
||||||
throw new \LogicException('getContent() can only be called once when using the resource return type and PHP below 5.6.');
|
throw new \LogicException('getContent() can only be called once when using the resource return type and PHP below 5.6.');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (true === $asResource) {
|
if (true === $asResource) {
|
||||||
|
if ($currentContentIsResource) {
|
||||||
|
rewind($this->content);
|
||||||
|
|
||||||
|
return $this->content;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Content passed in parameter (test)
|
||||||
|
if (is_string($this->content)) {
|
||||||
|
$resource = fopen('php://temp','r+');
|
||||||
|
fwrite($resource, $this->content);
|
||||||
|
rewind($resource);
|
||||||
|
|
||||||
|
return $resource;
|
||||||
|
}
|
||||||
|
|
||||||
$this->content = false;
|
$this->content = false;
|
||||||
|
|
||||||
return fopen('php://input', 'rb');
|
return fopen('php://input', 'rb');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($currentContentIsResource) {
|
||||||
|
rewind($this->content);
|
||||||
|
|
||||||
|
return stream_get_contents($this->content);
|
||||||
|
}
|
||||||
|
|
||||||
if (null === $this->content) {
|
if (null === $this->content) {
|
||||||
$this->content = file_get_contents('php://input');
|
$this->content = file_get_contents('php://input');
|
||||||
}
|
}
|
||||||
|
@ -923,6 +923,26 @@ class RequestTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertTrue(feof($retval));
|
$this->assertTrue(feof($retval));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testGetContentReturnsResourceWhenContentSetInConstructor()
|
||||||
|
{
|
||||||
|
$req = new Request(array(), array(), array(), array(), array(), array(), 'MyContent');
|
||||||
|
$resource = $req->getContent(true);
|
||||||
|
|
||||||
|
$this->assertTrue(is_resource($resource));
|
||||||
|
$this->assertEquals('MyContent', stream_get_contents($resource));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testContentAsResource()
|
||||||
|
{
|
||||||
|
$resource = fopen('php://memory','r+');
|
||||||
|
fwrite($resource, 'My other content');
|
||||||
|
rewind($resource);
|
||||||
|
|
||||||
|
$req = new Request(array(), array(), array(), array(), array(), array(), $resource);
|
||||||
|
$this->assertEquals('My other content', stream_get_contents($req->getContent(true)));
|
||||||
|
$this->assertEquals('My other content', $req->getContent());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \LogicException
|
* @expectedException \LogicException
|
||||||
* @dataProvider getContentCantBeCalledTwiceWithResourcesProvider
|
* @dataProvider getContentCantBeCalledTwiceWithResourcesProvider
|
||||||
@ -967,7 +987,6 @@ class RequestTest extends \PHPUnit_Framework_TestCase
|
|||||||
return array(
|
return array(
|
||||||
'Resource then fetch' => array(true, false),
|
'Resource then fetch' => array(true, false),
|
||||||
'Resource then resource' => array(true, true),
|
'Resource then resource' => array(true, true),
|
||||||
'Fetch then resource' => array(false, true),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user