diff --git a/CHANGELOG.md b/CHANGELOG.md index b306100..13b6f3c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ All notable changes to this project will be documented in this file, in reverse - `ServerRequestFactory::createServerRequestFromArray`, `ServerRequestFactory::createServerRequestFromArrays` and `ServerRequestFactory::createServerRequestFromGlobals`. Please use the new `nyholm/psr7-server` instead. - `UriFactory::createUriFromArray` since it was never used. +- `StreamFactory::copyToStream` ## 0.3.0 diff --git a/src/Factory/StreamFactory.php b/src/Factory/StreamFactory.php index ec65d63..c70ec0d 100644 --- a/src/Factory/StreamFactory.php +++ b/src/Factory/StreamFactory.php @@ -24,43 +24,4 @@ public function createStream($body = null) return Stream::create(null === $body ? '' : $body); } - - /** - * Copy the contents of a stream into another stream until the given number - * of bytes have been read. - * - * @author Michael Dowling and contributors to guzzlehttp/psr7 - * - * @param StreamInterface $source Stream to read from - * @param StreamInterface $dest Stream to write to - * @param int $maxLen Maximum number of bytes to read. Pass -1 - * to read the entire stream - * - * @throws \RuntimeException on error - */ - public function copyToStream(StreamInterface $source, StreamInterface $dest, $maxLen = -1) - { - if ($maxLen === -1) { - while (!$source->eof()) { - if (!$dest->write($source->read(1048576))) { - break; - } - } - - return; - } - - $bytes = 0; - while (!$source->eof()) { - $buf = $source->read($maxLen - $bytes); - if (!($len = strlen($buf))) { - break; - } - $bytes += $len; - $dest->write($buf); - if ($bytes === $maxLen) { - break; - } - } - } } diff --git a/src/UploadedFile.php b/src/UploadedFile.php index de375a3..5502431 100644 --- a/src/UploadedFile.php +++ b/src/UploadedFile.php @@ -5,7 +5,6 @@ namespace Nyholm\Psr7; use InvalidArgumentException; -use Nyholm\Psr7\Factory\StreamFactory; use Psr\Http\Message\StreamInterface; use Psr\Http\Message\UploadedFileInterface; use RuntimeException; @@ -184,11 +183,7 @@ public function moveTo($targetPath): void if ($stream->isSeekable()) { $stream->rewind(); } - (new StreamFactory())->copyToStream( - $stream, - Stream::createFromResource(fopen($targetPath, 'w')) - ); - + $this->copyToStream($stream, Stream::createFromResource(fopen($targetPath, 'w'))); $this->moved = true; } @@ -216,4 +211,43 @@ public function getClientMediaType(): ?string { return $this->clientMediaType; } + + /** + * Copy the contents of a stream into another stream until the given number + * of bytes have been read. + * + * @author Michael Dowling and contributors to guzzlehttp/psr7 + * + * @param StreamInterface $source Stream to read from + * @param StreamInterface $dest Stream to write to + * @param int $maxLen Maximum number of bytes to read. Pass -1 + * to read the entire stream + * + * @throws \RuntimeException on error + */ + private function copyToStream(StreamInterface $source, StreamInterface $dest, $maxLen = -1) + { + if ($maxLen === -1) { + while (!$source->eof()) { + if (!$dest->write($source->read(1048576))) { + break; + } + } + + return; + } + + $bytes = 0; + while (!$source->eof()) { + $buf = $source->read($maxLen - $bytes); + if (!($len = strlen($buf))) { + break; + } + $bytes += $len; + $dest->write($buf); + if ($bytes === $maxLen) { + break; + } + } + } }