ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Slim\Http\Stream Class Reference

Represents a data stream as defined in PSR-7. More...

+ Inheritance diagram for Slim\Http\Stream:
+ Collaboration diagram for Slim\Http\Stream:

Public Member Functions

 __construct ($stream)
 Create a new Stream. More...
 
 getMetadata ($key=null)
 Get stream metadata as an associative array or retrieve a specific key. More...
 
 detach ()
 Separates any underlying resources from the stream. 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...
 
 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...
 
 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...
 
 seek ($offset, $whence=SEEK_SET)
 Seek to a position in the stream. More...
 
 rewind ()
 Seek to the beginning of the stream. More...
 
 read ($length)
 Read data from the stream. More...
 
 write ($string)
 Write data to the stream. More...
 
 getContents ()
 Returns the remaining contents in a string. More...
 
 isPipe ()
 Returns whether or not the stream is a pipe. 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...
 

Data Fields

const FSTAT_MODE_S_IFIFO = 0010000
 Bit mask to determine if the stream is a pipe. More...
 

Protected Member Functions

 isAttached ()
 Is a resource attached to this stream? More...
 
 attach ($newStream)
 Attach new resource to this object. More...
 

Protected Attributes

 $stream
 
 $meta
 
 $readable
 
 $writable
 
 $seekable
 
 $size
 
 $isPipe
 

Static Protected Attributes

static $modes
 

Detailed Description

Represents a data stream as defined in PSR-7.

https://github.com/php-fig/http-message/blob/master/src/StreamInterface.php

Definition at line 20 of file Stream.php.

Constructor & Destructor Documentation

◆ __construct()

Slim\Http\Stream::__construct (   $stream)

Create a new Stream.

Parameters
resource$streamA PHP resource handle.
Exceptions
InvalidArgumentExceptionIf argument is not a resource.

Definition at line 96 of file Stream.php.

97 {
98 $this->attach($stream);
99 }
attach($newStream)
Attach new resource to this object.
Definition: Stream.php:146

References Slim\Http\Stream\$stream, and Slim\Http\Stream\attach().

+ Here is the call graph for this function:

Member Function Documentation

◆ __toString()

Slim\Http\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 194 of file Stream.php.

195 {
196 if (!$this->isAttached()) {
197 return '';
198 }
199
200 try {
201 $this->rewind();
202 return $this->getContents();
203 } catch (RuntimeException $e) {
204 return '';
205 }
206 }
isAttached()
Is a resource attached to this stream?
Definition: Stream.php:132
rewind()
Seek to the beginning of the stream.
Definition: Stream.php:367
getContents()
Returns the remaining contents in a string.
Definition: Stream.php:424

References Slim\Http\Stream\getContents(), Slim\Http\Stream\isAttached(), and Slim\Http\Stream\rewind().

+ Here is the call graph for this function:

◆ attach()

Slim\Http\Stream::attach (   $newStream)
protected

Attach new resource to this object.

Note: This method is not part of the PSR-7 standard.

Parameters
resource$newStreamA PHP resource handle.
Exceptions
InvalidArgumentExceptionIf argument is not a valid PHP resource.

Definition at line 146 of file Stream.php.

147 {
148 if (is_resource($newStream) === false) {
149 throw new InvalidArgumentException(__METHOD__ . ' argument must be a valid PHP resource');
150 }
151
152 if ($this->isAttached() === true) {
153 $this->detach();
154 }
155
156 $this->stream = $newStream;
157 }
detach()
Separates any underlying resources from the stream.
Definition: Stream.php:166

References Slim\Http\Stream\detach(), and Slim\Http\Stream\isAttached().

Referenced by Slim\Http\Stream\__construct().

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

◆ close()

Slim\Http\Stream::close ( )

Closes the stream and any underlying resources.

Implements Psr\Http\Message\StreamInterface.

Definition at line 211 of file Stream.php.

212 {
213 if ($this->isAttached() === true) {
214 if ($this->isPipe()) {
215 pclose($this->stream);
216 } else {
217 fclose($this->stream);
218 }
219 }
220
221 $this->detach();
222 }
isPipe()
Returns whether or not the stream is a pipe.
Definition: Stream.php:438

