Verzeichnisstruktur phpBB-3.2.0
- Veröffentlicht
- 06.01.2017
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
CachingStream.php
001 <?php
002 namespace GuzzleHttp\Stream;
003
004 use GuzzleHttp\Stream\Exception\SeekException;
005
006 /**
007 * Stream decorator that can cache previously read bytes from a sequentially
008 * read stream.
009 */
010 class CachingStream implements StreamInterface
011 {
012 use StreamDecoratorTrait;
013
014 /** @var StreamInterface Stream being wrapped */
015 private $remoteStream;
016
017 /** @var int Number of bytes to skip reading due to a write on the buffer */
018 private $skipReadBytes = 0;
019
020 /**
021 * We will treat the buffer object as the body of the stream
022 *
023 * @param StreamInterface $stream Stream to cache
024 * @param StreamInterface $target Optionally specify where data is cached
025 */
026 public function __construct(
027 StreamInterface $stream,
028 StreamInterface $target = null
029 ) {
030 $this->remoteStream = $stream;
031 $this->stream = $target ?: new Stream(fopen('php://temp', 'r+'));
032 }
033
034 public function getSize()
035 {
036 return max($this->stream->getSize(), $this->remoteStream->getSize());
037 }
038
039 /**
040 * {@inheritdoc}
041 * @throws SeekException When seeking with SEEK_END or when seeking
042 * past the total size of the buffer stream
043 */
044 public function seek($offset, $whence = SEEK_SET)
045 {
046 if ($whence == SEEK_SET) {
047 $byte = $offset;
048 } elseif ($whence == SEEK_CUR) {
049 $byte = $offset + $this->tell();
050 } else {
051 return false;
052 }
053
054 // You cannot skip ahead past where you've read from the remote stream
055 if ($byte > $this->stream->getSize()) {
056 throw new SeekException(
057 $this,
058 $byte,
059 sprintf('Cannot seek to byte %d when the buffered stream only'
060 . ' contains %d bytes', $byte, $this->stream->getSize())
061 );
062 }
063
064 return $this->stream->seek($byte);
065 }
066
067 public function read($length)
068 {
069 // Perform a regular read on any previously read data from the buffer
070 $data = $this->stream->read($length);
071 $remaining = $length - strlen($data);
072
073 // More data was requested so read from the remote stream
074 if ($remaining) {
075 // If data was written to the buffer in a position that would have
076 // been filled from the remote stream, then we must skip bytes on
077 // the remote stream to emulate overwriting bytes from that
078 // position. This mimics the behavior of other PHP stream wrappers.
079 $remoteData = $this->remoteStream->read(
080 $remaining + $this->skipReadBytes
081 );
082
083 if ($this->skipReadBytes) {
084 $len = strlen($remoteData);
085 $remoteData = substr($remoteData, $this->skipReadBytes);
086 $this->skipReadBytes = max(0, $this->skipReadBytes - $len);
087 }
088
089 $data .= $remoteData;
090 $this->stream->write($remoteData);
091 }
092
093 return $data;
094 }
095
096 public function write($string)
097 {
098 // When appending to the end of the currently read stream, you'll want
099 // to skip bytes from being read from the remote stream to emulate
100 // other stream wrappers. Basically replacing bytes of data of a fixed
101 // length.
102 $overflow = (strlen($string) + $this->tell()) - $this->remoteStream->tell();
103 if ($overflow > 0) {
104 $this->skipReadBytes += $overflow;
105 }
106
107 return $this->stream->write($string);
108 }
109
110 public function eof()
111 {
112 return $this->stream->eof() && $this->remoteStream->eof();
113 }
114
115 /**
116 * Close both the remote stream and buffer stream
117 */
118 public function close()
119 {
120 $this->remoteStream->close() && $this->stream->close();
121 }
122 }
123