Today I will tell you about the concepts and usage of PHP streams and wrappers. Let me summarize it for everyone.
We know that the concept of streams was introduced in php4.3. It is an abstraction of streaming data and is used to unify data operations, such as file data, network data, compressed data, etc., so that the same data can be shared. A set of functions, PHP's file system functions are such sharing. For example, the file_get_contents() function can open local files and access URLs. This is reflected in this. To put it simply, a stream is a resource object that exhibits streaming data behavior.
Read and write in a linear manner, and can search anywhere in the stream.
The stream is somewhat similar to the database abstraction layer. In terms of the database abstraction layer, no matter what kind of database is used, the data is operated in the same way above the abstraction layer. The stream is an abstraction of the data. It does not matter whether it is local. Whether it is a file, a remote file, a compressed file, etc., as long as it is streaming data, the operation method is the same.
With the concept of stream, the concept of wrapper is derived. Each stream corresponds to a wrapper. The stream is a concept generated from the perspective of unified operation, and the wrapper is derived from the understanding of A concept arising from the content of streaming data, that is, how this unified operation method operates or configures different content;
These contents are all presented in the form of streams, but the content rules are different, such as http The data transmitted by the protocol is in the form of a stream, but only the http wrapper understands the meaning of the data transmitted by the http protocol. It can be understood that a stream is a pipe with flowing water, but what it flows out is data, and the wrapper is the package. An interpreter on the outside of the stream pipe that understands the meaning of the data flowing out and can manipulate it.
The official manual says: "A wrapper is additional code that tells the stream how to handle a special protocol or encoding." Do you understand what this sentence means?
Wrapper can be nested. After a flow is wrapped with a wrapper, it can also be wrapped with the wrapper in the outer layer. At this time, the inner wrapper plays the role of the flow relative to the outer wrapper
The C language development document implemented at the bottom of PHP itself has this explanation:
The stream API operates a pair of different levels: at the basic level, the API defines the php_stream object to represent the streaming data source, and at a slightly higher level At the level, the api defines the php_stream_wrapper object.
It wraps the lower-level php_stream object to provide the ability to retrieve the content and metadata of the URL, add context parameters, and adjust the wrapper behavior;
Each stream is opened You can apply any number of filters on it, and the stream data will be processed by the filter. I think the word filter is a bit inaccurate and misleading.
The literal meaning seems to be to remove some data. It should be called a data adjuster, because it can remove some data, add it, and modify it. However, due to historical reasons, it is also called a data adjuster. It’s a filter, and everyone just needs to understand it.
We often see the following words to explain their differences:
Resources and data: Resources are a more macro term, usually including data, while data is a more concrete term. When developing programs, it is often said that it is data, while in software planning, it is said that it is resources. They are synonyms, just like the difference between software design and program development.
Context and parameters: Context is a relatively macro term, often used in communication. Specifically, it is the parameters of a communication itself, and the term parameter is often used in more specific things, such as functions
The conceptual stuff is explained above, let’s take a look at the specific content:
Please see here for the protocols and wrappers supported by php: http://php.net/manual/zh/wrappers .php:
(Author's note: The original title is: Supported protocols and packaging protocols. The Chinese translation is a bit misleading. To be precise, it is the supported protocols and wrappers. It is very clear from the English page)
Default support There are some protocols and wrappers, please use the stream_get_wrappers() function to view. You can also customize a wrapper and register it with stream_wrapper_register()
Although RFC 3986 can use: as a delimiter, PHP only allows ://, So please use the format of "scheme://target" for the URL
file:// — 访问本地文件系统,在用文件系统函数时默认就使用该包装器 http:// — 访问 HTTP(s) 网址 ftp:// — 访问 FTP(s) URLs php:// — 访问各个输入/输出流(I/O streams) zlib:// — 压缩流 data:// — 数据(RFC 2397) glob:// — 查找匹配的文件路径模式 phar:// — PHP 归档 ssh2:// — Secure Shell 2 rar:// — RAR ogg:// — 音频流 expect:// — 处理交互式的流
How to implement a custom wrapper:
Using fopen, fwrite, fread, fgets, feof, rewind, When file system functions such as file_put_contents, file_get_contents, etc. operate on streams, the data is first passed to the defined wrapper class object, and then the wrapper operates on the stream.
How to implement a custom stream wrapper? PHP provides a class prototype, which is just a prototype, not an interface or a class, and cannot be used for inheritance:
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 ) }
在这个原型里面定义的方法,根据自己需要去定义,并不要求全部实现,这就是为什么不定义成接口的原因,因为有些实现根本用不着某些方法,
这带来很多灵活性,比如包装器是不支持删除目录rmdir功能的,那么就不需要实现streamWrapper::rmdir
由于未实现它,如果用户在包装器上调用rmdir将有错误抛出,要自定义这个错误那么也可以实现它并在其内部抛出错误
streamWrapper也不是一个预定义类,测试class_exists("streamWrapper")就知道,它只是一个指导开发者的原型
官方手册提供了一个例子:http://php.net/manual/zh/stream.streamwrapper.example-1.php
本博客提供一个从drupal8系统中抽取修改过的包装器例子,请看drupal8源码分析关于流那一部分
流系列函数,官方手册:http://php.net/manual/zh/ref.stream.php
常用的函数如下:
stream_bucket_append函数:为队列添加数据 stream_bucket_make_writeable函数:从操作的队列中返回一个数据对象 stream_bucket_new函数:为当前队列创建一个新的数据 stream_bucket_prepend函数:预备数据到队列 stream_context_create函数:创建数据流上下文 stream_context_get_default函数:获取默认的数据流上下文 stream_context_get_options函数:获取数据流的设置 stream_context_set_option函数:对数据流、数据包或者上下文进行设置 stream_context_set_params函数:为数据流、数据包或者上下文设置参数 stream_copy_to_stream函数:在数据流之间进行复制操作 stream_filter_append函数:为数据流添加过滤器 stream_filter_prepend函数:为数据流预备添加过滤器 stream_filter_register函数:注册一个数据流的过滤器并作为PHP类执行 stream_filter_remove函数:从一个数据流中移除过滤器 stream_get_contents函数:读取数据流中的剩余数据到字符串 stream_get_filters函数:返回已经注册的数据流过滤器列表 stream_get_line函数:按照给定的定界符从数据流资源中获取行 stream_get_meta_data函数:从封装协议文件指针中获取报头/元数据 stream_get_transports函数:返回注册的Socket传输列表 stream_get_wrappers函数:返回注册的数据流列表 stream_register_wrapper函数:注册一个用PHP类实现的URL封装协议 stream_select函数:接收数据流数组并等待它们状态的改变 stream_set_blocking函数:将一个数据流设置为堵塞或者非堵塞状态 stream_set_timeout函数:对数据流进行超时设置 stream_set_write_buffer函数:为数据流设置缓冲区 stream_socket_accept函数:接受由函数stream_ socket_server()创建的Socket连接 stream_socket_client函数:打开网络或者UNIX主机的Socket连接 stream_socket_enable_crypto函数:为一个已经连接的Socket打开或者关闭数据加密 stream_socket_get_name函数:获取本地或者网络Socket的名称 stream_socket_pair函数:创建两个无区别的Socket数据流连接 stream_socket_recvfrom函数:从Socket获取数据,不管其连接与否 stream_socket_sendto函数:向Socket发送数据,不管其连接与否 stream_socket_server函数:创建一个网络或者UNIX Socket服务端 stream_wrapper_restore函数:恢复一个事先注销的数据包 stream_wrapper_unregister函数:注销一个URL地址包
相信看了这些案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
相关阅读:
The above is the detailed content of How to use PHP's Streams and wrapper wrapper. For more information, please follow other related articles on the PHP Chinese website!