Skip to content

Commit 2787e8f

Browse files
committed
Do not rewind any stream
1 parent 6d013c3 commit 2787e8f

File tree

3 files changed

+20
-34
lines changed

3 files changed

+20
-34
lines changed

spec/StreamFactory/StreamFactoryBehavior.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,24 @@ function it_creates_a_stream_from_non_seekable_resource()
3636
->shouldHaveType('Psr\Http\Message\StreamInterface');
3737
}
3838

39-
function it_rewinds_existing_stream()
39+
function it_does_not_rewind_existing_stream()
4040
{
4141
$stream = new Stream(fopen('php://memory', 'rw'));
4242
$stream->write('abcdef');
43-
$stream->read(3);
43+
$stream->seek(3);
4444

4545
$this->createStream($stream)
46-
->shouldHaveContent('abcdef');
46+
->shouldHaveContent('def');
4747
}
4848

49-
function it_rewinds_existing_resource()
49+
function it_does_not_rewind_existing_resource()
5050
{
5151
$resource = fopen('php://memory', 'rw');
5252
fwrite($resource, 'abcdef');
53-
fread($resource, 3);
53+
fseek($resource, 3);
5454

5555
$this->createStream($resource)
56-
->shouldHaveContent('abcdef');
56+
->shouldHaveContent('def');
5757
}
5858

5959
public function getMatchers()

src/StreamFactory/DiactorosStreamFactory.php

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,19 @@ final class DiactorosStreamFactory implements StreamFactory
1818
*/
1919
public function createStream($body = null)
2020
{
21-
if (!$body instanceof StreamInterface) {
22-
if (is_resource($body)) {
23-
$body = new Stream($body);
24-
} else {
25-
$stream = new Stream('php://memory', 'rw');
26-
27-
if (null === $body || '' === $body) {
28-
return $stream;
29-
}
30-
31-
$stream->write((string) $body);
21+
if ($body instanceof StreamInterface) {
22+
return $body;
23+
}
3224

33-
$body = $stream;
34-
}
25+
if (is_resource($body)) {
26+
return new Stream($body);
3527
}
3628

37-
if ($body->isSeekable()) {
38-
$body->rewind();
29+
$stream = new Stream('php://memory', 'rw');
30+
if (null !== $body && '' !== $body) {
31+
$stream->write((string) $body);
3932
}
4033

41-
return $body;
34+
return $stream;
4235
}
4336
}

src/StreamFactory/SlimStreamFactory.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,13 @@ public function createStream($body = null)
2323
}
2424

2525
if (is_resource($body)) {
26-
$stream = new Stream($body);
27-
} else {
28-
$resource = fopen('php://memory', 'r+');
29-
$stream = new Stream($resource);
30-
31-
if (null === $body || '' === $body) {
32-
return $stream;
33-
}
34-
35-
$stream->write((string) $body);
26+
return new Stream($body);
3627
}
3728

38-
if ($stream->isSeekable()) {
39-
$stream->rewind();
29+
$resource = fopen('php://memory', 'r+');
30+
$stream = new Stream($resource);
31+
if (null !== $body && '' !== $body) {
32+
$stream->write((string) $body);
4033
}
4134

4235
return $stream;

0 commit comments

Comments
 (0)