References Slim\Http\Stream\detach(), Slim\Http\Stream\isAttached(), and Slim\Http\Stream\isPipe().

+ Here is the call graph for this function:

◆ detach()

Slim\Http\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 166 of file Stream.php.

167 {
168 $oldResource = $this->stream;
169 $this->stream = null;
170 $this->meta = null;
171 $this->readable = null;
172 $this->writable = null;
173 $this->seekable = null;
174 $this->size = null;
175 $this->isPipe = null;
176
177 return $oldResource;
178 }
font size
Definition: langcheck.php:162

References Slim\Http\Stream\$stream, Slim\Http\Stream\isPipe(), and size.

Referenced by Slim\Http\Stream\attach(), and Slim\Http\Stream\close().

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

◆ eof()

Slim\Http\Stream::eof ( )

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

Returns
bool

Implements Psr\Http\Message\StreamInterface.

Definition at line 260 of file Stream.php.

261 {
262 return $this->isAttached() ? feof($this->stream) : true;
263 }

References Slim\Http\Stream\isAttached().

+ Here is the call graph for this function:

◆ getContents()

Slim\Http\Stream::getContents ( )

Returns the remaining contents in a string.

Returns
string
Exceptions
RuntimeExceptionif unable to read or an error occurs while reading.

Implements Psr\Http\Message\StreamInterface.

Definition at line 424 of file Stream.php.

425 {
426 if (!$this->isReadable() || ($contents = stream_get_contents($this->stream)) === false) {
427 throw new RuntimeException('Could not get contents of stream');
428 }
429
430 return $contents;
431 }
isReadable()
Returns whether or not the stream is readable.
Definition: Stream.php:270

References Slim\Http\Stream\isReadable().

Referenced by Slim\Http\Stream\__toString().

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

◆ getMetadata()

Slim\Http\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 115 of file Stream.php.

116 {
117 $this->meta = stream_get_meta_data($this->stream);
118 if (is_null($key) === true) {
119 return $this->meta;
120 }
121
122 return isset($this->meta[$key]) ? $this->meta[$key] : null;
123 }
$key
Definition: croninfo.php:18

References $key, and Slim\Http\Stream\$meta.

Referenced by Slim\Http\Stream\isReadable(), Slim\Http\Stream\isSeekable(), and Slim\Http\Stream\isWritable().

+ Here is the caller graph for this function:

◆ getSize()

Slim\Http\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 229 of file Stream.php.

230 {
231 if (!$this->size && $this->isAttached() === true) {
232 $stats = fstat($this->stream);
233 $this->size = isset($stats['size']) && !$this->isPipe() ? $stats['size'] : null;
234 }
235
236 return $this->size;
237 }

References Slim\Http\Stream\$size, Sabre\VObject\$stats, Slim\Http\Stream\isAttached(), Slim\Http\Stream\isPipe(), and size.

+ Here is the call graph for this function:

◆ isAttached()

Slim\Http\Stream::isAttached ( )
protected

Is a resource attached to this stream?

Note: This method is not part of the PSR-7 standard.

Returns
bool

Definition at line 132 of file Stream.php.

133 {
134 return is_resource($this->stream);
135 }

Referenced by Slim\Http\Stream\__toString(), Slim\Http\Stream\attach(), Slim\Http\Stream\close(), Slim\Http\Stream\eof(), Slim\Http\Stream\getSize(), Slim\Http\Stream\isPipe(), Slim\Http\Stream\isReadable(), Slim\Http\Stream\isSeekable(), Slim\Http\Stream\isWritable(), and Slim\Http\Stream\tell().

+ Here is the caller graph for this function:

◆ isPipe()

Slim\Http\Stream::isPipe ( )

Returns whether or not the stream is a pipe.

Returns
bool

Definition at line 438 of file Stream.php.

