yaf源碼閱讀之 -- 框架基本生命週期 (yaf.c)

MINIT

php-fpm啓動master時會啓動php擴展,在yaf源碼中,這裏對應的是MINIT及以前的操做,相關源碼位於yaf.c :php

  1. 讀取php.ini中的yaf配置
    在PHP_INI_BEGIN()和PHP_INI_END()之間定義相關參數的默認值、做用域、回調函數等bootstrap

  2. 在MINIT中定義YAF常量,如YAF_VERSION等session

  3. 在MINIT中載入yaf框架各個組件,自此YAF框架就常駐內存,yaf的快的優點也源於此。app

PHP_MINIT_FUNCTION(yaf)
{
	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);
	}

	/* startup components */
	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);

	return SUCCESS;
}

RINIT

請求初始化只是對yaf用戶應用級別的配置作了初始化框架

PHP_RINIT_FUNCTION(yaf)
{
	YAF_G(throw_exception) = 1;
	YAF_G(ext) = zend_string_init(YAF_DEFAULT_EXT, sizeof(YAF_DEFAULT_EXT) - 1, 0);
	YAF_G(view_ext) = zend_string_init(YAF_DEFAULT_VIEW_EXT, sizeof(YAF_DEFAULT_VIEW_EXT) - 1, 0);
	YAF_G(default_module) = zend_string_init(
			YAF_ROUTER_DEFAULT_MODULE, sizeof(YAF_ROUTER_DEFAULT_MODULE) - 1, 0);
	YAF_G(default_controller) = zend_string_init(
			YAF_ROUTER_DEFAULT_CONTROLLER, sizeof(YAF_ROUTER_DEFAULT_CONTROLLER) - 1, 0);
	YAF_G(default_action) = zend_string_init(
			YAF_ROUTER_DEFAULT_ACTION, sizeof(YAF_ROUTER_DEFAULT_ACTION) - 1, 0);
	return SUCCESS;
}

RSHUTDOWN

銷燬用戶應用級別的配置函數

PHP_RSHUTDOWN_FUNCTION(yaf)
{
	YAF_G(running) = 0;
	YAF_G(in_exception)	= 0;
	YAF_G(catch_exception) = 0;

	if (YAF_G(directory)) {
		zend_string_release(YAF_G(directory));
		YAF_G(directory) = NULL;
	}
	if (YAF_G(local_library)) {
		zend_string_release(YAF_G(local_library));
		YAF_G(local_library) = NULL;
	}
	if (YAF_G(local_namespaces)) {
		zend_string_release(YAF_G(local_namespaces));
		YAF_G(local_namespaces) = NULL;
	}
	if (YAF_G(bootstrap)) {
		zend_string_release(YAF_G(bootstrap));
		YAF_G(bootstrap) = NULL;
	}
	if (Z_TYPE(YAF_G(modules)) == IS_ARRAY) {
		zval_ptr_dtor(&YAF_G(modules));
		ZVAL_UNDEF(&YAF_G(modules));
	}
	if (YAF_G(base_uri)) {
		zend_string_release(YAF_G(base_uri));
		YAF_G(base_uri) = NULL;
	}
	if (YAF_G(view_directory)) {
		zend_string_release(YAF_G(view_directory));
		YAF_G(view_directory) = NULL;
	}
	if (YAF_G(view_ext)) {
		zend_string_release(YAF_G(view_ext));
	}
	if (YAF_G(default_module)) {
		zend_string_release(YAF_G(default_module));
	}
	if (YAF_G(default_controller)) {
		zend_string_release(YAF_G(default_controller));
	}
	if (YAF_G(default_action)) {
		zend_string_release(YAF_G(default_action));
	}
	if (YAF_G(ext)) {
		zend_string_release(YAF_G(ext));
	}
	YAF_G(default_route) = NULL;

	return SUCCESS;
}

MSHUTDOWN

銷燬yaf配置php-fpm

PHP_MSHUTDOWN_FUNCTION(yaf)
{
	UNREGISTER_INI_ENTRIES();

	if (YAF_G(configs)) {
		zend_hash_destroy(YAF_G(configs));
		pefree(YAF_G(configs), 1);
	}

	return SUCCESS;
}
相關文章
相關標籤/搜索