Skip to content

Commit

Permalink
Support dynmaic routing for Regex/Rewrite route
Browse files Browse the repository at this point in the history
  • Loading branch information
laruence committed Jan 14, 2013
1 parent e4bd8bf commit 7a78892
Show file tree
Hide file tree
Showing 51 changed files with 236 additions and 63 deletions.
2 changes: 1 addition & 1 deletion configs/ini.c
Expand Up @@ -14,7 +14,7 @@
+----------------------------------------------------------------------+
*/

/* $Id: ini.c 327626 2012-09-13 02:57:39Z laruence $ */
/* $Id: ini.c 329002 2013-01-07 12:55:53Z laruence $ */

zend_class_entry *yaf_config_ini_ce;

Expand Down
2 changes: 1 addition & 1 deletion configs/simple.c
Expand Up @@ -14,7 +14,7 @@
+----------------------------------------------------------------------+
*/

/* $Id: simple.c 327626 2012-09-13 02:57:39Z laruence $ */
/* $Id: simple.c 329002 2013-01-07 12:55:53Z laruence $ */

zend_class_entry *yaf_config_simple_ce;

Expand Down
2 changes: 1 addition & 1 deletion php_yaf.h
Expand Up @@ -14,7 +14,7 @@
+----------------------------------------------------------------------+
*/

/* $Id: php_yaf.h 328981 2013-01-04 10:53:59Z laruence $ */
/* $Id: php_yaf.h 329002 2013-01-07 12:55:53Z laruence $ */

#ifndef PHP_YAF_H
#define PHP_YAF_H
Expand Down
2 changes: 1 addition & 1 deletion requests/http.c
Expand Up @@ -14,7 +14,7 @@
+----------------------------------------------------------------------+
*/

/* $Id: http.c 327549 2012-09-09 03:02:48Z laruence $ */
/* $Id: http.c 329002 2013-01-07 12:55:53Z laruence $ */

#include "ext/standard/url.h"

Expand Down
2 changes: 1 addition & 1 deletion requests/simple.c
Expand Up @@ -14,7 +14,7 @@
+----------------------------------------------------------------------+
*/

/* $Id: simple.c 327591 2012-09-10 10:56:13Z laruence $ */
/* $Id: simple.c 329002 2013-01-07 12:55:53Z laruence $ */

static zend_class_entry *yaf_request_simple_ce;

Expand Down
2 changes: 1 addition & 1 deletion response/cli.c
Expand Up @@ -14,7 +14,7 @@
+----------------------------------------------------------------------+
*/

/* $Id: cli.c 321289 2011-12-21 02:53:29Z laruence $ */
/* $Id: cli.c 329002 2013-01-07 12:55:53Z laruence $ */


zend_class_entry * yaf_response_cli_ce;
Expand Down
2 changes: 1 addition & 1 deletion response/http.c
Expand Up @@ -14,7 +14,7 @@
+----------------------------------------------------------------------+
*/

/* $Id: http.c 321289 2011-12-21 02:53:29Z laruence $ */
/* $Id: http.c 329002 2013-01-07 12:55:53Z laruence $ */

zend_class_entry *yaf_response_http_ce;

Expand Down
2 changes: 1 addition & 1 deletion routes/interface.c
Expand Up @@ -14,7 +14,7 @@
+----------------------------------------------------------------------+
*/

/* $Id: interface.c 326042 2012-06-08 10:49:59Z laruence $ */
/* $Id: interface.c 329002 2013-01-07 12:55:53Z laruence $ */

#include "ext/standard/php_smart_str.h"

Expand Down
2 changes: 1 addition & 1 deletion routes/map.c
Expand Up @@ -14,7 +14,7 @@
+----------------------------------------------------------------------+
*/

/* $Id: map.c 327549 2012-09-09 03:02:48Z laruence $*/
/* $Id: map.c 329002 2013-01-07 12:55:53Z laruence $*/

zend_class_entry *yaf_route_map_ce;

Expand Down
39 changes: 30 additions & 9 deletions routes/regex.c
Expand Up @@ -14,7 +14,7 @@
+----------------------------------------------------------------------+
*/

/* $Id: regex.c 327549 2012-09-09 03:02:48Z laruence $ */
/* $Id: regex.c 329133 2013-01-14 07:24:17Z laruence $ */

zend_class_entry *yaf_route_regex_ce;

Expand Down Expand Up @@ -135,9 +135,9 @@ int yaf_route_regex_route(yaf_route_t *router, yaf_request_t *request TSRMLS_DC)

if (base_uri && IS_STRING == Z_TYPE_P(base_uri)
&& !strncasecmp(Z_STRVAL_P(zuri), Z_STRVAL_P(base_uri), Z_STRLEN_P(base_uri))) {
request_uri = estrdup(Z_STRVAL_P(zuri) + Z_STRLEN_P(base_uri));
request_uri = estrdup(Z_STRVAL_P(zuri) + Z_STRLEN_P(base_uri));
} else {
request_uri = estrdup(Z_STRVAL_P(zuri));
request_uri = estrdup(Z_STRVAL_P(zuri));
}

if (!(args = yaf_route_regex_match(router, request_uri, strlen(request_uri) TSRMLS_CC))) {
Expand All @@ -147,16 +147,37 @@ int yaf_route_regex_route(yaf_route_t *router, yaf_request_t *request TSRMLS_DC)
zval **module, **controller, **action, *routes;

routes = zend_read_property(yaf_route_regex_ce, router, ZEND_STRL(YAF_ROUTE_PROPETY_NAME_ROUTE), 1 TSRMLS_CC);
if (zend_hash_find(Z_ARRVAL_P(routes), ZEND_STRS("module"), (void **)&module) == SUCCESS) {
zend_update_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_MODULE), *module TSRMLS_CC);
if (zend_hash_find(Z_ARRVAL_P(routes), ZEND_STRS("module"), (void **)&module) == SUCCESS && IS_STRING == Z_TYPE_PP(module)) {
if (Z_STRVAL_PP(module)[0] != ':') {
zend_update_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_MODULE), *module TSRMLS_CC);
} else {
zval **m;
if (zend_hash_find(Z_ARRVAL_P(args), Z_STRVAL_PP(module) + 1, Z_STRLEN_PP(module), (void **)&m) == SUCCESS && IS_STRING == Z_TYPE_PP(m)) {
zend_update_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_MODULE), *m TSRMLS_CC);
}
}
}

if (zend_hash_find(Z_ARRVAL_P(routes), ZEND_STRS("controller"), (void **)&controller) == SUCCESS) {
zend_update_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_CONTROLLER), *controller TSRMLS_CC);
if (zend_hash_find(Z_ARRVAL_P(routes), ZEND_STRS("controller"), (void **)&controller) == SUCCESS && IS_STRING == Z_TYPE_PP(controller)) {
if (Z_STRVAL_PP(controller)[0] != ':') {
zend_update_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_CONTROLLER), *controller TSRMLS_CC);
} else {
zval **c;
if (zend_hash_find(Z_ARRVAL_P(args), Z_STRVAL_PP(controller) + 1, Z_STRLEN_PP(controller), (void **)&c) == SUCCESS && IS_STRING == Z_TYPE_PP(c)) {
zend_update_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_CONTROLLER), *c TSRMLS_CC);
}
}
}

if (zend_hash_find(Z_ARRVAL_P(routes), ZEND_STRS("action"), (void **)&action) == SUCCESS) {
zend_update_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_ACTION), *action TSRMLS_CC);
if (zend_hash_find(Z_ARRVAL_P(routes), ZEND_STRS("action"), (void **)&action) == SUCCESS && IS_STRING == Z_TYPE_PP(action)) {
if (Z_STRVAL_PP(action)[0] != ':') {
zend_update_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_ACTION), *action TSRMLS_CC);
} else {
zval **a;
if (zend_hash_find(Z_ARRVAL_P(args), Z_STRVAL_PP(action) + 1, Z_STRLEN_PP(action), (void **)&a) == SUCCESS && IS_STRING == Z_TYPE_PP(a)) {
zend_update_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_ACTION), *a TSRMLS_CC);
}
}
}

(void)yaf_request_set_params_multi(request, args TSRMLS_CC);
Expand Down
35 changes: 28 additions & 7 deletions routes/rewrite.c
Expand Up @@ -14,7 +14,7 @@
+----------------------------------------------------------------------+
*/

/* $Id: rewrite.c 327549 2012-09-09 03:02:48Z laruence $ */
/* $Id: rewrite.c 329133 2013-01-14 07:24:17Z laruence $ */

zend_class_entry *yaf_route_rewrite_ce;

Expand Down Expand Up @@ -188,16 +188,37 @@ int yaf_route_rewrite_route(yaf_route_t *router, yaf_request_t *request TSRMLS_D
zval **module, **controller, **action, *routes;

routes = zend_read_property(yaf_route_rewrite_ce, router, ZEND_STRL(YAF_ROUTE_PROPETY_NAME_ROUTE), 1 TSRMLS_CC);
if (zend_hash_find(Z_ARRVAL_P(routes), ZEND_STRS("module"), (void **)&module) == SUCCESS) {
zend_update_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_MODULE), *module TSRMLS_CC);
if (zend_hash_find(Z_ARRVAL_P(routes), ZEND_STRS("module"), (void **)&module) == SUCCESS && IS_STRING == Z_TYPE_PP(module)) {
if (Z_STRVAL_PP(module)[0] != ':') {
zend_update_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_MODULE), *module TSRMLS_CC);
} else {
zval **m;
if (zend_hash_find(Z_ARRVAL_P(args), Z_STRVAL_PP(module) + 1, Z_STRLEN_PP(module), (void **)&m) == SUCCESS && IS_STRING == Z_TYPE_PP(m)) {
zend_update_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_MODULE), *m TSRMLS_CC);
}
}
}

if (zend_hash_find(Z_ARRVAL_P(routes), ZEND_STRS("controller"), (void **)&controller) == SUCCESS) {
zend_update_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_CONTROLLER), *controller TSRMLS_CC);
if (zend_hash_find(Z_ARRVAL_P(routes), ZEND_STRS("controller"), (void **)&controller) == SUCCESS && IS_STRING == Z_TYPE_PP(controller)) {
if (Z_STRVAL_PP(controller)[0] != ':') {
zend_update_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_CONTROLLER), *controller TSRMLS_CC);
} else {
zval **c;
if (zend_hash_find(Z_ARRVAL_P(args), Z_STRVAL_PP(controller) + 1, Z_STRLEN_PP(controller), (void **)&c) == SUCCESS && IS_STRING == Z_TYPE_PP(c)) {
zend_update_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_CONTROLLER), *c TSRMLS_CC);
}
}
}

if (zend_hash_find(Z_ARRVAL_P(routes), ZEND_STRS("action"), (void **)&action) == SUCCESS) {
zend_update_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_ACTION), *action TSRMLS_CC);
if (zend_hash_find(Z_ARRVAL_P(routes), ZEND_STRS("action"), (void **)&action) == SUCCESS && IS_STRING == Z_TYPE_PP(action)) {
if (Z_STRVAL_PP(action)[0] != ':') {
zend_update_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_ACTION), *action TSRMLS_CC);
} else {
zval **a;
if (zend_hash_find(Z_ARRVAL_P(args), Z_STRVAL_PP(action) + 1, Z_STRLEN_PP(action), (void **)&a) == SUCCESS && IS_STRING == Z_TYPE_PP(a)) {
zend_update_property(yaf_request_ce, request, ZEND_STRL(YAF_REQUEST_PROPERTY_NAME_ACTION), *a TSRMLS_CC);
}
}
}

(void)yaf_request_set_params_multi(request, args TSRMLS_CC);
Expand Down
2 changes: 1 addition & 1 deletion routes/simple.c
Expand Up @@ -14,7 +14,7 @@
+----------------------------------------------------------------------+
*/

/* $Id: simple.c 327549 2012-09-09 03:02:48Z laruence $ */
/* $Id: simple.c 329002 2013-01-07 12:55:53Z laruence $ */

zend_class_entry *yaf_route_simple_ce;

Expand Down
2 changes: 1 addition & 1 deletion routes/static.c
Expand Up @@ -14,7 +14,7 @@
+----------------------------------------------------------------------+
*/

/* $Id: static.c 327627 2012-09-13 06:12:38Z laruence $ */
/* $Id: static.c 329002 2013-01-07 12:55:53Z laruence $ */

zend_class_entry * yaf_route_static_ce;

Expand Down
2 changes: 1 addition & 1 deletion routes/supervar.c
Expand Up @@ -14,7 +14,7 @@
+----------------------------------------------------------------------+
*/

/* $Id: supervar.c 327549 2012-09-09 03:02:48Z laruence $ */
/* $Id: supervar.c 329002 2013-01-07 12:55:53Z laruence $ */

#define YAF_ROUTE_SUPERVAR_PROPETY_NAME_VAR "_var_name"

Expand Down
63 changes: 63 additions & 0 deletions tests/063.phpt
@@ -0,0 +1,63 @@
--TEST--
Check for Yaf_Route_Rewrite with dynamic mvc
--SKIPIF--
<?php if (!extension_loaded("yaf")) print "skip"; ?>
--FILE--
<?php
$request = new Yaf_Request_Http("/subdir/ctl/act/name/value");

$router = new Yaf_Router();

$route = new Yaf_Route_Rewrite(
"/subdir/:con/:a/*",
array(
"module" => "m",
"controller" => ":1",
"action" => ":a",
)
);

$router->addRoute("subdir", $route)->addRoute("yaf", new Yaf_Route_Rewrite(
"/yaf/:action/*",
array(
"action" => ':action',
"controller" => "index",
)
))->route($request);

var_dump($router->getCurrentRoute());
print_r($request->getParams());
var_dump($request->getActionName());
var_dump($request->getControllerName());
var_dump($request->getModuleName());

$request = new Yaf_Request_Http("/yaf/act/name/value");
$router->route($request);

var_dump($router->getCurrentRoute());
print_r($request->getParams());
var_dump($request->getActionName());
var_dump($request->getControllerName());
var_dump($request->getModuleName());

?>
--EXPECTF--
string(6) "subdir"
Array
(
[con] => ctl
[a] => act
[name] => value
)
string(3) "act"
NULL
string(1) "m"
string(3) "yaf"
Array
(
[action] => act
[name] => value
)
string(3) "act"
string(5) "index"
NULL
68 changes: 68 additions & 0 deletions tests/064.phpt
@@ -0,0 +1,68 @@
--TEST--
Check for Yaf_Route_Regex with dynamic mvc
--SKIPIF--
<?php if (!extension_loaded("yaf")) print "skip"; ?>
--FILE--
<?php
$request = new Yaf_Request_Http("/subdir/ctl/act/name/value");

$router = new Yaf_Router();

$route = new Yaf_Route_Regex(
"#subdir/(.*?)/(.*?)/.*#",
array(
"module" => "m",
"controller" => ":c",
"action" => ":a",
),
array(
1 => "c",
2 => "a",
)
);

$router->addRoute("subdir", $route)->addRoute("yaf", new Yaf_Route_Regex(
"#yaf/(.*?)/.*#",
array(
"action" => ':action',
"controller" => "index",
),
array(
1 => "action",
)
))->route($request);

var_dump($router->getCurrentRoute());
print_r($request->getParams());
var_dump($request->getActionName());
var_dump($request->getControllerName());
var_dump($request->getModuleName());

$request = new Yaf_Request_Http("/yaf/act/name/value");
$router->route($request);

var_dump($router->getCurrentRoute());
print_r($request->getParams());
var_dump($request->getActionName());
var_dump($request->getControllerName());
var_dump($request->getModuleName());

?>
--EXPECTF--
string(6) "subdir"
Array
(
[c] => ctl
[a] => act
)
string(3) "act"
string(3) "ctl"
string(1) "m"
string(3) "yaf"
Array
(
[action] => act
)
string(3) "act"
string(5) "index"
NULL
2 changes: 1 addition & 1 deletion views/interface.c
Expand Up @@ -14,7 +14,7 @@
+----------------------------------------------------------------------+
*/

/* $Id: interface.c 327426 2012-09-02 04:01:00Z laruence $ */
/* $Id: interface.c 329002 2013-01-07 12:55:53Z laruence $ */

zend_class_entry *yaf_view_interface_ce;

Expand Down
2 changes: 1 addition & 1 deletion views/simple.c
Expand Up @@ -14,7 +14,7 @@
+----------------------------------------------------------------------+
*/

/* $Id: simple.c 328823 2012-12-18 10:10:55Z remi $ */
/* $Id: simple.c 329002 2013-01-07 12:55:53Z laruence $ */

#include "main/php_output.h"

Expand Down
2 changes: 1 addition & 1 deletion yaf.c
Expand Up @@ -14,7 +14,7 @@
+----------------------------------------------------------------------+
*/

/* $Id: yaf.c 328262 2012-11-06 10:31:55Z laruence $ */
/* $Id: yaf.c 329002 2013-01-07 12:55:53Z laruence $ */

#ifdef HAVE_CONFIG_H
#include "config.h"
Expand Down
2 changes: 1 addition & 1 deletion yaf_action.c
Expand Up @@ -14,7 +14,7 @@
+----------------------------------------------------------------------+
*/

/* $Id: yaf_action.c 321289 2011-12-21 02:53:29Z laruence $ */
/* $Id: yaf_action.c 329002 2013-01-07 12:55:53Z laruence $ */

#ifdef HAVE_CONFIG_H
#include "config.h"
Expand Down

1 comment on commit 7a78892

@bluegood
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我是路过来仰望大神的。

Please sign in to comment.