The principle of process file system and stream wrapper in Drupal

Source: Internet
Author: User
Tags abstract wrapper drupal

After Drupal7 and PHP (and not knowing that version), there is one more concept: stream, which is mainly used to represent file flow. BTW, back to the concept of stream, this should be very early in Java and C # and other OOP language already exist, PHP is simply encapsulated file/file_get_content functions, easy to use, so Phper are less concerned with the stream and the entire underlying operation.

When we use a complete framework (e.g., Drupal framework), Drupal makes it more perfect for the concept and usage of stream in PHP, making it more oo and easier to use.

Note: PHP natively supports Streamwrapper, and Drupal is just perfecting it, so PHP functions such as move, fopen, move_uploaded_file can be accessed directly in scheme form (similar to php:// The form), add a streamwrapper in PHP need to register one, need to call the Stream_wrapper_register function.

Detailed reference: http://php.net/..class.streamwrapper.php

What's the stream?

Stream is actually an abstract concept, similar to the transport layer, located on the lower level of the file system. Connected to the stream is the storage, that is, the stream is equivalent to a transmission line, one end of the connection storage, end of the connection program.


Operation of File

The operation of the file is basically read and write, the location of reading and writing is actually stream, read and write local files, we think is also a stream, you can think of Localfilestream, read and write network files (FTP, HTTP, S3, Ali Oss, NFF, etc.) is also a stream, so that the file's operational API should actually be an abstract function, rather than a specific function.

Using the database as an analogy, the data open, prepare, update, execute, Close is an abstract class, the backend with what database, there is a corresponding implementation, refer to the following picture:


The program calls the abstract interface of the database, which, according to the URL of the database, returns the different database operations classes according to the different scheme and completes the operation of the database.

Therefore, the operation of the file, we use database operations to do contrast, you can use the following figure to indicate:


From the above figure, we can understand that in fact, the operation of the file is the operation of convection, different streams correspond to different storage media, but by default we think that the stream is a local file, but as the storage becomes more complex and the cloud rises, the operation of the file cannot be easily understood as the operation of the local file, but rather the convection (Stream) operation.

Therefore, in other languages stream is a base class, depending on the storage medium, the stream has different implementations, such as C # in the integration of the stream in the following, through OO way, better understanding of the concept of stream.


The file API and stream wrapper in Drupal

A picture of the top of the word, combined with the above understanding, we did the following diagram, although not necessarily accurate, but it can help us understand the relationship between the file and stream wrapper in Drupal.


Drupal's stream wrapper is just an interface that allows you to implement any custom stream wrapper.

Drupal's third-party stream wrapper:

Remote Stream Wrapper (http://, https://, feeds://)

Https://www.drupal.org/project/remote_stream_wrapper

System Stream Wrapper (module://, theme://, profile://, library://)

Https://www.drupal.org/project/system_stream_wrapper

Therefore, the standardized file operation needs to be passed in a path with scheme, in PHP, the default without scheme is the local file. In Drupal, the default scheme is public and is the public directory of local files.

STORAGEAPI Module

Drupal may not be perfect for the logic above, maybe the abstraction layer is not perfect, so the Storageapi module appears, it expands the standard Drupal file operation, even if you do not understand the file and stream of Drupal, install this module, plus the corresponding service to achieve the above image of the cloud file operation. such as Amazon S3, FTP, database and so on.

(In my opinion, STORAGEAPI is the realization and perfection of logic in the document access structure diagram above)

Of course, this module is also flawed, that is, with some Third-party modules compatible with the problem, not all modules can be good compatible with the Storage API module, this is also a Drupal Third-party module with the disease.

Note that to enable Drupal file support for STORAGEAPI, the core bridge module needs to be enabled.

STORAGEAPI Module Reference Https://drupal.org/project/storage_api

Storage API Stream Wrapper:https://www.drupal.org/project/storage_api_stream_wrapper

Postscript

Review the first paragraph, PHP internal support streamwrapper, in Drupal, the PHP streamwrapper expansion, the main interface is: Drupalstreamwrapperinterface

Drupal itself implements the Drupallocalstreamwrapper through this interface, and a number of Third-party modules are implemented as follows:

Hpcloud:hpclouddrupalstreamwrapper

Storage Api:storageapistreamwrapper

Storage Core Bridge:drupalstoragestreamwrapper


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.