439 {
440 if ($this->isPipe === null) {
441 $this->isPipe = false;
442 if ($this->isAttached()) {
443 $mode = fstat($this->stream)['mode'];
444 $this->isPipe = ($mode & self::FSTAT_MODE_S_IFIFO) !== 0;
445 }
446 }
447
448 return $this->isPipe;
449 }
const FSTAT_MODE_S_IFIFO
Bit mask to determine if the stream is a pipe.
Definition: Stream.php:27

References Slim\Http\Stream\$isPipe, Slim\Http\Stream\FSTAT_MODE_S_IFIFO, Slim\Http\Stream\isAttached(), and Slim\Http\Stream\isPipe().

Referenced by Slim\Http\Stream\close(), Slim\Http\Stream\detach(), Slim\Http\Stream\getSize(), Slim\Http\Stream\isPipe(), Slim\Http\Stream\isReadable(), Slim\Http\Stream\isSeekable(), and Slim\Http\Stream\tell().

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

◆ isReadable()

Slim\Http\Stream::isReadable ( )

Returns whether or not the stream is readable.

Returns
bool

Implements Psr\Http\Message\StreamInterface.

Definition at line 270 of file Stream.php.

271 {
272 if ($this->readable === null) {
273 if ($this->isPipe()) {
274 $this->readable = true;
275 } else {
276 $this->readable = false;
277 if ($this->isAttached()) {
278 $meta = $this->getMetadata();
279 foreach (self::$modes['readable'] as $mode) {
280 if (strpos($meta['mode'], $mode) === 0) {
281 $this->readable = true;
282 break;
283 }
284 }
285 }
286 }
287 }
288
289 return $this->readable;
290 }
getMetadata($key=null)
Get stream metadata as an associative array or retrieve a specific key.
Definition: Stream.php:115

References Slim\Http\Stream\$meta, Slim\Http\Stream\$readable, Slim\Http\Stream\getMetadata(), Slim\Http\Stream\isAttached(), and Slim\Http\Stream\isPipe().

Referenced by Slim\Http\Stream\getContents(), and Slim\Http\Stream\read().

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

◆ isSeekable()

Slim\Http\Stream::isSeekable ( )

Returns whether or not the stream is seekable.

Returns
bool

Implements Psr\Http\Message\StreamInterface.

Definition at line 320 of file Stream.php.

321 {
322 if ($this->seekable === null) {
323 $this->seekable = false;
324 if ($this->isAttached()) {
325 $meta = $this->getMetadata();
326 $this->seekable = !$this->isPipe() && $meta['seekable'];
327 }
328 }
329
330 return $this->seekable;
331 }

References Slim\Http\Stream\$meta, Slim\Http\Stream\$seekable, Slim\Http\Stream\getMetadata(), Slim\Http\Stream\isAttached(), and Slim\Http\Stream\isPipe().

Referenced by Slim\Http\Stream\rewind(), and Slim\Http\Stream\seek().

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

◆ isWritable()

Slim\Http\Stream::isWritable ( )

Returns whether or not the stream is writable.

Returns
bool

Implements Psr\Http\Message\StreamInterface.

Definition at line 297 of file Stream.php.

298 {
299 if ($this->writable === null) {
300 $this->writable = false;
301 if ($this->isAttached()) {
302 $meta = $this->getMetadata();
303 foreach (self::$modes['writable'] as $mode) {
304 if (strpos($meta['mode'], $mode) === 0) {
305 $this->writable = true;
306 break;
307 }
308 }
309 }
310 }
311
312 return $this->writable;
313 }

References Slim\Http\Stream\$meta, Slim\Http\Stream\$writable, Slim\Http\Stream\getMetadata(), and Slim\Http\Stream\isAttached().

Referenced by Slim\Http\Stream\write().

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

◆ read()

Slim\Http\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
RuntimeExceptionif an error occurs.

Implements Psr\Http\Message\StreamInterface.

Definition at line 386 of file Stream.php.

387 {
388 if (!$this->isReadable() || ($data = fread($this->stream, $length)) === false) {
389 throw new RuntimeException('Could not read from stream');
390 }
391
392 return $data;
393 }
$data
Definition: bench.php:6

