Skip to content

Do not rewind streams #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions spec/StreamFactory/StreamFactoryBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace spec\Http\Message\StreamFactory;

use GuzzleHttp\Psr7\Stream;
use Psr\Http\Message\StreamInterface;

trait StreamFactoryBehavior
{
function it_is_a_stream_factory()
Expand Down Expand Up @@ -32,4 +35,33 @@ function it_creates_a_stream_from_non_seekable_resource()
$this->createStream($resource)
->shouldHaveType('Psr\Http\Message\StreamInterface');
}

function it_does_not_rewind_existing_stream()
{
$stream = new Stream(fopen('php://memory', 'rw'));
$stream->write('abcdef');
$stream->seek(3);

$this->createStream($stream)
->shouldHaveContent('def');
}

function it_does_not_rewind_existing_resource()
{
$resource = fopen('php://memory', 'rw');
fwrite($resource, 'abcdef');
fseek($resource, 3);

$this->createStream($resource)
->shouldHaveContent('def');
}

public function getMatchers()
{
return [
'haveContent' => function (StreamInterface $subject, $key) {
return $subject->getContents() === $key;
},
];
}
}
25 changes: 9 additions & 16 deletions src/StreamFactory/DiactorosStreamFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,19 @@ final class DiactorosStreamFactory implements StreamFactory
*/
public function createStream($body = null)
{
if (!$body instanceof StreamInterface) {
if (is_resource($body)) {
$body = new Stream($body);
} else {
$stream = new Stream('php://memory', 'rw');

if (null === $body || '' === $body) {
return $stream;
}

$stream->write((string) $body);
if ($body instanceof StreamInterface) {
return $body;
}

$body = $stream;
}
if (is_resource($body)) {
return new Stream($body);
}

if ($body->isSeekable()) {
$body->rewind();
$stream = new Stream('php://memory', 'rw');
if (null !== $body && '' !== $body) {
$stream->write((string) $body);
}

return $body;
return $stream;
}
}
17 changes: 5 additions & 12 deletions src/StreamFactory/SlimStreamFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,13 @@ public function createStream($body = null)
}

if (is_resource($body)) {
$stream = new Stream($body);
} else {
$resource = fopen('php://memory', 'r+');
$stream = new Stream($resource);

if (null === $body || '' === $body) {
return $stream;
}

$stream->write((string) $body);
return new Stream($body);
}

if ($stream->isSeekable()) {
$stream->rewind();
$resource = fopen('php://memory', 'r+');
$stream = new Stream($resource);
if (null !== $body && '' !== $body) {
$stream->write((string) $body);
}

return $stream;
Expand Down