ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
GuzzleHttp\Psr7\AppendStream Class Reference

Reads from multiple streams, one after the other. More...

+ Inheritance diagram for GuzzleHttp\Psr7\AppendStream:
+ Collaboration diagram for GuzzleHttp\Psr7\AppendStream:

Public Member Functions

 __construct (array $streams=[])
 
 __toString ()
 Reads all data from the stream into a string, from the beginning to end. More...
 
 addStream (StreamInterface $stream)
 Add a stream to the AppendStream. More...
 
 getContents ()
 Returns the remaining contents in a string. More...
 
 close ()
 Closes each attached stream. More...
 
 detach ()
 Detaches each attached stream. More...
 
 tell ()
 Returns the current position of the file read/write pointer. More...
 
 getSize ()
 Tries to calculate the size by adding the size of each stream. More...
 
 eof ()
 Returns true if the stream is at the end of the stream. More...
 
 rewind ()
 Seek to the beginning of the stream. More...
 
 seek ($offset, $whence=SEEK_SET)
 Attempts to seek to the given position. More...
 
 read ($length)
 Reads from all of the appended streams until the length is met or EOF. More...
 
 isReadable ()
 Returns whether or not the stream is readable. More...
 
 isWritable ()
 Returns whether or not the stream is writable. More...
 
 isSeekable ()
 Returns whether or not the stream is seekable. More...
 
 write ($string)
 Write data to the stream. More...
 
 getMetadata ($key=null)
 Get stream metadata as an associative array or retrieve a specific key. More...
 
 __toString ()
 Reads all data from the stream into a string, from the beginning to end. More...
 
 close ()
 Closes the stream and any underlying resources. More...
 
 detach ()
 Separates any underlying resources from the stream. More...
 
 getSize ()
 Get the size of the stream if known. More...
 
 tell ()
 Returns the current position of the file read/write pointer. More...
 
 eof ()
 Returns true if the stream is at the end of the stream. More...
 
 isSeekable ()
 Returns whether or not the stream is seekable. More...
 
 seek ($offset, $whence=SEEK_SET)
 Seek to a position in the stream. More...
 
 rewind ()
 Seek to the beginning of the stream. More...
 
 isWritable ()
 Returns whether or not the stream is writable. More...
 
 write ($string)
 Write data to the stream. More...
 
 isReadable ()
 Returns whether or not the stream is readable. More...
 
 read ($length)
 Read data from the stream. More...
 
 getContents ()
 Returns the remaining contents in a string. More...
 
 getMetadata ($key=null)
 Get stream metadata as an associative array or retrieve a specific key. More...
 

Private Attributes

 $streams = []
 
 $seekable = true
 
 $current = 0
 
 $pos = 0
 
 $detached = false
 

Detailed Description

Reads from multiple streams, one after the other.

This is a read-only stream decorator.

Definition at line 11 of file AppendStream.php.

Constructor & Destructor Documentation

◆ __construct()

GuzzleHttp\Psr7\AppendStream::__construct ( array  $streams = [])
Parameters
StreamInterface[]$streamsStreams to decorate. Each stream must be readable.

Definition at line 25 of file AppendStream.php.

26 {
27 foreach ($streams as $stream) {
28 $this->addStream($stream);
29 }
30 }
addStream(StreamInterface $stream)
Add a stream to the AppendStream.
$stream
PHP stream implementation.

References GuzzleHttp\Psr7\$stream, GuzzleHttp\Psr7\AppendStream\$streams, and GuzzleHttp\Psr7\AppendStream\addStream().

+ Here is the call graph for this function:

Member Function Documentation

◆ __toString()

GuzzleHttp\Psr7\AppendStream::__toString ( )

Reads all data from the stream into a string, from the beginning to end.

This method MUST attempt to seek to the beginning of the stream before reading data and read the stream until the end is reached.

Warning: This could attempt to load a large amount of data into memory.

This method MUST NOT raise an exception in order to conform with PHP's string casting operations.

See also
http://php.net/manual/en/language.oop5.magic.php#object.tostring
Returns
string

