| @@ -1,6 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | +declare (strict_types=1); | |
| 3 | 4 | namespace Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7; |
| 4 | 5 | |
| 5 | 6 | use Dudlewebs\WPMCS\s3\Psr\Http\Message\StreamInterface; |
| 6 | 7 | /** |
| @@ -5,12 +6,10 @@ | ||
| 5 | 6 | use Dudlewebs\WPMCS\s3\Psr\Http\Message\StreamInterface; |
| 6 | 7 | /** |
| 7 | 8 | * Stream decorator that can cache previously read bytes from a sequentially |
| 8 | 9 | * read stream. |
| 9 | - * | |
| 10 | - * @final | |
| 11 | 10 | */ |
| 12 | -class CachingStream implements StreamInterface | |
| 11 | +final class CachingStream implements StreamInterface | |
| 13 | 12 | { |
| 14 | 13 | use StreamDecoratorTrait; |
| 15 | 14 | /** @var StreamInterface Stream being wrapped */ |
| 16 | 15 | private $remoteStream; |
| @@ -16,19 +15,23 @@ | ||
| 16 | 15 | private $remoteStream; |
| 17 | 16 | /** @var int Number of bytes to skip reading due to a write on the buffer */ |
| 18 | 17 | private $skipReadBytes = 0; |
| 19 | 18 | /** |
| 19 | + * @var StreamInterface | |
| 20 | + */ | |
| 21 | + private $stream; | |
| 22 | + /** | |
| 20 | 23 | * We will treat the buffer object as the body of the stream |
| 21 | 24 | * |
| 22 | 25 | * @param StreamInterface $stream Stream to cache. The cursor is assumed to be at the beginning of the stream. |
| 23 | 26 | * @param StreamInterface $target Optionally specify where data is cached |
| 24 | 27 | */ |
| 25 | - public function __construct(StreamInterface $stream, StreamInterface $target = null) | |
| 28 | + public function __construct(StreamInterface $stream, ?StreamInterface $target = null) | |
| 26 | 29 | { |
| 27 | 30 | $this->remoteStream = $stream; |
| 28 | 31 | $this->stream = $target ?: new Stream(Utils::tryFopen('php://temp', 'r+')); |
| 29 | 32 | } |
| 30 | - public function getSize() | |
| 33 | + public function getSize() : ?int | |
| 31 | 34 | { |
| 32 | 35 | $remoteSize = $this->remoteStream->getSize(); |
| 33 | 36 | if (null === $remoteSize) { |
| 34 | 37 | return null; |
| @@ -34,19 +37,19 @@ | ||
| 34 | 37 | return null; |
| 35 | 38 | } |
| 36 | 39 | return \max($this->stream->getSize(), $remoteSize); |
| 37 | 40 | } |
| 38 | - public function rewind() | |
| 41 | + public function rewind() : void | |
| 39 | 42 | { |
| 40 | 43 | $this->seek(0); |
| 41 | 44 | } |
| 42 | - public function seek($offset, $whence = \SEEK_SET) | |
| 45 | + public function seek($offset, $whence = \SEEK_SET) : void | |
| 43 | 46 | { |
| 44 | - if ($whence == \SEEK_SET) { | |
| 47 | + if ($whence === \SEEK_SET) { | |
| 45 | 48 | $byte = $offset; |
| 46 | - } elseif ($whence == \SEEK_CUR) { | |
| 49 | + } elseif ($whence === \SEEK_CUR) { | |
| 47 | 50 | $byte = $offset + $this->tell(); |
| 48 | - } elseif ($whence == \SEEK_END) { | |
| 51 | + } elseif ($whence === \SEEK_END) { | |
| 49 | 52 | $size = $this->remoteStream->getSize(); |
| 50 | 53 | if ($size === null) { |
| 51 | 54 | $size = $this->cacheEntireStream(); |
| 52 | 55 | } |
| @@ -66,9 +69,9 @@ | ||
| 66 | 69 | // We can just do a normal seek since we've already seen this byte. |
| 67 | 70 | $this->stream->seek($byte); |
| 68 | 71 | } |
| 69 | 72 | } |
| 70 | - public function read($length) | |
| 73 | + public function read($length) : string | |
| 71 | 74 | { |
| 72 | 75 | // Perform a regular read on any previously read data from the buffer |
| 73 | 76 | $data = $this->stream->read($length); |
| 74 | 77 | $remaining = $length - \strlen($data); |
| @@ -88,9 +91,9 @@ | ||
| 88 | 91 | $this->stream->write($remoteData); |
| 89 | 92 | } |
| 90 | 93 | return $data; |
| 91 | 94 | } |
| 92 | - public function write($string) | |
| 95 | + public function write($string) : int | |
| 93 | 96 | { |
| 94 | 97 | // When appending to the end of the currently read stream, you'll want |
| 95 | 98 | // to skip bytes from being read from the remote stream to emulate |
| 96 | 99 | // other stream wrappers. Basically replacing bytes of data of a fixed |
| @@ -100,9 +103,9 @@ | ||
| 100 | 103 | $this->skipReadBytes += $overflow; |
| 101 | 104 | } |
| 102 | 105 | return $this->stream->write($string); |
| 103 | 106 | } |
| 104 | - public function eof() | |
| 107 | + public function eof() : bool | |
| 105 | 108 | { |
| 106 | 109 | return $this->stream->eof() && $this->remoteStream->eof(); |
| 107 | 110 | } |
| 108 | 111 | /** |
| @@ -107,13 +110,14 @@ | ||
| 107 | 110 | } |
| 108 | 111 | /** |
| 109 | 112 | * Close both the remote stream and buffer stream |
| 110 | 113 | */ |
| 111 | - public function close() | |
| 114 | + public function close() : void | |
| 112 | 115 | { |
| 113 | - $this->remoteStream->close() && $this->stream->close(); | |
| 116 | + $this->remoteStream->close(); | |
| 117 | + $this->stream->close(); | |
| 114 | 118 | } |
| 115 | - private function cacheEntireStream() | |
| 119 | + private function cacheEntireStream() : int | |
| 116 | 120 | { |
| 117 | 121 | $target = new FnStream(['write' => 'strlen']); |
| 118 | 122 | Utils::copyToStream($this, $target); |
| 119 | 123 | return $this->tell(); |