From 6f1adee640578d530d180a11d47b2b6e9fa427b9 Mon Sep 17 00:00:00 2001 From: demon Date: Sun, 2 Sep 2012 17:17:12 +0800 Subject: [PATCH 1/3] github test with ssh --- yaf.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/yaf.php b/yaf.php index 348268e6..55da9ab2 100644 --- a/yaf.php +++ b/yaf.php @@ -1,4 +1,7 @@ "; if(!extension_loaded('ap')) { From 9db7becab9683b2eda8634d6489ef9d5835de40a Mon Sep 17 00:00:00 2001 From: demon Date: Tue, 4 Sep 2012 17:27:17 +0800 Subject: [PATCH 2/3] yaf_route_simple_assemble complete --- routes/interface.c | 6 +++ routes/map.c | 8 +++ routes/regex.c | 8 +++ routes/rewrite.c | 8 +++ routes/simple.c | 118 +++++++++++++++++++++++++++++++++++++++++++++ routes/static.c | 8 +++ routes/supervar.c | 8 +++ tests/049.phpt | 111 +++++++----------------------------------- 8 files changed, 181 insertions(+), 94 deletions(-) diff --git a/routes/interface.c b/routes/interface.c index ffa153cd..a2f476e1 100644 --- a/routes/interface.c +++ b/routes/interface.c @@ -31,6 +31,11 @@ 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() /* }}} */ zend_class_entry *yaf_route_ce; @@ -138,6 +143,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} }; /* }}} */ diff --git a/routes/map.c b/routes/map.c index c47edf90..33c5c136 100644 --- a/routes/map.c +++ b/routes/map.c @@ -159,11 +159,19 @@ PHP_METHOD(yaf_route_map, __construct) { } /* }}} */ +/** {{{ proto public Yaf_Route_Map::assemble(zval *mvc, zval *query) +*/ +PHP_METHOD(yaf_route_map, assemble) { + +} +/* }}} */ + /** {{{ 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} }; /* }}} */ diff --git a/routes/regex.c b/routes/regex.c index 29cc4eb0..777a57bf 100644 --- a/routes/regex.c +++ b/routes/regex.c @@ -232,11 +232,19 @@ PHP_METHOD(yaf_route_regex, __construct) { } /** }}} */ +/** {{{ proto public Yaf_Route_regex::assemble(zval *mvc, zval *query) +*/ +PHP_METHOD(yaf_route_regex, assemble) { + +} +/* }}} */ + /** {{{ yaf_route_regex_methods */ zend_function_entry yaf_route_regex_methods[] = { PHP_ME(yaf_route_regex, __construct, yaf_route_regex_construct_arginfo, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR) PHP_ME(yaf_route_regex, route, yaf_route_route_arginfo, ZEND_ACC_PUBLIC) + PHP_ME(yaf_route_regex, assemble, yaf_route_assemble_arginfo, ZEND_ACC_PUBLIC) {NULL, NULL, NULL} }; /* }}} */ diff --git a/routes/rewrite.c b/routes/rewrite.c index b6598877..3c0aa2ac 100644 --- a/routes/rewrite.c +++ b/routes/rewrite.c @@ -287,11 +287,19 @@ PHP_METHOD(yaf_route_rewrite, __construct) { } /** }}} */ +/** {{{ proto public Yaf_Route_rewrite::assemble(zval *mvc, zval *query) +*/ +PHP_METHOD(yaf_route_rewrite, assemble) { + +} +/* }}} */ + /** {{{ yaf_route_rewrite_methods */ zend_function_entry yaf_route_rewrite_methods[] = { PHP_ME(yaf_route_rewrite, __construct, yaf_route_rewrite_construct_arginfo, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR) PHP_ME(yaf_route_rewrite, route, yaf_route_route_arginfo, ZEND_ACC_PUBLIC) + PHP_ME(yaf_route_rewrite, assemble, yaf_route_assemble_arginfo, ZEND_ACC_PUBLIC) {NULL, NULL, NULL} }; /* }}} */ diff --git a/routes/simple.c b/routes/simple.c index 8ae3ad80..ab2a0f7a 100644 --- a/routes/simple.c +++ b/routes/simple.c @@ -81,6 +81,108 @@ yaf_route_t * yaf_route_simple_instance(yaf_route_t *this_ptr, zval *module, zva } /* }}} */ +/** {{{ zval * yaf_route_simple_assemble(zval *mvc, zval *query TSRMLS_DC) + */ +zval * yaf_route_simple_assemble(yaf_route_t *this_ptr, zval *mvc, zval *query TSRMLS_DC) { + char *tvalue; + zval *nmodule, *ncontroller, *naction; + zval *uri; + + MAKE_STD_ZVAL(uri); + tvalue = emalloc(sizeof("?")); + tvalue[0] = '\0'; + strcat(tvalue, "?"); + + nmodule = zend_read_property(yaf_route_simple_ce, this_ptr, ZEND_STRL(YAF_ROUTE_SIMPLE_VAR_NAME_MODULE), 1 TSRMLS_CC); + ncontroller = zend_read_property(yaf_route_simple_ce, this_ptr, ZEND_STRL(YAF_ROUTE_SIMPLE_VAR_NAME_CONTROLLER), 1 TSRMLS_CC); + naction = zend_read_property(yaf_route_simple_ce, this_ptr, ZEND_STRL(YAF_ROUTE_SIMPLE_VAR_NAME_ACTION), 1 TSRMLS_CC); + + do { + zval **tmp; + char *tsprintf; + uint tlen; + + if (zend_hash_find(Z_ARRVAL_P(mvc), Z_STRVAL_P(nmodule), Z_STRLEN_P(nmodule) + 1, (void **)&tmp) == SUCCESS) { + tlen = snprintf(NULL, 0, "%s=%s&", Z_STRVAL_P(nmodule), Z_STRVAL_PP(tmp)); + if (!(tsprintf = emalloc((tlen + 1) * sizeof(char)))) { + break; + } + tlen = snprintf(tsprintf, tlen + 1, "%s=%s&", Z_STRVAL_P(nmodule), Z_STRVAL_PP(tmp)); + if (tlen) { + tvalue = erealloc(tvalue, strlen(tvalue) + tlen + 1); + strcat(tvalue, tsprintf); + } + efree(tsprintf); + } + + if (zend_hash_find(Z_ARRVAL_P(mvc), Z_STRVAL_P(ncontroller), Z_STRLEN_P(ncontroller) + 1, (void **)&tmp) == FAILURE) { + yaf_trigger_error(YAF_ERR_TYPE_ERROR TSRMLS_CC, "%s", "You need to specify the controller"); + break; + } + + tlen = snprintf(NULL, 0, "%s=%s&", Z_STRVAL_P(ncontroller), Z_STRVAL_PP(tmp)); + if (!(tsprintf = emalloc((tlen + 1) * sizeof(char)))) { + break; + } + tlen = snprintf(tsprintf, tlen + 1, "%s=%s&", Z_STRVAL_P(ncontroller), Z_STRVAL_PP(tmp)); + if (tlen) { + tvalue = erealloc(tvalue, strlen(tvalue) + tlen + 1); + strcat(tvalue, tsprintf); + } + efree(tsprintf); + + if(zend_hash_find(Z_ARRVAL_P(mvc), Z_STRVAL_P(naction), Z_STRLEN_P(naction) + 1, (void **)&tmp) == FAILURE) { + yaf_trigger_error(YAF_ERR_TYPE_ERROR TSRMLS_CC, "%s", "You need to specify the action"); + break; + } + + tlen = snprintf(NULL, 0, "%s=%s", Z_STRVAL_P(naction), Z_STRVAL_PP(tmp)); + if (!(tsprintf = emalloc((tlen + 1) * sizeof(char)))) { + break; + } + tlen = snprintf(tsprintf, tlen + 1, "%s=%s", Z_STRVAL_P(naction), Z_STRVAL_PP(tmp)); + if (tlen) { + tvalue = erealloc(tvalue, strlen(tvalue) + tlen + 1); + strcat(tvalue, tsprintf); + } + efree(tsprintf); + + if ( IS_ARRAY == Z_TYPE_P(query)) { + uint key_type, key_len; + char *key; + ulong key_idx; + + for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(query)); + zend_hash_get_current_data(Z_ARRVAL_P(query), (void **)&tmp) == SUCCESS; + zend_hash_move_forward(Z_ARRVAL_P(query))) { + + if (IS_STRING == Z_TYPE_PP(tmp) + && HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(Z_ARRVAL_P(query), &key, &key_len, &key_idx, 0, NULL)) { + + tlen = snprintf(NULL, 0, "&%s=%s", key, Z_STRVAL_PP(tmp)); + if (!(tsprintf = emalloc((tlen + 1) * sizeof(char)))) { + break; + } + tlen = snprintf(tsprintf, tlen + 1, "&%s=%s", key, Z_STRVAL_PP(tmp)); + if (tlen) { + tvalue = erealloc(tvalue, strlen(tvalue) + tlen + 1); + strcat(tvalue, tsprintf); + } + efree(tsprintf); + } + } + } + + ZVAL_STRING(uri, tvalue, 1); + efree(tvalue); + return uri; + } while (0); + + efree(tvalue); + ZVAL_NULL(uri); + return uri; +} + /** {{{ proto public Yaf_Route_Simple::route(Yaf_Request $req) */ PHP_METHOD(yaf_route_simple, route) { @@ -114,11 +216,27 @@ PHP_METHOD(yaf_route_simple, __construct) { } /* }}} */ +/** {{{ proto public Yaf_Route_Simple::assemble(zval *mvc, zval *query) +*/ +PHP_METHOD(yaf_route_simple, assemble) { + zval *mvc, *query; + zval *return_uri; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|z", &mvc, &query) == FAILURE) { + return; + } else { + return_uri = yaf_route_simple_assemble(getThis(), mvc, query TSRMLS_CC); + RETURN_ZVAL(return_uri, 1, 0); + } +} +/* }}} */ + /** {{{ yaf_route_simple_methods */ zend_function_entry yaf_route_simple_methods[] = { PHP_ME(yaf_route_simple, __construct, yaf_route_simple_construct_arginfo, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR) PHP_ME(yaf_route_simple, route, yaf_route_route_arginfo, ZEND_ACC_PUBLIC) + PHP_ME(yaf_route_simple, assemble, yaf_route_assemble_arginfo, ZEND_ACC_PUBLIC) {NULL, NULL, NULL} }; /* }}} */ diff --git a/routes/static.c b/routes/static.c index 02703a41..a82e1ae3 100644 --- a/routes/static.c +++ b/routes/static.c @@ -182,11 +182,19 @@ PHP_METHOD(yaf_route_static, match) { } /* }}} */ +/** {{{ proto public Yaf_Route_Static::assemble(zval *mvc, zval *query) +*/ +PHP_METHOD(yaf_route_static, assemble) { + +} +/* }}} */ + /** {{{ yaf_route_static_methods */ zend_function_entry yaf_route_static_methods[] = { PHP_ME(yaf_route_static, match, yaf_route_static_match_arginfo, ZEND_ACC_PUBLIC) PHP_ME(yaf_route_static, route, yaf_route_route_arginfo, ZEND_ACC_PUBLIC) + PHP_ME(yaf_route_static, assemble, yaf_route_assemble_arginfo, ZEND_ACC_PUBLIC) {NULL, NULL, NULL} }; /* }}} */ diff --git a/routes/supervar.c b/routes/supervar.c index c2a7cb86..0d9abcdd 100644 --- a/routes/supervar.c +++ b/routes/supervar.c @@ -101,11 +101,19 @@ PHP_METHOD(yaf_route_supervar, __construct) { } /** }}} */ +/** {{{ proto public Yaf_Route_Supervar::assemble(zval *mvc, zval *query) +*/ +PHP_METHOD(yaf_route_supervar, assemble) { + +} +/* }}} */ + /** {{{ 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) + PHP_ME(yaf_route_supervar, assemble, yaf_route_assemble_arginfo, ZEND_ACC_PUBLIC) {NULL, NULL, NULL} }; /* }}} */ diff --git a/tests/049.phpt b/tests/049.phpt index 43302f64..cd27e08f 100644 --- a/tests/049.phpt +++ b/tests/049.phpt @@ -1,104 +1,27 @@ --TEST-- -Check for Sample application with exception +Check for Yaf_Simple_Router_assemble --SKIPIF-- ---INI-- -yaf.use_spl_autoload=0 -yaf.lowcase_path=0 -report_memleaks=0 --FILE-- - array( - "directory" => APPLICATION_PATH . "/application/", - "dispatcher" => array ( - "catchException" => true, - ), - "library" => array( - ), - ), -); - -file_put_contents(APPLICATION_PATH . "/application/controllers/Error.php", <<_view->msg = \$exception->getMessage(); - } - } -PHP -); -file_put_contents(APPLICATION_PATH . "/application/Bootstrap.php", <<getConfig()); - } - public function _initPlugin(Yaf_Dispatcher \$dispatcher) { - \$dispatcher->registerPlugin(new TestPlugin()); - } - public function _initReturn(Yaf_Dispatcher \$dispatcher) { - \$dispatcher->returnResponse(true); - } - } -PHP -); +$router = new Yaf_Router(); -file_put_contents(APPLICATION_PATH . "/application/plugins/Test.php", <<"); -mkdir(APPLICATION_PATH . "/application/views/error/"); -file_put_contents(APPLICATION_PATH . "/application/views/error/error.phtml", - "catched: "); +$router->addRoute("simple", $route); -$app = new Yaf_Application($config); -$app->bootstrap()->run(); +var_dump($router->getRoute('simple')->assemble( + array( + 'a' => 'yafaction', + 'tkey' => 'tval', + 'c' => 'yafcontroller', + 'm' => 'yafmodule' + ), + array( + 'tkey1' => 'tval1', + 'tkey2' => 'tval2' + ) +)); --EXPECTF-- -string(13) "routerStartup" -string(14) "routerShutdown" -string(19) "dispatchLoopStartup" -string(11) "preDispatch" -string(4) "init" -string(6) "action" -catched: view exception +string(64) "?m=yafmodule&c=yafcontroller&a=yafaction&tkey1=tval1&tkey2=tval2" From 3a3059cbdaf18926a12db833ab9f5182a9b6b039 Mon Sep 17 00:00:00 2001 From: demon Date: Tue, 4 Sep 2012 19:29:01 +0800 Subject: [PATCH 3/3] yaf_route_simple_assemble complete --- tests/049.phpt | 111 +++++++++++++++++++++++++++++++------ tests/051.phpt | 27 +++++++++ tests/short_tag_test.phtml | 1 + 3 files changed, 122 insertions(+), 17 deletions(-) create mode 100644 tests/051.phpt create mode 100644 tests/short_tag_test.phtml diff --git a/tests/049.phpt b/tests/049.phpt index cd27e08f..43302f64 100644 --- a/tests/049.phpt +++ b/tests/049.phpt @@ -1,27 +1,104 @@ --TEST-- -Check for Yaf_Simple_Router_assemble +Check for Sample application with exception --SKIPIF-- +--INI-- +yaf.use_spl_autoload=0 +yaf.lowcase_path=0 +report_memleaks=0 --FILE-- + array( + "directory" => APPLICATION_PATH . "/application/", + "dispatcher" => array ( + "catchException" => true, + ), + "library" => array( + ), + ), +); + +file_put_contents(APPLICATION_PATH . "/application/controllers/Error.php", <<_view->msg = \$exception->getMessage(); + } + } +PHP +); -$router = new Yaf_Router(); +file_put_contents(APPLICATION_PATH . "/application/Bootstrap.php", <<getConfig()); + } + public function _initPlugin(Yaf_Dispatcher \$dispatcher) { + \$dispatcher->registerPlugin(new TestPlugin()); + } + public function _initReturn(Yaf_Dispatcher \$dispatcher) { + \$dispatcher->returnResponse(true); + } + } +PHP +); -$route = new Yaf_Route_Simple('m', 'c', 'a'); +file_put_contents(APPLICATION_PATH . "/application/plugins/Test.php", <<addRoute("simple", $route); +file_put_contents(APPLICATION_PATH . "/application/views/index/index.phtml", + ""); +mkdir(APPLICATION_PATH . "/application/views/error/"); +file_put_contents(APPLICATION_PATH . "/application/views/error/error.phtml", + "catched: "); -var_dump($router->getRoute('simple')->assemble( - array( - 'a' => 'yafaction', - 'tkey' => 'tval', - 'c' => 'yafcontroller', - 'm' => 'yafmodule' - ), - array( - 'tkey1' => 'tval1', - 'tkey2' => 'tval2' - ) -)); +$app = new Yaf_Application($config); +$app->bootstrap()->run(); --EXPECTF-- -string(64) "?m=yafmodule&c=yafcontroller&a=yafaction&tkey1=tval1&tkey2=tval2" +string(13) "routerStartup" +string(14) "routerShutdown" +string(19) "dispatchLoopStartup" +string(11) "preDispatch" +string(4) "init" +string(6) "action" +catched: view exception diff --git a/tests/051.phpt b/tests/051.phpt new file mode 100644 index 00000000..cd27e08f --- /dev/null +++ b/tests/051.phpt @@ -0,0 +1,27 @@ +--TEST-- +Check for Yaf_Simple_Router_assemble +--SKIPIF-- + +--FILE-- +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" diff --git a/tests/short_tag_test.phtml b/tests/short_tag_test.phtml new file mode 100644 index 00000000..1a6fc753 --- /dev/null +++ b/tests/short_tag_test.phtml @@ -0,0 +1 @@ + \ No newline at end of file