源码阅读:YAF框架系列文章(2) - (yaf.c)

@李彪  November 22, 2018

这个文件是yaf框架的整体组成框架文件,因为yaf的设计思路是模块化的,因此在这个文件中进行了总体整合。整合的思路分为以下几个部分。

  • INI配置文件的解析
  • 注册系统使用的常量
  • 注册各个模块
  • 对扩展模块的各个生命期做拦截监控
  • 注册扩展模块

一、INI配置文件解析功能

在php_yaf.h文件中对于全局变量进行了定义工作,随后在yaf.c中进行了声明工作,声明操作 ZEND_DECLARE_MODULE_GLOBALS(yaf); ,声明操作之后,并在PHP_GINIT_FUNCTION钩子函数期进行了全局变量的初始化工作,框架进行了配置文件的加载过程,配置文件的操作定义如下:

/** {{{ PHP_INI_MH(OnUpdateSeparator)
 */
PHP_INI_MH(OnUpdateSeparator) {
    YAF_G(name_separator) = ZSTR_VAL(new_value);
    YAF_G(name_separator_len) = ZSTR_LEN(new_value);
    return SUCCESS;
}
/* }}} */

/** {{{ PHP_INI
 */
PHP_INI_BEGIN()
    STD_PHP_INI_ENTRY("yaf.library",             "",  PHP_INI_ALL, OnUpdateString, global_library, zend_yaf_globals, yaf_globals)
    STD_PHP_INI_BOOLEAN("yaf.action_prefer",       "0", PHP_INI_ALL, OnUpdateBool, action_prefer, zend_yaf_globals, yaf_globals)
    STD_PHP_INI_BOOLEAN("yaf.lowcase_path",        "0", PHP_INI_ALL, OnUpdateBool, lowcase_path, zend_yaf_globals, yaf_globals)
    STD_PHP_INI_BOOLEAN("yaf.use_spl_autoload", "0", PHP_INI_ALL, OnUpdateBool, use_spl_autoload, zend_yaf_globals, yaf_globals)
    STD_PHP_INI_ENTRY("yaf.forward_limit",         "5", PHP_INI_ALL, OnUpdateLongGEZero, forward_limit, zend_yaf_globals, yaf_globals)
    STD_PHP_INI_BOOLEAN("yaf.name_suffix",         "1", PHP_INI_ALL, OnUpdateBool, name_suffix, zend_yaf_globals, yaf_globals)
    PHP_INI_ENTRY("yaf.name_separator",         "",  PHP_INI_ALL, OnUpdateSeparator)
/* {{{ This only effects internally */
    STD_PHP_INI_BOOLEAN("yaf.st_compatible",     "0", PHP_INI_ALL, OnUpdateBool, st_compatible, zend_yaf_globals, yaf_globals)
/* }}} */
    STD_PHP_INI_ENTRY("yaf.environ",            "product", PHP_INI_SYSTEM, OnUpdateString, environ_name, zend_yaf_globals, yaf_globals)
    STD_PHP_INI_BOOLEAN("yaf.use_namespace",       "0", PHP_INI_SYSTEM, OnUpdateBool, use_namespace, zend_yaf_globals, yaf_globals)
PHP_INI_END();
/* }}} */

这里对于yaf.name_separator参数的解析使用了自定义函数,对于INI文件解析的具体使用细节,可以参考文档:https://blog.icorer.com/index.php/archives/354/

