From 810fe60c85a4cda27120833560822dd07b3f9973 Mon Sep 17 00:00:00 2001 From: Demon Date: Thu, 11 Apr 2013 09:33:09 +0800 Subject: [PATCH 1/2] route assemble complete --- routes/yaf_route_interface.c | 19 ++++- routes/yaf_route_interface.h | 15 ++++ routes/yaf_route_map.c | 112 ++++++++++++++++++++++++++++++ routes/yaf_route_regex.c | 130 +++++++++++++++++++++++++++++++--- routes/yaf_route_regex.h | 2 +- routes/yaf_route_rewrite.c | 131 +++++++++++++++++++++++++++++++++-- routes/yaf_route_simple.c | 90 ++++++++++++++++++++++++ routes/yaf_route_static.c | 80 +++++++++++++++++++++ routes/yaf_route_supervar.c | 92 +++++++++++++++++++++++- tests/013.phpt | 16 ++++- tests/068.phpt | 35 ++++++++++ tests/069.phpt | 27 ++++++++ tests/070.phpt | 27 ++++++++ tests/071.phpt | 28 ++++++++ tests/072.phpt | 41 +++++++++++ tests/073.phpt | 34 +++++++++ 16 files changed, 854 insertions(+), 25 deletions(-) create mode 100644 tests/068.phpt create mode 100644 tests/069.phpt create mode 100644 tests/070.phpt create mode 100644 tests/071.phpt create mode 100644 tests/072.phpt create mode 100644 tests/073.phpt diff --git a/routes/yaf_route_interface.c b/routes/yaf_route_interface.c index 6e77eda2..00bca5bc 100644 --- a/routes/yaf_route_interface.c +++ b/routes/yaf_route_interface.c @@ -41,7 +41,7 @@ zend_class_entry *yaf_route_ce; /* {{{ yaf_route_t * yaf_route_instance(yaf_route_t *this_ptr, zval *config TSRMLS_DC) */ yaf_route_t * yaf_route_instance(yaf_route_t *this_ptr, zval *config TSRMLS_DC) { - zval **match, **def, **map, **ppzval; + zval **match, **def, **map, **verify, **reverse, **ppzval; yaf_route_t *instance = NULL; if (!config || IS_ARRAY != Z_TYPE_P(config)) { @@ -64,7 +64,11 @@ yaf_route_t * yaf_route_instance(yaf_route_t *this_ptr, zval *config TSRMLS_DC) return NULL; } - instance = yaf_route_rewrite_instance(NULL, *match, *def, NULL TSRMLS_CC); + if (zend_hash_find(Z_ARRVAL_P(config), ZEND_STRS("route"), (void **)&verify) == FAILURE) { + verify = NULL; + } + + instance = yaf_route_rewrite_instance(NULL, *match, *def, verify? *verify : NULL TSRMLS_CC); } else if (Z_STRLEN_PP(ppzval) == (sizeof("regex") - 1) && strncasecmp(Z_STRVAL_PP(ppzval), "regex", sizeof("regex") - 1) == 0) { if (zend_hash_find(Z_ARRVAL_P(config), ZEND_STRS("match"), (void **)&match) == FAILURE || Z_TYPE_PP(match) != IS_STRING) { @@ -78,7 +82,15 @@ yaf_route_t * yaf_route_instance(yaf_route_t *this_ptr, zval *config TSRMLS_DC) map = NULL; } - instance = yaf_route_regex_instance(NULL, *match, *def, map? *map : NULL, NULL TSRMLS_CC); + if (zend_hash_find(Z_ARRVAL_P(config), ZEND_STRS("route"), (void **)&verify) == FAILURE) { + verify = NULL; + } + + if (zend_hash_find(Z_ARRVAL_P(config), ZEND_STRS("route"), (void **)&reverse) == FAILURE) { + reverse = NULL; + } + + instance = yaf_route_regex_instance(NULL, *match, *def, map? *map : NULL, verify? *verify : NULL, reverse? *reverse : NULL TSRMLS_CC); } else if (Z_STRLEN_PP(ppzval) == (sizeof("map") - 1) && strncasecmp(Z_STRVAL_PP(ppzval), "map", sizeof("map") - 1) == 0) { char *delimiter = NULL; @@ -134,6 +146,7 @@ yaf_route_t * yaf_route_instance(yaf_route_t *this_ptr, zval *config TSRMLS_DC) */ zend_function_entry yaf_route_methods[] = { PHP_ABSTRACT_ME(yaf_route, route, yaf_route_route_arginfo) + PHP_ABSTRACT_ME(yaf_route, assemble, yaf_route_assemble_arginfo) {NULL, NULL, NULL} }; /* }}} */ diff --git a/routes/yaf_route_interface.h b/routes/yaf_route_interface.h index 303c0421..5832993e 100644 --- a/routes/yaf_route_interface.h +++ b/routes/yaf_route_interface.h @@ -19,10 +19,19 @@ #ifndef YAF_ROUTER_INTERFACE_H #define YAF_ROUTER_INTERFACE_H +#define YAF_ROUTE_VAR_NAME_MODULE "module" +#define YAF_ROUTE_VAR_NAME_CONTROLLER "controller" +#define YAF_ROUTE_VAR_NAME_ACTION "action" + +#define YAF_ROUTE_ASSEMBLE_MOUDLE_FORMAT ":m" +#define YAF_ROUTE_ASSEMBLE_ACTION_FORMAT ":a" +#define YAF_ROUTE_ASSEMBLE_CONTROLLER_FORMAT ":c" + #define YAF_ROUTE_PROPETY_NAME_MATCH "_route" #define YAF_ROUTE_PROPETY_NAME_ROUTE "_default" #define YAF_ROUTE_PROPETY_NAME_MAP "_maps" #define YAF_ROUTE_PROPETY_NAME_VERIFY "_verify" +#define YAF_ROUTE_PROPETY_NAME_REVERSE "_reverse" #define YAF_ROUTER_URL_DELIMIETER "/" #define YAF_ROUTE_REGEX_DILIMITER '#' @@ -31,6 +40,12 @@ YAF_BEGIN_ARG_INFO_EX(yaf_route_route_arginfo, 0, 0, 1) YAF_ARG_INFO(0, request) YAF_END_ARG_INFO() +YAF_BEGIN_ARG_INFO_EX(yaf_route_assemble_arginfo, 0, 0, 1) + YAF_ARG_INFO(0, mvc) + YAF_ARG_INFO(0, query) +YAF_END_ARG_INFO() + + extern zend_class_entry *yaf_route_ce; yaf_route_t * yaf_route_instance(yaf_route_t *this_ptr, zval *config TSRMLS_DC); diff --git a/routes/yaf_route_map.c b/routes/yaf_route_map.c index 38c0a5c1..88a6990a 100644 --- a/routes/yaf_route_map.c +++ b/routes/yaf_route_map.c @@ -156,6 +156,102 @@ PHP_METHOD(yaf_route_map, route) { } /* }}} */ + +/** {{{ zval * yaf_route_map_assemble(zval *mvc, zval *query TSRMLS_DC) + */ +zval * yaf_route_map_assemble(yaf_route_t *this_ptr, zval *mvc, zval *query TSRMLS_DC) { + char *tmp, *ptrptr, *pname; + smart_str tvalue = {0}; + uint tmp_len, has_delim = 0; + zval *uri, *delim, *ctl_prefer, **tmp_data; + + MAKE_STD_ZVAL(uri); + + ctl_prefer = zend_read_property(yaf_route_map_ce, this_ptr, ZEND_STRL(YAF_ROUTE_MAP_VAR_NAME_CTL_PREFER), 1 TSRMLS_CC); + delim = zend_read_property(yaf_route_map_ce, this_ptr, ZEND_STRL(YAF_ROUTE_MAP_VAR_NAME_DELIMETER), 1 TSRMLS_CC); + if (IS_STRING == Z_TYPE_P(delim) && Z_STRLEN_P(delim)) { + has_delim = 1; + } + + do { + if (Z_BVAL_P(ctl_prefer)) { + if (zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_VAR_NAME_ACTION), (void **)&tmp_data) == SUCCESS) { + pname = estrndup(Z_STRVAL_PP(tmp_data), Z_STRLEN_PP(tmp_data)); + } else { + yaf_trigger_error(YAF_ERR_TYPE_ERROR TSRMLS_CC, "%s", "Undefined the 'action' parameter for the 1st parameter"); + break; + } + } else { + if (zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_VAR_NAME_CONTROLLER), (void **)&tmp_data) == SUCCESS) { + pname = estrndup(Z_STRVAL_PP(tmp_data), Z_STRLEN_PP(tmp_data)); + } else { + yaf_trigger_error(YAF_ERR_TYPE_ERROR TSRMLS_CC, "%s", "Undefined the 'controller' parameter for the 1st parameter"); + break; + } + } + + tmp = php_strtok_r(pname, "_", &ptrptr); + while(tmp) { + tmp_len = strlen(tmp); + if (tmp_len) { + smart_str_appendc(&tvalue, '/'); + smart_str_appendl(&tvalue, tmp, tmp_len); + } + tmp = php_strtok_r(NULL, "_", &ptrptr); + } + efree(pname); + + if (IS_ARRAY == Z_TYPE_P(query)) { + uint key_type, key_len, i = 0; + char *key; + ulong key_idx; + zval **tmp_data; + + if (has_delim) { + smart_str_appendc(&tvalue, '/'); + smart_str_appendl(&tvalue, Z_STRVAL_P(delim), Z_STRLEN_P(delim)); + } + + for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(query)); + zend_hash_get_current_data(Z_ARRVAL_P(query), (void **)&tmp_data) == SUCCESS; + zend_hash_move_forward(Z_ARRVAL_P(query))) { + + if (IS_STRING == Z_TYPE_PP(tmp_data) + && HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(Z_ARRVAL_P(query), &key, &key_len, &key_idx, 0, NULL)) { + + if (has_delim) { + smart_str_appendc(&tvalue, '/'); + smart_str_appendl(&tvalue, key, key_len - 1); + smart_str_appendc(&tvalue, '/'); + smart_str_appendl(&tvalue, Z_STRVAL_PP(tmp_data), Z_STRLEN_PP(tmp_data)); + } else { + if (i == 0) { + smart_str_appendc(&tvalue, '?'); + smart_str_appendl(&tvalue, key, key_len - 1); + smart_str_appendc(&tvalue, '='); + smart_str_appendl(&tvalue, Z_STRVAL_PP(tmp_data), Z_STRLEN_PP(tmp_data)); + } else { + smart_str_appendc(&tvalue, '&'); + smart_str_appendl(&tvalue, key, key_len - 1); + smart_str_appendc(&tvalue, '='); + smart_str_appendl(&tvalue, Z_STRVAL_PP(tmp_data), Z_STRLEN_PP(tmp_data)); + } + } + } + i += 1; + } + } + + smart_str_0(&tvalue); + ZVAL_STRING(uri, tvalue.c, 1); + smart_str_free(&tvalue); + return uri; + } while (0); + + ZVAL_NULL(uri); + return uri; +} + /** {{{ proto public Yaf_Route_Simple::__construct(bool $controller_prefer=FALSE, string $delimer = '#!') */ PHP_METHOD(yaf_route_map, __construct) { @@ -173,11 +269,27 @@ PHP_METHOD(yaf_route_map, __construct) { } /* }}} */ +/** {{{ proto public Yaf_Route_Map::assemble(array $mvc[, array $query = NULL]) +*/ +PHP_METHOD(yaf_route_map, assemble) { + zval *mvc, *query; + zval *return_uri; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|a", &mvc, &query) == FAILURE) { + return; + } else { + return_uri = yaf_route_map_assemble(getThis(), mvc, query TSRMLS_CC); + RETURN_ZVAL(return_uri, 0, 1); + } +} +/* }}} */ + /** {{{ yaf_route_map_methods */ zend_function_entry yaf_route_map_methods[] = { PHP_ME(yaf_route_map, __construct, yaf_route_map_construct_arginfo, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR) PHP_ME(yaf_route_map, route, yaf_route_route_arginfo, ZEND_ACC_PUBLIC) + PHP_ME(yaf_route_map, assemble, yaf_route_assemble_arginfo, ZEND_ACC_PUBLIC) {NULL, NULL, NULL} }; /* }}} */ diff --git a/routes/yaf_route_regex.c b/routes/yaf_route_regex.c index ea5e11b1..7b717907 100644 --- a/routes/yaf_route_regex.c +++ b/routes/yaf_route_regex.c @@ -31,6 +31,8 @@ #include "yaf_router.h" #include "routes/yaf_route_interface.h" #include "routes/yaf_route_regex.h" +#include "ext/standard/php_string.h" +#include "ext/standard/php_smart_str.h" /* for smart_str */ zend_class_entry *yaf_route_regex_ce; @@ -38,15 +40,16 @@ zend_class_entry *yaf_route_regex_ce; */ ZEND_BEGIN_ARG_INFO_EX(yaf_route_regex_construct_arginfo, 0, 0, 2) ZEND_ARG_INFO(0, match) - ZEND_ARG_ARRAY_INFO(0, route, 0) - ZEND_ARG_ARRAY_INFO(0, map, 1) - ZEND_ARG_ARRAY_INFO(0, verify, 1) + ZEND_ARG_ARRAY_INFO(0, route, 0) + ZEND_ARG_ARRAY_INFO(0, map, 1) + ZEND_ARG_ARRAY_INFO(0, verify, 1) + ZEND_ARG_INFO(0, reverse) ZEND_END_ARG_INFO() /* }}} */ -/** {{{ yaf_route_t * yaf_route_regex_instance(yaf_route_t *this_ptr, zval *route, zval *def, zval *map, zval *verify TSRMLS_DC) +/** {{{ yaf_route_t * yaf_route_regex_instance(yaf_route_t *this_ptr, zval *route, zval *def, zval *map, zval *verify, zval reverse TSRMLS_DC) */ -yaf_route_t * yaf_route_regex_instance(yaf_route_t *this_ptr, zval *route, zval *def, zval *map, zval *verify TSRMLS_DC) { +yaf_route_t * yaf_route_regex_instance(yaf_route_t *this_ptr, zval *route, zval *def, zval *map, zval *verify, zval *reverse TSRMLS_DC) { yaf_route_t *instance; if (this_ptr) { @@ -68,6 +71,13 @@ yaf_route_t * yaf_route_regex_instance(yaf_route_t *this_ptr, zval *route, zval } else { zend_update_property(yaf_route_regex_ce, instance, ZEND_STRL(YAF_ROUTE_PROPETY_NAME_VERIFY), verify TSRMLS_CC); } + + + if (!reverse || IS_STRING != Z_TYPE_P(reverse)) { + zend_update_property_null(yaf_route_regex_ce, instance, ZEND_STRL(YAF_ROUTE_PROPETY_NAME_REVERSE) TSRMLS_CC); + } else { + zend_update_property(yaf_route_regex_ce, instance, ZEND_STRL(YAF_ROUTE_PROPETY_NAME_REVERSE), reverse TSRMLS_CC); + } return instance; } @@ -143,6 +153,82 @@ static zval * yaf_route_regex_match(yaf_route_t *route, char *uir, int len TSRML } /* }}} */ +/** {{{ zval * yaf_route_regex_assemble(yaf_route_t *this_ptr, zval *mvc, zval *query TSRMLS_DC) + */ +zval * yaf_route_regex_assemble(yaf_route_t *this_ptr, zval *mvc, zval *query TSRMLS_DC) { + zval *reverse, *uri; + zval **tmp; + char *tstr, *inter; + uint tlen; + smart_str squery = {0}; + + reverse = zend_read_property(yaf_route_regex_ce, this_ptr, ZEND_STRL(YAF_ROUTE_PROPETY_NAME_REVERSE), 0 TSRMLS_CC); + + if (Z_TYPE_P(reverse) != IS_STRING) { + yaf_trigger_error(YAF_ERR_TYPE_ERROR TSRMLS_CC, "%s", "Reverse property is not a valid string"); + return NULL; + } + + MAKE_STD_ZVAL(uri); + + tstr = estrndup(Z_STRVAL_P(reverse), Z_STRLEN_P(reverse)); + tlen = Z_STRLEN_P(reverse); + + if (zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_ASSEMBLE_MOUDLE_FORMAT), (void **)&tmp) == SUCCESS) { + inter = php_str_to_str(tstr, tlen, ZEND_STRL(YAF_ROUTE_ASSEMBLE_MOUDLE_FORMAT), Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp), &tlen); + efree(tstr); + tstr = inter; + } + + if (zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_ASSEMBLE_CONTROLLER_FORMAT), (void **)&tmp) == SUCCESS) { + inter = php_str_to_str(tstr, tlen, ZEND_STRL(YAF_ROUTE_ASSEMBLE_CONTROLLER_FORMAT), Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp), &tlen); + efree(tstr); + tstr = inter; + } + + if (zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_ASSEMBLE_ACTION_FORMAT), (void **)&tmp) == SUCCESS) { + inter = php_str_to_str(tstr, tlen, ZEND_STRL(YAF_ROUTE_ASSEMBLE_ACTION_FORMAT), Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp), &tlen); + efree(tstr); + tstr = inter; + } + + if (IS_ARRAY == Z_TYPE_P(query)) { + uint key_type, key_len; + char *key; + ulong key_idx; + HashTable *ht = Z_ARRVAL_P(query); + + smart_str_appendc(&squery, '?'); + for (zend_hash_internal_pointer_reset(ht); + zend_hash_get_current_data(ht, (void **)&tmp) == SUCCESS; + zend_hash_move_forward(ht)) { + + if (HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(ht, &key, &key_len, &key_idx, 0, NULL)) { + if (IS_STRING == Z_TYPE_PP(tmp)) { + smart_str_appendl(&squery, key, key_len - 1); + smart_str_appendc(&squery, '='); + smart_str_appendl(&squery, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp)); + smart_str_appendc(&squery, '&'); + } + } + } + } + + if (squery.len) { + squery.len--; /* get rid of the tail & */ + smart_str_0(&squery); + tstr = erealloc(tstr, tlen + squery.len + 1); + memcpy(tstr + tlen, squery.c, squery.len); + tlen += squery.len; + tstr[tlen] = '\0'; + } + + ZVAL_STRINGL(uri, tstr, tlen, 0); + smart_str_free(&squery); + return uri; +} +/** }}} */ + /** {{{ int yaf_route_regex_route(yaf_route_t *router, yaf_request_t *request TSRMLS_DC) */ int yaf_route_regex_route(yaf_route_t *router, yaf_request_t *request TSRMLS_DC) { @@ -231,13 +317,13 @@ PHP_METHOD(yaf_route_regex, route) { } /** }}} */ -/** {{{ proto public Yaf_Route_Regex::__construct(string $match, array $route, array $map = NULL, array $verify = NULL) +/** {{{ proto public Yaf_Route_Regex::__construct(string $match, array $route, array $map = NULL, array $verify = NULL, string reverse = NULL) */ PHP_METHOD(yaf_route_regex, __construct) { - zval *match, *route, *map = NULL, *verify = NULL; + zval *match, *route, *map = NULL, *verify = NULL, *reverse = NULL; yaf_route_t *self = getThis(); - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "za|aa", &match, &route, &map, &verify) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "za|aaz", &match, &route, &map, &verify, &reverse) == FAILURE) { YAF_UNINITIALIZED_OBJECT(getThis()); return; } @@ -250,11 +336,17 @@ PHP_METHOD(yaf_route_regex, __construct) { if (verify && IS_ARRAY != Z_TYPE_P(verify)) { YAF_UNINITIALIZED_OBJECT(getThis()); - yaf_trigger_error(YAF_ERR_TYPE_ERROR TSRMLS_CC, "Expects an array as verify parmater", yaf_route_regex_ce->name); + yaf_trigger_error(YAF_ERR_TYPE_ERROR TSRMLS_CC, "Expects an array as third parameter", yaf_route_regex_ce->name); + RETURN_FALSE; + } + + if (reverse && IS_STRING != Z_TYPE_P(reverse)) { + YAF_UNINITIALIZED_OBJECT(getThis()); + yaf_trigger_error(YAF_ERR_TYPE_ERROR TSRMLS_CC, "Expects a valid string reverse as fourth parameter"); RETURN_FALSE; } - (void)yaf_route_regex_instance(self, match, route, map, verify TSRMLS_CC); + (void)yaf_route_regex_instance(self, match, route, map, verify, reverse TSRMLS_CC); if (self) { RETURN_ZVAL(self, 1, 0); @@ -264,11 +356,28 @@ PHP_METHOD(yaf_route_regex, __construct) { } /** }}} */ +/** {{{ proto public Yaf_Route_regex::assemble(array $mvc[, array $query = NULL]) + */ +PHP_METHOD(yaf_route_regex, assemble) { + zval *mvc, *query, *return_uri = NULL; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|a", &mvc, &query) == FAILURE) { + return; + } else { + if (return_uri = yaf_route_regex_assemble(getThis(), mvc, query TSRMLS_CC)) { + RETURN_ZVAL(return_uri, 0, 1); + } + } + RETURN_FALSE; +} +/* }}} */ + /** {{{ yaf_route_regex_methods */ zend_function_entry yaf_route_regex_methods[] = { PHP_ME(yaf_route_regex, __construct, yaf_route_regex_construct_arginfo, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR) PHP_ME(yaf_route_regex, route, yaf_route_route_arginfo, ZEND_ACC_PUBLIC) + PHP_ME(yaf_route_regex, assemble, yaf_route_assemble_arginfo, ZEND_ACC_PUBLIC) {NULL, NULL, NULL} }; /* }}} */ @@ -286,6 +395,7 @@ YAF_STARTUP_FUNCTION(route_regex) { zend_declare_property_null(yaf_route_regex_ce, ZEND_STRL(YAF_ROUTE_PROPETY_NAME_ROUTE), ZEND_ACC_PROTECTED TSRMLS_CC); zend_declare_property_null(yaf_route_regex_ce, ZEND_STRL(YAF_ROUTE_PROPETY_NAME_MAP), ZEND_ACC_PROTECTED TSRMLS_CC); zend_declare_property_null(yaf_route_regex_ce, ZEND_STRL(YAF_ROUTE_PROPETY_NAME_VERIFY), ZEND_ACC_PROTECTED TSRMLS_CC); + zend_declare_property_null(yaf_route_regex_ce, ZEND_STRL(YAF_ROUTE_PROPETY_NAME_REVERSE), ZEND_ACC_PROTECTED TSRMLS_CC); return SUCCESS; } diff --git a/routes/yaf_route_regex.h b/routes/yaf_route_regex.h index 40b00c8b..c1644589 100644 --- a/routes/yaf_route_regex.h +++ b/routes/yaf_route_regex.h @@ -21,7 +21,7 @@ extern zend_class_entry *yaf_route_regex_ce; -yaf_route_t * yaf_route_regex_instance(yaf_route_t *this_ptr, zval *route, zval *def, zval *map, zval *verify TSRMLS_DC); +yaf_route_t * yaf_route_regex_instance(yaf_route_t *this_ptr, zval *route, zval *def, zval *map, zval *verify, zval *reverse TSRMLS_DC); YAF_STARTUP_FUNCTION(route_regex); diff --git a/routes/yaf_route_rewrite.c b/routes/yaf_route_rewrite.c index c6f4b6d3..135e40c7 100644 --- a/routes/yaf_route_rewrite.c +++ b/routes/yaf_route_rewrite.c @@ -33,14 +33,16 @@ #include "routes/yaf_route_interface.h" #include "routes/yaf_route_rewrite.h" +#include "ext/standard/php_string.h" + zend_class_entry *yaf_route_rewrite_ce; /** {{{ ARG_INFO */ ZEND_BEGIN_ARG_INFO_EX(yaf_route_rewrite_construct_arginfo, 0, 0, 2) ZEND_ARG_INFO(0, match) - ZEND_ARG_ARRAY_INFO(0, route, 0) - ZEND_ARG_ARRAY_INFO(0, verify, 1) + ZEND_ARG_ARRAY_INFO(0, route, 0) + ZEND_ARG_ARRAY_INFO(0, verify, 1) ZEND_END_ARG_INFO() /* }}} */ @@ -82,7 +84,7 @@ static zval * yaf_route_rewrite_match(yaf_route_t *router, char *uir, int len TS return NULL; } - match = zend_read_property(yaf_route_rewrite_ce, router, ZEND_STRL(YAF_ROUTE_PROPETY_NAME_MATCH), 1 TSRMLS_CC); + match = zend_read_property(yaf_route_rewrite_ce, router, ZEND_STRL(YAF_ROUTE_PROPETY_NAME_MATCH), 0 TSRMLS_CC); pmatch = estrndup(Z_STRVAL_P(match), Z_STRLEN_P(match)); smart_str_appendc(&pattern, YAF_ROUTE_REGEX_DILIMITER); @@ -184,8 +186,8 @@ int yaf_route_rewrite_route(yaf_route_t *router, yaf_request_t *request TSRMLS_D char *request_uri; zval *args, *base_uri, *zuri; - zuri = zend_read_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_URI), 1 TSRMLS_CC); - base_uri = zend_read_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_BASE), 1 TSRMLS_CC); + zuri = zend_read_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_URI), 0 TSRMLS_CC); + base_uri = zend_read_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_BASE), 0 TSRMLS_CC); if (base_uri && IS_STRING == Z_TYPE_P(base_uri) && !strncasecmp(Z_STRVAL_P(zuri), Z_STRVAL_P(base_uri), Z_STRLEN_P(base_uri))) { @@ -265,6 +267,103 @@ PHP_METHOD(yaf_route_rewrite, route) { } /** }}} */ +/** {{{ zval * yaf_route_rewrite_assemble(yaf_route_t *this_ptr, zval *idents, zval *query TSRMLS_DC) + */ +zval * yaf_route_rewrite_assemble(yaf_route_t *this_ptr, zval *idents, zval *query TSRMLS_DC) { + zval *reverse, *uri, *match, *pidents; + zval **tmp; + char *tstr, *inter, *seg, *pmatch, *ptrptr, *key; + uint tlen, seg_len, key_len; + ulong key_idx; + smart_str squery = {0}; + smart_str wildcard = {0}; + + MAKE_STD_ZVAL(uri); + MAKE_STD_ZVAL(pidents); + array_init(pidents); + + match = zend_read_property(yaf_route_rewrite_ce, this_ptr, ZEND_STRL(YAF_ROUTE_PROPETY_NAME_MATCH), 0 TSRMLS_CC); + pmatch = estrndup(Z_STRVAL_P(match), Z_STRLEN_P(match)); + tstr = estrndup(Z_STRVAL_P(match), Z_STRLEN_P(match)); + tlen = Z_STRLEN_P(match); + zend_hash_copy(Z_ARRVAL_P(pidents), Z_ARRVAL_P(idents), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *)); + + seg = php_strtok_r(pmatch, YAF_ROUTER_URL_DELIMIETER, &ptrptr); + while (seg) { + seg_len = strlen(seg); + if (seg_len) { + if(*(seg) == '*') { + for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(pidents)); + zend_hash_get_current_data(Z_ARRVAL_P(pidents), (void **)&tmp) == SUCCESS; + zend_hash_move_forward(Z_ARRVAL_P(pidents))) { + + if (HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(Z_ARRVAL_P(pidents), &key, &key_len, &key_idx, 0, NULL)) { + if (IS_STRING == Z_TYPE_PP(tmp)) { + smart_str_appendl(&wildcard, key + 1, key_len); + smart_str_appendl(&wildcard, YAF_ROUTER_URL_DELIMIETER, 1); + smart_str_appendl(&wildcard, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp)); + smart_str_appendl(&wildcard, YAF_ROUTER_URL_DELIMIETER, 1); + } + } + } + smart_str_0(&wildcard); + inter = php_str_to_str(tstr, tlen, "*", 1, wildcard.c, wildcard.len, &tlen); + efree(tstr); + tstr = inter; + break; + } + + if(*(seg) == ':') { + if (zend_hash_find(Z_ARRVAL_P(idents), seg, seg_len + 1, (void **)&tmp) == SUCCESS) { + inter = php_str_to_str(tstr, tlen, seg, seg_len, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp), &tlen); + efree(tstr); + tstr = inter; + zend_hash_del(Z_ARRVAL_P(pidents), seg, seg_len + 1); + } + } + } + seg = php_strtok_r(NULL, YAF_ROUTER_URL_DELIMIETER, &ptrptr); + } + + smart_str_free(&wildcard); + efree(pmatch); + zval_ptr_dtor(&pidents); + + if (IS_ARRAY == Z_TYPE_P(query)) { + uint key_type; + HashTable *ht = Z_ARRVAL_P(query); + + smart_str_appendc(&squery, '?'); + for (zend_hash_internal_pointer_reset(ht); + zend_hash_get_current_data(ht, (void **)&tmp) == SUCCESS; + zend_hash_move_forward(ht)) { + + if (HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(ht, &key, &key_len, &key_idx, 0, NULL)) { + if (IS_STRING == Z_TYPE_PP(tmp)) { + smart_str_appendl(&squery, key, key_len - 1); + smart_str_appendc(&squery, '='); + smart_str_appendl(&squery, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp)); + smart_str_appendc(&squery, '&'); + } + } + } + } + + if (squery.len) { + squery.len--; /* get rid of the tail & */ + smart_str_0(&squery); + tstr = erealloc(tstr, tlen + squery.len + 1); + memcpy(tstr + tlen, squery.c, squery.len); + tlen += squery.len; + tstr[tlen] = '\0'; + } + + ZVAL_STRINGL(uri, tstr, tlen, 0); + smart_str_free(&squery); + return uri; +} +/* }}} */ + /** {{{ proto public Yaf_Route_Rewrite::match(string $uri) */ PHP_METHOD(yaf_route_rewrite, match) { @@ -286,7 +385,7 @@ PHP_METHOD(yaf_route_rewrite, match) { } /** }}} */ -/** {{{ proto public Yaf_Route_Rewrite::__construct(string $match, array $route, array $verify = NULL) +/** {{{ proto public Yaf_Route_Rewrite::__construct(string $match, array $route, array $verify, string $reverse = NULL) */ PHP_METHOD(yaf_route_rewrite, __construct) { zval *match, *route, *verify = NULL; @@ -299,7 +398,7 @@ PHP_METHOD(yaf_route_rewrite, __construct) { if (IS_STRING != Z_TYPE_P(match) || !Z_STRLEN_P(match)) { YAF_UNINITIALIZED_OBJECT(getThis()); - yaf_trigger_error(YAF_ERR_TYPE_ERROR TSRMLS_CC, "Expects a valid string as the first parameter", yaf_route_rewrite_ce->name); + yaf_trigger_error(YAF_ERR_TYPE_ERROR TSRMLS_CC, "Expects a valid string match as the first parameter"); RETURN_FALSE; } @@ -319,11 +418,28 @@ PHP_METHOD(yaf_route_rewrite, __construct) { } /** }}} */ +/** {{{ proto public Yaf_Route_rewrite::assemble(array $idents[, array $query = NULL]) +*/ +PHP_METHOD(yaf_route_rewrite, assemble) { + zval *idents, *query, *return_uri = NULL; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|a", &idents, &query) == FAILURE) { + return; + } else { + if ((return_uri = yaf_route_rewrite_assemble(getThis(), idents, query TSRMLS_CC))) { + RETURN_ZVAL(return_uri, 0, 1); + } + } + RETURN_FALSE; +} +/* }}} */ + /** {{{ yaf_route_rewrite_methods */ zend_function_entry yaf_route_rewrite_methods[] = { PHP_ME(yaf_route_rewrite, __construct, yaf_route_rewrite_construct_arginfo, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR) PHP_ME(yaf_route_rewrite, route, yaf_route_route_arginfo, ZEND_ACC_PUBLIC) + PHP_ME(yaf_route_rewrite, assemble, yaf_route_assemble_arginfo, ZEND_ACC_PUBLIC) {NULL, NULL, NULL} }; /* }}} */ @@ -340,6 +456,7 @@ YAF_STARTUP_FUNCTION(route_rewrite) { zend_declare_property_null(yaf_route_rewrite_ce, ZEND_STRL(YAF_ROUTE_PROPETY_NAME_MATCH), ZEND_ACC_PROTECTED TSRMLS_CC); zend_declare_property_null(yaf_route_rewrite_ce, ZEND_STRL(YAF_ROUTE_PROPETY_NAME_ROUTE), ZEND_ACC_PROTECTED TSRMLS_CC); zend_declare_property_null(yaf_route_rewrite_ce, ZEND_STRL(YAF_ROUTE_PROPETY_NAME_VERIFY), ZEND_ACC_PROTECTED TSRMLS_CC); + zend_declare_property_null(yaf_route_rewrite_ce, ZEND_STRL(YAF_ROUTE_PROPETY_NAME_REVERSE), ZEND_ACC_PROTECTED TSRMLS_CC); return SUCCESS; } diff --git a/routes/yaf_route_simple.c b/routes/yaf_route_simple.c index 6014a1e9..9c197d47 100644 --- a/routes/yaf_route_simple.c +++ b/routes/yaf_route_simple.c @@ -31,6 +31,7 @@ #include "routes/yaf_route_interface.h" #include "routes/yaf_route_simple.h" +#include "ext/standard/php_smart_str.h" /* for smart_str */ zend_class_entry *yaf_route_simple_ce; @@ -106,6 +107,79 @@ PHP_METHOD(yaf_route_simple, route) { } /* }}} */ +/** {{{ zval * yaf_route_simple_assemble(zval *mvc, zval *query TSRMLS_DC) + */ +zval * yaf_route_simple_assemble(yaf_route_t *this_ptr, zval *mvc, zval *query TSRMLS_DC) { + smart_str tvalue = {0}; + zval *nmodule, *ncontroller, *naction; + zval *uri; + + MAKE_STD_ZVAL(uri); + smart_str_appendc(&tvalue, '?'); + + nmodule = zend_read_property(yaf_route_simple_ce, this_ptr, ZEND_STRL(YAF_ROUTE_SIMPLE_VAR_NAME_MODULE), 1 TSRMLS_CC); + ncontroller = zend_read_property(yaf_route_simple_ce, this_ptr, ZEND_STRL(YAF_ROUTE_SIMPLE_VAR_NAME_CONTROLLER), 1 TSRMLS_CC); + naction = zend_read_property(yaf_route_simple_ce, this_ptr, ZEND_STRL(YAF_ROUTE_SIMPLE_VAR_NAME_ACTION), 1 TSRMLS_CC); + + do { + zval **tmp; + + if (zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_VAR_NAME_MODULE), (void **)&tmp) == SUCCESS) { + smart_str_appendl(&tvalue, Z_STRVAL_P(nmodule), Z_STRLEN_P(nmodule)); + smart_str_appendc(&tvalue, '='); + smart_str_appendl(&tvalue, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp)); + smart_str_appendc(&tvalue, '&'); + } + + if (zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_VAR_NAME_CONTROLLER), (void **)&tmp) == FAILURE) { + yaf_trigger_error(YAF_ERR_TYPE_ERROR TSRMLS_CC, "%s", "You need to specify the controller"); + break; + } + + smart_str_appendl(&tvalue, Z_STRVAL_P(ncontroller), Z_STRLEN_P(ncontroller)); + smart_str_appendc(&tvalue, '='); + smart_str_appendl(&tvalue, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp)); + smart_str_appendc(&tvalue, '&'); + + if(zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_VAR_NAME_ACTION), (void **)&tmp) == FAILURE) { + yaf_trigger_error(YAF_ERR_TYPE_ERROR TSRMLS_CC, "%s", "You need to specify the action"); + break; + } + + smart_str_appendl(&tvalue, Z_STRVAL_P(naction), Z_STRLEN_P(naction)); + smart_str_appendc(&tvalue, '='); + smart_str_appendl(&tvalue, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp)); + + if (IS_ARRAY == Z_TYPE_P(query)) { + uint key_type, key_len; + char *key; + ulong key_idx; + + for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(query)); + zend_hash_get_current_data(Z_ARRVAL_P(query), (void **)&tmp) == SUCCESS; + zend_hash_move_forward(Z_ARRVAL_P(query))) { + + if (IS_STRING == Z_TYPE_PP(tmp) + && HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(Z_ARRVAL_P(query), &key, &key_len, &key_idx, 0, NULL)) { + smart_str_appendc(&tvalue, '&'); + smart_str_appendl(&tvalue, key, key_len - 1); + smart_str_appendc(&tvalue, '='); + smart_str_appendl(&tvalue, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp)); + } + } + } + + smart_str_0(&tvalue); + ZVAL_STRING(uri, tvalue.c, 1); + smart_str_free(&tvalue); + return uri; + } while (0); + + ZVAL_NULL(uri); + return uri; +} +/* }}} */ + /** {{{ proto public Yaf_Route_Simple::__construct(string $module, string $controller, string $action) */ PHP_METHOD(yaf_route_simple, __construct) { @@ -128,11 +202,27 @@ PHP_METHOD(yaf_route_simple, __construct) { } /* }}} */ +/** {{{ proto public Yaf_Route_Simple::assemble(array $mvc[, array $query = NULL]) + */ +PHP_METHOD(yaf_route_simple, assemble) { + zval *mvc, *query; + zval *return_uri; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|a", &mvc, &query) == FAILURE) { + return; + } else { + return_uri = yaf_route_simple_assemble(getThis(), mvc, query TSRMLS_CC); + RETURN_ZVAL(return_uri, 0, 1); + } +} +/* }}} */ + /** {{{ yaf_route_simple_methods */ zend_function_entry yaf_route_simple_methods[] = { PHP_ME(yaf_route_simple, __construct, yaf_route_simple_construct_arginfo, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR) PHP_ME(yaf_route_simple, route, yaf_route_route_arginfo, ZEND_ACC_PUBLIC) + PHP_ME(yaf_route_simple, assemble, yaf_route_assemble_arginfo, ZEND_ACC_PUBLIC) {NULL, NULL, NULL} }; /* }}} */ diff --git a/routes/yaf_route_static.c b/routes/yaf_route_static.c index cb92fff5..046519e7 100644 --- a/routes/yaf_route_static.c +++ b/routes/yaf_route_static.c @@ -31,6 +31,7 @@ #include "yaf_router.h" #include "routes/yaf_route_interface.h" #include "routes/yaf_route_static.h" +#include "ext/standard/php_smart_str.h" /* for smart_str */ zend_class_entry * yaf_route_static_ce; @@ -178,6 +179,69 @@ int yaf_route_static_route(yaf_route_t *route, yaf_request_t *request TSRMLS_DC) } /* }}} */ +/** {{{ zval * yaf_route_static_assemble(zval *mvc, zval *query TSRMLS_DC) + */ +zval * yaf_route_static_assemble(yaf_route_t *this_ptr, zval *mvc, zval *query TSRMLS_DC) { + smart_str tvalue = {0}; + zval *uri; + + MAKE_STD_ZVAL(uri); + + do { + zval **tmp; + + if (zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_VAR_NAME_MODULE), (void **)&tmp) == SUCCESS) { + smart_str_appendc(&tvalue, '/'); + smart_str_appendl(&tvalue, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp)); + } + + if (zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_VAR_NAME_CONTROLLER), (void **)&tmp) == FAILURE) { + yaf_trigger_error(YAF_ERR_TYPE_ERROR TSRMLS_CC, "%s", "You need to specify the controller"); + break; + } + + smart_str_appendc(&tvalue, '/'); + smart_str_appendl(&tvalue, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp)); + + if(zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_VAR_NAME_ACTION), (void **)&tmp) == FAILURE) { + yaf_trigger_error(YAF_ERR_TYPE_ERROR TSRMLS_CC, "%s", "You need to specify the action"); + break; + } + + smart_str_appendc(&tvalue, '/'); + smart_str_appendl(&tvalue, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp)); + + if (IS_ARRAY == Z_TYPE_P(query)) { + uint key_type, key_len; + char *key; + ulong key_idx; + + smart_str_appendc(&tvalue, '?'); + for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(query)); + zend_hash_get_current_data(Z_ARRVAL_P(query), (void **)&tmp) == SUCCESS; + zend_hash_move_forward(Z_ARRVAL_P(query))) { + + if (IS_STRING == Z_TYPE_PP(tmp) + && HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(Z_ARRVAL_P(query), &key, &key_len, &key_idx, 0, NULL)) { + smart_str_appendl(&tvalue, key, key_len - 1); + smart_str_appendc(&tvalue, '='); + smart_str_appendl(&tvalue, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp)); + smart_str_appendc(&tvalue, '&'); + } + } + } + + smart_str_0(&tvalue); + ZVAL_STRING(uri, tvalue.c, 1); + smart_str_free(&tvalue); + return uri; + } while (0); + + ZVAL_NULL(uri); + return uri; +} +/* }}} */ + /** {{{ proto public Yaf_Router_Static::route(Yaf_Request $req) */ PHP_METHOD(yaf_route_static, route) { @@ -198,11 +262,27 @@ PHP_METHOD(yaf_route_static, match) { } /* }}} */ +/** {{{ proto public Yaf_Route_Static::assemble(array $mvc[, array $query = NULL]) +*/ +PHP_METHOD(yaf_route_static, assemble) { + zval *mvc, *query; + zval *return_uri; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|a", &mvc, &query) == FAILURE) { + return; + } else { + return_uri = yaf_route_static_assemble(getThis(), mvc, query TSRMLS_CC); + RETURN_ZVAL(return_uri, 0, 1); + } +} +/* }}} */ + /** {{{ yaf_route_static_methods */ zend_function_entry yaf_route_static_methods[] = { PHP_ME(yaf_route_static, match, yaf_route_static_match_arginfo, ZEND_ACC_PUBLIC) PHP_ME(yaf_route_static, route, yaf_route_route_arginfo, ZEND_ACC_PUBLIC) + PHP_ME(yaf_route_static, assemble, yaf_route_assemble_arginfo, ZEND_ACC_PUBLIC) {NULL, NULL, NULL} }; /* }}} */ diff --git a/routes/yaf_route_supervar.c b/routes/yaf_route_supervar.c index e8679570..0281bbac 100644 --- a/routes/yaf_route_supervar.c +++ b/routes/yaf_route_supervar.c @@ -31,6 +31,7 @@ #include "routes/yaf_route_interface.h" #include "routes/yaf_route_static.h" /* for yaf_route_pathinfo_route */ #include "routes/yaf_route_supervar.h" +#include "ext/standard/php_smart_str.h" /* for smart_str */ zend_class_entry *yaf_route_supervar_ce; @@ -56,7 +57,7 @@ int yaf_route_supervar_route(yaf_route_t *route, yaf_request_t *request TSRMLS_D } req_uri = estrndup(Z_STRVAL_P(zuri), Z_STRLEN_P(zuri)); - yaf_route_pathinfo_route(request, req_uri, Z_STRLEN_P(zuri) TSRMLS_CC); + yaf_route_pathinfo_route(request, req_uri, Z_STRLEN_P(zuri) TSRMLS_CC); efree(req_uri); return 1; } @@ -97,6 +98,76 @@ PHP_METHOD(yaf_route_supervar, route) { } /** }}} */ +/** {{{ zval * yaf_route_supervar_assemble(zval *mvc, zval *query TSRMLS_DC) + */ +zval * yaf_route_supervar_assemble(yaf_route_t *this_ptr, zval *mvc, zval *query TSRMLS_DC) { + smart_str tvalue = {0}; + zval *pname; + zval *uri; + + MAKE_STD_ZVAL(uri); + + pname = zend_read_property(yaf_route_supervar_ce, this_ptr, ZEND_STRL(YAF_ROUTE_SUPERVAR_PROPETY_NAME_VAR), 1 TSRMLS_CC); + + do { + zval **tmp; + + smart_str_appendc(&tvalue, '?'); + smart_str_appendl(&tvalue, Z_STRVAL_P(pname), Z_STRLEN_P(pname)); + smart_str_appendc(&tvalue, '='); + + if (zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_VAR_NAME_MODULE), (void **)&tmp) == SUCCESS) { + smart_str_appendc(&tvalue, '/'); + smart_str_appendl(&tvalue, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp)); + } + + if (zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_VAR_NAME_CONTROLLER), (void **)&tmp) == FAILURE) { + yaf_trigger_error(YAF_ERR_TYPE_ERROR TSRMLS_CC, "%s", "You need to specify the controller"); + break; + } + + smart_str_appendc(&tvalue, '/'); + smart_str_appendl(&tvalue, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp)); + + if(zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_VAR_NAME_ACTION), (void **)&tmp) == FAILURE) { + yaf_trigger_error(YAF_ERR_TYPE_ERROR TSRMLS_CC, "%s", "You need to specify the action"); + break; + } + + smart_str_appendc(&tvalue, '/'); + smart_str_appendl(&tvalue, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp)); + + if (IS_ARRAY == Z_TYPE_P(query)) { + uint key_type, key_len; + char *key; + ulong key_idx; + + for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(query)); + zend_hash_get_current_data(Z_ARRVAL_P(query), (void **)&tmp) == SUCCESS; + zend_hash_move_forward(Z_ARRVAL_P(query))) { + + if (IS_STRING == Z_TYPE_PP(tmp) + && HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(Z_ARRVAL_P(query), &key, &key_len, &key_idx, 0, NULL)) { + smart_str_appendc(&tvalue, '&'); + smart_str_appendl(&tvalue, key, key_len - 1); + smart_str_appendc(&tvalue, '='); + smart_str_appendl(&tvalue, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp)); + } + } + } + + smart_str_0(&tvalue); + ZVAL_STRING(uri, tvalue.c, 1); + smart_str_free(&tvalue); + return uri; + } while (0); + + ZVAL_NULL(uri); + return uri; +} +/* }}} */ + + /** {{{ proto public Yaf_Route_Supervar::__construct(string $varname) */ PHP_METHOD(yaf_route_supervar, __construct) { @@ -117,12 +188,29 @@ PHP_METHOD(yaf_route_supervar, __construct) { } /** }}} */ +/** {{{ proto public Yaf_Route_Supervar::assemble(array $mvc[, array $query = NULL]) + */ +PHP_METHOD(yaf_route_supervar, assemble) { + zval *mvc, *query; + zval *return_uri; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|a", &mvc, &query) == FAILURE) { + return; + } else { + return_uri = yaf_route_supervar_assemble(getThis(), mvc, query TSRMLS_CC); + RETURN_ZVAL(return_uri, 0, 1); + } + +} +/* }}} */ + /** {{{ yaf_route_supervar_methods */ zend_function_entry yaf_route_supervar_methods[] = { PHP_ME(yaf_route_supervar, __construct, yaf_route_supervar_construct_arginfo, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR) PHP_ME(yaf_route_supervar, route, yaf_route_route_arginfo, ZEND_ACC_PUBLIC) - {NULL, NULL, NULL} + PHP_ME(yaf_route_supervar, assemble, yaf_route_assemble_arginfo, ZEND_ACC_PUBLIC) + {NULL, NULL, NULL} }; /* }}} */ diff --git a/tests/013.phpt b/tests/013.phpt index f8492094..867c1cf9 100644 --- a/tests/013.phpt +++ b/tests/013.phpt @@ -92,7 +92,13 @@ Array [2] => value ) - [_verify:protected] => + [_verify:protected] => Array + ( + [controller] => Index + [action] => action + ) + + [_reverse:protected] => ) [simple] => Yaf_Route_Simple Object @@ -116,7 +122,13 @@ Array [action] => action ) - [_verify:protected] => + [_verify:protected] => Array + ( + [controller] => Index + [action] => action + ) + + [_reverse:protected] => ) ) diff --git a/tests/068.phpt b/tests/068.phpt new file mode 100644 index 00000000..0011a29c --- /dev/null +++ b/tests/068.phpt @@ -0,0 +1,35 @@ +--TEST-- +Check for Yaf_Route_Regex::assemble +--SKIPIF-- + +--FILE-- + "product", //route to product controller, + ), + array(), + array(), + '/:m/:c/:a' +); + +$router->addRoute("regex", $route); + +var_dump($router->getRoute('regex')->assemble( + array( + ':m' => 'module', + ':c' => 'controller', + ':a' => 'action' + ), + array( + 'tkey1' => 'tval1', + 'tkey2' => 'tval2' + ) + ) +); +--EXPECTF-- +string(49) "/module/controller/action?tkey1=tval1&tkey2=tval2" diff --git a/tests/069.phpt b/tests/069.phpt new file mode 100644 index 00000000..48a486d8 --- /dev/null +++ b/tests/069.phpt @@ -0,0 +1,27 @@ +--TEST-- +Check for Yaf_Route_Simple::assemble +--SKIPIF-- + +--FILE-- +addRoute("simple", $route); + +var_dump($router->getRoute('simple')->assemble( + array( + 'action' => 'yafaction', + 'tkey' => 'tval', + 'controller' => 'yafcontroller', + 'module' => 'yafmodule' + ), + array( + 'tkey1' => 'tval1', + 'tkey2' => 'tval2' + ) +)); +--EXPECTF-- +string(64) "?m=yafmodule&c=yafcontroller&a=yafaction&tkey1=tval1&tkey2=tval2" diff --git a/tests/070.phpt b/tests/070.phpt new file mode 100644 index 00000000..a789c4f3 --- /dev/null +++ b/tests/070.phpt @@ -0,0 +1,27 @@ +--TEST-- +Check for Yaf_Route_Supervar::assemble +--SKIPIF-- + +--FILE-- +addRoute("supervar", $route); + +var_dump($router->getRoute('supervar')->assemble( + array( + 'action' => 'yafaction', + 'tkey' => 'tval', + 'controller' => 'yafcontroller', + 'module' => 'yafmodule' + ), + array( + 'tkey1' => 'tval1', + 'tkey2' => 'tval2' + ) +)); +--EXPECTF-- +string(61) "?r=/yafmodule/yafcontroller/yafaction&tkey1=tval1&tkey2=tval2" diff --git a/tests/071.phpt b/tests/071.phpt new file mode 100644 index 00000000..77afd364 --- /dev/null +++ b/tests/071.phpt @@ -0,0 +1,28 @@ +--TEST-- +Check for Yaf_Route_Static::assemble +--SKIPIF-- + +--FILE-- +addRoute("static", $route); + +var_dump($router->getRoute('static')->assemble( + array( + 'action' => 'yafaction', + 'tkey' => 'tval', + 'controller' => 'yafcontroller', + 'module' => 'yafmodule' + ), + array( + 'tkey1' => 'tval1', + 'tkey2' => 'tval2' + ) + ) +); +--EXPECTF-- +string(59) "/yafmodule/yafcontroller/yafaction?tkey1=tval1&tkey2=tval2&" diff --git a/tests/072.phpt b/tests/072.phpt new file mode 100644 index 00000000..ecfb9d97 --- /dev/null +++ b/tests/072.phpt @@ -0,0 +1,41 @@ +--TEST-- +Check for Yaf_Route_Map::assemble +--SKIPIF-- + +--FILE-- +addRoute("map", $route); + +var_dump($router->getRoute('map')->assemble( + array( + 'controller' => 'foo_bar' + ), + array( + 'tkey1' => 'tval1', + 'tkey2' => 'tval2' + ) + ) +); + +$route = new Yaf_Route_Map(true, '_'); +$router->addRoute("map", $route); + +var_dump($router->getRoute('map')->assemble( + array( + 'action' => 'foo_bar' + ), + array( + 'tkey1' => 'tval1', + 'tkey2' => 'tval2' + ) + ) +); + +--EXPECTF-- +string(32) "/foo/bar?tkey1=tval1&tkey2=tval2" +string(34) "/foo/bar/_/tkey1/tval1/tkey2/tval2" diff --git a/tests/073.phpt b/tests/073.phpt new file mode 100644 index 00000000..e8d8221b --- /dev/null +++ b/tests/073.phpt @@ -0,0 +1,34 @@ +--TEST-- +Check for Yaf_Route_Rewrite::assemble +--SKIPIF-- + +--FILE-- + "product", + ), + array() +); + +$router->addRoute("rewrite", $route); + +var_dump($router->getRoute('rewrite')->assemble( + array( + ':name' => 'foo', + ':id' => 'bar', + ':tmpkey1' => 'tmpval1' + ), + array( + 'tkey1' => 'tval1', + 'tkey2' => 'tval2' + ) + ) +); + +--EXPECTF-- +string(57) "/product/foo/bar/tmpkey1/tmpval1/?tkey1=tval1&tkey2=tval2" From 47e528e35e64c53a3658c6efba721f9a73349c97 Mon Sep 17 00:00:00 2001 From: Demon Date: Tue, 10 Sep 2013 13:43:13 +0800 Subject: [PATCH 2/2] yaf assemble complete --- routes/yaf_route_interface.c | 6 ++-- routes/yaf_route_interface.h | 4 --- routes/yaf_route_map.c | 17 ++++++------ routes/yaf_route_regex.c | 2 +- routes/yaf_route_rewrite.c | 5 ++-- routes/yaf_route_simple.c | 25 +++++++++-------- routes/yaf_route_static.c | 17 ++++++------ routes/yaf_route_supervar.c | 53 ++++++++++++++++++------------------ tests/070.phpt | 6 ++-- tests/071.phpt | 12 ++++---- tests/072.phpt | 8 +++--- tests/074.phpt | 27 ++++++++++++++++++ tests/075.phpt | 36 ++++++++++++++++++++++++ 13 files changed, 141 insertions(+), 77 deletions(-) create mode 100644 tests/074.phpt create mode 100644 tests/075.phpt diff --git a/routes/yaf_route_interface.c b/routes/yaf_route_interface.c index 00bca5bc..ec1f226a 100644 --- a/routes/yaf_route_interface.c +++ b/routes/yaf_route_interface.c @@ -65,10 +65,10 @@ yaf_route_t * yaf_route_instance(yaf_route_t *this_ptr, zval *config TSRMLS_DC) } if (zend_hash_find(Z_ARRVAL_P(config), ZEND_STRS("route"), (void **)&verify) == FAILURE) { - verify = NULL; - } + verify = NULL; + } - instance = yaf_route_rewrite_instance(NULL, *match, *def, verify? *verify : NULL TSRMLS_CC); + instance = yaf_route_rewrite_instance(NULL, *match, *def, verify? *verify : NULL TSRMLS_CC); } else if (Z_STRLEN_PP(ppzval) == (sizeof("regex") - 1) && strncasecmp(Z_STRVAL_PP(ppzval), "regex", sizeof("regex") - 1) == 0) { if (zend_hash_find(Z_ARRVAL_P(config), ZEND_STRS("match"), (void **)&match) == FAILURE || Z_TYPE_PP(match) != IS_STRING) { diff --git a/routes/yaf_route_interface.h b/routes/yaf_route_interface.h index 5832993e..a5f566a1 100644 --- a/routes/yaf_route_interface.h +++ b/routes/yaf_route_interface.h @@ -19,10 +19,6 @@ #ifndef YAF_ROUTER_INTERFACE_H #define YAF_ROUTER_INTERFACE_H -#define YAF_ROUTE_VAR_NAME_MODULE "module" -#define YAF_ROUTE_VAR_NAME_CONTROLLER "controller" -#define YAF_ROUTE_VAR_NAME_ACTION "action" - #define YAF_ROUTE_ASSEMBLE_MOUDLE_FORMAT ":m" #define YAF_ROUTE_ASSEMBLE_ACTION_FORMAT ":a" #define YAF_ROUTE_ASSEMBLE_CONTROLLER_FORMAT ":c" diff --git a/routes/yaf_route_map.c b/routes/yaf_route_map.c index 88a6990a..9f0ee13a 100644 --- a/routes/yaf_route_map.c +++ b/routes/yaf_route_map.c @@ -175,14 +175,14 @@ zval * yaf_route_map_assemble(yaf_route_t *this_ptr, zval *mvc, zval *query TSRM do { if (Z_BVAL_P(ctl_prefer)) { - if (zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_VAR_NAME_ACTION), (void **)&tmp_data) == SUCCESS) { + if (zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_ASSEMBLE_ACTION_FORMAT), (void **)&tmp_data) == SUCCESS) { pname = estrndup(Z_STRVAL_PP(tmp_data), Z_STRLEN_PP(tmp_data)); } else { yaf_trigger_error(YAF_ERR_TYPE_ERROR TSRMLS_CC, "%s", "Undefined the 'action' parameter for the 1st parameter"); break; } } else { - if (zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_VAR_NAME_CONTROLLER), (void **)&tmp_data) == SUCCESS) { + if (zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_ASSEMBLE_CONTROLLER_FORMAT), (void **)&tmp_data) == SUCCESS) { pname = estrndup(Z_STRVAL_PP(tmp_data), Z_STRLEN_PP(tmp_data)); } else { yaf_trigger_error(YAF_ERR_TYPE_ERROR TSRMLS_CC, "%s", "Undefined the 'controller' parameter for the 1st parameter"); @@ -275,12 +275,13 @@ PHP_METHOD(yaf_route_map, assemble) { zval *mvc, *query; zval *return_uri; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|a", &mvc, &query) == FAILURE) { - return; - } else { - return_uri = yaf_route_map_assemble(getThis(), mvc, query TSRMLS_CC); - RETURN_ZVAL(return_uri, 0, 1); - } + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|a", &mvc, &query) == FAILURE) { + return; + } else { + if (return_uri = yaf_route_map_assemble(getThis(), mvc, query TSRMLS_CC)) { + RETURN_ZVAL(return_uri, 0, 1); + } + } } /* }}} */ diff --git a/routes/yaf_route_regex.c b/routes/yaf_route_regex.c index 2da9cabc..e336108f 100644 --- a/routes/yaf_route_regex.c +++ b/routes/yaf_route_regex.c @@ -159,7 +159,7 @@ zval * yaf_route_regex_assemble(yaf_route_t *this_ptr, zval *mvc, zval *query TS zval *reverse, *uri; zval **tmp; char *tstr, *inter; - uint tlen; + int tlen; smart_str squery = {0}; reverse = zend_read_property(yaf_route_regex_ce, this_ptr, ZEND_STRL(YAF_ROUTE_PROPETY_NAME_REVERSE), 0 TSRMLS_CC); diff --git a/routes/yaf_route_rewrite.c b/routes/yaf_route_rewrite.c index d6cb6d73..9e49eb57 100644 --- a/routes/yaf_route_rewrite.c +++ b/routes/yaf_route_rewrite.c @@ -273,7 +273,8 @@ zval * yaf_route_rewrite_assemble(yaf_route_t *this_ptr, zval *idents, zval *que zval *reverse, *uri, *match, *pidents; zval **tmp; char *tstr, *inter, *seg, *pmatch, *ptrptr, *key; - uint tlen, seg_len, key_len; + int tlen; + uint seg_len, key_len; ulong key_idx; smart_str squery = {0}; smart_str wildcard = {0}; @@ -299,7 +300,7 @@ zval * yaf_route_rewrite_assemble(yaf_route_t *this_ptr, zval *idents, zval *que if (HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(Z_ARRVAL_P(pidents), &key, &key_len, &key_idx, 0, NULL)) { if (IS_STRING == Z_TYPE_PP(tmp)) { - smart_str_appendl(&wildcard, key + 1, key_len); + smart_str_appendl(&wildcard, key + 1, key_len - 2); smart_str_appendl(&wildcard, YAF_ROUTER_URL_DELIMIETER, 1); smart_str_appendl(&wildcard, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp)); smart_str_appendl(&wildcard, YAF_ROUTER_URL_DELIMIETER, 1); diff --git a/routes/yaf_route_simple.c b/routes/yaf_route_simple.c index 9c197d47..22d460e1 100644 --- a/routes/yaf_route_simple.c +++ b/routes/yaf_route_simple.c @@ -124,14 +124,14 @@ zval * yaf_route_simple_assemble(yaf_route_t *this_ptr, zval *mvc, zval *query T do { zval **tmp; - if (zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_VAR_NAME_MODULE), (void **)&tmp) == SUCCESS) { + if (zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_ASSEMBLE_MOUDLE_FORMAT), (void **)&tmp) == SUCCESS) { smart_str_appendl(&tvalue, Z_STRVAL_P(nmodule), Z_STRLEN_P(nmodule)); smart_str_appendc(&tvalue, '='); smart_str_appendl(&tvalue, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp)); smart_str_appendc(&tvalue, '&'); } - if (zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_VAR_NAME_CONTROLLER), (void **)&tmp) == FAILURE) { + if (zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_ASSEMBLE_CONTROLLER_FORMAT), (void **)&tmp) == FAILURE) { yaf_trigger_error(YAF_ERR_TYPE_ERROR TSRMLS_CC, "%s", "You need to specify the controller"); break; } @@ -141,7 +141,7 @@ zval * yaf_route_simple_assemble(yaf_route_t *this_ptr, zval *mvc, zval *query T smart_str_appendl(&tvalue, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp)); smart_str_appendc(&tvalue, '&'); - if(zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_VAR_NAME_ACTION), (void **)&tmp) == FAILURE) { + if(zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_ASSEMBLE_ACTION_FORMAT), (void **)&tmp) == FAILURE) { yaf_trigger_error(YAF_ERR_TYPE_ERROR TSRMLS_CC, "%s", "You need to specify the action"); break; } @@ -205,15 +205,16 @@ PHP_METHOD(yaf_route_simple, __construct) { /** {{{ proto public Yaf_Route_Simple::assemble(array $mvc[, array $query = NULL]) */ PHP_METHOD(yaf_route_simple, assemble) { - zval *mvc, *query; - zval *return_uri; - - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|a", &mvc, &query) == FAILURE) { - return; - } else { - return_uri = yaf_route_simple_assemble(getThis(), mvc, query TSRMLS_CC); - RETURN_ZVAL(return_uri, 0, 1); - } + zval *mvc, *query; + zval *return_uri; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|a", &mvc, &query) == FAILURE) { + return; + } else { + if (return_uri = yaf_route_simple_assemble(getThis(), mvc, query TSRMLS_CC)) { + RETURN_ZVAL(return_uri, 0, 1); + } + } } /* }}} */ diff --git a/routes/yaf_route_static.c b/routes/yaf_route_static.c index 046519e7..cbffb02f 100644 --- a/routes/yaf_route_static.c +++ b/routes/yaf_route_static.c @@ -190,12 +190,12 @@ zval * yaf_route_static_assemble(yaf_route_t *this_ptr, zval *mvc, zval *query T do { zval **tmp; - if (zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_VAR_NAME_MODULE), (void **)&tmp) == SUCCESS) { + if (zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_ASSEMBLE_MOUDLE_FORMAT), (void **)&tmp) == SUCCESS) { smart_str_appendc(&tvalue, '/'); smart_str_appendl(&tvalue, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp)); } - if (zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_VAR_NAME_CONTROLLER), (void **)&tmp) == FAILURE) { + if (zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_ASSEMBLE_CONTROLLER_FORMAT), (void **)&tmp) == FAILURE) { yaf_trigger_error(YAF_ERR_TYPE_ERROR TSRMLS_CC, "%s", "You need to specify the controller"); break; } @@ -203,7 +203,7 @@ zval * yaf_route_static_assemble(yaf_route_t *this_ptr, zval *mvc, zval *query T smart_str_appendc(&tvalue, '/'); smart_str_appendl(&tvalue, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp)); - if(zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_VAR_NAME_ACTION), (void **)&tmp) == FAILURE) { + if(zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_ASSEMBLE_ACTION_FORMAT), (void **)&tmp) == FAILURE) { yaf_trigger_error(YAF_ERR_TYPE_ERROR TSRMLS_CC, "%s", "You need to specify the action"); break; } @@ -268,12 +268,13 @@ PHP_METHOD(yaf_route_static, assemble) { zval *mvc, *query; zval *return_uri; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|a", &mvc, &query) == FAILURE) { - return; - } else { - return_uri = yaf_route_static_assemble(getThis(), mvc, query TSRMLS_CC); - RETURN_ZVAL(return_uri, 0, 1); + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|a", &mvc, &query) == FAILURE) { + return; + } else { + if (return_uri = yaf_route_static_assemble(getThis(), mvc, query TSRMLS_CC)) { + RETURN_ZVAL(return_uri, 0, 1); } + } } /* }}} */ diff --git a/routes/yaf_route_supervar.c b/routes/yaf_route_supervar.c index 0281bbac..d27490c8 100644 --- a/routes/yaf_route_supervar.c +++ b/routes/yaf_route_supervar.c @@ -107,7 +107,7 @@ zval * yaf_route_supervar_assemble(yaf_route_t *this_ptr, zval *mvc, zval *query MAKE_STD_ZVAL(uri); - pname = zend_read_property(yaf_route_supervar_ce, this_ptr, ZEND_STRL(YAF_ROUTE_SUPERVAR_PROPETY_NAME_VAR), 1 TSRMLS_CC); + pname = zend_read_property(yaf_route_supervar_ce, this_ptr, ZEND_STRL(YAF_ROUTE_SUPERVAR_PROPETY_NAME_VAR), 1 TSRMLS_CC); do { zval **tmp; @@ -116,12 +116,12 @@ zval * yaf_route_supervar_assemble(yaf_route_t *this_ptr, zval *mvc, zval *query smart_str_appendl(&tvalue, Z_STRVAL_P(pname), Z_STRLEN_P(pname)); smart_str_appendc(&tvalue, '='); - if (zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_VAR_NAME_MODULE), (void **)&tmp) == SUCCESS) { + if (zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_ASSEMBLE_MOUDLE_FORMAT), (void **)&tmp) == SUCCESS) { smart_str_appendc(&tvalue, '/'); smart_str_appendl(&tvalue, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp)); } - if (zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_VAR_NAME_CONTROLLER), (void **)&tmp) == FAILURE) { + if (zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_ASSEMBLE_CONTROLLER_FORMAT), (void **)&tmp) == FAILURE) { yaf_trigger_error(YAF_ERR_TYPE_ERROR TSRMLS_CC, "%s", "You need to specify the controller"); break; } @@ -129,7 +129,7 @@ zval * yaf_route_supervar_assemble(yaf_route_t *this_ptr, zval *mvc, zval *query smart_str_appendc(&tvalue, '/'); smart_str_appendl(&tvalue, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp)); - if(zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_VAR_NAME_ACTION), (void **)&tmp) == FAILURE) { + if(zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_ASSEMBLE_ACTION_FORMAT), (void **)&tmp) == FAILURE) { yaf_trigger_error(YAF_ERR_TYPE_ERROR TSRMLS_CC, "%s", "You need to specify the action"); break; } @@ -169,43 +169,44 @@ zval * yaf_route_supervar_assemble(yaf_route_t *this_ptr, zval *mvc, zval *query /** {{{ proto public Yaf_Route_Supervar::__construct(string $varname) - */ +*/ PHP_METHOD(yaf_route_supervar, __construct) { - zval *var; + zval *var; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &var) == FAILURE) { - YAF_UNINITIALIZED_OBJECT(getThis()); - return; - } + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &var) == FAILURE) { + YAF_UNINITIALIZED_OBJECT(getThis()); + return; + } - if (Z_TYPE_P(var) != IS_STRING || !Z_STRLEN_P(var)) { - YAF_UNINITIALIZED_OBJECT(getThis()); - yaf_trigger_error(YAF_ERR_TYPE_ERROR TSRMLS_CC, "Expects a valid string super var name", yaf_route_supervar_ce->name); - RETURN_FALSE; - } + if (Z_TYPE_P(var) != IS_STRING || !Z_STRLEN_P(var)) { + YAF_UNINITIALIZED_OBJECT(getThis()); + yaf_trigger_error(YAF_ERR_TYPE_ERROR TSRMLS_CC, "Expects a valid string super var name", yaf_route_supervar_ce->name); + RETURN_FALSE; + } - zend_update_property(yaf_route_supervar_ce, getThis(), ZEND_STRL(YAF_ROUTE_SUPERVAR_PROPETY_NAME_VAR), var TSRMLS_CC); + zend_update_property(yaf_route_supervar_ce, getThis(), ZEND_STRL(YAF_ROUTE_SUPERVAR_PROPETY_NAME_VAR), var TSRMLS_CC); } /** }}} */ /** {{{ proto public Yaf_Route_Supervar::assemble(array $mvc[, array $query = NULL]) - */ +*/ PHP_METHOD(yaf_route_supervar, assemble) { - zval *mvc, *query; - zval *return_uri; + zval *mvc, *query; + zval *return_uri; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|a", &mvc, &query) == FAILURE) { - return; - } else { - return_uri = yaf_route_supervar_assemble(getThis(), mvc, query TSRMLS_CC); - RETURN_ZVAL(return_uri, 0, 1); - } + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|a", &mvc, &query) == FAILURE) { + return; + } else { + if (return_uri = yaf_route_supervar_assemble(getThis(), mvc, query TSRMLS_CC)) { + RETURN_ZVAL(return_uri, 0, 1); + } + } } /* }}} */ /** {{{ yaf_route_supervar_methods - */ +*/ zend_function_entry yaf_route_supervar_methods[] = { PHP_ME(yaf_route_supervar, __construct, yaf_route_supervar_construct_arginfo, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR) PHP_ME(yaf_route_supervar, route, yaf_route_route_arginfo, ZEND_ACC_PUBLIC) diff --git a/tests/070.phpt b/tests/070.phpt index a789c4f3..235cd96c 100644 --- a/tests/070.phpt +++ b/tests/070.phpt @@ -13,10 +13,10 @@ $router->addRoute("supervar", $route); var_dump($router->getRoute('supervar')->assemble( array( - 'action' => 'yafaction', + ':a' => 'yafaction', 'tkey' => 'tval', - 'controller' => 'yafcontroller', - 'module' => 'yafmodule' + ':c' => 'yafcontroller', + ':m' => 'yafmodule' ), array( 'tkey1' => 'tval1', diff --git a/tests/071.phpt b/tests/071.phpt index 77afd364..02d12684 100644 --- a/tests/071.phpt +++ b/tests/071.phpt @@ -13,16 +13,16 @@ $router->addRoute("static", $route); var_dump($router->getRoute('static')->assemble( array( - 'action' => 'yafaction', + ':a' => 'yafaction', 'tkey' => 'tval', - 'controller' => 'yafcontroller', - 'module' => 'yafmodule' - ), + ':c' => 'yafcontroller', + ':m' => 'yafmodule' + ), array( 'tkey1' => 'tval1', 'tkey2' => 'tval2' - ) - ) + ) + ) ); --EXPECTF-- string(59) "/yafmodule/yafcontroller/yafaction?tkey1=tval1&tkey2=tval2&" diff --git a/tests/072.phpt b/tests/072.phpt index ecfb9d97..f46d8d9a 100644 --- a/tests/072.phpt +++ b/tests/072.phpt @@ -13,13 +13,13 @@ $router->addRoute("map", $route); var_dump($router->getRoute('map')->assemble( array( - 'controller' => 'foo_bar' + ':c' => 'foo_bar' ), array( 'tkey1' => 'tval1', 'tkey2' => 'tval2' - ) ) + ) ); $route = new Yaf_Route_Map(true, '_'); @@ -27,13 +27,13 @@ $router->addRoute("map", $route); var_dump($router->getRoute('map')->assemble( array( - 'action' => 'foo_bar' + ':a' => 'foo_bar' ), array( 'tkey1' => 'tval1', 'tkey2' => 'tval2' - ) ) + ) ); --EXPECTF-- diff --git a/tests/074.phpt b/tests/074.phpt new file mode 100644 index 00000000..8166cbc7 --- /dev/null +++ b/tests/074.phpt @@ -0,0 +1,27 @@ +--TEST-- +Check for Yaf_Route_Simple::assemble +--SKIPIF-- + +--FILE-- +addRoute("simple", $route); + +var_dump($router->getRoute('simple')->assemble( + array( + ':a' => 'yafaction', + 'tkey' => 'tval', + ':c' => 'yafcontroller', + ':m' => 'yafmodule' + ), + array( + 'tkey1' => 'tval1', + 'tkey2' => 'tval2' + ) + )); +--EXPECTF-- +string(64) "?m=yafmodule&c=yafcontroller&a=yafaction&tkey1=tval1&tkey2=tval2" diff --git a/tests/075.phpt b/tests/075.phpt new file mode 100644 index 00000000..90fd77f6 --- /dev/null +++ b/tests/075.phpt @@ -0,0 +1,36 @@ +--TEST-- +Check for Yaf_Route_Regex::assemble +--SKIPIF-- + +--FILE-- + "product", //route to product controller, + ), + array(), + array(), + '/:m/:c/:a' + ); + +$router->addRoute("regex", $route); + +var_dump($router->getRoute('regex')->assemble( + array( + ':m' => 'module', + ':c' => 'controller', + ':a' => 'action' + ), + array( + 'tkey1' => 'tval1', + 'tkey2' => + 'tval2' + ) + ) + ); +--EXPECTF-- +string(49) "/module/controller/action?tkey1=tval1&tkey2=tval2"