HTTP Response (PSR-7)


    Phalcon\Http\Message\Stream is an implementation of the HTTP messaging interface as defined by PHP-FIG.

    This class describes a data stream. Typically, an instance will wrap a PHP stream; this interface provides a wrapper around the most common operations, including serialization of the entire stream to a string.

    Constructor

    1. mixed $stream,
    2. string $mode = "rb"
    3. )

    The first parameter can be a string representing the location of the file on the file system or storage area. It can also be a resource, as returned by a method such as fopen. The second parameter is the open mode for the stream. The default mode is rb. For a list of available modes, you can check the documentation for .

    • stream - string or resource
    • mode - A string representing the mode the file is to be opened.If there is an error, a RuntimeException will be thrown.

    Getters

    Reads all data from the stream into a string, from the beginning to end. The method will first try to seek() to the beginning of the stream before reading the data and read the stream until the end is reached.

    1. <?php
    2. use Phalcon\Http\Message\Stream;
    3. $fileName = dataDir('assets/stream/mit.txt');
    4. $stream = new Stream($fileName, 'rb');
    5. echo (string) $stream; // 'The MIT License (MIT) ...'

    getContents()

    1. <?php
    2. use Phalcon\Http\Message\Stream;
    3. $fileName = dataDir('assets/stream/mit.txt');
    4. $stream = new Stream($fileName, 'rb');
    5. echo $stream->getContents(); // 'The MIT License (MIT) ...'

    Returns the stream metadata as an associative array. If the parameter $key is defined, the relevant string element will be returned. The method is a wrapper for PHP’s stream_get_meta_data() function. If the key is not found, the method will return null.

    1. <?php
    2. use Phalcon\Http\Message\Stream;
    3. $fileName = dataDir('assets/stream/mit.txt');
    4. $stream = new Stream($fileName, 'rb');
    5. var_dump(
    6. $stream->getMetadata()
    7. );
    8. // [
    9. // 'timed_out' => false,
    10. // 'blocked' => true,
    11. // 'eof' => false,
    12. // 'wrapper_type' => 'plainfile',
    13. // 'stream_type' => 'STDIO',
    14. // 'mode' => 'rb',
    15. // 'unread_bytes' => 0,
    16. // 'seekable' => true,
    17. // 'uri' => $fileName,
    18. // ];
    19. echo $stream->getMetadata('wrapper_type'); // 'plainfile'
    20. echo $stream->getMetadata('unknown'); // null

    getSize()

    Returns the size of the stream. If it is not known, null will be returned

    Is

    Returns if the stream is seekable, false otherwise.

    1. <?php
    2. use Phalcon\Http\Message\Stream;
    3. $fileName = dataDir('assets/stream/mit.txt');
    4. $stream = new Stream($fileName, 'rb');
    5. echo $stream->isSeekable(); // 'true'

    isReadable()

    Returns true if the stream is readable, false otherwise.

    1. <?php
    2. use Phalcon\Http\Message\Stream;
    3. $fileName = dataDir('assets/stream/mit.txt');
    4. $stream = new Stream($fileName, 'w');
    5. echo $stream->isReadable(); // 'false'

    Returns true if the stream is writable, false otherwise.

    1. <?php
    2. use Phalcon\Http\Message\Stream;
    3. $fileName = dataDir('assets/stream/mit.txt');
    4. $stream = new Stream($fileName, 'rb');
    5. echo $stream->isWritable(); // 'false'

    Closes the stream and any underlying resources.

    1. <?php
    2. use Phalcon\Http\Message\Stream;
    3. $fileName = dataDir('assets/stream/mit.txt');
    4. $stream = new Stream($fileName, 'rb');
    5. $stream->close();

    detach()

    eof()

    Returns true if the stream is at the end of the stream, false otherwise.

    1. <?php
    2. use Phalcon\Http\Message\Stream;
    3. $stream = new Stream($fileName, 'rb');
    4. echo $stream->eof(); // false
    5. $stream->seek(9999);

    read()

    Read data from the stream. The method accepts an integer specifying the number of bytes from the object and return them. The method could return less number of bytes than specified if the end of the stream is defined. If no more data is available an empty string will be returned. If an error occurs, a RuntimeException will be thrown.

    1. <?php
    2. use Phalcon\Http\Message\Stream;
    3. $fileName = dataDir('assets/stream/mit.txt');
    4. $stream = new Stream($fileName, 'rb');
    5. echo $stream->read(15); // 'The MIT License'

    Seek to the beginning of the stream. Uses calling seek(0) internally. If the stream is not seekable, a RuntimeException will be thrown.

    1. <?php
    2. use Phalcon\Http\Message\Stream;
    3. $fileName = dataDir('assets/stream/mit.txt');
    4. $stream = new Stream($fileName, 'rb');
    5. $stream->seek(8);
    6. echo $stream->read(7); // 'License'
    7. $stream->rewind();
    8. echo $stream->read(3); // 'The'

    seek()

    Seek to a position in the stream. Uses internally. It accepts:

    • offset - int The stream offset
    • whence - int 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.If an error occurs, a RuntimeException will be thrown.
    1. <?php
    2. use Phalcon\Http\Message\Stream;
    3. $fileName = dataDir('assets/stream/mit.txt');
    4. $stream = new Stream($fileName, 'rb');
    5. $stream->seek(8);
    6. echo $stream->read(7); // 'License'
    7. $stream->rewind();
    8. echo $stream->read(3); // 'The'

    tell()

    Returns the current position of the file read/write pointer as an integer. If an error occurs, a RuntimeException will be thrown.

    write()

    Write data to the stream. It accepts a string parameter as the contents to be written. The method returns the number of bytes written to the stream as an integer. If an error occurs, a RuntimeException will be thrown.

    1. <?php
    2. use Phalcon\Http\Message\Stream;
    3. $stream = new Stream('php://memory', 'wb');
    4. $source = 'The above copyright notice and this permission '
    5. . 'notice shall be included in all copies or '
    6. . 'substantial portions of the Software.';