Skip to content

Commit

Permalink
Added the stream object to the Response class
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Krämer committed Oct 12, 2016
1 parent 7c5135e commit 6f15732
Showing 1 changed file with 51 additions and 20 deletions.
71 changes: 51 additions & 20 deletions src/Network/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
use DateTimeZone;
use InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Zend\Diactoros\MessageTrait;
use Zend\Diactoros\Stream;

/**
* Cake Response is responsible for managing the response text, status and headers of a HTTP response.
Expand Down Expand Up @@ -359,13 +361,6 @@ class Response implements ResponseInterface
*/
protected $_contentType = 'text/html';

/**
* Buffer string or callable for response message
*
* @var string|callable
*/
protected $_body = null;

/**
* File object for file to be read out as response
*
Expand Down Expand Up @@ -421,6 +416,14 @@ class Response implements ResponseInterface
*/
public function __construct(array $options = [])
{
if (isset($options['stream'])) {
if (!$options['stream'] instanceof StreamInterface) {
throw new InvalidArgumentException('Stream option must be an object implement StreamInterface');
}
$this->stream = $options['stream'];
} else {
$this->stream = new Stream('php://memory', 'wb+');
}
if (isset($options['body'])) {
$this->body($options['body']);
}
Expand All @@ -447,7 +450,7 @@ public function __construct(array $options = [])
*/
public function send()
{
if (isset($this->headers['Location']) && $this->_status === 200) {
if ($this->hasHeader('Location') && $this->_status === 200) {
$this->statusCode(302);
}

Expand All @@ -458,7 +461,7 @@ public function send()
$this->_sendFile($this->_file, $this->_fileRange);
$this->_file = $this->_fileRange = null;
} else {
$this->_sendContent($this->_body);
$this->_sendContent($this->body());
}

if (function_exists('fastcgi_finish_request')) {
Expand Down Expand Up @@ -671,7 +674,7 @@ public function location($url = null)
{
if ($url === null) {
$result = $this->getHeaderLine('Location');
if (empty($result)) {
if (!$result) {
return null;
}

Expand Down Expand Up @@ -706,10 +709,39 @@ protected function _setHeader($header, $value)
public function body($content = null)
{
if ($content === null) {
return $this->_body;
$this->stream->rewind();
$result = $this->stream->getContents();
if (empty($result)) {
return null;
}

return $result;
}

return $this->_body = $content;
// BC compatibility
if (is_callable($content)) {
ob_start();
$result1 = $content();
$result2 = ob_get_contents();
ob_get_clean();

if ($result1) {
$content = $result1;
} else {
$content = $result2;
}
}

$this->stream = new Stream('php://memory', 'wb+');
$this->stream->write($content);
$this->stream->rewind();
$result = $this->stream->getContents();

if (empty($result)) {
return null;
}

return $result;
}

/**
Expand Down Expand Up @@ -1163,10 +1195,11 @@ public function modified($time = null)
{
if ($time !== null) {
$date = $this->_getUTCDate($time);
$this->headers['Last-Modified'] = $date->format('D, j M Y H:i:s') . ' GMT';
$this->_setHeader('Last-Modified', $date->format('D, j M Y H:i:s') . ' GMT');
}
if (isset($this->headers['Last-Modified'])) {
return $this->headers['Last-Modified'];

if ($this->hasHeader('Last-Modified')) {
return $this->getHeaderLine('Last-Modified');
}

return null;
Expand All @@ -1183,6 +1216,7 @@ public function notModified()
{
$this->statusCode(304);
$this->body('');

$remove = [
'Allow',
'Content-Encoding',
Expand Down Expand Up @@ -1393,11 +1427,8 @@ public function checkNotModified(Request $request)
*/
public function __toString()
{
if (!is_string($this->_body) && is_callable($this->_body)) {
return '';
}

return (string)$this->_body;
$this->stream->rewind();
return (string)$this->stream->getContents();
}

/**
Expand Down

0 comments on commit 6f15732

Please sign in to comment.