Implements Psr\Http\Message\StreamInterface.

Definition at line 32 of file AppendStream.php.

33 {
34 try {
35 $this->rewind();
36 return $this->getContents();
37 } catch (\Exception $e) {
38 return '';
39 }
40 }
rewind()
Seek to the beginning of the stream.
getContents()
Returns the remaining contents in a string.

References GuzzleHttp\Psr7\AppendStream\getContents(), and GuzzleHttp\Psr7\AppendStream\rewind().

+ Here is the call graph for this function:

◆ addStream()

GuzzleHttp\Psr7\AppendStream::addStream ( StreamInterface  $stream)

Add a stream to the AppendStream.

Parameters
StreamInterface$streamStream to append. Must be readable.
Exceptions

InvalidArgumentException if the stream is not readable

Definition at line 49 of file AppendStream.php.

50 {
51 if (!$stream->isReadable()) {
52 throw new \InvalidArgumentException('Each stream must be readable');
53 }
54
55 // The stream is only seekable if all streams are seekable
56 if (!$stream->isSeekable()) {
57 $this->seekable = false;
58 }
59
60 $this->streams[] = $stream;
61 }

References GuzzleHttp\Psr7\$stream.

Referenced by GuzzleHttp\Psr7\AppendStream\__construct().

+ Here is the caller graph for this function:

◆ close()

GuzzleHttp\Psr7\AppendStream::close ( )

Closes each attached stream.

{Closes the stream and any underlying resources.

Returns
void
}

Implements Psr\Http\Message\StreamInterface.

Definition at line 73 of file AppendStream.php.

74 {
75 $this->pos = $this->current = 0;
76
77 foreach ($this->streams as $stream) {
78 $stream->close();
79 }
80
81 $this->streams = [];
82 }

References GuzzleHttp\Psr7\$stream.

Referenced by GuzzleHttp\Psr7\AppendStream\detach().

+ Here is the caller graph for this function:

◆ detach()

GuzzleHttp\Psr7\AppendStream::detach ( )

Detaches each attached stream.

{Separates any underlying resources from the stream.After the stream has been detached, the stream is in an unusable state.

Returns
resource|null Underlying PHP stream, if any
}

Implements Psr\Http\Message\StreamInterface.

Definition at line 89 of file AppendStream.php.

90 {
91 $this->close();
92 $this->detached = true;
93 }
close()
Closes each attached stream.

References GuzzleHttp\Psr7\AppendStream\close().

+ Here is the call graph for this function:

◆ eof()

GuzzleHttp\Psr7\AppendStream::eof ( )

Returns true if the stream is at the end of the stream.

Returns
bool

Implements Psr\Http\Message\StreamInterface.

Definition at line 123 of file AppendStream.php.

124 {
125 return !$this->streams ||
126 ($this->current >= count($this->streams) - 1 &&
127 $this->streams[$this->current]->eof());
128 }

References GuzzleHttp\Psr7\AppendStream\$current.

Referenced by GuzzleHttp\Psr7\AppendStream\read(), and GuzzleHttp\Psr7\AppendStream\seek().

+ Here is the caller graph for this function:

◆ getContents()

GuzzleHttp\Psr7\AppendStream::getContents ( )

Returns the remaining contents in a string.

Returns
string
Exceptions

RuntimeException if unable to read or an error occurs while reading.

Implements Psr\Http\Message\StreamInterface.

Definition at line 63 of file AppendStream.php.

64 {
65 return copy_to_string($this);
66 }
copy_to_string(StreamInterface $stream, $maxLen=-1)
Copy the contents of a stream into a string until the given number of bytes have been read.
Definition: functions.php:328

References GuzzleHttp\Psr7\copy_to_string().

Referenced by GuzzleHttp\Psr7\AppendStream\__toString().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getMetadata()

GuzzleHttp\Psr7\AppendStream::getMetadata (   $key = null)

Get stream metadata as an associative array or retrieve a specific key.

The keys returned are identical to the keys returned from PHP's stream_get_meta_data() function.

