@@ -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*/
161257PHP_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*/
178290zend_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/* }}} */
0 commit comments