From 6d013c31b7217c33583079f4103b29344380c3b8 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Mon, 16 Jan 2017 12:41:30 +0100 Subject: [PATCH 1/2] Test if we rewind existing stream/resource --- spec/StreamFactory/StreamFactoryBehavior.php | 32 ++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/spec/StreamFactory/StreamFactoryBehavior.php b/spec/StreamFactory/StreamFactoryBehavior.php index afc67ef..20b06d0 100644 --- a/spec/StreamFactory/StreamFactoryBehavior.php +++ b/spec/StreamFactory/StreamFactoryBehavior.php @@ -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() @@ -32,4 +35,33 @@ function it_creates_a_stream_from_non_seekable_resource() $this->createStream($resource) ->shouldHaveType('Psr\Http\Message\StreamInterface'); } + + function it_rewinds_existing_stream() + { + $stream = new Stream(fopen('php://memory', 'rw')); + $stream->write('abcdef'); + $stream->read(3); + + $this->createStream($stream) + ->shouldHaveContent('abcdef'); + } + + function it_rewinds_existing_resource() + { + $resource = fopen('php://memory', 'rw'); + fwrite($resource, 'abcdef'); + fread($resource, 3); + + $this->createStream($resource) + ->shouldHaveContent('abcdef'); + } + + public function getMatchers() + { + return [ + 'haveContent' => function (StreamInterface $subject, $key) { + return $subject->getContents() === $key; + }, + ]; + } } From 2787e8f1ae6f84eb71e95f99ec1dad8e00191b27 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Mon, 16 Jan 2017 17:29:02 +0100 Subject: [PATCH 2/2] Do not rewind any stream --- spec/StreamFactory/StreamFactoryBehavior.php | 12 +++++----- src/StreamFactory/DiactorosStreamFactory.php | 25 +++++++------------- src/StreamFactory/SlimStreamFactory.php | 17 ++++--------- 3 files changed, 20 insertions(+), 34 deletions(-) diff --git a/spec/StreamFactory/StreamFactoryBehavior.php b/spec/StreamFactory/StreamFactoryBehavior.php index 20b06d0..f78a1c6 100644 --- a/spec/StreamFactory/StreamFactoryBehavior.php +++ b/spec/StreamFactory/StreamFactoryBehavior.php @@ -36,24 +36,24 @@ function it_creates_a_stream_from_non_seekable_resource() ->shouldHaveType('Psr\Http\Message\StreamInterface'); } - function it_rewinds_existing_stream() + function it_does_not_rewind_existing_stream() { $stream = new Stream(fopen('php://memory', 'rw')); $stream->write('abcdef'); - $stream->read(3); + $stream->seek(3); $this->createStream($stream) - ->shouldHaveContent('abcdef'); + ->shouldHaveContent('def'); } - function it_rewinds_existing_resource() + function it_does_not_rewind_existing_resource() { $resource = fopen('php://memory', 'rw'); fwrite($resource, 'abcdef'); - fread($resource, 3); + fseek($resource, 3); $this->createStream($resource) - ->shouldHaveContent('abcdef'); + ->shouldHaveContent('def'); } public function getMatchers() diff --git a/src/StreamFactory/DiactorosStreamFactory.php b/src/StreamFactory/DiactorosStreamFactory.php index a73abb0..a75ec98 100644 --- a/src/StreamFactory/DiactorosStreamFactory.php +++ b/src/StreamFactory/DiactorosStreamFactory.php @@ -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; } } diff --git a/src/StreamFactory/SlimStreamFactory.php b/src/StreamFactory/SlimStreamFactory.php index ba0bb17..efcadc4 100644 --- a/src/StreamFactory/SlimStreamFactory.php +++ b/src/StreamFactory/SlimStreamFactory.php @@ -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;