Class yii\mongodb\file\StreamWrapper

Inheritanceyii\mongodb\file\StreamWrapper » yii\base\BaseObject
Available since extension's version2.1
Source Code https://github.com/yiisoft/yii2-mongodb/blob/master/src/file/StreamWrapper.php

StreamWrapper provides stream wrapper for MongoDB GridFS, allowing file operations via regular PHP stream resources.

Before feature can be used this wrapper should be registered via register() method. It is usually performed via yii\mongodb\Connection::registerFileStreamWrapper().

Note: do not use this class directly - its instance will be created and maintained by PHP internally once corresponding stream resource is created.

Resource path should be specified in following format:

'protocol://databaseName.fileCollectionPrefix?file_attribute=value'

Write example:

$resource = fopen('gridfs://mydatabase.fs?filename=new_file.txt', 'w');
fwrite($resource, 'some content');
// ...
fclose($resource);

Read example:

$resource = fopen('gridfs://mydatabase.fs?filename=my_file.txt', 'r');
$fileContent = stream_get_contents($resource);

See also http://php.net/manual/en/function.stream-wrapper-register.php.

Public Properties

Hide inherited properties

Property Type Description Defined By
$context resource Associated stream resource context. yii\mongodb\file\StreamWrapper
$contextOptions array Context options. yii\mongodb\file\StreamWrapper

Public Methods

Hide inherited methods

Method Description Defined By
getContextOptions() Returns options associated with $context. yii\mongodb\file\StreamWrapper
register() Registers this steam wrapper. yii\mongodb\file\StreamWrapper
stream_close() Closes a resource. yii\mongodb\file\StreamWrapper
stream_eof() Tests for end-of-file on a file pointer. yii\mongodb\file\StreamWrapper
stream_flush() This method is called in response to fflush() and when the stream is being closed while any unflushed data has been written to it before. yii\mongodb\file\StreamWrapper
stream_open() Opens file. yii\mongodb\file\StreamWrapper
stream_read() Reads from stream. yii\mongodb\file\StreamWrapper
stream_seek() Seeks to specific location in a stream. yii\mongodb\file\StreamWrapper
stream_stat() Retrieve information about a file resource. yii\mongodb\file\StreamWrapper
stream_tell() Retrieve the current position of a stream. yii\mongodb\file\StreamWrapper
stream_write() Writes to stream. yii\mongodb\file\StreamWrapper

Property Details

Hide inherited properties

$context public property

Associated stream resource context. This property is set automatically by PHP once wrapper is instantiated.

public resource $context null
$contextOptions public property

Context options.

public array $contextOptions null

Method Details

Hide inherited methods

getContextOptions() public method

Returns options associated with $context.

public array getContextOptions ( )
return array

Context options.

                public function getContextOptions()
{
    if ($this->_contextOptions === null) {
        $this->_contextOptions = stream_context_get_options($this->context);
    }
    return $this->_contextOptions;
}

            
register() public static method

Registers this steam wrapper.

public static void register ( $protocol 'gridfs', $force false )
$protocol string

Name of the protocol to be used.

$force boolean

Whether to register wrapper, even if protocol is already taken.

                public static function register($protocol = 'gridfs', $force = false)
{
    if (in_array($protocol, stream_get_wrappers())) {
        if (!$force) {
            return;
        }
        stream_wrapper_unregister($protocol);
    }
    stream_wrapper_register($protocol, get_called_class(), STREAM_IS_URL);
}

            
stream_close() public method

Closes a resource.

This method is called in response to fclose().

See also \yii\mongodb\file\fclose().

public void stream_close ( )

                public function stream_close()
{
    if ($this->_upload !== null) {
        $this->_upload->complete();
        $this->_upload = null;
    }
    if ($this->_download !== null) {
        $this->_download = null;
    }
}

            
stream_eof() public method

Tests for end-of-file on a file pointer.

This method is called in response to feof().

See also \yii\mongodb\file\feof().

public boolean stream_eof ( )
return boolean

true if the read/write position is at the end of the stream and if no more data is available to be read, or false otherwise.

                public function stream_eof()
{
    return $this->_download !== null
        ? ($this->_pointerOffset >= $this->_download->getSize())
        : true;
}

            
stream_flush() public method

