PHP Wrapper application method on SAE, WRAPPERSAE
This article describes the application of PHP wrapper on the SAE. Share to everyone for your reference, as follows:
First, what is PHP wrapper
Starting with PHP 4.3, PHP began allowing users to customize URL-style protocols via Stream_wrapper_register (). When a user uses a file system function such as fopen () and copy () to manipulate the wrapper protocol, PHP invokes the corresponding function in the class provided by the registration protocol.
The PHP manual gives an example of registering the Variablestream class as the VAR://protocol, which allows users to read and write global variables directly using file system functions. For example, users can read and write $GLOBALS [' foo '] through "Var://foo".
Second, why does the SAE need PHP Wrapper
For performance and security reasons, local file reads and writes and external data fetching are disabled on the SAE platform. Accordingly, we provide the corresponding services to do the same thing.
Because the interface of the new service is not the same as the interface of PHP itself, the program developed specifically for our platform will not have problems, but a large number of existing programs and open source projects are faced with complicated migration work. By using PHP wrapper to encapsulate the interface of our services, users can more easily migrate programs to the SAE platform.
Third, how to write PHP Wrapper
To encapsulate a protocol through PHP wrapper, first, we need to write a Streamwrapper class, the class name can be customized, the format of the class is:
Streamwrapper {public Resource $context, __construct (void) public bool Dir_closedir (void) public bool Dir_opendir (ST Ring $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 $pa th, int $options) public resource Stream_cast (Int. $cast _as) public void stream_close (void) public bool Stream_eof (v OID) public bool Stream_flush (void) publicly bool Stream_lock (mode $operation) 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 $arg 1, Int. $arg 2) Public Array St Ream_stat (void) public int stream_tell (void) public int stream_write (string $data) public bool Unlink (string $path ) Public Array Url_stat(string $path, int $flags)}
Description of the methods in the class:
Streamwrapper::__construct-constructor, called only before Stream_open
Streamwrapper::d ir_closedir-Close the directory handle, respond to the Closedir () function
Streamwrapper::d ir_opendir-Open the directory handle, respond to the opendir () function
Streamwrapper::d ir_readdir-reads entries from the directory handle, responds to the Readdir () function
Streamwrapper::d ir_rewinddir-back to the directory handle, respond to the Rewinddir () function
streamwrapper::mkdir-Creating a directory, responding to the mkdir () function
streamwrapper::rename-directory or file Rename, response rename () function
streamwrapper::rmdir-Deleting a directory, responding to the rmdir () function
Streamwrapper::stream_cast-retrieving the underlying resource, responding to the Stream_select () function
Streamwrapper::stream_close-Close resource, respond to fclose () function
streamwrapper::stream_eof-checks whether the file pointer is already at the end of the file, responds to the feof () function
streamwrapper::stream_flush-clear output cache, Response Fflush () function
streamwrapper::stream_lock-consultation file Lock, Response flock () function
streamwrapper::stream_open-open file or URL for stream, response fopen () function
Streamwrapper::stream_read-reads content from the stream, responds to Fread (), fgets () function
streamwrapper::stream_seek-position the pointer in the stream, responding to the fseek () function
streamwrapper::stream_set_option-Changing flow settings
Streamwrapper::stream_stat-retrieving information about a file resource, responding to the Fstat () function
streamwrapper::stream_tell-retrieves the position of the pointer in the stream, responds to the Ftell () function
Streamwrapper::stream_write-writes content to the stream, responds to Fwrite (), fputs () function
streamwrapper::unlink-Deleting a file, responding to the unlink () function
streamwrapper::url_stat-retrieves the information of the file in response to all stat () related functions such as file_exists (), Is_dir (), Is_file (), FileSize (), Fileinode (), etc.
For more information, please refer to the PHP manual: http://cn2.php.net/manual/en/class.streamwrapper.php
Once you have written the Streamwrapper class, use Stream_wrapper_register () to register the class with wrapper and you are ready to start using it. The function uses the following methods:
BOOL Stream_wrapper_register (String $protocol, string $classname [, int $flags = 0])
For example:
Stream_wrapper_register ("Saemc", "Saememcachewrapper");
Since the SAE platform does not support write operations on local files, some open source projects such as smarty that need to be written locally cannot be used directly on the SAE platform, and with SAEMC Wrapper, users can save Smarty compiled templates in the MC. It is convenient to migrate Smarty to the SAE platform.
In the annex we provide you with the SAE on the Memcache wrapper implementation code, you can download this attachment for testing.
Before testing, a memcached service with Port 22222 needs to be started locally:
Memcached-m 10-p 22222-u nobody-l 127.0.0.1
You can then test it with the following code:
Include the attachment code, register SAEMC wrapperinclude_once (' wrapper.php ');//test SAEMC WRAPPER$FP = fopen ("Saemc://test.txt", "w+") or Die (" fopen faild! "); Fwrite ($fp, "line1\n") or Die ("fwrite line1 faild!"); Fwrite ($fp, "line2\n") or Die ("Fwrite line2 faild!"); Fwrite ($fp, "line3\n") or Die ("fwrite line3 faild!"); Var_dump (Ftell ($FP)), fseek ($fp, 0), while (!feof ($fp)) {$c = fgets ($fp) or Die ("fgets faild!"); Var_dump ($c);} Fclose ($FP); Var_dump (file_get_contents ("Saemc://test.txt"); Var_dump (File_put_contents ("Saemc://path/test.txt") , "Hello world!\n")); Var_dump (File_put_contents ("Saemc://path/test.txt", "Hello world!\n", file_append)); Var_dump ( File_get_contents ("Saemc://path/test.txt")); Var_dump (Copy ("Saemc://path/test.txt", "Saemc://path/test_new.txt") ), Var_dump (file_get_contents ("Saemc://path/test_new.txt")), Var_dump (unlink ("Saemc://path/test.txt")), Var_dump ( File_get_contents ("Saemc://path/test.txt")); Var_dump (Rename ("Saemc://path/test_new.txt", "Saemc://path/test.txt ")); Var_dump (File_Get_contents ("Saemc://path/test.txt")); echo "====test include====\n"; include_once ("Saemc://path/test.txt");
Test the output of the page:
Int (+) string (6) "Line1" string (6) "Line2" string (6) "Line3" string () "Line1line2line3" int (all) int (all) string (26) " Hello World!hello world! " BOOL (TRUE) string ("Hello World!hello world!") BOOL (TRUE) bool (FALSE) bool (TRUE) string ("Hello World!hello world!") ====test Include====hello World!hello world!
We provide the Memcache wrapper does not implement the directory operation of some methods and Memcache timeout, you can refer to the PHP manual, try to implement directory operations, or through the context to make this wrapper support Memcache timeout.
Alternatively, you can view the source code of the Sae_include in SAE Stdlib at the address below, including the implementation of the Saestor wrapper that we encapsulated for the storage service and the HTTP Fetchurl that we repackaged for the wrapper service:
http://stdlib.sinaapp.com/?f=sae_include.function.php
Iv. some precautions when writing wrapper
1. Constructors
The Streamwrapper class is special, and its constructors are not called every time. It is only called when your action triggers a stream_open-related action, such as when you use File_get_contents (). And when your action triggers a function unrelated to stream, such as file_exists triggers the Url_stat method, this time the constructor is not called.
2. Read the implementation
Wrapper inside has the concept of position and seek, but a lot of services are actually read all the data at once, this can be read back at stream_open time, put in a property, In the future seek and tell when the direct operation of the properties of the data stored inside it.
3. Append Write implementation
There are many services that write all data at once, do not support append write functions (such as memcache), which requires us to implement the append write in wrapper. You can read the entire value one time, append the data that needs to be appended to the read, and then write back once.
However, the implementation of this additional write performance will be poor, especially after the content is large, one-time reading of all the content is very resource-intensive, so in some services we have to abandon the support for additional write.
4. Implementation of Url_stat
In the implementation of Streamwrapper class, the realization of url_stat is a difficult point. It is necessary to implement url_stat correctly to make the functions of querying file meta-information such as is_writable and is_readable work properly.
And we need to forge these values for our virtual devices. Take MC For example, we give you some reference data:
Url_stat should return an array of 13 items with the following contents:
Dev Device number-write 0;
Ino inode number-write 0;
Mode file mode-This is the permission control symbol for the file, which is explained later in detail;
Nlink Link-write 0;
UID Uid-linux on the use of Posix_get_uid can be taken, Windows 0;
GID Gid-linux on the use of Posix_get_gid can be taken, Windows 0;
Rdev Device Type-When the inode device has a value;
Size-File size;
Atime-The last read time format is the UNIX timestamp;
Mtime-Last write time;
CTime-creation time;
Blksize-blocksize of filesystem IO write 0;
Blocks-number of 512-byte blocks allocated write 0 can;
Where the value of mode must be written to:
If it is a file, its value is:
0100000 + file permissions, such as 0100000 + 0777.
If it is a directory, its value is:
040000 + directory permissions, such as 0400000 + 0777.
5. About the stat cache
PHP caches the meta-information of the file during the execution of the same page.
According to the PHP documentation for Clearstatcache () This method is known: In the use of stat (), Lstat (), file_exists (), is_writable (), is_readable (), is_executable (), Is_file (), Is_dir (), Is_link (), Filectime (), Fileatime (), Filemtime (), Fileinode (), filegroup (), Fileowner (), FileSize ( ), filetype (), or the Fileperms () method queries the file information, PHP caches the file's stat to improve performance. The Clearstatcache () method can be used to clear this cache when unlink () automatically clears the stat cache.
In fact, PHP will clear the stat cache only when the local files are unlink, rename and RmDir, while unlink, rename, and rmdir operations through other wrapper do not clear the stat cache. So when we write wrapper we have to clear the stat cache by Clearstatcache () in unlink and other methods.
Click here to download the attachment.
More about PHP related content readers can view the topic: "PHP Curl Usage Summary", "PHP Socket Usage Summary", "PHP Network Programming Skills Summary", "PHP Basic Grammar Primer Tutorial", "PHP operation Office Document tips summary (including word, excel,access,ppt), "PHP Date and Time usage summary", "PHP primer for Object-oriented programming", "PHP String Usage Summary", "Getting Started with Php+mysql database operations" and "PHP Common Database Operations Skills Summary"
I hope this article is helpful to you in PHP programming.
http://www.bkjia.com/PHPjc/1125234.html www.bkjia.com true http://www.bkjia.com/PHPjc/1125234.html techarticle PHP Wrapper on the SAE application method, Wrappersae This article describes the PHP wrapper application method on the SAE. Share to everyone for reference, as follows: First, PHP wrapper is what from ...