1 Star 0 Fork 1

54leifeng / yaf

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
yaf_dispatcher.c 44.26 KB
一键复制 编辑 原始数据 按行查看 历史
Remi Collet 提交于 2017-06-25 08:03 . fix for PHP 7.2
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382
/*
+----------------------------------------------------------------------+
| Yet Another Framework |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Xinchen Hui <laruence@php.net> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "main/SAPI.h" /* for sapi_module */
#include "Zend/zend_interfaces.h" /* for zend_call_method_with_* */
#include "Zend/zend_exceptions.h" /* for zend_exception_get_default */
#include "php_yaf.h"
#include "yaf_namespace.h"
#include "yaf_dispatcher.h"
#include "yaf_controller.h"
#include "yaf_action.h"
#include "yaf_application.h"
#include "yaf_view.h"
#include "yaf_response.h"
#include "yaf_loader.h"
#include "yaf_router.h"
#include "yaf_request.h"
#include "yaf_config.h"
#include "yaf_plugin.h"
#include "yaf_exception.h"
zend_class_entry *yaf_dispatcher_ce;
/** {{{ ARG_INFO
*/
ZEND_BEGIN_ARG_INFO_EX(yaf_dispatcher_void_arginfo, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_dispatcher_dispatch_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, request)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_dispatcher_seterrhdler_arginfo, 0, 0, 2)
ZEND_ARG_INFO(0, callback)
ZEND_ARG_INFO(0, error_types)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_dispatcher_flush_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, flag)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_dispatcher_regplugin_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, plugin)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_dispatcher_setrequest_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, request)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_dispatcher_throwex_arginfo, 0, 0, 0)
ZEND_ARG_INFO(0, flag)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_dispatcher_catchex_arginfo, 0, 0, 0)
ZEND_ARG_INFO(0, flag)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_dispatcher_autorender_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, flag)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_dispatcher_returnresp_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, flag)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_dispatcher_initview_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, templates_dir)
ZEND_ARG_ARRAY_INFO(0, options, 1)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_dispatcher_setview_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, view)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_dispatcher_setctrl_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, controller)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_dispatcher_setmodule_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, module)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(yaf_dispatcher_setaction_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, action)
ZEND_END_ARG_INFO()
/* }}} */
yaf_dispatcher_t *yaf_dispatcher_instance(yaf_dispatcher_t *this_ptr) /* {{{ */ {
zval plugins;
yaf_router_t *router, rv = {{0}};
yaf_dispatcher_t *instance;
instance = zend_read_static_property(yaf_dispatcher_ce, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_INSTANCE), 1);
if (IS_OBJECT == Z_TYPE_P(instance)
&& instanceof_function(Z_OBJCE_P(instance), yaf_dispatcher_ce)) {
return instance;
}
if (Z_ISUNDEF_P(this_ptr)) {
object_init_ex(this_ptr, yaf_dispatcher_ce);
} else {
return this_ptr;
}
array_init(&plugins);
zend_update_property(yaf_dispatcher_ce, this_ptr, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_PLUGINS), &plugins);
zval_ptr_dtor(&plugins);
router = yaf_router_instance(&rv);
zend_update_property(yaf_dispatcher_ce, this_ptr, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_ROUTER), router);
zval_ptr_dtor(router);
zend_update_property_str(yaf_dispatcher_ce,
this_ptr, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_MODULE), YAF_G(default_module));
zend_update_property_str(yaf_dispatcher_ce,
this_ptr, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_CONTROLLER), YAF_G(default_controller));
zend_update_property_str(yaf_dispatcher_ce,
this_ptr, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_ACTION), YAF_G(default_action));
zend_update_static_property(yaf_dispatcher_ce, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_INSTANCE), this_ptr);
return this_ptr;
}
/* }}} */
static void yaf_dispatcher_get_call_parameters(zend_class_entry *request_ce, yaf_request_t *request, zend_function *fptr, zval **params, uint *count) /* {{{ */ {
zval *args, *arg;
zend_arg_info *arg_info;
uint current;
HashTable *params_ht;
args = zend_read_property(request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_PARAMS), 1, NULL);
params_ht = Z_ARRVAL_P(args);
arg_info = fptr->common.arg_info;
*params = safe_emalloc(sizeof(zval), fptr->common.num_args, 0);
for (current = 0; current < fptr->common.num_args; current++, arg_info++) {
if ((arg = zend_hash_find(params_ht, arg_info->name)) != NULL) {
ZVAL_COPY_VALUE(&((*params)[current]), arg);
(*count)++;
} else {
zend_string *key;
arg = NULL;
/* since we need search ignoring case, can't use zend_hash_find */
ZEND_HASH_FOREACH_STR_KEY_VAL(params_ht, key, arg) {
if (key) {
if (zend_string_equals(key, arg_info->name)) {
/* return when we find first match, there is a trap
* when multi different parameters in different case presenting in params_ht
* only the first take affect
*/
ZVAL_COPY_VALUE(&((*params)[current]), arg);
(*count)++;
break;
}
}
} ZEND_HASH_FOREACH_END();
if (NULL == arg) {
break;
}
}
}
}
/* }}} */
static yaf_view_t *yaf_dispatcher_init_view(yaf_dispatcher_t *dispatcher, zval *tpl_dir, zval *options, yaf_view_t *ptr) /* {{{ */ {
yaf_view_t *view = zend_read_property(yaf_dispatcher_ce,
dispatcher, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_VIEW), 1, NULL);
if (EXPECTED(IS_OBJECT == Z_TYPE_P(view)
&& instanceof_function(Z_OBJCE_P(view), yaf_view_interface_ce))) {
return view;
}
view = yaf_view_instance(ptr, tpl_dir, options);
if (!view) {
return NULL;
}
zend_update_property(yaf_dispatcher_ce, dispatcher, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_VIEW), view);
zval_ptr_dtor(view);
return view;
}
/* }}} */
static inline void yaf_dispatcher_fix_default(yaf_dispatcher_t *dispatcher, yaf_request_t *request) /* {{{ */ {
zval *module, *controller, *action;
module = zend_read_property(yaf_request_ce,
request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_MODULE), 1, NULL);
action = zend_read_property(yaf_request_ce,
request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_ACTION), 1, NULL);
controller = zend_read_property(yaf_request_ce, request,
ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_CONTROLLER), 1, NULL);
if (Z_TYPE_P(module) != IS_STRING || !Z_STRLEN_P(module)) {
zval *default_module = zend_read_property(yaf_dispatcher_ce,
dispatcher, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_MODULE), 1, NULL);
zend_update_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_MODULE), default_module);
} else {
char *p = zend_str_tolower_dup(Z_STRVAL_P(module), Z_STRLEN_P(module));
*p = toupper(*p);
zend_update_property_stringl(yaf_request_ce,
request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_MODULE), p, Z_STRLEN_P(module));
efree(p);
}
if (Z_TYPE_P(controller) != IS_STRING || !Z_STRLEN_P(controller)) {
zval *default_controller = zend_read_property(yaf_dispatcher_ce,
dispatcher, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_CONTROLLER), 1, NULL);
zend_update_property(yaf_request_ce,
request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_CONTROLLER), default_controller);
} else {
char *q, *p = zend_str_tolower_dup(Z_STRVAL_P(controller), Z_STRLEN_P(controller));
/**
* Upper controller name
* eg: Index_sub -> Index_Sub
*/
q = p;
*q = toupper(*q);
while (*q != '\0') {
if (*q == '_' || *q == '\\') {
if (*(q+1) != '\0') {
*(q+1) = toupper(*(q+1));
q++;
}
}
q++;
}
zend_update_property_stringl(yaf_request_ce, request,
ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_CONTROLLER), p, Z_STRLEN_P(controller));
efree(p);
}
if (Z_TYPE_P(action) != IS_STRING || !Z_STRLEN_P(action)) {
zval *default_action = zend_read_property(yaf_dispatcher_ce,
dispatcher, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_ACTION), 1, NULL);
zend_update_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_ACTION), default_action);
} else {
char *p = zend_str_tolower_dup(Z_STRVAL_P(action), Z_STRLEN_P(action));
zend_update_property_stringl(yaf_request_ce,
request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_ACTION), p, Z_STRLEN_P(action));
efree(p);
}
}
/* }}} */
int yaf_dispatcher_set_request(yaf_dispatcher_t *dispatcher, yaf_request_t *request) /* {{{ */ {
if (request) {
zend_update_property(yaf_dispatcher_ce,
dispatcher, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_REQUEST), request);
return 1;
}
return 0;
}
/* }}} */
zend_class_entry *yaf_dispatcher_get_controller(zend_string *app_dir, zend_string *module, zend_string *controller, int def_module) /* {{{ */ {
char *directory;
size_t directory_len;
if (def_module) {
directory_len = spprintf(&directory, 0,
"%s%c%s", ZSTR_VAL(app_dir), DEFAULT_SLASH, YAF_CONTROLLER_DIRECTORY_NAME);
} else {
directory_len = spprintf(&directory, 0,
"%s%c%s%c%s%c%s", ZSTR_VAL(app_dir), DEFAULT_SLASH, YAF_MODULE_DIRECTORY_NAME,
DEFAULT_SLASH, ZSTR_VAL(module), DEFAULT_SLASH, YAF_CONTROLLER_DIRECTORY_NAME);
}
if (EXPECTED(directory_len)) {
zend_string *class;
zend_string *class_lowercase;
zend_class_entry *ce = NULL;
if (YAF_G(name_suffix)) {
class = strpprintf(0, "%s%s%s", ZSTR_VAL(controller), YAF_G(name_separator), "Controller");
} else {
class = strpprintf(0, "%s%s%s", "Controller", YAF_G(name_separator), ZSTR_VAL(controller));
}
class_lowercase = zend_string_tolower(class);
if ((ce = zend_hash_find_ptr(EG(class_table), class_lowercase)) == NULL) {
if (!yaf_internal_autoload(ZSTR_VAL(controller), ZSTR_LEN(controller), &directory)) {
yaf_trigger_error(YAF_ERR_NOTFOUND_CONTROLLER,
"Failed opening controller script %s: %s", directory, strerror(errno));
zend_string_release(class);
zend_string_release(class_lowercase);
efree(directory);
return NULL;
} else if ((ce = zend_hash_find_ptr(EG(class_table), class_lowercase)) == NULL) {
yaf_trigger_error(YAF_ERR_AUTOLOAD_FAILED,
"Could not find class %s in controller script %s", ZSTR_VAL(class), directory);
zend_string_release(class);
zend_string_release(class_lowercase);
efree(directory);
return 0;
} else if (!instanceof_function(ce, yaf_controller_ce)) {
yaf_trigger_error(YAF_ERR_TYPE_ERROR,
"Controller must be an instance of %s", ZSTR_VAL(yaf_controller_ce->name));
zend_string_release(class);
zend_string_release(class_lowercase);
efree(directory);
return 0;
}
}
zend_string_release(class);
zend_string_release(class_lowercase);
efree(directory);
return ce;
}
return NULL;
}
/* }}} */
zend_class_entry *yaf_dispatcher_get_action(zend_string *app_dir, yaf_controller_t *controller, char *module, int def_module, zend_string *action) /* {{{ */ {
zval *paction, *actions_map;
actions_map = zend_read_property(Z_OBJCE_P(controller),
controller, ZEND_STRL(YAF_CONTROLLER_PROPERTY_NAME_ACTIONS), 1, NULL);
if (EXPECTED(IS_ARRAY == Z_TYPE_P(actions_map))) {
zend_class_entry *ce;
zend_string *class;
zend_string *class_lowercase;
char *action_upper = estrndup(ZSTR_VAL(action), ZSTR_LEN(action));
*(action_upper) = toupper(*action_upper);
if (YAF_G(name_suffix)) {
class = strpprintf(0, "%s%s%s", action_upper, YAF_G(name_separator), "Action");
} else {
class = strpprintf(0, "%s%s%s", "Action", YAF_G(name_separator), action_upper);
}
class_lowercase = zend_string_tolower(class);
if ((ce = zend_hash_find_ptr(EG(class_table), class_lowercase)) != NULL) {
efree(action_upper);
zend_string_release(class_lowercase);
if (instanceof_function(ce, yaf_action_ce)) {
zend_string_release(class);
return ce;
} else {
yaf_trigger_error(YAF_ERR_TYPE_ERROR,
"Action %s must extends from %s", ZSTR_VAL(class), ZSTR_VAL(yaf_action_ce->name));
zend_string_release(class);
return NULL;
}
}
if ((paction = zend_hash_find(Z_ARRVAL_P(actions_map), action)) != NULL) {
zend_string *action_path;
action_path = strpprintf(0, "%s%c%s", ZSTR_VAL(app_dir), DEFAULT_SLASH, Z_STRVAL_P(paction));
if (yaf_loader_import(action_path, 0)) {
if ((ce = zend_hash_find_ptr(EG(class_table), class_lowercase)) != NULL) {
zend_string_release(action_path);
efree(action_upper);
zend_string_release(class_lowercase);
if (instanceof_function(ce, yaf_action_ce)) {
zend_string_release(class);
return ce;
} else {
yaf_trigger_error(YAF_ERR_TYPE_ERROR,
"Action %s must extends from %s", ZSTR_VAL(class), ZSTR_VAL(yaf_action_ce->name));
zend_string_release(class);
}
} else {
yaf_trigger_error(YAF_ERR_NOTFOUND_ACTION,
"Could not find action %s in %s", ZSTR_VAL(class), ZSTR_VAL(action_path));
}
zend_string_release(action_path);
efree(action_upper);
zend_string_release(class);
zend_string_release(class_lowercase);
} else {
yaf_trigger_error(YAF_ERR_NOTFOUND_ACTION,
"Failed opening action script %s: %s", ZSTR_VAL(action_path), strerror(errno));
zend_string_release(action_path);
}
} else {
yaf_trigger_error(YAF_ERR_NOTFOUND_ACTION, "There is no method %s%s in %s::$%s", ZSTR_VAL(action),
"Action", ZSTR_VAL(Z_OBJCE_P(controller)->name), YAF_CONTROLLER_PROPERTY_NAME_ACTIONS);
}
} else
/* {{{ This only effects internally */
if (YAF_G(st_compatible)) {
char *directory, *class, *class_lowercase, *p;
uint class_len;
zend_class_entry *ce;
char *action_upper = estrndup(ZSTR_VAL(action), ZSTR_LEN(action));
/**
* upper Action Name
* eg: Index_sub -> Index_Sub
*/
p = action_upper;
*(p) = toupper(*p);
while (*p != '\0') {
if (*p == '_' || *p == '\\') {
if (*(p+1) != '\0') {
*(p+1) = toupper(*(p+1));
p++;
}
}
p++;
}
if (def_module) {
spprintf(&directory, 0, "%s%c%s", ZSTR_VAL(app_dir), DEFAULT_SLASH, "actions");
} else {
spprintf(&directory, 0, "%s%c%s%c%s%c%s", ZSTR_VAL(app_dir), DEFAULT_SLASH,
"modules", DEFAULT_SLASH, module, DEFAULT_SLASH, "actions");
}
if (YAF_G(name_suffix)) {
class_len = spprintf(&class, 0, "%s%s%s", action_upper, YAF_G(name_separator), "Action");
} else {
class_len = spprintf(&class, 0, "%s%s%s", "Action", YAF_G(name_separator), action_upper);
}
class_lowercase = zend_str_tolower_dup(class, class_len);
if ((ce = zend_hash_str_find_ptr(EG(class_table), class_lowercase, class_len)) == NULL) {
if (!yaf_internal_autoload(action_upper, ZSTR_LEN(action), &directory)) {
yaf_trigger_error(YAF_ERR_NOTFOUND_ACTION, "Failed opening action script %s: %s", directory, strerror(errno));
efree(class);
efree(action_upper);
efree(class_lowercase);
efree(directory);
return NULL;
} else if ((ce = zend_hash_str_find_ptr(EG(class_table), class_lowercase, class_len)) == NULL) {
yaf_trigger_error(YAF_ERR_AUTOLOAD_FAILED, "Could find class %s in action script %s", class, directory);
efree(class);
efree(action_upper);
efree(class_lowercase);
efree(directory);
return NULL;
} else if (!instanceof_function(ce, yaf_action_ce)) {
yaf_trigger_error(YAF_ERR_TYPE_ERROR, "Action must be an instance of %s", ZSTR_VAL(yaf_action_ce->name));
efree(class);
efree(action_upper);
efree(class_lowercase);
efree(directory);
return NULL;
}
}
efree(class);
efree(action_upper);
efree(class_lowercase);
efree(directory);
return ce;
} else
/* }}} */
{
yaf_trigger_error(YAF_ERR_NOTFOUND_ACTION,
"There is no method %s%s in %s", ZSTR_VAL(action), "Action", ZSTR_VAL(Z_OBJCE_P(controller)->name));
}
return NULL;
}
/* }}} */
int yaf_dispatcher_handle(yaf_dispatcher_t *dispatcher, yaf_request_t *request, yaf_response_t *response, yaf_view_t *view) /* {{{ */ {
zend_class_entry *request_ce;
zend_string *app_dir = YAF_G(directory);
request_ce = Z_OBJCE_P(request);
yaf_request_set_dispatched(request, 1);
if (!app_dir) {
yaf_trigger_error(YAF_ERR_STARTUP_FAILED,
"%s requires %s(which set the application.directory) to be initialized first",
ZSTR_VAL(yaf_dispatcher_ce->name), ZSTR_VAL(yaf_application_ce->name));
return 0;
} else {
int is_def_module = 0;
zval *module, *controller, *dmodule, *instantly_flush;
zend_class_entry *ce;
yaf_controller_t *executor;
zend_function *fptr;
module = zend_read_property(request_ce,
request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_MODULE), 1, NULL);
controller = zend_read_property(request_ce,
request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_CONTROLLER), 1, NULL);
dmodule = zend_read_property(yaf_dispatcher_ce,
dispatcher, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_MODULE), 1, NULL);
if (UNEXPECTED(Z_TYPE_P(module) != IS_STRING || !Z_STRLEN_P(module))) {
yaf_trigger_error(YAF_ERR_DISPATCH_FAILED, "Unexcepted a empty module name");
return 0;
} else if (UNEXPECTED(!yaf_application_is_module_name(Z_STR_P(module)))) {
yaf_trigger_error(YAF_ERR_NOTFOUND_MODULE, "There is no module %s", Z_STRVAL_P(module));
return 0;
}
if (UNEXPECTED(Z_TYPE_P(controller) != IS_STRING || !Z_STRLEN_P(controller))) {
yaf_trigger_error(YAF_ERR_DISPATCH_FAILED, "Unexcepted a empty controller name");
return 0;
}
if (zend_string_equals(Z_STR_P(dmodule), Z_STR_P(module))) {
is_def_module = 1;
}
ce = yaf_dispatcher_get_controller(app_dir, Z_STR_P(module), Z_STR_P(controller), is_def_module);
if (!ce) {
return 0;
} else {
zval ret;
zval action, *render;
zend_string *view_dir;
zend_string *func_name;
yaf_controller_t icontroller;
object_init_ex(&icontroller, ce);
yaf_controller_construct(ce, &icontroller, request, response, view, NULL);
if (EG(exception)) {
zval_ptr_dtor(&icontroller);
return 0;
}
if (!yaf_request_is_dispatched(request)) {
/* forward is called in init method */
zval_ptr_dtor(&icontroller);
return yaf_dispatcher_handle(dispatcher, request, response, view TSRMLS_CC);
}
/* view template directory for application, please notice that view engine's directory has high priority */
if (is_def_module) {
view_dir = strpprintf(0, "%s%c%s", ZSTR_VAL(app_dir), DEFAULT_SLASH, "views");
} else {
view_dir = strpprintf(0, "%s%c%s%c%s%c%s", ZSTR_VAL(app_dir),
DEFAULT_SLASH, "modules", DEFAULT_SLASH, Z_STRVAL_P(module), DEFAULT_SLASH, "views");
}
if (YAF_G(view_directory)) {
zend_string_release(YAF_G(view_directory));
}
YAF_G(view_directory) = view_dir;
zend_update_property(ce, &icontroller, ZEND_STRL(YAF_CONTROLLER_PROPERTY_NAME_NAME), controller);
ZVAL_COPY(&action,
zend_read_property(request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_ACTION), 1, NULL));
func_name = strpprintf(0, "%s%s", Z_STRVAL(action), "action");
zend_str_tolower(ZSTR_VAL(func_name), ZSTR_LEN(func_name));
/* @TODO: Magic __call supports? */
if ((fptr = zend_hash_find_ptr(&((ce)->function_table), func_name)) != NULL) {
uint count = 0;
zval *call_args = NULL;
executor = &icontroller;
if (fptr->common.num_args) {
zval method_name;
yaf_dispatcher_get_call_parameters(request_ce, request, fptr, &call_args, &count);
ZVAL_STR(&method_name, func_name);
call_user_function_ex(&(ce)->function_table,
&icontroller, &method_name, &ret, count, call_args, 1, NULL);
efree(call_args);
} else {
zend_call_method(&icontroller, ce,
NULL, ZSTR_VAL(func_name), ZSTR_LEN(func_name), &ret, 0, NULL, NULL);
}
zend_string_release(func_name);
if (Z_ISUNDEF(ret)) {
zval_ptr_dtor(&action);
zval_ptr_dtor(executor);
return 0;
}
if ((Z_TYPE(ret) == IS_FALSE)) {
/* no auto-renderring */
zval_ptr_dtor(&action);
zval_ptr_dtor(executor);
return 1;
}
zval_ptr_dtor(&ret);
} else if ((ce = yaf_dispatcher_get_action(app_dir,
&icontroller, Z_STRVAL_P(module), is_def_module, Z_STR(action))) &&
(fptr = zend_hash_str_find_ptr(&(ce->function_table),
YAF_ACTION_EXECUTOR_NAME, sizeof(YAF_ACTION_EXECUTOR_NAME) - 1))) {
zval *call_args;
yaf_action_t iaction;
uint count = 0;
zend_string_release(func_name);
object_init_ex(&iaction, ce);
yaf_controller_construct(ce, &iaction, request, response, view, NULL);
executor = &iaction;
zend_update_property(ce, &iaction, ZEND_STRL(YAF_CONTROLLER_PROPERTY_NAME_NAME), controller);
zend_update_property(ce, &iaction, ZEND_STRL(YAF_ACTION_PROPERTY_NAME_CTRL), &icontroller);
zval_ptr_dtor(&icontroller);
if (fptr->common.num_args) {
zval method_name;
yaf_dispatcher_get_call_parameters(request_ce, request, fptr, &call_args, &count);
ZVAL_STRINGL(&method_name, YAF_ACTION_EXECUTOR_NAME, sizeof(YAF_ACTION_EXECUTOR_NAME) - 1);
call_user_function_ex(&(ce)->function_table,
&iaction, &method_name, &ret, count, call_args, 1, NULL);
zval_ptr_dtor(&method_name);
efree(call_args);
} else {
zend_call_method_with_0_params(&iaction, ce, NULL, "execute", &ret);
}
if (Z_ISUNDEF(ret)) {
zval_ptr_dtor(&action);
zval_ptr_dtor(&iaction);
return 0;
}
if ((Z_TYPE(ret) == IS_FALSE)) {
/* no auto-renderring */
zval_ptr_dtor(&action);
zval_ptr_dtor(&iaction);
return 1;
}
zval_ptr_dtor(&ret);
} else {
zend_string_release(func_name);
zval_ptr_dtor(&icontroller);
return 0;
}
if (executor) {
/* controller's property can override the Dispatcher's */
zval rv;
int auto_render;
render = zend_read_property(ce, executor, ZEND_STRL(YAF_CONTROLLER_PROPERTY_NAME_RENDER), 1, &rv);
if (render == &EG(uninitialized_zval)) {
render = zend_read_property(yaf_dispatcher_ce,
dispatcher, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_RENDER), 1, NULL);
}
auto_render = (Z_TYPE_P(render) == IS_TRUE || (Z_TYPE_P(render) == IS_LONG && Z_LVAL_P(render)));
instantly_flush = zend_read_property(yaf_dispatcher_ce,
dispatcher, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_FLUSH), 1, NULL);
if (auto_render) {
if (Z_TYPE_P(instantly_flush) == IS_FALSE) {
zend_call_method_with_1_params(executor, ce, NULL, "render", &ret, &action);
zval_ptr_dtor(executor);
if (UNEXPECTED(Z_ISUNDEF(ret))) {
zval_ptr_dtor(&action);
return 0;
} else if (IS_FALSE == Z_TYPE(ret)) {
zval_ptr_dtor(&action);
return 0;
}
if (Z_TYPE(ret) == IS_STRING && Z_STRLEN(ret)) {
yaf_response_alter_body(response, NULL, Z_STR(ret), YAF_RESPONSE_APPEND );
}
zval_ptr_dtor(&ret);
} else {
zend_call_method_with_1_params(executor, ce, NULL, "display", &ret, &action);
zval_ptr_dtor(executor);
if (UNEXPECTED(Z_ISUNDEF(ret))) {
zval_ptr_dtor(&action);
return 0;
}
if ((Z_TYPE(ret) == IS_FALSE)) {
zval_ptr_dtor(&action);
return 0;
}
zval_ptr_dtor(&ret);
}
} else {
zval_ptr_dtor(executor);
}
}
zval_ptr_dtor(&action);
}
return 1;
}
return 0;
}
/* }}} */
void yaf_dispatcher_exception_handler(yaf_dispatcher_t *dispatcher, yaf_request_t *request, yaf_response_t *response) /* {{{ */ {
zval *module, controller, action, exception;
yaf_view_t *view, rv = {{0}};
zend_string *exception_str;
const zend_op *opline;
if (YAF_G(in_exception) || !EG(exception)) {
return;
}
YAF_G(in_exception) = 1;
module = zend_read_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_MODULE), 1, NULL);
if (Z_TYPE_P(module) != IS_STRING || !Z_STRLEN_P(module)) {
module = zend_read_property(yaf_dispatcher_ce,
dispatcher, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_MODULE), 1, NULL);
zend_update_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_MODULE), module);
}
ZVAL_STRING(&controller, YAF_ERROR_CONTROLLER);
ZVAL_STRING(&action, YAF_ERROR_ACTION);
ZVAL_OBJ(&exception, EG(exception));
EG(exception) = NULL;
opline = EG(opline_before_exception);
#if ZEND_DEBUG
EG(opline_before_exception) = NULL;
#endif
zend_update_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_CONTROLLER), &controller);
zend_update_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_ACTION), &action);
zend_update_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_EXCEPTION), &exception);
zval_ptr_dtor(&controller);
zval_ptr_dtor(&action);
/** use $request->getException() instand of */
exception_str = zend_string_init(ZEND_STRL("exception"), 0);
if (yaf_request_set_params_single(request, exception_str, &exception)) {
zend_string_release(exception_str);
zval_ptr_dtor(&exception);
} else {
/* failover to uncaught exception */
zend_string_release(exception_str);
EG(exception) = Z_OBJ(exception);
return;
}
yaf_request_set_dispatched(request, 0);
view = yaf_dispatcher_init_view(dispatcher, NULL, NULL, &rv);
if (UNEXPECTED(!view)) {
return;
}
if (!yaf_dispatcher_handle(dispatcher, request, response, view)) {
if (EG(exception) &&
instanceof_function(EG(exception)->ce,
yaf_buildin_exceptions[YAF_EXCEPTION_OFFSET(YAF_ERR_NOTFOUND_CONTROLLER)])) {
zval *m = zend_read_property(yaf_dispatcher_ce,
dispatcher, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_MODULE), 1, NULL);
/* failover to default module error catcher */
zend_update_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_MODULE), m);
EG(exception) = NULL;
(void)yaf_dispatcher_handle(dispatcher, request, response, view);
}
}
(void)yaf_response_send(response);
EG(opline_before_exception) = opline;
YAF_EXCEPTION_ERASE_EXCEPTION();
}
/* }}} */
int yaf_dispatcher_route(yaf_dispatcher_t *dispatcher, yaf_request_t *request) /* {{{ */ {
zval ret;
zend_class_entry *router_ce;
yaf_router_t *router = zend_read_property(yaf_dispatcher_ce,
dispatcher, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_ROUTER), 1, NULL);
if (EXPECTED(IS_OBJECT == Z_TYPE_P(router))) {
if (EXPECTED((router_ce = Z_OBJCE_P(router)) == yaf_router_ce)) {
/* use built-in route */
if (yaf_router_route(router, request)) {
return 1;
}
} else {
/* user custom route */
zend_call_method_with_1_params(router, router_ce, NULL, "route", &ret, request);
if (Z_TYPE(ret) != IS_TRUE) {
yaf_trigger_error(YAF_ERR_ROUTE_FAILED, "Routing request faild");
zval_ptr_dtor(&ret);
return 0;
}
zval_ptr_dtor(&ret);
}
return 1;
}
return 0;
}
/* }}} */
yaf_response_t *yaf_dispatcher_dispatch(yaf_dispatcher_t *dispatcher, zval *response_ptr) /* {{{ */ {
zval *return_response, *plugins, *view, rv;
yaf_response_t *response;
yaf_request_t *request;
uint nesting = YAF_G(forward_limit);
response = yaf_response_instance(response_ptr, sapi_module.name);
request = zend_read_property(yaf_dispatcher_ce,
dispatcher, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_REQUEST), 1, NULL);
plugins = zend_read_property(yaf_dispatcher_ce,
dispatcher, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_PLUGINS), 1, NULL);
if (UNEXPECTED(IS_OBJECT != Z_TYPE_P(request))) {
yaf_trigger_error(YAF_ERR_TYPE_ERROR, "Expect a %s instance", ZSTR_VAL(yaf_request_ce->name));
zval_ptr_dtor(response);
return NULL;
}
/* route request */
if (!yaf_request_is_routed(request)) {
YAF_PLUGIN_HANDLE(plugins, YAF_PLUGIN_HOOK_ROUTESTARTUP, request, response);
YAF_EXCEPTION_HANDLE(dispatcher, request, response);
if (!yaf_dispatcher_route(dispatcher, request)) {
yaf_trigger_error(YAF_ERR_ROUTE_FAILED, "Routing request failed");
YAF_EXCEPTION_HANDLE_NORET(dispatcher, request, response);
zval_ptr_dtor(response);
return NULL;
}
yaf_dispatcher_fix_default(dispatcher, request);
YAF_PLUGIN_HANDLE(plugins, YAF_PLUGIN_HOOK_ROUTESHUTDOWN, request, response);
YAF_EXCEPTION_HANDLE(dispatcher, request, response);
(void)yaf_request_set_routed(request, 1);
} else {
yaf_dispatcher_fix_default(dispatcher, request);
}
YAF_PLUGIN_HANDLE(plugins, YAF_PLUGIN_HOOK_LOOPSTARTUP, request, response);
YAF_EXCEPTION_HANDLE(dispatcher, request, response);
ZVAL_UNDEF(&rv);
view = yaf_dispatcher_init_view(dispatcher, NULL, NULL, &rv);
if (!view) {
return NULL;
}
do {
YAF_PLUGIN_HANDLE(plugins, YAF_PLUGIN_HOOK_PREDISPATCH, request, response);
YAF_EXCEPTION_HANDLE(dispatcher, request, response);
if (!yaf_dispatcher_handle(dispatcher, request, response, view)) {
YAF_EXCEPTION_HANDLE(dispatcher, request, response);
zval_ptr_dtor(response);
return NULL;
}
yaf_dispatcher_fix_default(dispatcher, request);
YAF_PLUGIN_HANDLE(plugins, YAF_PLUGIN_HOOK_POSTDISPATCH, request, response);
YAF_EXCEPTION_HANDLE(dispatcher, request, response);
} while (--nesting > 0 && !yaf_request_is_dispatched(request));
YAF_PLUGIN_HANDLE(plugins, YAF_PLUGIN_HOOK_LOOPSHUTDOWN, request, response);
YAF_EXCEPTION_HANDLE(dispatcher, request, response);
if (0 == nesting && !yaf_request_is_dispatched(request)) {
yaf_trigger_error(YAF_ERR_DISPATCH_FAILED, "The max dispatch nesting %ld was reached", YAF_G(forward_limit));
YAF_EXCEPTION_HANDLE_NORET(dispatcher, request, response);
zval_ptr_dtor(response);
return NULL;
}
return_response = zend_read_property(yaf_dispatcher_ce,
dispatcher, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_RETURN), 1, NULL);
if (Z_TYPE_P(return_response) == IS_FALSE) {
zval ret;
zend_call_method_with_0_params(response, Z_OBJCE_P(response), NULL, "response", &ret);
/* it must return bool */
/* zval_ptr_dtor(&ret); */
yaf_response_clear_body(response, NULL);
}
return response;
}
/* }}} */
/** {{{ proto private Yaf_Dispatcher::__construct(void)
*/
PHP_METHOD(yaf_dispatcher, __construct) {
}
/* }}} */
/** {{{ proto private Yaf_Dispatcher::__sleep(void)
*/
PHP_METHOD(yaf_dispatcher, __sleep) {
}
/* }}} */
/** {{{ proto private Yaf_Dispatcher::__wakeup(void)
*/
PHP_METHOD(yaf_dispatcher, __wakeup) {
}
/* }}} */
/** {{{ proto public Yaf_Dispatcher::setErrorHandler(string $callbacak[, int $error_types = E_ALL | E_STRICT ] )
*/
PHP_METHOD(yaf_dispatcher, setErrorHandler) {
zval *callback, *error_type = NULL;
zval params[2];
zval function = {{0}};
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|z", &callback, &error_type) == FAILURE) {
return;
}
ZVAL_COPY(&params[0], callback);
if (error_type) {
ZVAL_COPY(&params[1], error_type);
}
ZVAL_STRING(&function, "set_error_handler");
if (call_user_function(EG(function_table), NULL, &function, return_value, ZEND_NUM_ARGS(), params) == FAILURE) {
zval_ptr_dtor(return_value);
zval_ptr_dtor(&params[0]);
if (error_type) {
zval_ptr_dtor(&params[1]);
}
zval_ptr_dtor(&function);
php_error_docref(NULL, E_WARNING, "Call to set_error_handler failed");
RETURN_FALSE;
}
zval_ptr_dtor(return_value);
zval_ptr_dtor(&function);
zval_ptr_dtor(&params[0]);
if (error_type) {
zval_ptr_dtor(&params[1]);
}
RETURN_ZVAL(getThis(), 1, 0);
}
/* }}} */
/** {{{ proto public Yaf_Dispatcher::disableView()
*/
PHP_METHOD(yaf_dispatcher, disableView) {
yaf_dispatcher_t *self = getThis();
zend_update_property_bool(yaf_dispatcher_ce, self, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_RENDER), 0);
RETURN_ZVAL(self, 1, 0);
}
/* }}} */
/** {{{ proto public Yaf_Dispatcher::enableView()
*/
PHP_METHOD(yaf_dispatcher, enableView) {
yaf_dispatcher_t *self = getThis();
zend_update_property_bool(yaf_dispatcher_ce, self, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_RENDER), 1);
RETURN_ZVAL(self, 1, 0);
}
/* }}} */
/** {{{ proto public Yaf_Dispatcher::returnResponse()
*/
PHP_METHOD(yaf_dispatcher, returnResponse) {
zend_bool auto_response;
yaf_dispatcher_t *self = getThis();
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &auto_response) == FAILURE) {
return;
}
if (ZEND_NUM_ARGS()) {
zend_update_property_bool(yaf_dispatcher_ce,
self, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_RETURN), auto_response);
RETURN_ZVAL(self, 1, 0);
} else {
RETURN_BOOL(Z_TYPE_P(zend_read_property(yaf_dispatcher_ce,
self, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_RETURN), 1, NULL)) == IS_TRUE ? 1 : 0);
}
}
/* }}} */
/** {{{ proto public Yaf_Dispatcher::flushInstantly(bool $flag)
*/
PHP_METHOD(yaf_dispatcher, flushInstantly) {
zend_bool instantly_flush;
yaf_dispatcher_t *self = getThis();
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &instantly_flush) == FAILURE) {
return;
}
if (ZEND_NUM_ARGS()) {
zend_update_property_bool(yaf_dispatcher_ce,
self, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_FLUSH), instantly_flush);
RETURN_ZVAL(self, 1, 0);
} else {
RETURN_BOOL(Z_TYPE_P(zend_read_property(yaf_dispatcher_ce,
self, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_FLUSH), 1, NULL)) == IS_TRUE ? 1 : 0);
}
}
/* }}} */
/** {{{ proto public Yaf_Dispatcher::registerPlugin(Yaf_Plugin_Abstract $plugin)
*/
PHP_METHOD(yaf_dispatcher, registerPlugin) {
zval *plugin, *plugins;
yaf_dispatcher_t *self = getThis();
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &plugin) == FAILURE) {
return;
}
if (EXPECTED(Z_TYPE_P(plugin) != IS_OBJECT ||
!instanceof_function(Z_OBJCE_P(plugin), yaf_plugin_ce))) {
php_error_docref(NULL, E_WARNING, "Expect a %s instance", ZSTR_VAL(yaf_plugin_ce->name));
RETURN_FALSE;
}
plugins = zend_read_property(yaf_dispatcher_ce, self, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_PLUGINS), 1, NULL);
Z_ADDREF_P(plugin);
add_next_index_zval(plugins, plugin);
RETURN_ZVAL(self, 1, 0);
}
/* }}} */
/** {{{ proto public Yaf_Dispatcher::setRequest(Yaf_Request_Abstract $request)
*/
PHP_METHOD(yaf_dispatcher, setRequest) {
yaf_request_t *request;
yaf_dispatcher_t *self;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &request) == FAILURE) {
return;
}
if (IS_OBJECT != Z_TYPE_P(request) ||
!instanceof_function(Z_OBJCE_P(request), yaf_request_ce)) {
php_error_docref(NULL, E_WARNING, "Expects a %s instance", ZSTR_VAL(yaf_request_ce->name));
RETURN_FALSE;
}
self = getThis();
if (yaf_dispatcher_set_request(self, request)) {
RETURN_ZVAL(self, 1, 0);
}
RETURN_FALSE;
}
/* }}} */
/** {{{ proto public Yaf_Dispatcher::getInstance(void)
*/
PHP_METHOD(yaf_dispatcher, getInstance) {
zval *instance, rv = {{0}};
if ((instance = yaf_dispatcher_instance(&rv)) != NULL) {
RETURN_ZVAL(instance, 1, 0);
}
}
/* }}} */
/** {{{ proto public Yaf_Dispatcher::getRouter(void)
*/
PHP_METHOD(yaf_dispatcher, getRouter) {
yaf_router_t *router = zend_read_property(yaf_dispatcher_ce,
getThis(), ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_ROUTER), 1, NULL);
RETURN_ZVAL(router, 1, 0);
}
/* }}} */
/** {{{ proto public Yaf_Dispatcher::getRequest(void)
*/
PHP_METHOD(yaf_dispatcher, getRequest) {
yaf_request_t *request = zend_read_property(yaf_dispatcher_ce,
getThis(), ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_REQUEST), 1, NULL);
RETURN_ZVAL(request, 1, 0);
}
/* }}} */
/** {{{ proto public Yaf_Dispatcher::getApplication(void)
*/
PHP_METHOD(yaf_dispatcher, getApplication) {
PHP_MN(yaf_application_app)(INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
/* }}} */
/** {{{ proto public Yaf_Dispatcher::dispatch(yaf_request_t $request)
*/
PHP_METHOD(yaf_dispatcher, dispatch) {
yaf_request_t *request;
yaf_response_t *response, rresponse;
yaf_dispatcher_t *self = getThis();
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &request, yaf_request_ce) == FAILURE) {
return;
}
zend_update_property(yaf_dispatcher_ce, self, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_REQUEST), request);
ZVAL_NULL(&rresponse);
if ((response = yaf_dispatcher_dispatch(self, &rresponse))) {
RETURN_ZVAL(response, 1, 1);
}
RETURN_FALSE;
}
/* }}} */
/** {{{ proto public Yaf_Dispatcher::throwException(bool $flag=0)
*/
PHP_METHOD(yaf_dispatcher, throwException) {
zend_bool flag;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &flag) == FAILURE) {
return;
}
if (ZEND_NUM_ARGS()) {
YAF_G(throw_exception) = flag? 1: 0;
RETURN_ZVAL(getThis(), 1, 0);
} else {
RETURN_BOOL(YAF_G(throw_exception));
}
}
/* }}} */
/** {{{ proto public Yaf_Dispatcher::catchException(bool $flag=0)
*/
PHP_METHOD(yaf_dispatcher, catchException) {
zend_bool flag;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &flag) == FAILURE) {
return;
}
if (ZEND_NUM_ARGS()) {
YAF_G(catch_exception) = flag? 1: 0;
RETURN_ZVAL(getThis(), 1, 0);
} else {
RETURN_BOOL(YAF_G(catch_exception));
}
}
/* }}} */
/** {{{ proto public Yaf_Dispatcher::autoRender(int $flag)
*/
PHP_METHOD(yaf_dispatcher, autoRender) {
zend_bool flag;
yaf_dispatcher_t *self = getThis();
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &flag) == FAILURE) {
return;
}
if (ZEND_NUM_ARGS()) {
zend_update_property_bool(yaf_dispatcher_ce, self, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_RENDER), flag? 1 : 0);
RETURN_ZVAL(self, 1, 0);
} else {
RETURN_BOOL(Z_TYPE_P(zend_read_property(yaf_dispatcher_ce,
self, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_RENDER), 1, NULL)) == IS_TRUE ? 1 : 0);
}
}
/* }}} */
/** {{{ proto public Yaf_Dispatcher::initView(string $tpl_dir, array $options = NULL)
*/
PHP_METHOD(yaf_dispatcher, initView) {
yaf_view_t *view, rv = {{0}};
zval *tpl_dir, *options = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|z", &tpl_dir, &options) == FAILURE) {
WRONG_PARAM_COUNT;
}
view = yaf_dispatcher_init_view(getThis(), tpl_dir, options, &rv);
RETURN_ZVAL(view, 1, 0);
}
/* }}} */
/** {{{ proto public Yaf_Dispatcher::setView(Yaf_View_Interface $view)
*/
PHP_METHOD(yaf_dispatcher, setView) {
yaf_view_t *view;
yaf_dispatcher_t *self = getThis();
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &view) == FAILURE) {
return;
}
if (IS_OBJECT == Z_TYPE_P(view)
&& instanceof_function(Z_OBJCE_P(view), yaf_view_interface_ce)) {
zend_update_property(yaf_dispatcher_ce, self, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_VIEW), view);
RETURN_ZVAL(self, 1, 0);
}
RETURN_FALSE;
}
/* }}} */
/** {{{ proto public Yaf_Dispatcher::setDefaultModule(string $name)
*/
PHP_METHOD(yaf_dispatcher, setDefaultModule) {
zval *module;
zval *self = getThis();
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &module) == FAILURE) {
return;
}
if (IS_STRING == Z_TYPE_P(module) && Z_STRLEN_P(module)
&& yaf_application_is_module_name(Z_STR_P(module))) {
zval module_std;
ZVAL_STRING(&module_std, zend_str_tolower_dup(Z_STRVAL_P(module), Z_STRLEN_P(module)));
*Z_STRVAL(module_std) = toupper(*Z_STRVAL(module_std));
zend_update_property(yaf_dispatcher_ce, self, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_MODULE), &module_std);
zval_ptr_dtor(&module_std);
RETURN_ZVAL(self, 1, 0);
}
RETURN_FALSE;
}
/* }}} */
/** {{{ proto public Yaf_Dispatcher::setDefaultController(string $name)
*/
PHP_METHOD(yaf_dispatcher, setDefaultController) {
zval *controller;
zval *self = getThis();
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &controller) == FAILURE) {
return;
}
if (IS_STRING == Z_TYPE_P(controller) && Z_STRLEN_P(controller)) {
zval controller_std;
ZVAL_STRING(&controller_std, zend_str_tolower_dup(Z_STRVAL_P(controller), Z_STRLEN_P(controller)));
*Z_STRVAL_P(&controller_std) = toupper(*Z_STRVAL(controller_std));
zend_update_property(yaf_dispatcher_ce, self, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_CONTROLLER), &controller_std);
RETURN_ZVAL(self, 1, 0);
}
RETURN_FALSE;
}
/* }}} */
/** {{{ proto public Yaf_Dispatcher::setDefaultAction(string $name)
*/
PHP_METHOD(yaf_dispatcher, setDefaultAction) {
zval *action;
zval *self = getThis();
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &action) == FAILURE) {
return;
}
if (IS_STRING == Z_TYPE_P(action) && Z_STRLEN_P(action)) {
zval action_lower;
ZVAL_STRING(&action_lower, zend_str_tolower_dup(Z_STRVAL_P(action), Z_STRLEN_P(action)));
zend_update_property(yaf_dispatcher_ce, self, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_ACTION), &action_lower);
zval_ptr_dtor(&action_lower);
RETURN_ZVAL(self, 1, 0);
}
RETURN_FALSE;
}
/* }}} */
/** {{{ proto public Yaf_Dispatcher::__desctruct(void)
*/
PHP_METHOD(yaf_dispatcher, __destruct) {
}
/* }}} */
/** {{{ proto private Yaf_Dispatcher::__clone(void)
*/
PHP_METHOD(yaf_dispatcher, __clone) {
}
/* }}} */
/** {{{ yaf_dispatcher_methods
*/
zend_function_entry yaf_dispatcher_methods[] = {
PHP_ME(yaf_dispatcher, __construct, NULL, ZEND_ACC_PRIVATE | ZEND_ACC_CTOR)
PHP_ME(yaf_dispatcher, __clone, NULL, ZEND_ACC_PRIVATE)
PHP_ME(yaf_dispatcher, __sleep, NULL, ZEND_ACC_PRIVATE)
PHP_ME(yaf_dispatcher, __wakeup, NULL, ZEND_ACC_PRIVATE)
PHP_ME(yaf_dispatcher, enableView, yaf_dispatcher_void_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(yaf_dispatcher, disableView, yaf_dispatcher_void_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(yaf_dispatcher, initView, yaf_dispatcher_initview_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(yaf_dispatcher, setView, yaf_dispatcher_setview_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(yaf_dispatcher, setRequest, yaf_dispatcher_setrequest_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(yaf_dispatcher, getApplication, yaf_dispatcher_void_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(yaf_dispatcher, getRouter, yaf_dispatcher_void_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(yaf_dispatcher, getRequest, yaf_dispatcher_void_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(yaf_dispatcher, setErrorHandler, yaf_dispatcher_seterrhdler_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(yaf_dispatcher, setDefaultModule, yaf_dispatcher_setmodule_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(yaf_dispatcher, setDefaultController, yaf_dispatcher_setctrl_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(yaf_dispatcher, setDefaultAction, yaf_dispatcher_setaction_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(yaf_dispatcher, returnResponse, yaf_dispatcher_returnresp_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(yaf_dispatcher, autoRender, yaf_dispatcher_autorender_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(yaf_dispatcher, flushInstantly, yaf_dispatcher_flush_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(yaf_dispatcher, getInstance, yaf_dispatcher_void_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME(yaf_dispatcher, dispatch, yaf_dispatcher_dispatch_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(yaf_dispatcher, throwException, yaf_dispatcher_throwex_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(yaf_dispatcher, catchException, yaf_dispatcher_catchex_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(yaf_dispatcher, registerPlugin, yaf_dispatcher_regplugin_arginfo, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
/* }}} */
/** {{{ YAF_STARTUP_FUNCTION
*/
YAF_STARTUP_FUNCTION(dispatcher) {
zend_class_entry ce;
YAF_INIT_CLASS_ENTRY(ce, "Yaf_Dispatcher", "Yaf\\Dispatcher", yaf_dispatcher_methods);
yaf_dispatcher_ce = zend_register_internal_class_ex(&ce, NULL);
yaf_dispatcher_ce->ce_flags |= ZEND_ACC_FINAL;
zend_declare_property_null(yaf_dispatcher_ce, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_ROUTER), ZEND_ACC_PROTECTED);
zend_declare_property_null(yaf_dispatcher_ce, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_VIEW), ZEND_ACC_PROTECTED);
zend_declare_property_null(yaf_dispatcher_ce, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_REQUEST), ZEND_ACC_PROTECTED);
zend_declare_property_null(yaf_dispatcher_ce, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_PLUGINS), ZEND_ACC_PROTECTED);
zend_declare_property_null(yaf_dispatcher_ce, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_INSTANCE), ZEND_ACC_PROTECTED|ZEND_ACC_STATIC);
zend_declare_property_bool(yaf_dispatcher_ce, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_RENDER), 1, ZEND_ACC_PROTECTED);
zend_declare_property_bool(yaf_dispatcher_ce, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_RETURN), 0, ZEND_ACC_PROTECTED);
zend_declare_property_bool(yaf_dispatcher_ce, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_FLUSH), 0, ZEND_ACC_PROTECTED);
zend_declare_property_null(yaf_dispatcher_ce, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_MODULE), ZEND_ACC_PROTECTED);
zend_declare_property_null(yaf_dispatcher_ce, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_CONTROLLER), ZEND_ACC_PROTECTED);
zend_declare_property_null(yaf_dispatcher_ce, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_ACTION), ZEND_ACC_PROTECTED);
/*zend_declare_property_null(yaf_dispatcher_ce, ZEND_STRL(YAF_DISPATCHER_PROPERTY_NAME_ARGS), ZEND_ACC_PROTECTED);*/
return SUCCESS;
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/362484032/yaf.git
git@gitee.com:362484032/yaf.git
362484032
yaf
yaf
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891