二、注册INI配置选项和系统使用的常量

    REGISTER_INI_ENTRIES();

    if (YAF_G(use_namespace)) {

        REGISTER_STRINGL_CONSTANT("YAF\\VERSION", PHP_YAF_VERSION,     sizeof(PHP_YAF_VERSION) - 1, CONST_PERSISTENT | CONST_CS);
        REGISTER_STRINGL_CONSTANT("YAF\\ENVIRON", YAF_G(environ_name), strlen(YAF_G(environ_name)), CONST_PERSISTENT | CONST_CS);

        REGISTER_LONG_CONSTANT("YAF\\ERR\\STARTUP_FAILED",         YAF_ERR_STARTUP_FAILED, CONST_PERSISTENT | CONST_CS);
        REGISTER_LONG_CONSTANT("YAF\\ERR\\ROUTE_FAILED",         YAF_ERR_ROUTE_FAILED, CONST_PERSISTENT | CONST_CS);
        REGISTER_LONG_CONSTANT("YAF\\ERR\\DISPATCH_FAILED",     YAF_ERR_DISPATCH_FAILED, CONST_PERSISTENT | CONST_CS);
        REGISTER_LONG_CONSTANT("YAF\\ERR\\AUTOLOAD_FAILED",     YAF_ERR_AUTOLOAD_FAILED, CONST_PERSISTENT | CONST_CS);
        REGISTER_LONG_CONSTANT("YAF\\ERR\\NOTFOUND\\MODULE",     YAF_ERR_NOTFOUND_MODULE, CONST_PERSISTENT | CONST_CS);
        REGISTER_LONG_CONSTANT("YAF\\ERR\\NOTFOUND\\CONTROLLER",YAF_ERR_NOTFOUND_CONTROLLER, CONST_PERSISTENT | CONST_CS);
        REGISTER_LONG_CONSTANT("YAF\\ERR\\NOTFOUND\\ACTION",     YAF_ERR_NOTFOUND_ACTION, CONST_PERSISTENT | CONST_CS);
        REGISTER_LONG_CONSTANT("YAF\\ERR\\NOTFOUND\\VIEW",         YAF_ERR_NOTFOUND_VIEW, CONST_PERSISTENT | CONST_CS);
        REGISTER_LONG_CONSTANT("YAF\\ERR\\CALL_FAILED",            YAF_ERR_CALL_FAILED, CONST_PERSISTENT | CONST_CS);
        REGISTER_LONG_CONSTANT("YAF\\ERR\\TYPE_ERROR",            YAF_ERR_TYPE_ERROR, CONST_PERSISTENT | CONST_CS);

    } else {
        REGISTER_STRINGL_CONSTANT("YAF_VERSION", PHP_YAF_VERSION,     sizeof(PHP_YAF_VERSION) - 1,     CONST_PERSISTENT | CONST_CS);
        REGISTER_STRINGL_CONSTANT("YAF_ENVIRON", YAF_G(environ_name),strlen(YAF_G(environ_name)),     CONST_PERSISTENT | CONST_CS);

        REGISTER_LONG_CONSTANT("YAF_ERR_STARTUP_FAILED",         YAF_ERR_STARTUP_FAILED, CONST_PERSISTENT | CONST_CS);
        REGISTER_LONG_CONSTANT("YAF_ERR_ROUTE_FAILED",             YAF_ERR_ROUTE_FAILED, CONST_PERSISTENT | CONST_CS);
        REGISTER_LONG_CONSTANT("YAF_ERR_DISPATCH_FAILED",         YAF_ERR_DISPATCH_FAILED, CONST_PERSISTENT | CONST_CS);
        REGISTER_LONG_CONSTANT("YAF_ERR_AUTOLOAD_FAILED",         YAF_ERR_AUTOLOAD_FAILED, CONST_PERSISTENT | CONST_CS);
        REGISTER_LONG_CONSTANT("YAF_ERR_NOTFOUND_MODULE",         YAF_ERR_NOTFOUND_MODULE, CONST_PERSISTENT | CONST_CS);
        REGISTER_LONG_CONSTANT("YAF_ERR_NOTFOUND_CONTROLLER",     YAF_ERR_NOTFOUND_CONTROLLER, CONST_PERSISTENT | CONST_CS);
        REGISTER_LONG_CONSTANT("YAF_ERR_NOTFOUND_ACTION",         YAF_ERR_NOTFOUND_ACTION, CONST_PERSISTENT | CONST_CS);
        REGISTER_LONG_CONSTANT("YAF_ERR_NOTFOUND_VIEW",         YAF_ERR_NOTFOUND_VIEW, CONST_PERSISTENT | CONST_CS);
        REGISTER_LONG_CONSTANT("YAF_ERR_CALL_FAILED",            YAF_ERR_CALL_FAILED, CONST_PERSISTENT | CONST_CS);
        REGISTER_LONG_CONSTANT("YAF_ERR_TYPE_ERROR",            YAF_ERR_TYPE_ERROR, CONST_PERSISTENT | CONST_CS);
    }

三、启动各个系统组件

    YAF_STARTUP(application);
    YAF_STARTUP(bootstrap);
    YAF_STARTUP(dispatcher);
    YAF_STARTUP(loader);
    YAF_STARTUP(request);
    YAF_STARTUP(response);
    YAF_STARTUP(controller);
    YAF_STARTUP(action);
    YAF_STARTUP(config);
    YAF_STARTUP(view);
    YAF_STARTUP(router);
    YAF_STARTUP(plugin);
    YAF_STARTUP(registry);
    YAF_STARTUP(session);
    YAF_STARTUP(exception);

四、对于PHP函数的生命期进行监控

  • PHP_MINIT_FUNCTION
  • PHP_MSHUTDOWN_FUNCTION
  • PHP_RINIT_FUNCTION
  • PHP_RSHUTDOWN_FUNCTION
  • PHP_MINFO_FUNCTION

评论已关闭