From 79375f0709d5d0e2ca6f3cc55d3d7835d941c11b Mon Sep 17 00:00:00 2001 From: Joel Wurtz Date: Fri, 22 Jul 2016 16:00:08 +0200 Subject: [PATCH 1/2] Add Message cloner --- spec/MessageClonerSpec.php | 26 ++++++ src/MemoryClonedStream.php | 182 +++++++++++++++++++++++++++++++++++++ src/MessageCloner.php | 31 +++++++ 3 files changed, 239 insertions(+) create mode 100644 spec/MessageClonerSpec.php create mode 100644 src/MemoryClonedStream.php create mode 100644 src/MessageCloner.php diff --git a/spec/MessageClonerSpec.php b/spec/MessageClonerSpec.php new file mode 100644 index 0000000..f3be349 --- /dev/null +++ b/spec/MessageClonerSpec.php @@ -0,0 +1,26 @@ +shouldHaveType('Http\Message\MessageCloner'); + } + + public function it_clone_a_message(MessageInterface $message, MessageInterface $messageCloned) + { + $stream = new MemoryStream('test'); + $message->getBody()->willReturn($stream); + $message->withBody(Argument::type(MemoryClonedStream::class))->willReturn($messageCloned); + + $this->cloneMessage($message)->shouldEqual($messageCloned); + } +} diff --git a/src/MemoryClonedStream.php b/src/MemoryClonedStream.php new file mode 100644 index 0000000..327a23b --- /dev/null +++ b/src/MemoryClonedStream.php @@ -0,0 +1,182 @@ +size = 0; + + if ($useFileBuffer) { + $this->resource = fopen('php://temp/maxmemory:'.$memoryBuffer, 'rw+'); + } else { + $this->resource = fopen('php://memory', 'rw+'); + } + + $position = null; + + if ($stream->isSeekable()) { + $position = $stream->tell(); + $stream->rewind(); + } + + while (!$stream->eof()) { + $this->size += fwrite($this->resource, $stream->read(self::COPY_BUFFER)); + } + + if ($stream->isSeekable()) { + $stream->seek($position); + } + + rewind($this->resource); + } + + /** + * {@inheritdoc} + */ + public function __toString() + { + return $this->getContents(); + } + + /** + * {@inheritdoc} + */ + public function close() + { + fclose($this->resource); + } + + /** + * {@inheritdoc} + */ + public function detach() + { + $resource = $this->resource; + $this->resource = null; + + return $resource; + } + + /** + * {@inheritdoc} + */ + public function getSize() + { + return $this->size; + } + + /** + * {@inheritdoc} + */ + public function tell() + { + return ftell($this->resource); + } + + /** + * {@inheritdoc} + */ + public function eof() + { + return feof($this->resource); + } + + /** + * {@inheritdoc} + */ + public function isSeekable() + { + return true; + } + + /** + * {@inheritdoc} + */ + public function seek($offset, $whence = SEEK_SET) + { + return fseek($this->resource, $offset); + } + + /** + * {@inheritdoc} + */ + public function rewind() + { + return rewind($this->resource); + } + + /** + * {@inheritdoc} + */ + public function isWritable() + { + return true; + } + + /** + * {@inheritdoc} + */ + public function write($string) + { + return fwrite($this->resource, $string); + } + + /** + * {@inheritdoc} + */ + public function isReadable() + { + return true; + } + + /** + * {@inheritdoc} + */ + public function read($length) + { + return fread($this->resource, $length); + } + + /** + * {@inheritdoc} + */ + public function getContents() + { + $this->rewind(); + + return $this->read($this->size); + } + + /** + * {@inheritdoc} + */ + public function getMetadata($key = null) + { + $metadata = stream_get_meta_data($this->resource); + + if (null === $key) { + return $metadata; + } + + return $metadata[$key]; + } +} diff --git a/src/MessageCloner.php b/src/MessageCloner.php new file mode 100644 index 0000000..5943698 --- /dev/null +++ b/src/MessageCloner.php @@ -0,0 +1,31 @@ +withBody(new MemoryClonedStream($message->getBody())); + } +} From 5422e8434a6d34f55434a8e43c392cc794fe580b Mon Sep 17 00:00:00 2001 From: Joel Wurtz Date: Fri, 22 Jul 2016 16:09:29 +0200 Subject: [PATCH 2/2] Fix test on php 5.4 --- spec/MessageClonerSpec.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spec/MessageClonerSpec.php b/spec/MessageClonerSpec.php index f3be349..9480ac7 100644 --- a/spec/MessageClonerSpec.php +++ b/spec/MessageClonerSpec.php @@ -2,7 +2,6 @@ namespace spec\Http\Message; -use Http\Message\MemoryClonedStream; use PhpSpec\ObjectBehavior; use Prophecy\Argument; use Psr\Http\Message\MessageInterface; @@ -19,7 +18,7 @@ public function it_clone_a_message(MessageInterface $message, MessageInterface $ { $stream = new MemoryStream('test'); $message->getBody()->willReturn($stream); - $message->withBody(Argument::type(MemoryClonedStream::class))->willReturn($messageCloned); + $message->withBody(Argument::type('Http\Message\MemoryClonedStream'))->willReturn($messageCloned); $this->cloneMessage($message)->shouldEqual($messageCloned); }