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 f5d5969195

View file

@ -79,52 +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) {
'handler' => $handler, if (!isset(self :: $patterns[$http_method]))
'additional_info' => ( self :: $patterns[$http_method] = array();
is_array($additional_info)? // Check overwrite
$additional_info:array() if (isset(self :: $patterns[$http_method][$pattern])) {
), if (!$overwrite) {
'authenticated' => $authenticated, Log :: error(
'api_mode' => $api_mode, "URL : pattern '%s' already defined for HTTP method %s: do not overwrite.".
'methods' => $methods, $pattern, $http_method);
); $error = true;
continue;
} }
elseif ($override) {
Log :: debug( Log :: debug(
"URL : override pattern '%s' with handler '%s' (old handler = '%s')". "URL : overwrite pattern '%s' for HTTP method %s with handler '%s' ".
$pattern, format_callable($handler), vardump(self :: $patterns[$pattern]) "(old handler = '%s')".
$pattern, $http_method, format_callable($handler),
vardump(self :: $patterns[$http_method][$pattern])
); );
self :: $patterns[$pattern] = array( }
// 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)?
@ -132,13 +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",
$pattern, $http_method, vardump(self :: $patterns[$http_method][$pattern])
); );
} }
else {
Log :: debug("URL : pattern '$pattern' already defined : do not override."); return !$error;
}
}
} }
/** /**
@ -248,12 +265,13 @@ class Url {
} }
Log :: debug("URL : current url = '$current_url'"); Log :: debug("URL : current url = '$current_url'");
if (array_key_exists($_SERVER['REQUEST_METHOD'], self :: $patterns)) {
Log :: trace( Log :: trace(
"URL : check current url with the following URL patterns :\n - ". "URL : check current url with the following URL patterns :\n - ".
implode("\n - ", array_keys(self :: $patterns)) implode("\n - ", array_keys(self :: $patterns))
); );
foreach (self :: $patterns as $pattern => $handler_infos) { foreach (self :: $patterns[$_SERVER['REQUEST_METHOD']] as $pattern => $handler_infos) {
$m = self :: url_match($pattern, $current_url, $handler_infos['methods']); $m = self :: url_match($pattern, $current_url);
if (is_array($m)) { if (is_array($m)) {
$request = new UrlRequest($current_url, $handler_infos, $m); $request = new UrlRequest($current_url, $handler_infos, $m);
// Reset last redirect // Reset last redirect
@ -263,6 +281,12 @@ class Url {
return $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);
@ -287,13 +311,10 @@ class Url {
* *
* @param string $pattern The URL pattern * @param string $pattern The URL pattern
* @param string|false $current_url The current URL (optional) * @param string|false $current_url The current URL (optional)
* @param array|null $methods HTTP method (optional, default: no check)
* *
* @return array|false The URL info if pattern matched, false otherwise. * @return array|false The URL info if pattern matched, false otherwise.
**/ **/
public static function url_match($pattern, $current_url=false, $methods=null) { public static function url_match($pattern, $current_url=false) {
if ($methods && !in_array($_SERVER['REQUEST_METHOD'], $methods))
return false;
if ($current_url === false) { if ($current_url === false) {
$current_url = self :: get_current_url(); $current_url = self :: get_current_url();
if (!$current_url) return False; if (!$current_url) return False;