public static function get($rule_names = '', $args = array(), $useall = true, $noamp = false, $prepend_site = true)
{
$f_args = self::extract_args($args);
$f_args = Plugins::filter('url_args', $f_args, $args, $rule_names);
$url = URL::instance();
if ($rule_names == '') {
// Retrieve current matched RewriteRule
$selectedrule = $url->get_matched_rule();
// Retrieve arguments name the RewriteRule can use to build a URL.
$rr_named_args = $selectedrule->named_args;
$rr_args = array_merge($rr_named_args['required'], $rr_named_args['optional']);
// For each argument, check if the handler_vars array has that argument and if it does, use it.
foreach ($rr_args as $rr_arg) {
if (!isset($f_args[$rr_arg])) {
$rr_arg_value = Controller::get_var($rr_arg);
if ($rr_arg_value != '') {
$f_args[$rr_arg] = $rr_arg_value;
}
}
}
} else {
$url->load_rules();
$selectedrule = null;
if (!is_array($rule_names)) {
$rule_names = array($rule_names);
}
foreach ($rule_names as $rule_name) {
if ($rules = $url->rules->by_name($rule_name)) {
$rating = null;
foreach ($rules as $rule) {
$newrating = $rule->arg_match($f_args);
// Is the rating perfect?
if ($rating == 0) {
$selectedrule = $rule;
break;
}
if (empty($rating) || $newrating < $rating) {
$rating = $newrating;
$selectedrule = $rule;
}
}
if (isset($selectedrule)) {
break;
}
}
}
}
if ($selectedrule instanceof RewriteRule) {
$return_url = $selectedrule->build($f_args, $useall, $noamp);
if ($prepend_site) {
return Site::get_url('site', true) . $return_url;
} else {
return $return_url;
}
} else {
$error = new \Exception();
$error_trace = $error->getTrace();
// Since URL::out() calls this function, the index 0 is URL::get() which is not the proper failing call.
if (isset($error_trace[1]['class']) && isset($error_trace[1]['function']) && $error_trace[1]['class'] == 'URL' && $error_trace[1]['function'] == 'out') {
$error_args = $error_trace[1];
} else {
$error_args = $error_trace[0];
}
EventLog::log(_t('Could not find a rule matching the following names: %s. File: %s (line %s)', array(implode(', ', $rule_names), $error_args['file'], $error_args['line'])), 'notice', 'rewriterules', 'habari');
}
}