Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Phalcon\Mvc\Router::convert()/getConverters() #1572

Merged
merged 2 commits into from Nov 21, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 51 additions & 44 deletions ext/mvc/router.c
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,23 @@ PHP_METHOD(Phalcon_Mvc_Router, addHead){
RETURN_MM();
}

static int phalcon_router_call_convert(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
{
zval *route, key = zval_used_for_init;
assert(num_args == 1);

route = va_arg(args, zval*);
if (hash_key->nKeyLength) {
ZVAL_STRINGL(&key, hash_key->arKey, hash_key->nKeyLength-1, 0);
}
else {
ZVAL_LONG(&key, hash_key->h);
}

phalcon_call_method_params(NULL, NULL, route, SL("convert"), zend_inline_hash_func(SS("convert")) TSRMLS_CC, 2, &key, *((zval**)pDest));
return ZEND_HASH_APPLY_KEEP;
}

/**
* Mounts a group of routes in the router
*
Expand All @@ -1159,70 +1176,60 @@ PHP_METHOD(Phalcon_Mvc_Router, addHead){
*/
PHP_METHOD(Phalcon_Mvc_Router, mount){

zval *group, *group_routes, *before_match, *route = NULL;
zval *hostname, *routes, *new_routes;
HashTable *ah0, *ah1;
HashPosition hp0, hp1;
zval **hd;

PHALCON_MM_GROW();
zval *group, *group_routes, *before_match;
zval *hostname, *converters, *routes, *new_routes;
HashPosition hp0;
zval **route;

phalcon_fetch_params(1, 1, 0, &group);
phalcon_fetch_params(0, 1, 0, &group);
PHALCON_VERIFY_CLASS_EX(group, phalcon_mvc_router_group_ce, phalcon_mvc_router_exception_ce, 0);

if (Z_TYPE_P(group) != IS_OBJECT) {
PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_router_exception_ce, "The group of routes is not valid");
return;
}
PHALCON_MM_GROW();

PHALCON_INIT_VAR(group_routes);
phalcon_call_method(group_routes, group, "getroutes");
if (!phalcon_fast_count_ev(group_routes TSRMLS_CC)) {
if (Z_TYPE_P(group_routes) != IS_ARRAY || !zend_hash_num_elements(Z_ARRVAL_P(group_routes))) {
PHALCON_THROW_EXCEPTION_STR(phalcon_mvc_router_exception_ce, "The group of routes does not contain any routes");
return;
}

/**
* Get the before-match condition
*/
/* Get the before-match condition */
PHALCON_INIT_VAR(before_match);
phalcon_call_method(before_match, group, "getbeforematch");
if (Z_TYPE_P(before_match) != IS_NULL) {

phalcon_is_iterable(group_routes, &ah0, &hp0, 0, 0);

while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {

PHALCON_GET_HVALUE(route);

phalcon_call_method_p1_noret(route, "beforematch", before_match);

zend_hash_move_forward_ex(ah0, &hp0);
}

}

/**
* Get the hostname restriction
*/
/* Get the hostname restriction */
PHALCON_INIT_VAR(hostname);
phalcon_call_method(hostname, group, "gethostname");
if (Z_TYPE_P(hostname) != IS_NULL) {

phalcon_is_iterable(group_routes, &ah1, &hp1, 0, 0);

while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) {

PHALCON_GET_HVALUE(route);
/* Get converters */
PHALCON_INIT_VAR(converters);
phalcon_call_method(converters, group, "getconverters");

if (Z_TYPE_P(before_match) != IS_NULL || Z_TYPE_P(hostname) != IS_NULL || Z_TYPE_P(converters) != IS_NULL) {
int has_before_match = (Z_TYPE_P(before_match) != IS_NULL);
int has_hostname = (Z_TYPE_P(hostname) != IS_NULL);
int has_converters = (Z_TYPE_P(converters) != IS_NULL);

for (
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(group_routes), &hp0);
zend_hash_get_current_data_ex(Z_ARRVAL_P(group_routes), (void**)&route, &hp0) == SUCCESS;
zend_hash_move_forward_ex(Z_ARRVAL_P(group_routes), &hp0)
) {
if (has_before_match) {
phalcon_call_method_p1_noret(*route, "beforematch", before_match);
}

phalcon_call_method_p1_noret(route, "sethostname", hostname);
if (has_hostname) {
phalcon_call_method_p1_noret(*route, "sethostname", hostname);
}

zend_hash_move_forward_ex(ah1, &hp1);
if (has_converters) {
zend_hash_apply_with_arguments(Z_ARRVAL_P(converters) TSRMLS_CC, phalcon_router_call_convert, 1, *route);
}
}

}

PHALCON_OBS_VAR(routes);
phalcon_read_property_this(&routes, this_ptr, SL("_routes"), PH_NOISY_CC);
routes = phalcon_fetch_nproperty_this(this_ptr, SL("_routes"), PH_NOISY_CC);
if (Z_TYPE_P(routes) == IS_ARRAY) {
PHALCON_INIT_VAR(new_routes);
phalcon_fast_array_merge(new_routes, &routes, &group_routes TSRMLS_CC);
Expand Down
27 changes: 27 additions & 0 deletions ext/mvc/router/group.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ PHALCON_INIT_CLASS(Phalcon_Mvc_Router_Group){
zend_declare_property_null(phalcon_mvc_router_group_ce, SL("_paths"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_mvc_router_group_ce, SL("_routes"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_mvc_router_group_ce, SL("_beforeMatch"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_mvc_router_group_ce, SL("_converters"), ZEND_ACC_PROTECTED TSRMLS_CC);

return SUCCESS;
}
Expand Down Expand Up @@ -531,3 +532,29 @@ PHP_METHOD(Phalcon_Mvc_Router_Group, clear){
PHALCON_MM_RESTORE();
}

/**
* Adds a converter to perform an additional transformation for certain parameter
*
* @param string $name
* @param callable $converter
* @return Phalcon\Mvc\Router\Group
*/
PHP_METHOD(Phalcon_Mvc_Router_Group, convert){

zval **name, **converter;

phalcon_fetch_params_ex(2, 0, &name, &converter);

phalcon_update_property_array(this_ptr, SL("_converters"), *name, *converter TSRMLS_CC);
RETURN_THISW();
}

/**
* Returns the router converter
*
* @return array|null
*/
PHP_METHOD(Phalcon_Mvc_Router_Group, getConverters) {

RETURN_MEMBER(this_ptr, "_converters");
}
9 changes: 9 additions & 0 deletions ext/mvc/router/group.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ PHP_METHOD(Phalcon_Mvc_Router_Group, addDelete);
PHP_METHOD(Phalcon_Mvc_Router_Group, addOptions);
PHP_METHOD(Phalcon_Mvc_Router_Group, addHead);
PHP_METHOD(Phalcon_Mvc_Router_Group, clear);
PHP_METHOD(Phalcon_Mvc_Router_Group, convert);
PHP_METHOD(Phalcon_Mvc_Router_Group, getConverters);

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_mvc_router_group___construct, 0, 0, 0)
ZEND_ARG_INFO(0, paths)
Expand Down Expand Up @@ -103,6 +105,11 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_mvc_router_group_addhead, 0, 0, 1)
ZEND_ARG_INFO(0, paths)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_mvc_router_group_convert, 0, 0, 2)
ZEND_ARG_INFO(0, name)
ZEND_ARG_INFO(0, converter)
ZEND_END_ARG_INFO()

PHALCON_INIT_FUNCS(phalcon_mvc_router_group_method_entry){
PHP_ME(Phalcon_Mvc_Router_Group, __construct, arginfo_phalcon_mvc_router_group___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
PHP_ME(Phalcon_Mvc_Router_Group, setHostname, arginfo_phalcon_mvc_router_group_sethostname, ZEND_ACC_PUBLIC)
Expand All @@ -124,6 +131,8 @@ PHALCON_INIT_FUNCS(phalcon_mvc_router_group_method_entry){
PHP_ME(Phalcon_Mvc_Router_Group, addOptions, arginfo_phalcon_mvc_router_group_addoptions, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Mvc_Router_Group, addHead, arginfo_phalcon_mvc_router_group_addhead, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Mvc_Router_Group, clear, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Mvc_Router_Group, convert, arginfo_phalcon_mvc_router_group_convert, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Mvc_Router_Group, getConverters, NULL, ZEND_ACC_PUBLIC)
PHP_FE_END
};

56 changes: 56 additions & 0 deletions ext/tests/issue-1555.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
--TEST--
The use route converter and beforeMatch for Router\Group - https://github.com/phalcon/cphalcon/issues/1555
--SKIPIF--
<?php include('skipif.inc'); ?>
--FILE--
<?php

function addstars($s)
{
return '*' . $s . '*';
}

$router = new \Phalcon\Mvc\Router(false);
$blog = new \Phalcon\Mvc\Router\Group(array('module' => 'blog', 'controller' => 'index'));

$blog->setPrefix('/blog');

$blog->add('/delete/{id}', array('action' => 'delete'));
$blog->add('/edit/{id}', array('action' => 'edit'));

$blog->convert('id', 'addstars');
$blog->convert('xx', 'strtolower');

var_dump($blog->getConverters());

$router->mount($blog);

$routes = $router->getRoutes();
assert(count($routes) == 2);

$route = $routes[0];
var_dump($route->getConverters());

$route = $routes[1];
var_dump($route->getConverters());

?>
--EXPECT--
array(2) {
["id"]=>
string(8) "addstars"
["xx"]=>
string(10) "strtolower"
}
array(2) {
["id"]=>
string(8) "addstars"
["xx"]=>
string(10) "strtolower"
}
array(2) {
["id"]=>
string(8) "addstars"
["xx"]=>
string(10) "strtolower"
}