Class yii\mongodb\file\StreamWrapper
Inheritance | yii\mongodb\file\StreamWrapper » yii\base\BaseObject |
---|---|
Available since extension's version | 2.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
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
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
Associated stream resource context. This property is set automatically by PHP once wrapper is instantiated.
Method Details
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;
}
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);
}
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;
}
}
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 |
|
---|
public function stream_eof()
{
return $this->_download !== null
? ($this->_pointerOffset >= $this->_download->getSize())
: true;
}
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;
}
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 |
$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;
}
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 |
---|
public function stream_read($count)
{
if ($this->_download === null) {
return false;
}
$result = $this->_download->substr($this->_pointerOffset, $count);
$this->_pointerOffset += $count;
return $result;
}
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:
|
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;
}
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;
}
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;
}
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;
}