References $data, and Slim\Http\Stream\isReadable().

+ Here is the call graph for this function:

◆ rewind()

Slim\Http\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()

RuntimeException on failure.

Implements Psr\Http\Message\StreamInterface.

Definition at line 367 of file Stream.php.

368 {
369 if (!$this->isSeekable() || rewind($this->stream) === false) {
370 throw new RuntimeException('Could not rewind stream');
371 }
372 }
isSeekable()
Returns whether or not the stream is seekable.
Definition: Stream.php:320

References Slim\Http\Stream\isSeekable(), and Slim\Http\Stream\rewind().

Referenced by Slim\Http\RequestBody\__construct(), Slim\Http\Stream\__toString(), and Slim\Http\Stream\rewind().

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

◆ seek()

Slim\Http\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. RuntimeException on failure.

Implements Psr\Http\Message\StreamInterface.

Definition at line 347 of file Stream.php.

348 {
349 // Note that fseek returns 0 on success!
350 if (!$this->isSeekable() || fseek($this->stream, $offset, $whence) === -1) {
351 throw new RuntimeException('Could not seek in stream');
352 }
353 }

References Slim\Http\Stream\isSeekable().

+ Here is the call graph for this function:

◆ tell()

Slim\Http\Stream::tell ( )

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

Returns
int Position of the file pointer
Exceptions
RuntimeExceptionon error.

Implements Psr\Http\Message\StreamInterface.

Definition at line 246 of file Stream.php.

247 {
248 if (!$this->isAttached() || ($position = ftell($this->stream)) === false || $this->isPipe()) {
249 throw new RuntimeException('Could not get the position of the pointer in stream');
250 }
251
252 return $position;
253 }

References Slim\Http\Stream\isAttached(), and Slim\Http\Stream\isPipe().

+ Here is the call graph for this function:

◆ write()

Slim\Http\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
RuntimeExceptionon failure.

Implements Psr\Http\Message\StreamInterface.

Definition at line 404 of file Stream.php.

405 {
406 if (!$this->isWritable() || ($written = fwrite($this->stream, $string)) === false) {
407 throw new RuntimeException('Could not write to stream');
408 }
409
410 // reset size so that it will be recalculated on next call to getSize()
411 $this->size = null;
412
413 return $written;
414 }
isWritable()
Returns whether or not the stream is writable.
Definition: Stream.php:297

References Slim\Http\Stream\isWritable(), and size.

+ Here is the call graph for this function:

Field Documentation

◆ $isPipe

Slim\Http\Stream::$isPipe
protected

Definition at line 87 of file Stream.php.

Referenced by Slim\Http\Stream\isPipe().

◆ $meta

Slim\Http\Stream::$meta
protected

◆ $modes

Slim\Http\Stream::$modes
staticprotected
Initial value:
= [
'readable' => ['r', 'r+', 'w+', 'a+', 'x+', 'c+'],
'writable' => ['r+', 'w', 'w+', 'a', 'a+', 'x', 'x+', 'c', 'c+'],
]

Definition at line 35 of file Stream.php.

◆ $readable

Slim\Http\Stream::$readable
protected

Definition at line 59 of file Stream.php.

Referenced by Slim\Http\Stream\isReadable().

◆ $seekable

Slim\Http\Stream::$seekable
protected

Definition at line 73 of file Stream.php.

Referenced by Slim\Http\Stream\isSeekable().

◆ $size

Slim\Http\Stream::$size
protected

Definition at line 80 of file Stream.php.

Referenced by Slim\Http\Stream\getSize().

◆ $stream

Slim\Http\Stream::$stream
protected

◆ $writable

Slim\Http\Stream::$writable
protected

Definition at line 66 of file Stream.php.

Referenced by Slim\Http\Stream\isWritable().

◆ FSTAT_MODE_S_IFIFO

const Slim\Http\Stream::FSTAT_MODE_S_IFIFO = 0010000

Bit mask to determine if the stream is a pipe.

This is octal as per header stat.h

Definition at line 27 of file Stream.php.

Referenced by Slim\Http\Stream\isPipe().


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