這里閱讀的php版本為PHP-7.1.0 RC3,閱讀代碼的平臺(tái)為linux

首先是尋找php的入口,php有很多種模式,apache,php-fpm, cli模式,我要入手的話,只能先從最簡(jiǎn)單的cli模型開始。

那么,我需要先尋找

php -r 'echo 12;'

這個(gè)命令是如何執(zhí)行的。

首先還是尋找main入口,由于我們看的是命令行的php程序。所以,這個(gè)入口在sapi/cli/php_cli.c中。

首先是定義一系列的變量

int c;
zend_file_handle file_handle; int behavior = PHP_MODE_STANDARD; char *reflection_what = NULL; volatile int request_started = 0; volatile int exit_status = 0; char *php_optarg = NULL, *orig_optarg = NULL; int php_optind = 1, orig_optind = 1; char *exec_direct=NULL, *exec_run=NULL, *exec_begin=NULL, *exec_end=NULL; char *arg_free=NULL, **arg_excp=&arg_free; char *script_file=NULL, *translated_path = NULL; int interactive=0; int lineno = 0; const char *param_error=NULL; int hide_argv = 0;

然后是這個(gè)

sapi_module_struct *sapi_module = &cli_sapi_module;

這是一個(gè)sapi_module_struct結(jié)構(gòu),這個(gè)結(jié)構(gòu)是sapi中最重要的數(shù)據(jù)結(jié)構(gòu)。它的定義在main/SAPI.h中。

下面是增加了注釋的代碼:

