Skip to content

Commit

Permalink
yaf assemble complete
Browse files Browse the repository at this point in the history
  • Loading branch information
zxcvdavid committed Sep 10, 2013
1 parent b42c4dd commit 47e528e
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 77 deletions.
6 changes: 3 additions & 3 deletions routes/yaf_route_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 0 additions & 4 deletions routes/yaf_route_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
17 changes: 9 additions & 8 deletions routes/yaf_route_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
}
}
}
/* }}} */

Expand Down
2 changes: 1 addition & 1 deletion routes/yaf_route_regex.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions routes/yaf_route_rewrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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);
Expand Down
25 changes: 13 additions & 12 deletions routes/yaf_route_simple.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
}
}
}
/* }}} */

Expand Down
17 changes: 9 additions & 8 deletions routes/yaf_route_static.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,20 +190,20 @@ 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;
}

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;
}
Expand Down Expand Up @@ -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);
}
}
}
/* }}} */

Expand Down
53 changes: 27 additions & 26 deletions routes/yaf_route_supervar.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -116,20 +116,20 @@ 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;
}

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;
}
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions tests/070.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
12 changes: 6 additions & 6 deletions tests/071.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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&"
8 changes: 4 additions & 4 deletions tests/072.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,27 @@ $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, '_');
$router->addRoute("map", $route);

var_dump($router->getRoute('map')->assemble(
array(
'action' => 'foo_bar'
':a' => 'foo_bar'
),
array(
'tkey1' => 'tval1',
'tkey2' => 'tval2'
)
)
)
);

--EXPECTF--
Expand Down
27 changes: 27 additions & 0 deletions tests/074.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
Check for Yaf_Route_Simple::assemble
--SKIPIF--
<?php if (!extension_loaded("yaf")) print "skip"; ?>
--FILE--
<?php

$router = new Yaf_Router();

$route = new Yaf_Route_Simple('m', 'c', 'a');

$router->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"
Loading

0 comments on commit 47e528e

Please sign in to comment.