Improve handling of multiple routes per action

This commit is contained in:
Kijin Sung 2020-06-13 11:48:58 +09:00
parent 7fc3d83888
commit bf8d2c8b09
3 changed files with 40 additions and 18 deletions

View file

@ -18,6 +18,7 @@ class ModuleActionParser
'alphanum' => '[a-zA-Z0-9]+', 'alphanum' => '[a-zA-Z0-9]+',
'hex' => '[0-9a-f]+', 'hex' => '[0-9a-f]+',
'word' => '[a-zA-Z0-9_]+', 'word' => '[a-zA-Z0-9_]+',
'any' => '[^/]+',
); );
/** /**
@ -106,17 +107,22 @@ class ModuleActionParser
} }
// Parse routes. // Parse routes.
$route = trim($action['route']); $route_attr = trim($action['route']);
$route_tags = $action->route;
$method = trim($action['method']); $method = trim($action['method']);
$route_arg = []; $route_arg = [];
if ($route) if ($route_attr || count($route_tags))
{ {
$methods = $method ? explode('|', strtoupper($method)) : (starts_with('proc', $action_name) ? ['POST'] : ['GET']); $methods = $method ? explode('|', strtoupper($method)) : (starts_with('proc', $action_name) ? ['POST'] : ['GET']);
$routes = explode_with_escape('|', $route); $routes = $route_attr ? explode_with_escape('|', $route_attr) : array();
foreach ($route_tags as $route_tag)
{
$routes[] = trim($route_tag['route']);
}
foreach ($routes as $route) foreach ($routes as $route)
{ {
$route_info = self::analyzeRoute($route); $route_info = self::analyzeRoute($route);
$route_arg[$route] = $route_info->vars; $route_arg[$route_info->route] = $route_info->vars;
foreach ($methods as $method) foreach ($methods as $method)
{ {
$info->route->{$method}[$route_info->regexp] = $action_name; $info->route->{$method}[$route_info->regexp] = $action_name;
@ -180,16 +186,18 @@ class ModuleActionParser
$var_regexp = '#\\$([a-zA-Z0-9_]+)(?::(' . implode('|', array_keys(self::$_shortcuts)) . '))?#'; $var_regexp = '#\\$([a-zA-Z0-9_]+)(?::(' . implode('|', array_keys(self::$_shortcuts)) . '))?#';
$vars = array(); $vars = array();
$regexp = preg_replace_callback($var_regexp, function($match) use(&$vars) { $regexp = preg_replace_callback($var_regexp, function($match) use(&$vars) {
if (isset($match[2]) && isset(self::$_shortcuts[$match[2]])) if (isset($match[2]))
{ {
$var_type = $match[2];
$var_pattern = self::$_shortcuts[$match[2]]; $var_pattern = self::$_shortcuts[$match[2]];
} }
else else
{ {
$var_pattern = ends_with('_srl', $match[1]) ? '[0-9]+' : '[^/]+'; $var_type = ends_with('_srl', $match[1]) ? 'number' : 'any';
$var_pattern = self::$_shortcuts[$var_type];
} }
$named_group = '(?P<' . $match[1] . '>' . $var_pattern . ')'; $named_group = '(?P<' . $match[1] . '>' . $var_pattern . ')';
$vars[] = $match[1]; $vars[$match[1]] = $var_type;
return $named_group; return $named_group;
}, $route); }, $route);
@ -198,6 +206,7 @@ class ModuleActionParser
// Return the regexp and variable list. // Return the regexp and variable list.
$result = new \stdClass; $result = new \stdClass;
$result->route = preg_replace($var_regexp, '\\$$1', $route);
$result->regexp = $regexp; $result->regexp = $regexp;
$result->vars = $vars; $result->vars = $vars;
return $result; return $result;

View file

@ -158,15 +158,20 @@ class Router
return urlencode($args[$keys[0]]); return urlencode($args[$keys[0]]);
} }
// If $mid and $act exist, try routes defined in the module. // If $mid exists, try routes defined in the module.
if (isset($args['mid']) && isset($args['act']) && $rewrite_level == 2) if (isset($args['mid']) && isset($args['act']) && $rewrite_level == 2)
{ {
// Remove $mid and $act from arguments and work with the remainder. // Remove $mid and $act from arguments and work with the remainder.
$remaining_args = array_diff_key($args, ['mid' => 'mid', 'act' => 'act']); $remaining_args = array_diff_key($args, ['mid' => 'mid', 'act' => 'act']);
// Check if $act has any routes defined. // Get module action info.
$action_info = self::_getModuleActionInfo($args['mid']); $action_info = self::_getModuleActionInfo($args['mid']);
$action = $action_info->action->{$args['act']};
// If there is no $act, use the default action.
$act = isset($args['act']) ? $args['act'] : $action_info->default_index_act;
// Check if $act has any routes defined.
$action = $action_info->action->{$act};
if ($action->route) if ($action->route)
{ {
// If the action only has one route, select it. // If the action only has one route, select it.
@ -182,7 +187,7 @@ class Router
$reordered_routes = array(); $reordered_routes = array();
foreach ($action->route as $route => $route_vars) foreach ($action->route as $route => $route_vars)
{ {
$matched_arguments = array_intersect_key(array_combine($route_vars, $route_vars), $remaining_args); $matched_arguments = array_intersect_key($route_vars, $remaining_args);
if (count($matched_arguments) === count($route_vars)) if (count($matched_arguments) === count($route_vars))
{ {
$reordered_routes[$route] = count($matched_arguments); $reordered_routes[$route] = count($matched_arguments);
@ -214,6 +219,8 @@ class Router
return $args['mid'] . '/' . $args['act'] . (count($remaining_args) ? ('?' . http_build_query($remaining_args)) : ''); return $args['mid'] . '/' . $args['act'] . (count($remaining_args) ? ('?' . http_build_query($remaining_args)) : '');
} }
// Try XE-compatible global routes.
// If no route matches, just create a query string. // If no route matches, just create a query string.
return 'index.php?' . http_build_query($args); return 'index.php?' . http_build_query($args);
} }

View file

@ -56,13 +56,19 @@
</grant> </grant>
</grants> </grants>
<actions> <actions>
<action name="dispBoardContent" type="view" permission="list" standalone="false" index="true" /> <action name="dispBoardContent" type="view" permission="list" standalone="false" index="true">
<action name="dispBoardWrite" type="view" permission="write_document" standalone="false" meta-noindex="true" route="write|edit/$document_srl" /> <route route="page/$page:int" />
<action name="dispBoardDelete" type="view" permission="write_document" standalone="false" meta-noindex="true" route="delete/$document_srl" /> <route route="search/$search_target/$search_keyword" />
<action name="dispBoardWriteComment" type="view" permission="write_comment" standalone="false" meta-noindex="true" route="comment/write/$document_srl" /> </action>
<action name="dispBoardReplyComment" type="view" permission="write_comment" standalone="false" meta-noindex="true" route="comment/reply/$comment_srl"/> <action name="dispBoardWrite" type="view" permission="write_document" standalone="false" meta-noindex="true">
<action name="dispBoardModifyComment" type="view" permission="write_comment" standalone="false" meta-noindex="true" route="comment/edit/$comment_srl" /> <route route="write" />
<action name="dispBoardDeleteComment" type="view" permission="write_comment" standalone="false" meta-noindex="true" route="comment/delete/$comment_srl" /> <route route="$document_srl/edit" />
</action>
<action name="dispBoardDelete" type="view" permission="write_document" standalone="false" meta-noindex="true" route="$document_srl/delete" />
<action name="dispBoardWriteComment" type="view" permission="write_comment" standalone="false" meta-noindex="true" route="$document_srl/comment" />
<action name="dispBoardReplyComment" type="view" permission="write_comment" standalone="false" meta-noindex="true" route="comment/$comment_srl/reply"/>
<action name="dispBoardModifyComment" type="view" permission="write_comment" standalone="false" meta-noindex="true" route="comment/$comment_srl/edit" />
<action name="dispBoardDeleteComment" type="view" permission="write_comment" standalone="false" meta-noindex="true" route="comment/$comment_srl/delete" />
<action name="dispBoardDeleteTrackback" type="view" permission="list,view" standalone="false" meta-noindex="true" /> <action name="dispBoardDeleteTrackback" type="view" permission="list,view" standalone="false" meta-noindex="true" />
<action name="dispBoardContentList" type="view" permission="list" standalone="false" /> <action name="dispBoardContentList" type="view" permission="list" standalone="false" />
<action name="dispBoardContentView" type="view" permission="view" standalone="false" /> <action name="dispBoardContentView" type="view" permission="view" standalone="false" />