Skip to content

Commit

Permalink
Merge branch 'feature-assemble' of https://github.com/zxcvdavid/php-yaf
Browse files Browse the repository at this point in the history
Conflicts:
	tests/070.phpt
	tests/071.phpt
	tests/072.phpt
  • Loading branch information
laruence committed Dec 7, 2013
2 parents f2a23ac + 47e528e commit 42a85bd
Show file tree
Hide file tree
Showing 23 changed files with 953 additions and 38 deletions.
19 changes: 16 additions & 3 deletions routes/yaf_route_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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}
};
/* }}} */
Expand Down
11 changes: 11 additions & 0 deletions routes/yaf_route_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@
#ifndef YAF_ROUTER_INTERFACE_H
#define YAF_ROUTER_INTERFACE_H

#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 '#'
Expand All @@ -31,6 +36,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);
Expand Down
12 changes: 12 additions & 0 deletions routes/yaf_route_interface.lo
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# routes/yaf_route_interface.lo - a libtool object file
# Generated by ltmain.sh - GNU libtool 1.5.26 (1.1220.2.492 2008/01/30 06:40:56)
#
# Please DO NOT delete this file!
# It is necessary for linking the library.

# Name of the PIC object.
pic_object='.libs/yaf_route_interface.o'

# Name of the non-PIC object.
non_pic_object=none

113 changes: 113 additions & 0 deletions routes/yaf_route_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_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_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");
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) {
Expand All @@ -173,11 +269,28 @@ 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 {
if (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}
};
/* }}} */
Expand Down
12 changes: 12 additions & 0 deletions routes/yaf_route_map.lo
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# routes/yaf_route_map.lo - a libtool object file
# Generated by ltmain.sh - GNU libtool 1.5.26 (1.1220.2.492 2008/01/30 06:40:56)
#
# Please DO NOT delete this file!
# It is necessary for linking the library.

# Name of the PIC object.
pic_object='.libs/yaf_route_map.o'

# Name of the non-PIC object.
non_pic_object=none

Loading

0 comments on commit 42a85bd

Please sign in to comment.