ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
GuzzleHttp\Psr7\Stream Class Reference
+ Inheritance diagram for GuzzleHttp\Psr7\Stream:
+ Collaboration diagram for GuzzleHttp\Psr7\Stream:

Public Member Functions

 __construct ($stream, $options=[])
 This constructor accepts an associative array of options. More...
 
 __get ($name)
 
 __destruct ()
 Closes the stream when the destructed. More...
 
 __toString ()
 Reads all data from the stream into a string, from the beginning to end. More...
 
 getContents ()
 Returns the remaining contents in a string. 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...
 
 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...
 
 eof ()
 Returns true if the stream is at the end of the stream. More...
 
 tell ()
 Returns the current position of the file read/write pointer. More...
 
 rewind ()
 Seek to the beginning of the stream. More...
 
 seek ($offset, $whence=SEEK_SET)
 Seek to a position in the stream. More...
 
 read ($length)
 Read data from the stream. 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

 $stream
 
 $size
 
 $seekable
 
 $readable
 
 $writable
 
 $uri
 
 $customMetadata
 

Static Private Attributes

static $readWriteHash
 

Detailed Description

Definition at line 11 of file Stream.php.

Constructor & Destructor Documentation

◆ __construct()

GuzzleHttp\Psr7\Stream::__construct (   $stream,
  $options = [] 
)

This constructor accepts an associative array of options.

  • size: (int) If a read stream would otherwise have an indeterminate size, but the size is known due to foreknowledge, then you can provide that size, in bytes.
  • metadata: (array) Any additional metadata to return when the metadata of the stream is accessed.
Parameters
resource$streamStream resource to wrap.
array$optionsAssociative array of options.
Exceptions

InvalidArgumentException if the stream is not a stream resource

Definition at line 51 of file Stream.php.

52 {
53 if (!is_resource($stream)) {
54 throw new \InvalidArgumentException('Stream must be a resource');
55 }
56
57 if (isset($options['size'])) {
58 $this->size = $options['size'];
59 }
60
61 $this->customMetadata = isset($options['metadata'])
62 ? $options['metadata']
63 : [];
64
65 $this->stream = $stream;
66 $meta = stream_get_meta_data($this->stream);
67 $this->seekable = $meta['seekable'];
68 $this->readable = isset(self::$readWriteHash['read'][$meta['mode']]);
69 $this->writable = isset(self::$readWriteHash['write'][$meta['mode']]);
70 $this->uri = $this->getMetadata('uri');
71 }
if(!isset( $_REQUEST[ 'ReturnTo'])) if(!isset($_REQUEST['AuthId'])) $options
Definition: as_login.php:20
getMetadata($key=null)
Get stream metadata as an associative array or retrieve a specific key.
Definition: Stream.php:243

References $options, GuzzleHttp\Psr7\Stream\$stream, and GuzzleHttp\Psr7\Stream\getMetadata().

+ Here is the call graph for this function:

◆ __destruct()

GuzzleHttp\Psr7\Stream::__destruct ( )

Closes the stream when the destructed.

Definition at line 85 of file Stream.php.

86 {
87 $this->close();
88 }
close()
Closes the stream and any underlying resources.
Definition: Stream.php:111

References GuzzleHttp\Psr7\Stream\close().

+ Here is the call graph for this function:

Member Function Documentation

◆ __get()

GuzzleHttp\Psr7\Stream::__get (   $name)

Definition at line 73 of file Stream.php.

74 {
75 if ($name == 'stream') {
76 throw new \RuntimeException('The stream is detached');
77 }
78
79 throw new \BadMethodCallException('No value for ' . $name);
80 }
if($format !==null) $name
Definition: metadata.php:146

References $name.

◆ __toString()

GuzzleHttp\Psr7\Stream::__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 90 of file Stream.php.

91 {
92 try {
93 $this->seek(0);
94 return (string) stream_get_contents($this->stream);
95 } catch (\Exception $e) {
96 return '';
97 }
98 }
seek($offset, $whence=SEEK_SET)
Seek to a position in the stream.
Definition: Stream.php:195

References GuzzleHttp\Psr7\Stream\seek().

+ Here is the call graph for this function:

◆ close()

