Skip to content

Commit

Permalink
Move StreamFactory::copyToStream to (private) UploadedFile (#60)
Browse files Browse the repository at this point in the history
* Move StreamFactory::copyToStream to (private) UploadedFile

* cs

* Added changelog
  • Loading branch information
Nyholm committed Aug 1, 2018
1 parent 38507df commit f4a4ac3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 45 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
39 changes: 0 additions & 39 deletions src/Factory/StreamFactory.php
Expand Up @@ -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;
}
}
}
}
46 changes: 40 additions & 6 deletions src/UploadedFile.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
}
}
}

0 comments on commit f4a4ac3

Please sign in to comment.