Skip to content

Commit

Permalink
Yaf_Route_Regex's map argument now is optional
Browse files Browse the repository at this point in the history
  • Loading branch information
laruence committed Jan 16, 2013
1 parent 7a78892 commit b057202
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
4 changes: 2 additions & 2 deletions routes/interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ yaf_route_t * yaf_route_instance(yaf_route_t *this_ptr, zval *config TSRMLS_DC)
return NULL;
}
if (zend_hash_find(Z_ARRVAL_P(config), ZEND_STRS("map"), (void **)&map) == FAILURE || Z_TYPE_PP(map) != IS_ARRAY) {
return NULL;
map = NULL;
}

instance = yaf_route_regex_instance(NULL, *match, *def, *map, NULL TSRMLS_CC);
instance = yaf_route_regex_instance(NULL, *match, *def, map? *map : NULL, 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
14 changes: 10 additions & 4 deletions routes/regex.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ yaf_route_t * yaf_route_regex_instance(yaf_route_t *this_ptr, zval *route, zval

zend_update_property(yaf_route_regex_ce, instance, ZEND_STRL(YAF_ROUTE_PROPETY_NAME_MATCH), route TSRMLS_CC);
zend_update_property(yaf_route_regex_ce, instance, ZEND_STRL(YAF_ROUTE_PROPETY_NAME_ROUTE), def TSRMLS_CC);
zend_update_property(yaf_route_regex_ce, instance, ZEND_STRL(YAF_ROUTE_PROPETY_NAME_MAP), map TSRMLS_CC);

if (map) {
zend_update_property(yaf_route_regex_ce, instance, ZEND_STRL(YAF_ROUTE_PROPETY_NAME_MAP), map TSRMLS_CC);
}

if (!verify) {
zend_update_property_null(yaf_route_regex_ce, instance, ZEND_STRL(YAF_ROUTE_PROPETY_NAME_VERIFY) TSRMLS_CC);
Expand Down Expand Up @@ -76,6 +79,9 @@ static zval * yaf_route_regex_match(yaf_route_t *route, char *uir, int len TSRML
ZVAL_NULL(subparts);

map = zend_read_property(yaf_route_regex_ce, route, ZEND_STRL(YAF_ROUTE_PROPETY_NAME_MAP), 1 TSRMLS_CC);
if (IS_ARRAY != Z_TYPE_P(map)) {
map = NULL;
}

php_pcre_match_impl(pce_regexp, uir, len, matches, subparts /* subpats */,
0/* global */, 0/* ZEND_NUM_ARGS() >= 4 */, 0/*flags PREG_OFFSET_CAPTURE*/, 0/* start_offset */ TSRMLS_CC);
Expand Down Expand Up @@ -104,7 +110,7 @@ static zval * yaf_route_regex_match(yaf_route_t *route, char *uir, int len TSRML
}

if (zend_hash_get_current_key_ex(ht, &key, &len, &idx, 0, NULL) == HASH_KEY_IS_LONG) {
if (zend_hash_index_find(Z_ARRVAL_P(map), idx, (void **)&name) == SUCCESS) {
if (map && zend_hash_index_find(Z_ARRVAL_P(map), idx, (void **)&name) == SUCCESS) {
Z_ADDREF_P(*ppzval);
zend_hash_update(Z_ARRVAL_P(ret), Z_STRVAL_PP(name), Z_STRLEN_PP(name) + 1, (void **)ppzval, sizeof(zval *), NULL);
}
Expand Down Expand Up @@ -215,10 +221,10 @@ PHP_METHOD(yaf_route_regex, route) {
/** {{{ proto public Yaf_Route_Regex::__construct(string $match, array $route, array $map = NULL, array $verify = NULL)
*/
PHP_METHOD(yaf_route_regex, __construct) {
zval *match, *route, *map, *verify = NULL;
zval *match, *route, *map = NULL, *verify = NULL;
yaf_route_t *self = getThis();

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zaa|a", &match, &route, &map, &verify) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "za|aa", &match, &route, &map, &verify) == FAILURE) {
YAF_UNINITIALIZED_OBJECT(getThis());
return;
}
Expand Down
40 changes: 40 additions & 0 deletions tests/065.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
--TEST--
Yaf_Route_Regex map is optional
--SKIPIF--
<?php if (!extension_loaded("yaf")) print "skip"; ?>
--FILE--
<?php

$request = new Yaf_Request_Http("/subdir/ap/1.2/name/value", "/subdir");

$router = new Yaf_Router();

$router->addConfig(
array(
array(
"type" => "regex",
"match" => "#^/ap/([^/]*)/*#i",
"route" => array(
array(
"action" => 'ap',
),
),
)
)
)->route($request);

var_dump($router->getCurrentRoute());
var_dump($request->getActionName());


$router->addRoute("regex", new Yaf_Route_Regex("#^/ap/([^/]*)/*#i", array("action" => "ap")))->route($request);

var_dump($router->getCurrentRoute());
var_dump($request->getActionName());

?>
--EXPECT--
int(0)
NULL
string(5) "regex"
string(2) "ap"

0 comments on commit b057202

Please sign in to comment.