Url::add_url_handler(): clean code and return bool

This commit is contained in:
Benjamin Renard 2023-02-16 00:21:57 +01:00
parent 5ce273cfad
commit 705d917655

View file

@ -79,36 +79,68 @@ class Url {
* authenticated users (optional, default: true if the * authenticated users (optional, default: true if the
* special force_authentication function is defined, * special force_authentication function is defined,
* false otherwise) * false otherwise)
* @param boolean $override Allow override if a command already exists with the * @param boolean $overwrite Allow overwrite if a command already exists with the
* same name (optional, default: false) * same name (optional, default: false)
* @param boolean $api_mode Enable API mode (optional, default: false) * @param boolean $api_mode Enable API mode (optional, default: false)
* @param array|string|null $methods HTTP method (optional, default: array('GET', 'POST')) * @param array|string|null $http_methods HTTP method (optional, default: array('GET', 'POST'))
* @return bool
**/ **/
public static function add_url_handler($pattern, $handler=null, $additional_info=null, public static function add_url_handler($pattern, $handler=null, $additional_info=null,
$authenticated=null, $override=true, $api_mode=false, $authenticated=null, $overwrite=true, $api_mode=false,
$methods=null) { $http_methods=null) {
$authenticated = ( $authenticated = (
is_null($authenticated)? is_null($authenticated)?
function_exists('force_authentication'): function_exists('force_authentication'):
(bool)$authenticated (bool)$authenticated
); );
if (is_null($methods))
$methods = array('GET', 'POST'); // Check HTTP methods parameter
elseif (!is_array($methods)) if (is_null($http_methods))
$methods = array($methods); $http_methods = array('GET', 'POST');
elseif (!is_array($http_methods))
$http_methods = array($http_methods);
// If multiple patterns specify using an array, add each of them
if (is_array($pattern)) { if (is_array($pattern)) {
$error = false;
if (is_null($handler)) if (is_null($handler))
foreach($pattern as $p => $h) foreach($pattern as $p => $h)
self :: add_url_handler( if (
$p, $h, $additional_info, $authenticated, $override, $api_mode, $methods); !self :: add_url_handler(
$p, $h, $additional_info, $authenticated, $overwrite, $api_mode, $http_methods)
) $error = true;
else else
foreach($pattern as $p) foreach($pattern as $p)
self :: add_url_handler( if (
$p, $handler, $additional_info, $authenticated, $override, $api_mode, $methods); !self :: add_url_handler(
$p, $handler, $additional_info, $authenticated, $overwrite, $api_mode, $http_methods)
) $error = true;
return !$error;
} }
else {
if (!isset(self :: $patterns[$pattern])) { $error = false;
self :: $patterns[$pattern] = array( foreach($http_methods as $http_method) {
if (!isset(self :: $patterns[$http_method]))
self :: $patterns[$http_method] = array();
// Check overwrite
if (isset(self :: $patterns[$http_method][$pattern])) {
if (!$overwrite) {
Log :: error(
"URL : pattern '%s' already defined for HTTP method %s: do not overwrite.".
$pattern, $http_method);
$error = true;
continue;
}
Log :: debug(
"URL : overwrite pattern '%s' for HTTP method %s with handler '%s' ".
"(old handler = '%s')".
$pattern, $http_method, format_callable($handler),
vardump(self :: $patterns[$http_method][$pattern])
);
}
// Register URL pattern info
self :: $patterns[$http_method][$pattern] = array(
'handler' => $handler, 'handler' => $handler,
'additional_info' => ( 'additional_info' => (
is_array($additional_info)? is_array($additional_info)?
@ -116,29 +148,14 @@ class Url {
), ),
'authenticated' => $authenticated, 'authenticated' => $authenticated,
'api_mode' => $api_mode, 'api_mode' => $api_mode,
'methods' => $methods, );
); Log :: trace(
} "URL: Register pattern '%s' on HTTP %s with :\n%s",
elseif ($override) { $pattern, $http_method, vardump(self :: $patterns[$http_method][$pattern])
Log :: debug( );
"URL : override pattern '%s' with handler '%s' (old handler = '%s')".
$pattern, format_callable($handler), vardump(self :: $patterns[$pattern])
);
self :: $patterns[$pattern] = array(
'handler' => $handler,
'additional_info' => (
is_array($additional_info)?
$additional_info:array()
),
'authenticated' => $authenticated,
'api_mode' => $api_mode,
'methods' => $methods,
);
}
else {
Log :: debug("URL : pattern '$pattern' already defined : do not override.");
}
} }
return !$error;
} }
/** /**
@ -248,21 +265,28 @@ class Url {
} }
Log :: debug("URL : current url = '$current_url'"); Log :: debug("URL : current url = '$current_url'");
Log :: trace( if (array_key_exists($_SERVER['REQUEST_METHOD'], self :: $patterns)) {
"URL : check current url with the following URL patterns :\n - ". Log :: trace(
implode("\n - ", array_keys(self :: $patterns)) "URL : check current url with the following URL patterns :\n - ".
); implode("\n - ", array_keys(self :: $patterns))
foreach (self :: $patterns as $pattern => $handler_infos) { );
$m = self :: url_match($pattern, $current_url, $handler_infos['methods']); foreach (self :: $patterns[$_SERVER['REQUEST_METHOD']] as $pattern => $handler_infos) {
if (is_array($m)) { $m = self :: url_match($pattern, $current_url, $handler_infos['methods']);
$request = new UrlRequest($current_url, $handler_infos, $m); if (is_array($m)) {
// Reset last redirect $request = new UrlRequest($current_url, $handler_infos, $m);
if (isset($_SESSION['last_redirect'])) // Reset last redirect
unset($_SESSION['last_redirect']); if (isset($_SESSION['last_redirect']))
Log :: trace("URL : result :\n".vardump($request)); unset($_SESSION['last_redirect']);
return $request; Log :: trace("URL : result :\n".vardump($request));
return $request;
}
} }
} }
else {
Log :: debug(
'URL: no URL pattern registered for %s HTTP method',
$_SERVER['REQUEST_METHOD']);
}
if ($default_url !== false) { if ($default_url !== false) {
Log :: debug("Current url match with no pattern. Redirect to default url ('$default_url')"); Log :: debug("Current url match with no pattern. Redirect to default url ('$default_url')");
self :: redirect($default_url); self :: redirect($default_url);