string $key Specific metadata to retrieve. array|mixed|null Returns an associative array if no key is provided. Returns a specific key value if a key is provided and the value is found, or null if the key is not found.

Implements Psr\Http\Message\StreamInterface.

Definition at line 229 of file AppendStream.php.

230 {
231 return $key ? null : [];
232 }
$key
Definition: croninfo.php:18

References $key.

◆ getSize()

GuzzleHttp\Psr7\AppendStream::getSize ( )

Tries to calculate the size by adding the size of each stream.

If any of the streams do not return a valid number, then the size of the append stream cannot be determined and null is returned.

{Get the size of the stream if known.

Returns
int|null Returns the size in bytes if known, or null if unknown.
}

Implements Psr\Http\Message\StreamInterface.

Definition at line 108 of file AppendStream.php.

109 {
110 $size = 0;
111
112 foreach ($this->streams as $stream) {
113 $s = $stream->getSize();
114 if ($s === null) {
115 return null;
116 }
117 $size += $s;
118 }
119
120 return $size;
121 }
$size
Definition: RandomTest.php:84
$s
Definition: pwgen.php:45

References $s, $size, and GuzzleHttp\Psr7\$stream.

◆ isReadable()

GuzzleHttp\Psr7\AppendStream::isReadable ( )

Returns whether or not the stream is readable.

Returns
bool

Implements Psr\Http\Message\StreamInterface.

Definition at line 209 of file AppendStream.php.

210 {
211 return true;
212 }

◆ isSeekable()

GuzzleHttp\Psr7\AppendStream::isSeekable ( )

Returns whether or not the stream is seekable.

Returns
bool

Implements Psr\Http\Message\StreamInterface.

Definition at line 219 of file AppendStream.php.

220 {
221 return $this->seekable;
222 }

References GuzzleHttp\Psr7\AppendStream\$seekable.

◆ isWritable()

GuzzleHttp\Psr7\AppendStream::isWritable ( )

Returns whether or not the stream is writable.

Returns
bool

Implements Psr\Http\Message\StreamInterface.

Definition at line 214 of file AppendStream.php.

215 {
216 return false;
217 }

◆ read()

GuzzleHttp\Psr7\AppendStream::read (   $length)

Reads from all of the appended streams until the length is met or EOF.

{Read data from the stream.

Parameters
int$lengthRead up to $length bytes from the object and return them. Fewer than $length bytes may be returned if underlying stream call returns fewer bytes.
Returns
string Returns the data read from the stream, or an empty string if no bytes are available.
Exceptions

RuntimeException if an error occurs.}

Implements Psr\Http\Message\StreamInterface.

Definition at line 174 of file AppendStream.php.

175 {
176 $buffer = '';
177 $total = count($this->streams) - 1;
178 $remaining = $length;
179 $progressToNext = false;
180
181 while ($remaining > 0) {
182
183 // Progress to the next stream if needed.
184 if ($progressToNext || $this->streams[$this->current]->eof()) {
185 $progressToNext = false;
186 if ($this->current === $total) {
187 break;
188 }
189 $this->current++;
190 }
191
192 $result = $this->streams[$this->current]->read($remaining);
193
194 // Using a loose comparison here to match on '', false, and null
195 if ($result == null) {
196 $progressToNext = true;
197 continue;
198 }
199
200 $buffer .= $result;
201 $remaining = $length - strlen($buffer);
202 }
203
204 $this->pos += strlen($buffer);
205
206 return $buffer;
207 }
$result
$total
Definition: Utf8Test.php:87
eof()
Returns true if the stream is at the end of the stream.
if($state['core:TerminatedAssocId'] !==null) $remaining

References GuzzleHttp\Psr7\AppendStream\$current, $remaining, $result, $total, and GuzzleHttp\Psr7\AppendStream\eof().

Referenced by GuzzleHttp\Psr7\AppendStream\seek().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ rewind()

GuzzleHttp\Psr7\AppendStream::rewind ( )

Seek to the beginning of the stream.

If the stream is not seekable, this method will raise an exception; otherwise, it will perform a seek(0).

