PHP streams and how the wrapper wrapper used

Source: Internet
Author: User
Today, let's talk about PHP flow streams, wrapper wrapper concepts and usage. Summary to everyone to see.

We know that the concept of flow streams is introduced in php4.3, is the abstraction of streaming data, for unified data operations, such as file data, network data, compressed data, etc., so that the same set of functions can be shared, PHP file system function is such a sharing, such as file_get_ Contents () function to open a local file or to access the URL is the embodiment. Simply put, a flow is a resource object that shows the behavior of streaming data.

Read and write in a linear manner and can search anywhere in the stream.

The flow is a bit like the database abstraction layer, in the database abstraction layer, regardless of the database used, the abstraction layer is used in the same way to manipulate the data, and the flow is the abstraction of the data, whether it is a local file or remote files or compressed files and so on, as long as the flow of data, then the operation is the same way.

With the concept of the flow of the wrapper wrapper this concept, each flow corresponds to a wrapper, the flow is from a unified operation from the perspective of a concept, and the wrapper is a concept from the understanding of the flow of data content, that is, the unified operation of how to operate or configure different content;

This content is presented in a streaming manner, but the content rules are different, such as the HTTP protocol is the flow of data, but only the HTTP wrapper to understand the HTTP protocol came from the meaning of the data, it can be understood that the flow is a pipe of water, but it flows out of the data, A wrapper is an interpreter in the outer layer of the flow that understands the meaning of the outgoing data and can manipulate it.

The Official Handbook says, "A wrapper is an additional code that tells the flow how to deal with a special protocol or code." Do you understand the meaning of this statement?

The wrapper can be nested, after a flow of wrapping a wrapper, can also continue to wrap the wrapper in the outer layer, this time the inside of the wrapper relative to the outer wrapper to act as a stream role
The C language Development documentation implemented at the bottom of PHP itself is interpreted as:

Stream API operations a pair of different levels: at the base level, the API defines the Php_stream object representing the streaming data source, at a slightly higher level, the API defines the Php_stream_wrapper object.

It wraps a low-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 can be applied to any number of filters on the above, the flow of data will be processed by the filter, I think the word filter is a bit inaccurate, some misleading.

From the literal point of view as if it is to remove some of the data, it should be called data regulator, because it can remove some data, but also can be added, but also can be modified, but the historical reasons are established, also known as filters, everyone in the mind to understand good.

We often see the following words to explain their differences:

Resources and data: resources are relatively macroscopic, usually contain data, and the data is more figurative, in the development of the process is often said to be data, and in software planning is said to be resources, they are synonyms, like software design and program development differences.

Context and Parameters: Context is a relatively macro, often used in communication, the specific point is a communication itself parameters, and the argument is often used in more specific things above, such as function

The above explains the conceptual stuff, and here's a look at the details:

PHP supported protocols and wrappers see here: http://php.net/manual/zh/wrappers.php:
(The author notes: The original title is: Support Protocol and encapsulation Protocol, Chinese translation is a bit misleading, accurate is to support the protocol and packaging, from the English layout is very clear)
Some protocols and wrappers are supported by default, please check with the Stream_get_wrappers () function. You can also customize a wrapper, register with Stream_wrapper_register ()
Although RFC 3986 can be used: do the delimiter, but PHP only allow://, so the URL please use "scheme://target" format

file://-accesses the local file system, which is used by default when using File system functions http://-access HTTP (s) URLs ftp://-access FTP (s) urlsphp://-access to each input/output stream (I/O streams) zlib://  -Compressed stream data://-data (RFC 2397) glob://-Find matching file path Mode phar://-php archive ssh2://-secure Shell 2rar://-rarogg://-Audio stream expect:// -Process Interactive Streams

How to implement a custom wrapper:

When manipulating flows with fopen, Fwrite, Fread, fgets, feof, Rewind, file_put_contents, file_get_contents, and so on, the data is passed to the defined wrapper class object first. The wrapper then goes to the operation flow.
How do I implement a custom flow wrapper? PHP provides a class prototype, just a prototype, not an interface or a class, and cannot be used for inheritance:

Streamwrapper {/* Property */public resource $context;/* method */__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) publicly 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, I NT $whence = seek_set) public bool Stream_set_option (int $option, int. $arg 1, int $arg 2) public array stream_stat (voi d) 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)}

The method defined in this prototype, which is defined according to its own needs, does not require full implementation, which is why it is not defined as an interface, because some implementations do not need certain methods at all.
This brings a lot of flexibility, such as the wrapper does not support the removal of the directory rmdir function, then there is no need to implement Streamwrapper::rmdir
Because it is not implemented, if the user calls RmDir on the wrapper there will be an error thrown, to customize this error can also implement it and throw an error inside it
Streamwrapper is also not a predefined class, testing class_exists ("Streamwrapper") knows that it is just a prototype that guides the developer

The Official Handbook provides an example: http://php.net/manual/zh/stream.streamwrapper.example-1.php

This blog provides an example of a modified wrapper from the DRUPAL8 system, see Drupal8 Source analysis about the flow that part

Stream series functions, Official Handbook: http://php.net/manual/zh/ref.stream.php

The usual functions are as follows:

Stream_bucket_append function: Add data to the queue Stream_bucket_make_writeable function: Returns a data object from the operation's queue stream_bucket_ New function: Create a fresh data stream_bucket_prepend function for the current queue: Prepare data to queue Stream_context_create function: Create a data flow context stream_context_get_ Default function: Gets the data flow context Stream_context_get_options function: Gets the setting of the data flow stream_context_set_option function: Sets the data flow, packet, or context STREAM_ Context_set_params function: Set parameter Stream_copy_to_stream function for data flow, packet, or context: Copy operations between data streams Stream_filter_ Append function: Add filter stream_filter_prepend function for data flow: Prepare a filter stream_filter_register function for Data flow: Register a filter for a data stream and execute as a PHP class STREAM_ Filter_remove function: Remove filter stream_get_contents function from a data stream: reads the remaining data in the data stream to the string Stream_get_filters function: Returns the list of data flow filters that have been registered STREAM_ Get_line function: Gets the row Stream_get_meta_data function from the data flow resource according to the given delimiter: Gets the header/metadata from the encapsulated protocol file pointer stream_get_ Transports function: Returns the registered socket transfer List Stream_get_wrappers function: Returns a list of registered data flows stream_register_ Wrapper function: Register a URL wrapper protocol implemented in PHP class Stream_select function: receive the data stream array and wait for their state to change stream_set_blocking function: Set a data flow to a blocked or non-clogging state stream_ Set_timeout function: Timeout setting for Data flow Stream_set_write_buffer function: Set buffer stream_socket_accept function for data flow: Accept by Function Stream_ Socket_server () Create a socket connection Stream_soCket_client function: Open the socket connection of the network or UNIX host Stream_socket_enable_crypto function: Turn data encryption on or off for an already connected socket stream_socket_get_ Name function: Get the names of local or network sockets Stream_socket_pair function: Create two non-differentiated socket data stream connection Stream_socket_recvfrom function: Get data from socket, Whether it's connected or not Stream_socket_sendto function: Send data to the socket, regardless of its connection Stream_socket_server function: Create a network or UNIX socket server Stream_wrapper_ Restore function: Recover a pre-logoff packet Stream_wrapper_unregister function: Unregister a URL address pack


Believe that you have seen these cases you have mastered the method, more wonderful please pay attention to the PHP Chinese network other related articles!

Related reading:

PHP deployment Environment using GIT

Some use cases of git

JavaScript data types and git use code explained

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.