Stream streams This concept is introduced in php4.3, is the abstraction of the convection data, for unified data operation, such as file data, network data, compressed data, so that can share the same set of functions, PHP file system function is such a share, such as file_get_contents () function to open a local file or access the URL is the embodiment. To put it simply, a stream is a resource object that behaves as a stream of data.
Read and write in a linear fashion, and can search anywhere in the stream.
The stream is somewhat similar to the database abstraction layer, and in the database abstraction layer, no matter what database you use, the same way you manipulate the data on top of the abstraction layer,
And the flow is the abstraction of the data, whether it is local files or remote files or compressed files, and so on, as long as the flow of data, then the way the operation is the same
With the concept of the flow of the wrapper wrapper this concept, each flow corresponds to a wrapper,
Flow is a concept generated from the perspective of unified operation, and the wrapper is a concept from the understanding of streaming data content, that is, how to operate or configure different content of this unified operation mode;
The content is presented in a streaming manner, but the content rules are different, such as the way the data from the HTTP protocol is streamed, but only the HTTP wrapper understands the meaning of the data from the HTTP protocol,
It can be understood that the flow is a pipe of water, but it flows out of data, the wrapper is set in the flow of the pipe outside an interpreter, it understands the meaning of the outflow of data, and can manipulate it
The Official Handbook says: "A wrapper is an additional code that tells the stream how to handle a particular protocol or code." Understand what this means?
The wrapper can be nested, a flow outside the wrapping of a wrapper, you can continue to wrap the wrapper in the outer layer, this time the layer of the wrapper relative to the outer wrapper to act as a stream role
The C language Development document that is implemented at the bottom of PHP itself has this explanation:
The Flow API operates at a different level: at the basic 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 the lower level of Php_stream objects to provide the ability to retrieve the content and metadata of URLs, add contextual parameters, and adjust wrapper behavior;
Each stream can be opened after the use of any number of filters on the above, stream data will be processed by the filter, the author thinks that the word filter is a little inaccurate, some misleading
It's literally like removing some of the data from the feeling that it should be called a data regulator, because it can remove some data, can also be added, but also can be modified, but historical reasons for the conventional,
Also known as the filter, we know in the heart of good.
We often see the following words to explain their differences:
Resources and data: Resources are more macroscopic, usually contain data, and the data is more like the saying, in the development of the program is often said to be data, and in software planning is said to be resources, they are synonyms, like software design and the difference between program development.
Context and Parameters: Context is a relatively macroscopic statement, often used in communication above, specific point is a communication itself parameters, and the argument is often used in more specific things above, such as function
It explains the conceptual stuff, and here's what to look at:
PHP supported protocols and wrappers look here : http://php.net/manual/zh/wrappers.php: (I note: The original title is: supported protocols and encapsulation agreements, Chinese translation is a bit misleading, precisely speaking is the support of the Protocol and the wrapper, It's clear from the English page.
Some protocols and wrappers are supported by default, please use the stream_get_wrappers () function to view them. You can also customize a wrapper and register with Stream_wrapper_register ()
Although RFC 3986 can be used: do the delimiter, but PHP only allow://, so the URL please use the "scheme://target" format
file://-access to the local file system, which is used by default when using File system functions
http://-access to HTTP (s) URLs
ftp://-access FTP (s) URLs
php://-access to each input/output stream (I/O streams)
zlib://-Compression Stream
data://-data (RFC 2397)
glob://-find a matching file path pattern
phar://-php Archive
Ssh2://-secure Shell 2
Rar://-rar
ogg://-Audio Stream
expect://-Processes Interactive Streams
How to implement a custom wrapper: When you operate a stream with file system functions such as fopen, Fwrite, Fread, fgets, feof, Rewind, file_put_contents, file_get_contents, and so on, The data is passed first to the defined wrapper class object, and the wrapper is then manipulated to manipulate the stream.
How to 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_clo Sedir (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 vo ID 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 $pa Th, 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 $arg 1, int $arg 2) 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)}
The method defined in this prototype, 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 deletion of directory rmdir functionality, then do not need to implement Streamwrapper::rmdir
Because it is not implemented, if the user invokes rmdir on the wrapper and there is an error, you can customize this error to implement it and throw an error inside it
Streamwrapper is also not a predefined class, and the Test 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 a sample of the modified wrapper from the DRUPAL8 system, Please see Drupal8 source analysis about the stream that part
Stream series functions,Official Handbook: http://php.net/manual/zh/ref.stream.php
The common functions are as follows:
Stream_bucket_append function: Adding data for queues
stream_bucket_make_writeable function: Returns a data object from the operation's queue
Stream_bucket_new function: Create a new data for the current queue
Stream_bucket_prepend functions: Preparing data to queues
Stream_context_create function: Creating a data flow context
Stream_context_get_default function: Gets the default data flow context
stream_context_get_options function: Getting Data flow settings
stream_context_set_option function: Setting up a data flow, packet, or context
Stream_context_set_params function: Setting parameters for Data flow, packet, or context
Stream_copy_to_stream function: Copy operation between data streams
stream_filter_append function: Adding filters for Data flow
Stream_filter_prepend function: Prepare add filter for data flow
Stream_filter_register function: Registers a filter for a data stream and executes it as a PHP class
Stream_filter_remove function: Removing filters from a data stream
stream_get_contents function: Reading the remaining data in the data stream to a string
Stream_get_filters function: Returns a list of registered data flow filters
Stream_get_line function: Fetching rows from a data flow resource according to the given delimiter
Stream_get_meta_data function: Get header/Meta data from encapsulated protocol file pointer
Stream_get_transports function: Returns the registered socket transfer list
Stream_get_wrappers function: Returns a list of registered data streams
Stream_register_wrapper function: Registering a URL encapsulation protocol implemented with a PHP class
Stream_select function: Receives an array of data streams and waits for their state to change
stream_set_blocking function: Set a data stream to a blocked or non-blocking state
Stream_set_timeout function: Timeout setting for data flow
Stream_set_write_buffer function: Set buffer for data flow
stream_socket_accept function: Accept socket connection created by function Stream_ Socket_server ()
stream_socket_client function: Open a network or socket connection to a UNIX host
Stream_socket_enable_crypto function: Turn data encryption on or off for an already connected socket
Stream_socket_get_name function: Gets the name of the local or network socket
Stream_socket_pair function: Create two no difference socket data stream connections
Stream_socket_recvfrom function: Get data from the socket, regardless of whether it is connected or not
Stream_socket_sendto function: Send data to the socket, regardless of whether it is connected or not
Stream_socket_server function: Create a network or UNIX socket server
Stream_wrapper_restore function: Restores a previously logged out packet
Stream_wrapper_unregister function: Unregister a URL address package
an explanation of a filter:
RELATED links:
User filter base class: Http://php.net/manual/zh/class.php-user-filter.php
Filter Registration: http://php.net/manual/zh/function.stream-filter-register.php
<?php
/* Defines a filter *
/class Strtoupper_filter extends Php_user_filter {
function filter ($in, $out, &$ Consumed, $closing)
{
while ($bucket = stream_bucket_make_writeable ($in)) {//Take a piece of data out of the stream
$bucket-> data = Strtoupper ($bucket->data);
$consumed + + $bucket->datalen;
Stream_bucket_append ($out, $bucket); Send the modified data to the output where} return
psfs_pass_on}
/* Register filter to PHP *
/Stream_filter_register ("Strtoupper", "Strtoupper_filter")
or Die ("Failed to register filter ");
$fp = fopen ("Foo-bar.txt", "w");
/* Apply filter to a stream * * *
stream_filter_append ($fp, "Strtoupper");
Fwrite ($fp, "line1\n");
Fwrite ($fp, "word-2\n");
Fwrite ($FP, "easy as 123\n");
Fclose ($FP);
Read and display the content will all become uppercase
ReadFile ("Foo-bar.txt");
? >