PHP內(nèi)核探索 —— 再次探討SAPI
在PHP的生命周期的各個階段,一些與服務相關的操作都是通過SAPI接口實現(xiàn)。 這些內(nèi)置實現(xiàn)的物理位置在PHP源碼的SAPI目錄。這個目錄存放了PHP對各個服務器抽象層的代碼, 例如命令行程序的實現(xiàn),Apache的mod_php5模塊實現(xiàn)以及fastcgi的實現(xiàn)等等。
在各個服務器抽象層之間遵守著相同的約定,這里我們稱之為SAPI接口。 每個SAPI實現(xiàn)都是一個_sapi_module_struct結構體變量。(SAPI接口)。 在PHP的源碼中,當需要調用服務器相關信息時,全部通過SAPI接口中對應方法調用實現(xiàn), 而這對應的方法在各個服務器抽象層實現(xiàn)時都會有各自的實現(xiàn)。
下面是為SAPI的簡單示意圖:
以cgi模式和apache2服務器為例,它們的啟動方法如下:
cgi_sapi_module.startup(&cgi_sapi_module) // cgi模式 cgi/cgi_main.c文件apache2_sapi_module.startup(&apache2_sapi_module); // apache2服務器 apache2handler/sapi_apache2.c文件
這里的cgi_sapi_module是sapi_module_struct結構體的靜態(tài)變量。 它的startup方法指向php_cgi_startup函數(shù)指針。在這個結構體中除了startup函數(shù)指針,還有許多其它方法或字段。 其部分定義如下
struct _sapi_module_struct { char *name; // 名字(標識用) char *pretty_name; // 更好理解的名字(自己翻譯的) int (*startup)(struct _sapi_module_struct *sapi_module); // 啟動函數(shù) int (*shutdown)(struct _sapi_module_struct *sapi_module); // 關閉方法 int (*activate)(TSRMLS_D); // 激活 int (*deactivate)(TSRMLS_D); // 停用 int (*ub_write)(const char *str, unsigned int str_length TSRMLS_DC); // 不緩存的寫操作(unbuffered write) void (*flush)(void *server_context); // flush struct stat *(*get_stat)(TSRMLS_D); // get uid char *(*getenv)(char *name, size_t name_len TSRMLS_DC); // getenv void (*sapi_error)(int type, const char *error_msg, ...); /* error handler */ int (*header_handler)(sapi_header_struct *sapi_header, sapi_header_op_enum op,sapi_headers_struct *sapi_headers TSRMLS_DC); /* header handler */ /* send headers handler */ int (*send_headers)(sapi_headers_struct *sapi_headers TSRMLS_DC); void (*send_header)(sapi_header_struct *sapi_header, void *server_context TSRMLS_DC); /* send header handler */ int (*read_post)(char *buffer, uint count_bytes TSRMLS_DC); /* read POST data */ char *(*read_cookies)(TSRMLS_D); /* read Cookies */ /* register server variables */ void (*register_server_variables)(zval *track_vars_array TSRMLS_DC); void (*log_message)(char *message); /* Log message */ time_t (*get_request_time)(TSRMLS_D); /* Request Time */ void (*terminate_process)(TSRMLS_D); /* Child Terminate */ char *php_ini_path_override; // 覆蓋的ini路徑 ... ...};
以上的這些結構在各服務器的接口實現(xiàn)中都有定義。如Apache2的定義:
static sapi_module_struct apache2_sapi_module = { 'apache2handler', 'Apache 2.0 Handler', php_apache2_startup,/* startup */ php_module_shutdown_wrapper, /* shutdown */ ...}
目前PHP內(nèi)置的很多SAPI實現(xiàn)都已不再維護或者變的有些非主流了,PHP社區(qū)目前正在考慮將一些SAPI移出代碼庫。 社區(qū)對很多功能的考慮是除非真的非常必要,或者某些功能已近非常通用了,否則就在PECL庫中, 例如非常流行的APC緩存擴展將進入核心代碼庫中。
整個SAPI類似于一個面向對象中的模板方法模式的應用。 SAPI.c和SAPI.h文件所包含的一些函數(shù)就是模板方法模式中的抽象模板, 各個服務器對于sapi_module的定義及相關實現(xiàn)則是一個個具體的模板。
這樣的結構在PHP的源碼中有多處使用, 比如在PHP擴展開發(fā)中,每個擴展都需要定義一個zend_module_entry結構體。 這個結構體的作用與sapi_module_struct結構體類似,都是一個類似模板方法模式的應用。 在PHP的生命周期中如果需要調用某個擴展,其調用的方法都是zend_module_entry結構體中指定的方法, 如在上一小節(jié)中提到的在執(zhí)行各個擴展的請求初始化時,都是統(tǒng)一調用request_startup_func方法, 而在每個擴展的定義時,都通過宏PHP_RINIT指定request_startup_func對應的函數(shù)。 以VLD擴展為例:其請求初始化為PHP_RINIT(vld),與之對應在擴展中需要有這個函數(shù)的實現(xiàn):
PHP_RINIT_FUNCTION(vld) {}
所以, 我們在寫擴展時也需要實現(xiàn)擴展的這些接口,同樣,當實現(xiàn)各服務器接口時也需要實現(xiàn)其對應的SAPI。
相關文章:
