Chris@0: remoteStream = $stream; Chris@0: $this->stream = $target ?: new Stream(fopen('php://temp', 'r+')); Chris@0: } Chris@0: Chris@0: public function getSize() Chris@0: { Chris@0: return max($this->stream->getSize(), $this->remoteStream->getSize()); Chris@0: } Chris@0: Chris@0: public function rewind() Chris@0: { Chris@0: $this->seek(0); Chris@0: } Chris@0: Chris@0: public function seek($offset, $whence = SEEK_SET) Chris@0: { Chris@0: if ($whence == SEEK_SET) { Chris@0: $byte = $offset; Chris@0: } elseif ($whence == SEEK_CUR) { Chris@0: $byte = $offset + $this->tell(); Chris@0: } elseif ($whence == SEEK_END) { Chris@0: $size = $this->remoteStream->getSize(); Chris@0: if ($size === null) { Chris@0: $size = $this->cacheEntireStream(); Chris@0: } Chris@0: $byte = $size + $offset; Chris@0: } else { Chris@0: throw new \InvalidArgumentException('Invalid whence'); Chris@0: } Chris@0: Chris@0: $diff = $byte - $this->stream->getSize(); Chris@0: Chris@0: if ($diff > 0) { Chris@0: // Read the remoteStream until we have read in at least the amount Chris@0: // of bytes requested, or we reach the end of the file. Chris@0: while ($diff > 0 && !$this->remoteStream->eof()) { Chris@0: $this->read($diff); Chris@0: $diff = $byte - $this->stream->getSize(); Chris@0: } Chris@0: } else { Chris@0: // We can just do a normal seek since we've already seen this byte. Chris@0: $this->stream->seek($byte); Chris@0: } Chris@0: } Chris@0: Chris@0: public function read($length) Chris@0: { Chris@0: // Perform a regular read on any previously read data from the buffer Chris@0: $data = $this->stream->read($length); Chris@0: $remaining = $length - strlen($data); Chris@0: Chris@0: // More data was requested so read from the remote stream Chris@0: if ($remaining) { Chris@0: // If data was written to the buffer in a position that would have Chris@0: // been filled from the remote stream, then we must skip bytes on Chris@0: // the remote stream to emulate overwriting bytes from that Chris@0: // position. This mimics the behavior of other PHP stream wrappers. Chris@0: $remoteData = $this->remoteStream->read( Chris@0: $remaining + $this->skipReadBytes Chris@0: ); Chris@0: Chris@0: if ($this->skipReadBytes) { Chris@0: $len = strlen($remoteData); Chris@0: $remoteData = substr($remoteData, $this->skipReadBytes); Chris@0: $this->skipReadBytes = max(0, $this->skipReadBytes - $len); Chris@0: } Chris@0: Chris@0: $data .= $remoteData; Chris@0: $this->stream->write($remoteData); Chris@0: } Chris@0: Chris@0: return $data; Chris@0: } Chris@0: Chris@0: public function write($string) Chris@0: { Chris@0: // When appending to the end of the currently read stream, you'll want Chris@0: // to skip bytes from being read from the remote stream to emulate Chris@0: // other stream wrappers. Basically replacing bytes of data of a fixed Chris@0: // length. Chris@0: $overflow = (strlen($string) + $this->tell()) - $this->remoteStream->tell(); Chris@0: if ($overflow > 0) { Chris@0: $this->skipReadBytes += $overflow; Chris@0: } Chris@0: Chris@0: return $this->stream->write($string); Chris@0: } Chris@0: Chris@0: public function eof() Chris@0: { Chris@0: return $this->stream->eof() && $this->remoteStream->eof(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Close both the remote stream and buffer stream Chris@0: */ Chris@0: public function close() Chris@0: { Chris@0: $this->remoteStream->close() && $this->stream->close(); Chris@0: } Chris@0: Chris@0: private function cacheEntireStream() Chris@0: { Chris@0: $target = new FnStream(['write' => 'strlen']); Chris@0: copy_to_stream($this, $target); Chris@0: Chris@0: return $this->tell(); Chris@0: } Chris@0: }