这里阅读的php版本为PHP-7.1.0 RC3,阅读代码的平台为linux
首先是寻找php的入口,php有很多种模式,apache,php-fpm, cli模式,我要入手的话,只能先从最简单的cli模型开始。
那么,我需要先寻找
php -r 'echo 12;'
这个命令是如何执行的。
首先还是寻找main入口,由于我们看的是命令行的php程序。所以,这个入口在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;
然后是这个
sapi_module_struct *sapi_module = &cli_sapi_module;
这是一个sapi_module_struct结构,这个结构是sapi中最重要的数据结构。它的定义在main/SAPI.h中。
下面是增加了注释的代码:
struct _sapi_module_struct {
char *name;
char *pretty_name;
int (*startup)(struct _sapi_module_struct *sapi_module);
int (*shutdown)(struct _sapi_module_struct *sapi_module);
int (*activate)(void);
int (*deactivate)(void);
size_t (*ub_write)(const char *str, size_t str_length);
void (*flush)(void *server_context);
zend_stat_t *(*get_stat)(void);
char *(*getenv)(char *name, size_t name_len);
void (*sapi_error)(int type, const char *error_msg, ...) ZEND_ATTRIBUTE_FORMAT(printf, 2, 3);
int (*header_handler)(sapi_header_struct *sapi_header, sapi_header_op_enum op, sapi_headers_struct *sapi_headers);
int (*send_headers)(sapi_headers_struct *sapi_headers);
void (*send_header)(sapi_header_struct *sapi_header, void *server_context);
size_t (*read_post)(char *buffer, size_t count_bytes);
char *(*read_cookies)(void);
void (*register_server_variables)(zval *track_vars_array);
void (*log_message)(char *message, int syslog_type_int);
double (*get_request_time)(void);
void (*terminate_process)(void);
char *php_ini_path_override;
void (*default_post_reader)(void);
void (*treat_data)(int arg, char *str, zval *destArray);
char *executable_location;
int php_ini_ignore;
int php_ini_ignore_cwd;
int (*get_fd)(int *fd);
int (*force_http_10)(void);
int (*get_target_uid)(uid_t *);
int (*get_target_gid)(gid_t *);
unsigned int (*input_filter)(int arg, char *var, char **val, size_t val_len, size_t *new_val_len);
void (*ini_defaults)(HashTable *configuration_hash);
int phpinfo_as_text;
char *ini_entries;
const zend_function_entry *additional_functions;
unsigned int (*input_filter_init)(void);
};
那么我们看下cli的SAPI的module是什么样子的呢?
其中我把里面原先有的STANDARD_SAPI_MODULE_PROPERTIES宏给解出来展示如下:
static sapi_module_struct cli_sapi_module = {
"cli",
"Command Line Interface",
php_cli_startup,
php_module_shutdown_wrapper,
NULL,
sapi_cli_deactivate,
sapi_cli_ub_write,
sapi_cli_flush,
NULL,
NULL,
php_error,
sapi_cli_header_handler,
sapi_cli_send_headers,
sapi_cli_send_header,
NULL,
sapi_cli_read_cookies,
sapi_cli_register_variables,
sapi_cli_log_message,
NULL,
NULL,
NULL, \
NULL, \
NULL, \
NULL, \
0, \
0, \
NULL, \
NULL, \
NULL, \
NULL, \
NULL, \
NULL, \
0, \
NULL, \
NULL, \
NULL
};
有几个点可以总结:
cli模式是不需要发送header的,所以对应header处理的三个函数
sapi_cli_header_handler
sapi_cli_send_headers
sapi_cli_send_header
实际上都是空实现。
cookie也是同样道理
sapi_cli_read_cookies
其他的一些定义的函数,等到我们遇到的时候再分析吧。
main
回到main函数,根据上面的那个结构,我们就理解了
argv = save_ps_args(argc, argv);
cli_sapi_module.additional_functions = additional_functions;
signal
#ifdef HAVE_SIGNAL_H
#if defined(SIGPIPE) && defined(SIG_IGN)
signal(SIGPIPE, SIG_IGN);
#endif
#endif 本文转自轩脉刃博客园博客,原文链接:http://www.cnblogs.com/yjf512/p/6084963.html,如需转载请自行联系原作者