struct _sapi_module_struct { // SAPI模塊結(jié)構(gòu) char *name; // 應(yīng)用層名稱,比如cli,cgi等 char *pretty_name; // 應(yīng)用層更易讀的名字,比如cli對(duì)應(yīng)的就是Command Line Interface int (*startup)(struct _sapi_module_struct *sapi_module); // 當(dāng)一個(gè)應(yīng)用要調(diào)用php的時(shí)候,這個(gè)模塊啟動(dòng)的時(shí)候會(huì)調(diào)用的函數(shù) int (*shutdown)(struct _sapi_module_struct *sapi_module); // 當(dāng)一個(gè)應(yīng)用要調(diào)用php的時(shí)候,這個(gè)模塊結(jié)束的時(shí)候會(huì)調(diào)用的函數(shù) int (*activate)(void); // 在處理每個(gè)request的時(shí)候,激活需要調(diào)用的函數(shù) int (*deactivate)(void); // 在處理完每個(gè)request的時(shí)候,收尾時(shí)候要調(diào)用的函數(shù) size_t (*ub_write)(const char *str, size_t str_length); // 這個(gè)函數(shù)告訴php如何輸出數(shù)據(jù) void (*flush)(void *server_context); // 提供給php的刷新緩存的函數(shù)指針 zend_stat_t *(*get_stat)(void); // 用來判斷要執(zhí)行文件的權(quán)限,來判斷是否有執(zhí)行權(quán)限 char *(*getenv)(char *name, size_t name_len); // 獲取環(huán)境變量的方法 void (*sapi_error)(int type, const char *error_msg, ...) ZEND_ATTRIBUTE_FORMAT(printf, 2, 3); // 錯(cuò)誤處理方法 int (*header_handler)(sapi_header_struct *sapi_header, sapi_header_op_enum op, sapi_headers_struct *sapi_headers); // 這個(gè)函數(shù)會(huì)在我們調(diào)用header()的時(shí)候被調(diào)用 int (*send_headers)(sapi_headers_struct *sapi_headers); // 發(fā)送所有的header void (*send_header)(sapi_header_struct *sapi_header, void *server_context); // 單獨(dú)發(fā)送某一個(gè)header size_t (*read_post)(char *buffer, size_t count_bytes); // 如何獲取HTTP POST中的數(shù)據(jù) char *(*read_cookies)(void); // 如何獲取cookie中的數(shù)據(jù) void (*register_server_variables)(zval *track_vars_array); // 這個(gè)函數(shù)可以給$_SERVER中獲取變量 void (*log_message)(char *message, int syslog_type_int); // 輸出錯(cuò)誤信息函數(shù) double (*get_request_time)(void); // 獲取請(qǐng)求時(shí)間的函數(shù) void (*terminate_process)(void); // TODO: 調(diào)用exit的時(shí)候調(diào)用的方法 char *php_ini_path_override; // PHP的ini文件被復(fù)寫了所復(fù)寫的地址 void (*default_post_reader)(void); // 這里和前面的read_post有個(gè)差別,read_post負(fù)責(zé)如何獲取POST數(shù)據(jù),而這里的函數(shù)負(fù)責(zé)如何解析POST數(shù)據(jù) void (*treat_data)(int arg, char *str, zval *destArray); // 對(duì)數(shù)據(jù)進(jìn)行處理,比如進(jìn)行安全過濾等。 default_post_reader/tread_data/input_filter是三個(gè)能對(duì)輸入進(jìn)行過濾和處理的函數(shù) char *executable_location; // 執(zhí)行的地理位置 int php_ini_ignore; // 是否不使用任何ini配置文件,比如php -n 就將這個(gè)位置設(shè)置為1 int php_ini_ignore_cwd; // 不在當(dāng)前路徑尋找php.ini int (*get_fd)(int *fd); // 獲取執(zhí)行文件的fd int (*force_http_10)(void); // 強(qiáng)制使用http1.0 int (*get_target_uid)(uid_t *); // 獲取執(zhí)行程序的uid int (*get_target_gid)(gid_t *); // 獲取執(zhí)行程序的gid unsigned int (*input_filter)(int arg, char *var, char **val, size_t val_len, size_t *new_val_len); // 對(duì)輸入進(jìn)行過濾。比如將輸入?yún)?shù)填充到自動(dòng)全局變量$_GET, $_POST, $_COOKIE中 void (*ini_defaults)(HashTable *configuration_hash); // 默認(rèn)的ini配置 int phpinfo_as_text; // 是否打印phpinfo信息 char *ini_entries; // 有沒有附帶的ini配置,比如使用php -d date.timezone=America/Adak,可以在命令行中設(shè)置時(shí)區(qū) const zend_function_entry *additional_functions; // 每個(gè)SAPI模塊特有的一些函數(shù)注冊(cè),比如cli的cli_get_process_title unsigned int (*input_filter_init)(void); // TODO: };

那么我們看下cli的SAPI的module是什么樣子的呢?

其中我把里面原先有的STANDARD_SAPI_MODULE_PROPERTIES宏給解出來展示如下:

static sapi_module_struct cli_sapi_module = { "cli", /* name */ "Command Line Interface", /* pretty name */ php_cli_startup, /* startup */ php_module_shutdown_wrapper, /* shutdown */ NULL, /* activate */ sapi_cli_deactivate, /* deactivate */ sapi_cli_ub_write, /* unbuffered write */ sapi_cli_flush, /* flush */ NULL, /* get uid */ NULL, /* getenv */ php_error, /* error handler */ sapi_cli_header_handler, /* header handler */ sapi_cli_send_headers, /* send headers handler */ sapi_cli_send_header, /* send header handler */ NULL, /* read POST data */ sapi_cli_read_cookies, /* read Cookies */ sapi_cli_register_variables, /* register server variables */ sapi_cli_log_message, /* Log message */ NULL, /* Get request time */ NULL, /* Child terminate */ NULL, /* php_ini_path_override   */ \ NULL, /* default_post_reader     */ \ NULL, /* treat_data              */ \ NULL, /* executable_location     */ \ 0, /* php_ini_ignore          */ \ 0, /* php_ini_ignore_cwd      */ \ NULL, /* get_fd                  */ \ NULL, /* force_http_10           */ \ NULL, /* get_target_uid          */ \ NULL, /* get_target_gid          */ \ NULL, /* input_filter            */ \ NULL, /* ini_defaults            */ \ 0, /* phpinfo_as_text;        */ \ NULL, /* ini_entries;            */ \ NULL, /* additional_functions    */ \ NULL /* input_filter_init       */ };

有幾個(gè)點(diǎn)可以總結(jié):

cli模式是不需要發(fā)送header的,所以對(duì)應(yīng)header處理的三個(gè)函數(shù)

sapi_cli_header_handler sapi_cli_send_headers
sapi_cli_send_header

實(shí)際上都是空實(shí)現(xiàn)。

cookie也是同樣道理

sapi_cli_read_cookies

其他的一些定義的函數(shù),等到我們遇到的時(shí)候再分析吧。

main

回到main函數(shù),根據(jù)上面的那個(gè)結(jié)構(gòu),我們就理解了

argv = save_ps_args(argc, argv); //這里獲取一次當(dāng)前執(zhí)行進(jìn)程的參數(shù),環(huán)境變量等。為的是對(duì)特定平臺(tái),修正下argv變量以供后續(xù)使用。 cli_sapi_module.additional_functions = additional_functions; // cli模式特有的函數(shù)

signal

#ifdef HAVE_SIGNAL_H #if defined(SIGPIPE) && defined(SIG_IGN) // 忽略SIGPIPE是為了如果php是socket的客戶端,那么當(dāng)服務(wù)端關(guān)閉的話,會(huì)返回一個(gè)PIPE的信號(hào),為的是當(dāng)前的程序不會(huì)因?yàn)檫@個(gè)而結(jié)束 signal(SIGPIPE, SIG_IGN); #endif #endif