From 7dbea43ee03a83155ef59858c56f10b6816e6210 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9F=93=E5=BF=B5?= <38277217+dyedd@users.noreply.github.com> Date: Thu, 6 Aug 2020 18:05:25 +0800 Subject: [PATCH 1/3] Update Macaw.php Add routing group --- Macaw.php | 46 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/Macaw.php b/Macaw.php index 2adca52..be98a1a 100644 --- a/Macaw.php +++ b/Macaw.php @@ -9,8 +9,11 @@ * @method static Macaw delete(string $route, Callable $callback) * @method static Macaw options(string $route, Callable $callback) * @method static Macaw head(string $route, Callable $callback) + * @method static Macaw any(string $route, Callable $callback) + * @method static Macaw map(string $route, Callable $callback) */ class Macaw { + public static $uri; public static $halts = false; public static $routes = array(); public static $methods = array(); @@ -60,16 +63,8 @@ public static function haltOnMatch($flag = true) { * Runs the callback for the given request */ public static function dispatch(){ - $uri = rtrim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/'); - if (!empty(self::$root) && self::$root !== '/') { - self::$root = rtrim(self::$root, '/'); - if (self::$root === $uri) { - $uri = '/'; - } else { - // Remove the root directory from uri, remove only the first occurrence - $uri = substr_replace($uri, '', strpos($uri, self::$root), strlen(self::$root)); - } - } + self::$uri = rtrim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/'); + self::addRoot(); $method = $_SERVER['REQUEST_METHOD']; $searches = array_keys(static::$patterns); @@ -181,4 +176,35 @@ public static function dispatch(){ call_user_func(self::$error_callback); } } + /** + * Set the directory of the group + */ + public static function group($root, $member) + { + if (!empty($root)){ + self::$root = $root; + } + if (!is_array($member)) + self::$errorCallback = function() { + echo 'Arrays are best'; + }; + } + /** + * Add routing group + */ + public static function addRoot() + { + if (!empty(self::$root) && self::$root !== '/') { + self::$root = rtrim(self::$root, '/'); + if (self::$root === self::$uri) { + self::$uri = '/'; + } else { + if (strpos(self::$uri, self::$root)) + // Delete the root directory and only delete the first directory + self::$uri = substr_replace(self::$uri, '', strpos(self::$uri, self::$root), strlen(self::$root) + 1); + } + } + // Prevent next non routing loop + self::$root = null; + } } From 24199366330554b6cfb4451f7f6c821cb3226bc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9F=93=E5=BF=B5?= <38277217+dyedd@users.noreply.github.com> Date: Thu, 6 Aug 2020 19:27:45 +0800 Subject: [PATCH 2/3] Added support for multi-level directory --- Macaw.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Macaw.php b/Macaw.php index be98a1a..c699430 100644 --- a/Macaw.php +++ b/Macaw.php @@ -26,6 +26,8 @@ class Macaw { ); public static $error_callback; public static $root; + // Multi level directory + public static $app = '/x/x'; /** * Defines a route w/ callback and method @@ -63,7 +65,9 @@ public static function haltOnMatch($flag = true) { * Runs the callback for the given request */ public static function dispatch(){ - self::$uri = rtrim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/'); + $uri = rtrim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/'); + $repUri = implode('/', array_intersect(explode( '\\', self::$app), explode('/', $uri))); + self::$uri = preg_replace('/\/'. $repUri .'/', '', $uri); self::addRoot(); $method = $_SERVER['REQUEST_METHOD']; @@ -75,8 +79,8 @@ public static function dispatch(){ self::$routes = preg_replace('/\/+/', '/', self::$routes); // Check if route is defined without regex - if (in_array($uri, self::$routes)) { - $route_pos = array_keys(self::$routes, $uri); + if (in_array(self::$uri, self::$routes)) { + $route_pos = array_keys(self::$routes, self::$uri); foreach ($route_pos as $route) { // Using an ANY option to match both GET and POST requests @@ -118,7 +122,7 @@ public static function dispatch(){ $route = str_replace($searches, $replaces, $route); } - if (preg_match('#^' . $route . '$#', $uri, $matched)) { + if (preg_match('#^' . $route . '$#', self::$uri, $matched)) { if (self::$methods[$pos] == $method || self::$methods[$pos] == 'ANY' || (!empty(self::$maps[$pos]) && in_array($method, self::$maps[$pos]))) { $found_route = true; From 58215d6f1bf40607d9b22b09ea87db647a35d7c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9F=93=E5=BF=B5?= <38277217+dyedd@users.noreply.github.com> Date: Thu, 6 Aug 2020 20:08:07 +0800 Subject: [PATCH 3/3] Resolve routing group conflicts --- Macaw.php | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/Macaw.php b/Macaw.php index c699430..4527822 100644 --- a/Macaw.php +++ b/Macaw.php @@ -25,7 +25,7 @@ class Macaw { ':all' => '.*' ); public static $error_callback; - public static $root; + public static $root = array(); // Multi level directory public static $app = '/x/x'; @@ -186,7 +186,7 @@ public static function dispatch(){ public static function group($root, $member) { if (!empty($root)){ - self::$root = $root; + self::$root[$root] = $root; } if (!is_array($member)) self::$errorCallback = function() { @@ -198,14 +198,18 @@ public static function group($root, $member) */ public static function addRoot() { - if (!empty(self::$root) && self::$root !== '/') { - self::$root = rtrim(self::$root, '/'); - if (self::$root === self::$uri) { - self::$uri = '/'; - } else { - if (strpos(self::$uri, self::$root)) - // Delete the root directory and only delete the first directory - self::$uri = substr_replace(self::$uri, '', strpos(self::$uri, self::$root), strlen(self::$root) + 1); + foreach (self::$root as $root) { + if (!empty($root) && $root !== '/') { + $root = rtrim($root, '/'); + if ($root === self::$uri) { + self::$uri = '/'; + } else { + if (strpos(self::$uri, $root)) { + // Delete the root directory and only delete the first directory + self::$uri = substr_replace(self::$uri, '', strpos(self::$uri, $root), strlen($root) + 1); + break; + } + } } } // Prevent next non routing loop