From c90032486020f317a0eb2d25b9fdb6ba4ad21f91 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Thu, 16 Feb 2023 00:21:57 +0100 Subject: [PATCH] Url::add_url_handler(): clean code and return bool --- src/Url.php | 65 +++++++++++++++++++++++++---------------------------- 1 file changed, 30 insertions(+), 35 deletions(-) diff --git a/src/Url.php b/src/Url.php index d87c70e..4cda8cd 100644 --- a/src/Url.php +++ b/src/Url.php @@ -79,13 +79,14 @@ class Url { * authenticated users (optional, default: true if the * special force_authentication function is defined, * 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) * @param boolean $api_mode Enable API mode (optional, default: false) * @param array|string|null $methods HTTP method (optional, default: array('GET', 'POST')) + * @return bool **/ 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) { $authenticated = ( is_null($authenticated)? @@ -97,48 +98,42 @@ class Url { elseif (!is_array($methods)) $methods = array($methods); if (is_array($pattern)) { + $error = false; if (is_null($handler)) foreach($pattern as $p => $h) - self :: add_url_handler( - $p, $h, $additional_info, $authenticated, $override, $api_mode, $methods); + if ( + !self :: add_url_handler( + $p, $h, $additional_info, $authenticated, $overwrite, $api_mode, $methods) + ) $error = true; else foreach($pattern as $p) - self :: add_url_handler( - $p, $handler, $additional_info, $authenticated, $override, $api_mode, $methods); + if ( + !self :: add_url_handler( + $p, $handler, $additional_info, $authenticated, $overwrite, $api_mode, $methods) + ) $error = true; + return !$error; } - else { - if (!isset(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, - ); - } - elseif ($override) { + + if (!isset(self :: $patterns[$pattern]) || $overwrite) { + if (isset(self :: $patterns[$pattern])) Log :: debug( - "URL : override pattern '%s' with handler '%s' (old handler = '%s')". + "URL : overwrite 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."); - } + self :: $patterns[$pattern] = array( + 'handler' => $handler, + 'additional_info' => ( + is_array($additional_info)? + $additional_info:array() + ), + 'authenticated' => $authenticated, + 'api_mode' => $api_mode, + 'methods' => $methods, + ); + return true; } + Log :: debug("URL : pattern '$pattern' already defined : do not overwrite."); + return false; } /**