GuzzleHttp\Psr7\Stream::close ( )

Closes the stream and any underlying resources.

Returns
void

Implements Psr\Http\Message\StreamInterface.

Definition at line 111 of file Stream.php.

112 {
113 if (isset($this->stream)) {
114 if (is_resource($this->stream)) {
115 fclose($this->stream);
116 }
117 $this->detach();
118 }
119 }
detach()
Separates any underlying resources from the stream.
Definition: Stream.php:121

References GuzzleHttp\Psr7\Stream\detach().

Referenced by GuzzleHttp\Psr7\Stream\__destruct().

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

◆ detach()

GuzzleHttp\Psr7\Stream::detach ( )

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 121 of file Stream.php.

122 {
123 if (!isset($this->stream)) {
124 return null;
125 }
126
128 unset($this->stream);
129 $this->size = $this->uri = null;
130 $this->readable = $this->writable = $this->seekable = false;
131
132 return $result;
133 }
$result

References $result, and GuzzleHttp\Psr7\Stream\$stream.

Referenced by GuzzleHttp\Psr7\Stream\close().

+ Here is the caller graph for this function:

◆ eof()

GuzzleHttp\Psr7\Stream::eof ( )

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

Returns
bool

Implements Psr\Http\Message\StreamInterface.

Definition at line 174 of file Stream.php.

175 {
176 return !$this->stream || feof($this->stream);
177 }

◆ getContents()

GuzzleHttp\Psr7\Stream::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 100 of file Stream.php.

101 {
102 $contents = stream_get_contents($this->stream);
103
104 if ($contents === false) {
105 throw new \RuntimeException('Unable to read stream contents');
106 }
107
108 return $contents;
109 }

References $contents.

◆ getMetadata()

GuzzleHttp\Psr7\Stream::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 243 of file Stream.php.

244 {
245 if (!isset($this->stream)) {
246 return $key ? null : [];
247 } elseif (!$key) {
248 return $this->customMetadata + stream_get_meta_data($this->stream);
249 } elseif (isset($this->customMetadata[$key])) {
250 return $this->customMetadata[$key];
251 }
252
253 $meta = stream_get_meta_data($this->stream);
254
255 return isset($meta[$key]) ? $meta[$key] : null;
256 }
$key
Definition: croninfo.php:18

References $key.

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

+ Here is the caller graph for this function:

◆ getSize()

GuzzleHttp\Psr7\Stream::getSize ( )

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 135 of file Stream.php.

136 {
137 if ($this->size !== null) {
138 return $this->size;
139 }
140
141 if (!isset($this->stream)) {
142 return null;
143 }
144
145 // Clear the stat cache if the stream has a URI
146 if ($this->uri) {
147 clearstatcache(true, $this->uri);
148 }
149
150 $stats = fstat($this->stream);
151 if (isset($stats['size'])) {
152 $this->size = $stats['size'];
153 return $this->size;
154 }
155
156 return null;
157 }
$stats

References GuzzleHttp\Psr7\Stream\$size, and $stats.

◆ isReadable()

GuzzleHttp\Psr7\Stream::isReadable ( )

Returns whether or not the stream is readable.

Returns
bool

Implements Psr\Http\Message\StreamInterface.

Definition at line 159 of file Stream.php.

160 {
161 return $this->readable;
162 }

References GuzzleHttp\Psr7\Stream\$readable.

◆ isSeekable()

GuzzleHttp\Psr7\Stream::isSeekable ( )

Returns whether or not the stream is seekable.

Returns
bool

Implements Psr\Http\Message\StreamInterface.

Definition at line 169 of file Stream.php.

170 {
171 return $this->seekable;
172 }

References GuzzleHttp\Psr7\Stream\$seekable.

◆ isWritable()

GuzzleHttp\Psr7\Stream::isWritable ( )

Returns whether or not the stream is writable.

Returns
bool

Implements Psr\Http\Message\StreamInterface.

Definition at line 164 of file Stream.php.

165 {
166 return $this->writable;
167 }

References GuzzleHttp\Psr7\Stream\$writable.

◆ read()

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

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 205 of file Stream.php.