See also
seek() on failure.

Implements Psr\Http\Message\StreamInterface.

Definition at line 130 of file AppendStream.php.

131 {
132 $this->seek(0);
133 }
seek($offset, $whence=SEEK_SET)
Attempts to seek to the given position.

References GuzzleHttp\Psr7\AppendStream\seek().

Referenced by GuzzleHttp\Psr7\AppendStream\__toString().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ seek()

GuzzleHttp\Psr7\AppendStream::seek (   $offset,
  $whence = SEEK_SET 
)

Attempts to seek to the given position.

Only supports SEEK_SET.

{Seek to a position in the stream.int $offset Stream offset int $whence Specifies how the cursor position will be calculated based on the seek offset. Valid values are identical to the built-in PHP $whence values for fseek(). SEEK_SET: Set position equal to offset bytes SEEK_CUR: Set position to current location plus offset SEEK_END: Set position to end-of-stream plus offset. on failure.}

Implements Psr\Http\Message\StreamInterface.

Definition at line 140 of file AppendStream.php.

141 {
142 if (!$this->seekable) {
143 throw new \RuntimeException('This AppendStream is not seekable');
144 } elseif ($whence !== SEEK_SET) {
145 throw new \RuntimeException('The AppendStream can only seek with SEEK_SET');
146 }
147
148 $this->pos = $this->current = 0;
149
150 // Rewind each stream
151 foreach ($this->streams as $i => $stream) {
152 try {
153 $stream->rewind();
154 } catch (\Exception $e) {
155 throw new \RuntimeException('Unable to seek stream '
156 . $i . ' of the AppendStream', 0, $e);
157 }
158 }
159
160 // Seek to the actual position by reading from each stream
161 while ($this->pos < $offset && !$this->eof()) {
162 $result = $this->read(min(8096, $offset - $this->pos));
163 if ($result === '') {
164 break;
165 }
166 }
167 }
read($length)
Reads from all of the appended streams until the length is met or EOF.
$i
Definition: disco.tpl.php:19

References $i, $result, GuzzleHttp\Psr7\$stream, GuzzleHttp\Psr7\AppendStream\eof(), and GuzzleHttp\Psr7\AppendStream\read().

Referenced by GuzzleHttp\Psr7\AppendStream\rewind().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ tell()

GuzzleHttp\Psr7\AppendStream::tell ( )

Returns the current position of the file read/write pointer.

Returns
int Position of the file pointer
Exceptions

RuntimeException on error.

Implements Psr\Http\Message\StreamInterface.

Definition at line 95 of file AppendStream.php.

References GuzzleHttp\Psr7\AppendStream\$pos.

◆ write()

GuzzleHttp\Psr7\AppendStream::write (   $string)

Write data to the stream.

Parameters
string$stringThe string that is to be written.
Returns
int Returns the number of bytes written to the stream.
Exceptions

RuntimeException on failure.

Implements Psr\Http\Message\StreamInterface.

Definition at line 224 of file AppendStream.php.

225 {
226 throw new \RuntimeException('Cannot write to an AppendStream');
227 }

Field Documentation

◆ $current

GuzzleHttp\Psr7\AppendStream::$current = 0
private

◆ $detached

GuzzleHttp\Psr7\AppendStream::$detached = false
private

Definition at line 19 of file AppendStream.php.

◆ $pos

GuzzleHttp\Psr7\AppendStream::$pos = 0
private

Definition at line 18 of file AppendStream.php.

Referenced by GuzzleHttp\Psr7\AppendStream\tell().

◆ $seekable

GuzzleHttp\Psr7\AppendStream::$seekable = true
private

Definition at line 16 of file AppendStream.php.

Referenced by GuzzleHttp\Psr7\AppendStream\isSeekable().

◆ $streams

GuzzleHttp\Psr7\AppendStream::$streams = []
private

Definition at line 14 of file AppendStream.php.

Referenced by GuzzleHttp\Psr7\AppendStream\__construct().


The documentation for this class was generated from the following file: