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) {
/** {{{ static int yaf_view_simple_render_write(const char *str, uint str_length TSRMLS_DC) */ static int yaf_view_simple_render_write(const char *str, uint str_length TSRMLS_DC) { char *target; yaf_view_simple_buffer *buffer = YAF_G(buffer); if (!buffer->size) { buffer->size = (str_length | VIEW_BUFFER_SIZE_MASK) + 1; buffer->len = str_length; buffer->buffer = emalloc(buffer->size); target = buffer->buffer; } else { size_t len = buffer->len + str_length; if (buffer->size < len + 1) { buffer->size = (len | VIEW_BUFFER_SIZE_MASK) + 1; buffer->buffer = erealloc(buffer->buffer, buffer->size); if (!buffer->buffer) { php_error_docref(NULL TSRMLS_CC, E_ERROR, "Yaf output buffer collapsed"); } } target = buffer->buffer + buffer->len; buffer->len = len; } memcpy(target, str, str_length); target[str_length] = '\0'; return str_length; }
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; }
/** {{{void yaf_trigger_error(int type TSRMLS_DC, char *format, ...) */ void yaf_trigger_error(int type TSRMLS_DC, char *format, ...) { va_list args; char *message; uint msg_len; va_start(args, format); msg_len = vspprintf(&message, 0, format, args); va_end(args); if (YAF_G(throw_exception)) { yaf_throw_exception(type, message TSRMLS_CC); } else { yaf_application_t *app = zend_read_static_property(yaf_application_ce, ZEND_STRL(YAF_APPLICATION_PROPERTY_NAME_APP), 1 TSRMLS_CC); zend_update_property_long(yaf_application_ce, app, ZEND_STRL(YAF_APPLICATION_PROPERTY_NAME_ERRNO), type TSRMLS_CC); zend_update_property_stringl(yaf_application_ce, app, ZEND_STRL(YAF_APPLICATION_PROPERTY_NAME_ERRMSG), message, msg_len TSRMLS_CC); php_error_docref(NULL TSRMLS_CC, E_RECOVERABLE_ERROR, message); } efree(message); }
ZEND_ARG_ARRAY_INFO(0, parameters, 1) ZEND_END_ARG_INFO() /* }}} */ /** {{{ zval * yaf_controller_render(yaf_controller_t *instance, char *action_name, int len, zval *var_array TSRMLS_DC) */ zval * yaf_controller_render(yaf_controller_t *instance, char *action_name, int len, zval *var_array TSRMLS_DC) { char *path, *view_ext, *self_name, *tmp; zval *name, *param, *ret = NULL; int path_len; yaf_view_t *view; zend_class_entry *view_ce; view = zend_read_property(yaf_controller_ce, instance, ZEND_STRL(YAF_CONTROLLER_PROPERTY_NAME_VIEW), 0 TSRMLS_CC); name = zend_read_property(yaf_controller_ce, instance, ZEND_STRL(YAF_CONTROLLER_PROPERTY_NAME_NAME), 0 TSRMLS_CC); view_ext = YAF_G(view_ext); self_name = zend_str_tolower_dup(Z_STRVAL_P(name), Z_STRLEN_P(name)); tmp = self_name; while (*tmp != '\0') { if (*tmp == '_') { *tmp = DEFAULT_SLASH; } tmp++; } action_name = estrndup(action_name, len); tmp = action_name; while (*tmp != '\0') {
static int yaf_route_pathinfo_route(yaf_request_t *request, char *req_uri, int req_uri_len TSRMLS_DC) /* {{{ */ { zval *params; char *module = NULL, *controller = NULL, *action = NULL, *rest = NULL; do { #define strip_slashs(p) while (*p == ' ' || *p == '/') { ++p; } char *s, *p, *q; char *uri; if (req_uri_len == 0 || (req_uri_len == 1 && *req_uri == '/')) { break; } uri = req_uri; s = p = uri; q = req_uri + req_uri_len - 1; while (*q == ' ' || *q == '/') { *q-- = '\0'; } strip_slashs(p); if ((s = strstr(p, "/")) != NULL) { if (yaf_application_is_module_name(p, s-p TSRMLS_CC)) { module = estrndup(p, s - p); p = s + 1; strip_slashs(p); if ((s = strstr(p, "/")) != NULL) { controller = estrndup(p, s - p); p = s + 1; } } else { controller = estrndup(p, s - p); p = s + 1; } } strip_slashs(p); if ((s = strstr(p, "/")) != NULL) { action = estrndup(p, s - p); p = s + 1; } strip_slashs(p); if (*p != '\0') { do { if (!module && !controller && !action) { if (yaf_application_is_module_name(p, strlen(p) TSRMLS_CC)) { module = estrdup(p); break; } } if (!controller) { controller = estrdup(p); break; } if (!action) { action = estrdup(p); break; } rest = estrdup(p); } while (0); } if (module && controller == NULL) { controller = module; module = NULL; } else if (module && action == NULL) { action = controller; controller = module; module = NULL; } else if (controller && action == NULL ) { /* /controller */ if (YAF_G(action_prefer)) { action = controller; controller = NULL; } } } while (0); if (module != NULL) { zend_update_property_string(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_MODULE), module TSRMLS_CC); efree(module); } if (controller != NULL) { zend_update_property_string(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_CONTROLLER), controller TSRMLS_CC); efree(controller); } if (action != NULL) { zend_update_property_string(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_ACTION), action TSRMLS_CC); efree(action); } if (rest) { params = yaf_router_parse_parameters(rest TSRMLS_CC); (void)yaf_request_set_params_multi(request, params TSRMLS_CC); zval_ptr_dtor(¶ms); efree(rest); } }
zval_ptr_dtor(&autoload); php_error_docref(NULL, E_WARNING, "Unable to register autoload function %s", YAF_AUTOLOAD_FUNC_NAME); return 0; } zval_ptr_dtor(&function); zval_ptr_dtor(&autoload); } while (0); return 1; } /* }}} */ static int yaf_loader_is_category(char *class, size_t class_len, char *category, size_t category_len) /* {{{ */ { size_t separator_len = YAF_G(name_separator_len); if (YAF_G(name_suffix)) { if (class_len > category_len && strncmp(class + class_len - category_len, category, category_len) == 0) { if (!separator_len || !strncmp(class + class_len - category_len - separator_len, YAF_G(name_separator), separator_len)) { return 1; } } } else { if (strncmp(class, category, category_len) == 0) { if (!separator_len || strncmp(class + category_len, YAF_G(name_separator), separator_len) == 0) { return 1; } }