Application Method of PHP Wrapper on SAE, wrappersae

Source: Internet
Author: User

Application Method of PHP Wrapper on SAE, wrappersae

This article describes the application of PHP Wrapper on SAE. We will share this with you for your reference. The details are as follows:

I. What is PHP Wrapper?

Starting from PHP 4.3, PHP allows users to customize URL-style protocols through stream_wrapper_register. When you use file system functions such as fopen () and copy () to operate the encapsulation protocol, PHP calls the corresponding functions in the class provided during the registration protocol.
The PHP Manual provides an example that registers the VariableStream class as the var: // protocol. Through this protocol, you can use file system functions to directly read and write global variables. For example, you can use "var: // foo" to read and write $ GLOBALS ['foo'].

Ii. Why does SAE need PHP Wrapper?

For performance and security considerations, local file reading and writing and external data capturing are disabled on the SAE platform. Correspondingly, we provide corresponding services to do the same thing.

Because the interfaces of the new service are not the same as those of PHP, the programs specially developed for our platform certainly do not have problems, but a large number of existing programs and open-source projects, it is faced with complicated migration work. After using PHP Wrapper to encapsulate our service interfaces, You can migrate programs to the SAE platform more conveniently.

Iii. 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 class format is:

streamWrapper {public resource $context ;__construct ( 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 ( 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 $arg1 , int $arg2 )public array stream_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 )}

Class description:

StreamWrapper ::__ construct-constructor, called only before stream_open
StreamWrapper: dir_closedir-close the directory handle and respond to the closedir () function
StreamWrapper: dir_opendir-open the directory handle and respond to the opendir () function
StreamWrapper: dir_readdir-reads entries from the directory handle and responds to the readdir () function
StreamWrapper: dir_rewinddir-returns the directory handle and returns the rewinddir () function.
StreamWrapper: mkdir-create a directory and respond to the mkdir () function.
StreamWrapper: rename-directory or file rename, response rename () function
StreamWrapper: rmdir-delete the Directory and respond to the rmdir () function.
StreamWrapper: stream_cast-retrieves basic resources and responds to the stream_select () function.
StreamWrapper: stream_close-close the resource and respond to the fclose () function.
StreamWrapper: stream_eof-check whether the file pointer is at the end of the file and respond to the feof () function
StreamWrapper: stream_flush-clear output cache and respond to the fflush () function
StreamWrapper: stream_lock-consultation file lock, response flock () function
StreamWrapper: stream_open-when a file is opened or the URL is a stream, the fopen () function is returned.
StreamWrapper: stream_read-read content from the stream, response fread (), fgets () function
StreamWrapper: stream_seek-locate the pointer in the stream and respond to the fseek () function
StreamWrapper: stream_set_option-change stream settings
StreamWrapper: stream_stat-retrieves information about file resources and responds to the fstat () function.
StreamWrapper: stream_tell-retrieves the position of the pointer in the stream, responding to the ftell () function
StreamWrapper: stream_write-write content to the stream, response fwrite (), fputs () function
StreamWrapper: unlink-delete a file and respond to the unlink () function.
StreamWrapper: url_stat-retrieves file information and responds to all stat () related functions, such as file_exists (), is_dir (), is_file (), filesize (), fileinode (), etc.

For more information, see the PHP Manual: http://cn2.php.net/manual/en/class.streamwrapper.php

After writing the streamWrapper class, you can use stream_wrapper_register () to register the class to Wrapper. Function usage:

Bool stream_wrapper_register (string $ protocol, string $ classname [, int $ flags = 0])

For example:

stream_wrapper_register("saemc", "SaeMemcacheWrapper");

Because the SAE platform does not support write operations on local files, some open-source projects such as Smarty that need to write files locally cannot be used directly on the SAE platform. With saemc Wrapper, you can save the template compiled by Smarty in MC to easily migrate Smarty to the SAE platform.

In the attachment, we provide you with the implementation code of Memcache Wrapper on SAE. You can download this attachment for testing.

Before testing, start a locally deployed Memcached service with port 22222:

memcached -m 10 -p 22222 -u nobody -l 127.0.0.1

Then use the following code to test:

// Contains the attachment code and registers 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 ");

Output result of the test page:

int(18)string(6) "line1"string(6) "line2"string(6) "line3"string(18) "line1line2line3"int(13)int(13)string(26) "hello world!hello world!"bool(true)string(26) "hello world!hello world!"bool(true)bool(false)bool(true)string(26) "hello world!hello world!"====test include====hello world!hello world!

The Memcache Wrapper provided does not implement directory operations or Memcache Timeout. You can refer to the PHP manual to try directory operations or use context to make this Wrapper support Memcache Timeout.

In addition, you can view the sae_include source code in SAE Stdlib at the following address, and the implementation of saestor Wrapper encapsulated for the Storage service and re-encapsulated for the Fetchurl service:

Http://stdlib.sinaapp.com /? F = sae_include.function.php

4. Precautions for writing Wrapper

1. Constructor

The streamWrapper class is special, and its constructor does not call it every time. It is called only when your operation triggers stream_open-related operations. For example, you use file_get_contents. When your operation triggers a function unrelated to stream, for example, file_exists triggers the url_stat method, the constructor will not be called at this time.

2. Read implementation

Wrapper has the concepts of Position and Seek, but many services actually read all the data at a time. This can be read back at one time during stream_open and put into one attribute, in the future, seek and tell can directly operate on the data stored in the attribute.

3. append write implementation

Many services write all data at a time and do not support append writing (such as Memcache). Therefore, we need to implement append writing in Wrapper. You can read the entire value at a time, append the data to be appended to the read content, and then write it back at a time.

However, the performance of this append write implementation method is relatively poor, especially when the content size is large, it will consume a lot of resources to read all the content at a time, therefore, in some services, we have to discard the support for append writing.

4. url_stat implementation

In the implementation of the streamWrapper class, url_stat is a difficult implementation. Url_stat must be implemented correctly to enable functions such as is_writable and is_readable to query object metadata normally.

We need to forge these values for our virtual devices. Taking mc as an example, we will give you some reference data:

Url_stat should return an array with 13 items. The content is as follows:

Dev device number-enter 0;
Ino inode No.-Enter 0;
Mode file mode-this is the File Permission control symbol, which will be described in detail later;
Nlink link-write 0;
Uid-posix_get_uid can be obtained in Linux, and 0 in windows;
Gid-posix_get_gid can be obtained in Linux and 0 in windows;
Rdev device type-there is a value when it is an inode device;
Size-File size;
Atime-the last read time format is a unix timestamp;
Mtime-last write time;
Ctime-creation time;
Blksize-blocksize of filesystem IO can be written to zero;
Blocks-number of 512-byte blocks allocated can be written to zero;

The mode value must be written:

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 stat Cache

PHP caches the object metadata during the execution of the same page.
According to the instructions in the PHP document on the clearstatcache () method, we know that stat (), lstat (), file_exists (), is_writable (), is_readable (), is_executable (), is_file (), is_dir (), is_link (), filectime (), fileatime (), filemtime (), fileinode (), filegroup (), fileowner (), filesize (), when the filetype (), or fileperms () method queries file information, PHP caches the file stat to improve performance. The clearstatcache () method can be used to clear the cache. When unlink () is used, the stat cache is automatically cleared.

In fact, PHP only clears the stat cache when unlink, rename, and rmdir operations are performed on local files, while unlink, rename, and rmdir operations are performed through other wrapper, the stat cache is not cleared. Therefore, when writing wrapper, we need to clear the stat cache through clearstatcache () in unlink and other methods.

Click here to download the attachment.

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.