This method is called in response to fflush() and when the stream is being closed while any unflushed data has been written to it before.

See also \yii\mongodb\file\fflush().

public boolean stream_flush ( )
return boolean

Whether cached data was successfully stored.

                public function stream_flush()
{
    return true;
}

            
stream_open() public method

Opens file.

This method is called immediately after the wrapper is initialized (f.e. by fopen() and file_get_contents()).

See also \yii\mongodb\file\fopen().

public boolean stream_open ( $path, $mode, $options, &$openedPath )
$path string

Specifies the URL that was passed to the original function.

$mode string

Mode used to open the file, as detailed for fopen().

$options integer

Additional flags set by the streams API.

$openedPath string

Real opened path.

return boolean

Whether operation is successful.

                public function stream_open($path, $mode, $options, &$openedPath)
{
    if ($options & STREAM_USE_PATH) {
        $openedPath = $path;
    }
    $this->parsePath($path);
    switch ($mode) {
        case 'r':
            return $this->prepareDownload();
        case 'w':
            return $this->prepareUpload();
    }
    return false;
}

            
stream_read() public method

Reads from stream.

This method is called in response to fread() and fgets().

See also \yii\mongodb\file\fread().

public string|false stream_read ( $count )
$count integer

Count of bytes of data from the current position should be returned.

return string|false

If there are less than count bytes available, return as many as are available. If no more data is available, return false.

                public function stream_read($count)
{
    if ($this->_download === null) {
        return false;
    }
    $result = $this->_download->substr($this->_pointerOffset, $count);
    $this->_pointerOffset += $count;
    return $result;
}

            
stream_seek() public method

Seeks to specific location in a stream.

This method is called in response to fseek().

See also \yii\mongodb\file\fseek().

public boolean stream_seek ( $offset, $whence SEEK_SET )
$offset integer

The stream offset to seek to.

$whence integer

Possible values:

  • SEEK_SET - Set position equal to offset bytes.
  • SEEK_CUR - Set position to current location plus offset.
  • SEEK_END - Set position to end-of-file plus offset.
return boolean

Return true if the position was updated, false otherwise.

                public function stream_seek($offset, $whence = SEEK_SET)
{
    switch ($whence) {
        case SEEK_SET:
            if ($offset < $this->_download->getSize() && $offset >= 0) {
                $this->_pointerOffset = $offset;
                return true;
            }
            return false;
        case SEEK_CUR:
            if ($offset >= 0) {
                $this->_pointerOffset += $offset;
                return true;
            }
            return false;
        case SEEK_END:
            if ($this->_download->getSize() + $offset >= 0) {
                $this->_pointerOffset = $this->_download->getSize() + $offset;
                return true;
            }
            return false;
    }
    return false;
}

            
stream_stat() public method

Retrieve information about a file resource.

This method is called in response to stat().

See also \yii\mongodb\file\stat().

public array stream_stat ( )
return array

File statistic information.

                public function stream_stat()
{
    $statistics = $this->fileStatisticsTemplate();
    if ($this->_download !== null) {
        $statistics[7] = $statistics['size'] = $this->_download->getSize();
    }
    if ($this->_upload !== null) {
        $statistics[7] = $statistics['size'] = $this->_pointerOffset;
    }
    return $statistics;
}

            
stream_tell() public method

Retrieve the current position of a stream.

This method is called in response to fseek() to determine the current position.

See also \yii\mongodb\file\fseek().

public integer stream_tell ( )
return integer

Should return the current position of the stream.

                public function stream_tell()
{
    return $this->_pointerOffset;
}

            
stream_write() public method

Writes to stream.

This method is called in response to fwrite().

See also \yii\mongodb\file\fwrite().

public integer stream_write ( $data )
$data string

String to be stored into the underlying stream.

return integer

The number of bytes that were successfully stored.

                public function stream_write($data)
{
    if ($this->_upload === null) {
        return false;
    }
    $this->_upload->addContent($data);
    $result = StringHelper::byteLength($data);
    $this->_pointerOffset += $result;
    return $result;
}