206 {
207 if (!$this->readable) {
208 throw new \RuntimeException('Cannot read from non-readable stream');
209 }
210 if ($length < 0) {
211 throw new \RuntimeException('Length parameter cannot be negative');
212 }
213
214 if (0 === $length) {
215 return '';
216 }
217
218 $string = fread($this->stream, $length);
219 if (false === $string) {
220 throw new \RuntimeException('Unable to read from stream');
221 }
222
223 return $string;
224 }

◆ rewind()

GuzzleHttp\Psr7\Stream::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 190 of file Stream.php.

191 {
192 $this->seek(0);
193 }

References GuzzleHttp\Psr7\Stream\seek().

+ Here is the call graph for this function:

◆ seek()

GuzzleHttp\Psr7\Stream::seek (   $offset,
  $whence = 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 195 of file Stream.php.

196 {
197 if (!$this->seekable) {
198 throw new \RuntimeException('Stream is not seekable');
199 } elseif (fseek($this->stream, $offset, $whence) === -1) {
200 throw new \RuntimeException('Unable to seek to stream position '
201 . $offset . ' with whence ' . var_export($whence, true));
202 }
203 }

Referenced by GuzzleHttp\Psr7\Stream\__toString(), and GuzzleHttp\Psr7\Stream\rewind().

+ Here is the caller graph for this function:

◆ tell()

GuzzleHttp\Psr7\Stream::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 179 of file Stream.php.

180 {
181 $result = ftell($this->stream);
182
183 if ($result === false) {
184 throw new \RuntimeException('Unable to determine stream position');
185 }
186
187 return $result;
188 }

References $result.

◆ write()

GuzzleHttp\Psr7\Stream::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 226 of file Stream.php.

227 {
228 if (!$this->writable) {
229 throw new \RuntimeException('Cannot write to a non-writable stream');
230 }
231
232 // We can't know the size after writing anything
233 $this->size = null;
234 $result = fwrite($this->stream, $string);
235
236 if ($result === false) {
237 throw new \RuntimeException('Unable to write to stream');
238 }
239
240 return $result;
241 }

References $result.

Field Documentation

◆ $customMetadata

GuzzleHttp\Psr7\Stream::$customMetadata
private

Definition at line 19 of file Stream.php.

◆ $readable

GuzzleHttp\Psr7\Stream::$readable
private

Definition at line 16 of file Stream.php.

Referenced by GuzzleHttp\Psr7\Stream\isReadable().

◆ $readWriteHash

GuzzleHttp\Psr7\Stream::$readWriteHash
staticprivate
Initial value:
= [
'read' => [
'r' => true, 'w+' => true, 'r+' => true, 'x+' => true, 'c+' => true,
'rb' => true, 'w+b' => true, 'r+b' => true, 'x+b' => true,
'c+b' => true, 'rt' => true, 'w+t' => true, 'r+t' => true,
'x+t' => true, 'c+t' => true, 'a+' => true
],
'write' => [
'w' => true, 'w+' => true, 'rw' => true, 'r+' => true, 'x+' => true,
'c+' => true, 'wb' => true, 'w+b' => true, 'r+b' => true,
'x+b' => true, 'c+b' => true, 'w+t' => true, 'r+t' => true,
'x+t' => true, 'c+t' => true, 'a' => true, 'a+' => true
]
]

Definition at line 22 of file Stream.php.

◆ $seekable

GuzzleHttp\Psr7\Stream::$seekable
private

Definition at line 15 of file Stream.php.

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

◆ $size

GuzzleHttp\Psr7\Stream::$size
private

Definition at line 14 of file Stream.php.

Referenced by GuzzleHttp\Psr7\Stream\getSize().

◆ $stream

GuzzleHttp\Psr7\Stream::$stream
private

Definition at line 13 of file Stream.php.

Referenced by GuzzleHttp\Psr7\Stream\__construct(), and GuzzleHttp\Psr7\Stream\detach().

◆ $uri

GuzzleHttp\Psr7\Stream::$uri
private

Definition at line 18 of file Stream.php.

◆ $writable

GuzzleHttp\Psr7\Stream::$writable
private

Definition at line 17 of file Stream.php.

Referenced by GuzzleHttp\Psr7\Stream\isWritable().


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