文字

The streamWrapper class

(PHP 4 >= 4.3.2, PHP 5)

简介

Allows you to implement your own protocol handlers and streams for use with all the other filesystem functions (such as fopen() , fread() etc.).

Note:

This is NOT a real class, only a prototype of how a class defining its own protocol should be.

Note:

Implementing the methods in other ways then described here can lead to undefined behaviour.

An instance of this class is initialized as soon as a stream function tries to access the protocol it is associated with.

类摘要

streamWrapper {
public resource $context ;
__construct ( void )
__destruct ( void )
public bool dir_closedir ( void )
public bool dir_opendir ( string $path , int $options )
public string dir_readdir ( void )
public bool dir_rewinddir ( void )
public bool mkdir ( string $path , int $mode , int $options )
public bool rename ( string $path_from , string $path_to )
public bool rmdir ( string $path , int $options )
public resource stream_cast ( int $cast_as )
public void stream_close ( void )
public bool stream_eof ( void )
public bool stream_flush ( void )
public bool stream_lock ( int $operation )
public bool stream_metadata ( string $path , int $option , mixed $value )
public bool stream_open ( string $path , string $mode , int $options , string &$opened_path )
public string stream_read ( int $count )
public bool stream_seek ( int $offset , int $whence = SEEK_SET )
public bool stream_set_option ( int $option , int $arg1 , int $arg2 )
public array stream_stat ( void )
public int stream_tell ( void )
public bool stream_truncate ( int $new_size )
public int stream_write ( string $data )
public bool unlink ( string $path )
public array url_stat ( string $path , int $flags )
}

属性

resource context

The current context, or NULL if no context was passed to the caller function.

Use the stream_context_get_options() to parse the context.

Note:

This property must be public so PHP can populate it with the actual context resource.

更新日志

版本 说明
5.0.0 Added the context property.

参见

  • Example class registered as stream wrapper
  • stream_wrapper_register()
  • stream_wrapper_unregister()
  • stream_wrapper_restore()

Table of Contents

  • streamWrapper::__construct — Constructs a new stream wrapper
  • streamWrapper::__destruct — Destructs an existing stream wrapper
  • streamWrapper::dir_closedir — Close directory handle
  • streamWrapper::dir_opendir — Open directory handle
  • streamWrapper::dir_readdir — Read entry from directory handle
  • streamWrapper::dir_rewinddir — Rewind directory handle
  • streamWrapper::mkdir — Create a directory
  • streamWrapper::rename — Renames a file or directory
  • streamWrapper::rmdir — Removes a directory
  • streamWrapper::stream_cast — Retrieve the underlaying resource
  • streamWrapper::stream_close — Close a resource
  • streamWrapper::stream_eof — Tests for end-of-file on a file pointer
  • streamWrapper::stream_flush — Flushes the output
  • streamWrapper::stream_lock — Advisory file locking
  • streamWrapper::stream_metadata — Change stream options
  • streamWrapper::stream_open — Opens file or URL
  • streamWrapper::stream_read — Read from stream
  • streamWrapper::stream_seek — Seeks to specific location in a stream
  • streamWrapper::stream_set_option — Change stream options
  • streamWrapper::stream_stat — Retrieve information about a file resource
  • streamWrapper::stream_tell — Retrieve the current position of a stream
  • streamWrapper::stream_truncate — Truncate stream
  • streamWrapper::stream_write — Write to stream
  • streamWrapper::unlink — Delete a file
  • streamWrapper::url_stat — Retrieve information about a file

用户评论:

[#1] Anonymous [2011-09-21 12:02:36]

Here is a very simple stream wrapper which calls your callback function for reads:

<?php

class CallbackUrl
{
    const 
WRAPPER_NAME 'callback';

    public 
$context;
    private 
$_cb;
    private 
$_eof false;

    private static 
$_isRegistered false;

    public static function 
getContext($cb)
    {
        if (!
self::$_isRegistered) {
            
stream_wrapper_register(self::WRAPPER_NAMEget_class());
            
self::$_isRegistered true;
        }
        if (!
is_callable($cb)) return false;
        return 
stream_context_create(array(self::WRAPPER_NAME => array('cb' => $cb)));
    }

    public function 
stream_open($path$mode$options, &$opened_path)
    {
        if (!
preg_match('/^r[bt]?$/'$mode) || !$this->context) return false;
        
$opt stream_context_get_options($this->context);
        if (!
is_array($opt[self::WRAPPER_NAME]) ||
            !isset(
$opt[self::WRAPPER_NAME]['cb']) ||
            !
is_callable($opt[self::WRAPPER_NAME]['cb'])) return false;
        
$this->_cb $opt[self::WRAPPER_NAME]['cb'];
        return 
true;
    }

    public function 
stream_read($count)
    {
        if (
$this->_eof || !$count) return '';
        if ((
$s call_user_func($this->_cb$count)) == ''$this->_eof true;
        return 
$s;
    }

    public function 
stream_eof()
    {
        return 
$this->_eof;
    }
}

class 
Test {
    private 
$_s;
    public function 
__construct($s)
    {
        
$this->_s $s;
    }
    public function 
read($count) {
        return 
fread($this->_s$count);
    }
}

$t = new Test(fopen('/etc/services''r'));
$fd fopen('callback://''r'falseCallbackUrl::getContext(array($t'read')));
while((
$buf fread($fd128)) != '') {
    print 
$buf;
}
?>

[#2] isaac dot z dot no dot foster at spam dot gmail dot please dot com [2010-10-22 09:12:57]

It's worth noting that the interface defined by yannick at gmail should not always be implemented by a stream wrapper class, as several of the methods should not be implemented if the class has no use for them (as per the manual). 

Specifically, mkdir, rename, rmdir, and unlink are methods that "should not be defined" if the wrapper has no use for them. The consequence is that the appropriate error message will not be returned. 

If the interface is implemented, you won't have the flexibility to not implement those methods.

Not trying to be academic, but it was useful for me.

[#3] yannick dot battail at gmail dot com [2009-07-17 02:38:09]

a php interface for wrapper

<?php
interface WrapperInterface
{
    

    //public $context;

    
    
public function __construct();

    

    
public function dir_closedir();

    

    
public function dir_opendir($path $options);

    

    
public function dir_readdir();

    

    
public function dir_rewinddir();

    

    
public function mkdir($path $mode $options);

    

    
public function rename($path_from $path_to);

    

    
public function rmdir($path $options);

    

    
public function stream_cast($cast_as);

    

    
public function stream_close();

    

    
public function stream_eof();

    

    
public function stream_flush();

    

    
public function stream_lock($operation);

    

    
public function stream_open($path $mode $options , &$opened_path);

    

    
public function stream_read($count);

    

    
public function stream_seek($offset $whence SEEK_SET);

    

    
public function stream_set_option($option $arg1 $arg2);

    

    
public function stream_stat();

    

    
public function stream_tell();

    

    
public function stream_write($data);

    

    
public function unlink($path);

    

    
public function url_stat($path $flags);
}

?>

上一篇: 下一篇: