Skip to content

Commit 42a85bd

Browse files
committed
Merge branch 'feature-assemble' of https://github.com/zxcvdavid/php-yaf
Conflicts: tests/070.phpt tests/071.phpt tests/072.phpt
2 parents f2a23ac + 47e528e commit 42a85bd

23 files changed

+953
-38
lines changed

routes/yaf_route_interface.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ zend_class_entry *yaf_route_ce;
4141
/* {{{ yaf_route_t * yaf_route_instance(yaf_route_t *this_ptr, zval *config TSRMLS_DC)
4242
*/
4343
yaf_route_t * yaf_route_instance(yaf_route_t *this_ptr, zval *config TSRMLS_DC) {
44-
zval **match, **def, **map, **ppzval;
44+
zval **match, **def, **map, **verify, **reverse, **ppzval;
4545
yaf_route_t *instance = NULL;
4646

4747
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)
6464
return NULL;
6565
}
6666

67-
instance = yaf_route_rewrite_instance(NULL, *match, *def, NULL TSRMLS_CC);
67+
if (zend_hash_find(Z_ARRVAL_P(config), ZEND_STRS("route"), (void **)&verify) == FAILURE) {
68+
verify = NULL;
69+
}
70+
71+
instance = yaf_route_rewrite_instance(NULL, *match, *def, verify? *verify : NULL TSRMLS_CC);
6872
} else if (Z_STRLEN_PP(ppzval) == (sizeof("regex") - 1)
6973
&& strncasecmp(Z_STRVAL_PP(ppzval), "regex", sizeof("regex") - 1) == 0) {
7074
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)
7882
map = NULL;
7983
}
8084

81-
instance = yaf_route_regex_instance(NULL, *match, *def, map? *map : NULL, NULL TSRMLS_CC);
85+
if (zend_hash_find(Z_ARRVAL_P(config), ZEND_STRS("route"), (void **)&verify) == FAILURE) {
86+
verify = NULL;
87+
}
88+
89+
if (zend_hash_find(Z_ARRVAL_P(config), ZEND_STRS("route"), (void **)&reverse) == FAILURE) {
90+
reverse = NULL;
91+
}
92+
93+
instance = yaf_route_regex_instance(NULL, *match, *def, map? *map : NULL, verify? *verify : NULL, reverse? *reverse : NULL TSRMLS_CC);
8294
} else if (Z_STRLEN_PP(ppzval) == (sizeof("map") - 1)
8395
&& strncasecmp(Z_STRVAL_PP(ppzval), "map", sizeof("map") - 1) == 0) {
8496
char *delimiter = NULL;
@@ -134,6 +146,7 @@ yaf_route_t * yaf_route_instance(yaf_route_t *this_ptr, zval *config TSRMLS_DC)
134146
*/
135147
zend_function_entry yaf_route_methods[] = {
136148
PHP_ABSTRACT_ME(yaf_route, route, yaf_route_route_arginfo)
149+
PHP_ABSTRACT_ME(yaf_route, assemble, yaf_route_assemble_arginfo)
137150
{NULL, NULL, NULL}
138151
};
139152
/* }}} */

routes/yaf_route_interface.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,15 @@
1919
#ifndef YAF_ROUTER_INTERFACE_H
2020
#define YAF_ROUTER_INTERFACE_H
2121

22+
#define YAF_ROUTE_ASSEMBLE_MOUDLE_FORMAT ":m"
23+
#define YAF_ROUTE_ASSEMBLE_ACTION_FORMAT ":a"
24+
#define YAF_ROUTE_ASSEMBLE_CONTROLLER_FORMAT ":c"
25+
2226
#define YAF_ROUTE_PROPETY_NAME_MATCH "_route"
2327
#define YAF_ROUTE_PROPETY_NAME_ROUTE "_default"
2428
#define YAF_ROUTE_PROPETY_NAME_MAP "_maps"
2529
#define YAF_ROUTE_PROPETY_NAME_VERIFY "_verify"
30+
#define YAF_ROUTE_PROPETY_NAME_REVERSE "_reverse"
2631

2732
#define YAF_ROUTER_URL_DELIMIETER "/"
2833
#define YAF_ROUTE_REGEX_DILIMITER '#'
@@ -31,6 +36,12 @@ YAF_BEGIN_ARG_INFO_EX(yaf_route_route_arginfo, 0, 0, 1)
3136
YAF_ARG_INFO(0, request)
3237
YAF_END_ARG_INFO()
3338

39+
YAF_BEGIN_ARG_INFO_EX(yaf_route_assemble_arginfo, 0, 0, 1)
40+
YAF_ARG_INFO(0, mvc)
41+
YAF_ARG_INFO(0, query)
42+
YAF_END_ARG_INFO()
43+
44+
3445
extern zend_class_entry *yaf_route_ce;
3546

3647
yaf_route_t * yaf_route_instance(yaf_route_t *this_ptr, zval *config TSRMLS_DC);

routes/yaf_route_interface.lo

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# routes/yaf_route_interface.lo - a libtool object file
2+
# Generated by ltmain.sh - GNU libtool 1.5.26 (1.1220.2.492 2008/01/30 06:40:56)
3+
#
4+
# Please DO NOT delete this file!
5+
# It is necessary for linking the library.
6+
7+
# Name of the PIC object.
8+
pic_object='.libs/yaf_route_interface.o'
9+
10+
# Name of the non-PIC object.
11+
non_pic_object=none
12+

routes/yaf_route_map.c

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,102 @@ PHP_METHOD(yaf_route_map, route) {
156156
}
157157
/* }}} */
158158

159+
160+
/** {{{ zval * yaf_route_map_assemble(zval *mvc, zval *query TSRMLS_DC)
161+
*/
162+
zval * yaf_route_map_assemble(yaf_route_t *this_ptr, zval *mvc, zval *query TSRMLS_DC) {
163+
char *tmp, *ptrptr, *pname;
164+
smart_str tvalue = {0};
165+
uint tmp_len, has_delim = 0;
166+
zval *uri, *delim, *ctl_prefer, **tmp_data;
167+
168+
MAKE_STD_ZVAL(uri);
169+
170+
ctl_prefer = zend_read_property(yaf_route_map_ce, this_ptr, ZEND_STRL(YAF_ROUTE_MAP_VAR_NAME_CTL_PREFER), 1 TSRMLS_CC);
171+
delim = zend_read_property(yaf_route_map_ce, this_ptr, ZEND_STRL(YAF_ROUTE_MAP_VAR_NAME_DELIMETER), 1 TSRMLS_CC);
172+
if (IS_STRING == Z_TYPE_P(delim) && Z_STRLEN_P(delim)) {
173+
has_delim = 1;
174+
}
175+
176+
do {
177+
if (Z_BVAL_P(ctl_prefer)) {
178+
if (zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_ASSEMBLE_ACTION_FORMAT), (void **)&tmp_data) == SUCCESS) {
179+
pname = estrndup(Z_STRVAL_PP(tmp_data), Z_STRLEN_PP(tmp_data));
180+
} else {
181+
yaf_trigger_error(YAF_ERR_TYPE_ERROR TSRMLS_CC, "%s", "Undefined the 'action' parameter for the 1st parameter");
182+
break;
183+
}
184+
} else {
185+
if (zend_hash_find(Z_ARRVAL_P(mvc), ZEND_STRS(YAF_ROUTE_ASSEMBLE_CONTROLLER_FORMAT), (void **)&tmp_data) == SUCCESS) {
186+
pname = estrndup(Z_STRVAL_PP(tmp_data), Z_STRLEN_PP(tmp_data));
187+
} else {
188+
yaf_trigger_error(YAF_ERR_TYPE_ERROR TSRMLS_CC, "%s", "Undefined the 'controller' parameter for the 1st parameter");
189+
break;
190+
}
191+
}
192+
193+
tmp = php_strtok_r(pname, "_", &ptrptr);
194+
while(tmp) {
195+
tmp_len = strlen(tmp);
196+
if (tmp_len) {
197+
smart_str_appendc(&tvalue, '/');
198+
smart_str_appendl(&tvalue, tmp, tmp_len);
199+
}
200+
tmp = php_strtok_r(NULL, "_", &ptrptr);
201+
}
202+
efree(pname);
203+
204+
if (IS_ARRAY == Z_TYPE_P(query)) {
205+
uint key_type, key_len, i = 0;
206+
char *key;
207+
ulong key_idx;
208+
zval **tmp_data;
209+
210+
if (has_delim) {
211+
smart_str_appendc(&tvalue, '/');
212+
smart_str_appendl(&tvalue, Z_STRVAL_P(delim), Z_STRLEN_P(delim));
213+
}
214+
215+
for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(query));
216+
zend_hash_get_current_data(Z_ARRVAL_P(query), (void **)&tmp_data) == SUCCESS;
217+
zend_hash_move_forward(Z_ARRVAL_P(query))) {
218+
219+
if (IS_STRING == Z_TYPE_PP(tmp_data)
220+
&& HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(Z_ARRVAL_P(query), &key, &key_len, &key_idx, 0, NULL)) {
221+
222+
if (has_delim) {
223+
smart_str_appendc(&tvalue, '/');
224+
smart_str_appendl(&tvalue, key, key_len - 1);
225+
smart_str_appendc(&tvalue, '/');
226+
smart_str_appendl(&tvalue, Z_STRVAL_PP(tmp_data), Z_STRLEN_PP(tmp_data));
227+
} else {
228+
if (i == 0) {
229+
smart_str_appendc(&tvalue, '?');
230+
smart_str_appendl(&tvalue, key, key_len - 1);
231+
smart_str_appendc(&tvalue, '=');
232+
smart_str_appendl(&tvalue, Z_STRVAL_PP(tmp_data), Z_STRLEN_PP(tmp_data));
233+
} else {
234+
smart_str_appendc(&tvalue, '&');
235+
smart_str_appendl(&tvalue, key, key_len - 1);
236+
smart_str_appendc(&tvalue, '=');
237+
smart_str_appendl(&tvalue, Z_STRVAL_PP(tmp_data), Z_STRLEN_PP(tmp_data));
238+
}
239+
}
240+
}
241+
i += 1;
242+
}
243+
}
244+
245+
smart_str_0(&tvalue);
246+
ZVAL_STRING(uri, tvalue.c, 1);
247+
smart_str_free(&tvalue);
248+
return uri;
249+
} while (0);
250+
251+
ZVAL_NULL(uri);
252+
return uri;
253+
}
254+
159255
/** {{{ proto public Yaf_Route_Simple::__construct(bool $controller_prefer=FALSE, string $delimer = '#!')
160256
*/
161257
PHP_METHOD(yaf_route_map, __construct) {
@@ -173,11 +269,28 @@ PHP_METHOD(yaf_route_map, __construct) {
173269
}
174270
/* }}} */
175271

272+
/** {{{ proto public Yaf_Route_Map::assemble(array $mvc[, array $query = NULL])
273+
*/
274+
PHP_METHOD(yaf_route_map, assemble) {
275+
zval *mvc, *query;
276+
zval *return_uri;
277+
278+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|a", &mvc, &query) == FAILURE) {
279+
return;
280+
} else {
281+
if (return_uri = yaf_route_map_assemble(getThis(), mvc, query TSRMLS_CC)) {
282+
RETURN_ZVAL(return_uri, 0, 1);
283+
}
284+
}
285+
}
286+
/* }}} */
287+
176288
/** {{{ yaf_route_map_methods
177289
*/
178290
zend_function_entry yaf_route_map_methods[] = {
179291
PHP_ME(yaf_route_map, __construct, yaf_route_map_construct_arginfo, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
180292
PHP_ME(yaf_route_map, route, yaf_route_route_arginfo, ZEND_ACC_PUBLIC)
293+
PHP_ME(yaf_route_map, assemble, yaf_route_assemble_arginfo, ZEND_ACC_PUBLIC)
181294
{NULL, NULL, NULL}
182295
};
183296
/* }}} */

routes/yaf_route_map.lo

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# routes/yaf_route_map.lo - a libtool object file
2+
# Generated by ltmain.sh - GNU libtool 1.5.26 (1.1220.2.492 2008/01/30 06:40:56)
3+
#
4+
# Please DO NOT delete this file!
5+
# It is necessary for linking the library.
6+
7+
# Name of the PIC object.
8+
pic_object='.libs/yaf_route_map.o'
9+
10+
# Name of the non-PIC object.
11+
non_pic_object=none
12+

0 commit comments

Comments
 (0)