From 9a04c906a72fbc9ed70f910bef8b4dd4b022bb40 Mon Sep 17 00:00:00 2001 From: Splashsky Date: Fri, 16 Jul 2021 04:43:30 -0500 Subject: [PATCH 1/5] Add @return tags to comments --- src/Splashsky/Router.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Splashsky/Router.php b/src/Splashsky/Router.php index b9d1177..0006039 100644 --- a/src/Splashsky/Router.php +++ b/src/Splashsky/Router.php @@ -34,6 +34,7 @@ class Router * * @param string $route * @param callable $action + * @return Router */ public static function get(string $route, callable $action) { @@ -45,6 +46,7 @@ class Router * * @param string $route * @param callable $action + * @return Router */ public static function post(string $route, callable $action) { @@ -65,6 +67,7 @@ class Router * Defines an action to be called when a path isn't found - i.e. a 404 * * @param callable $action + * @return void */ public static function pathNotFound(callable $action) { @@ -75,6 +78,7 @@ class Router * Defines an action to be called with a method isn't allowed on a route - i.e. a 405 * * @param callable $action + * @return void */ public static function methodNotAllowed(callable $action) { @@ -149,6 +153,7 @@ class Router * * @param string $basePath * @param boolean $multimatch + * @return void */ public static function run(string $basePath = '', bool $multimatch = false) { From 1393e0face008325fbdeee6c05f93aa8ed40a4d1 Mon Sep 17 00:00:00 2001 From: Splashsky Date: Fri, 16 Jul 2021 07:06:37 -0500 Subject: [PATCH 2/5] =?UTF-8?q?=F0=9F=9A=A7=20Build=20new=20prefix=20group?= =?UTF-8?q?=20type?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Splashsky/Router.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/Splashsky/Router.php b/src/Splashsky/Router.php index 0006039..f77b83c 100644 --- a/src/Splashsky/Router.php +++ b/src/Splashsky/Router.php @@ -9,6 +9,7 @@ class Router private static $pathNotFound; private static $methodNotAllowed; private static string $defaultConstraint = '([\w\-]+)'; + private static string $currentPrefix = ''; /** * A quick static function to register a route in the router. Used by the shorthand methods as well. @@ -20,6 +21,11 @@ class Router */ public static function add(string $route, callable|string $action, string|array $methods = 'GET') { + // If a prefix exists, prepend it to the route + if (!empty(self::$currentPrefix)) { + $route = self::$currentPrefix.$route; + } + self::$routes[] = [ 'route' => $route, 'action' => $action, @@ -96,6 +102,22 @@ class Router self::$defaultConstraint = $constraint; } + /** + * Accepts a callable that defines routes, and adds a prefix to them. + * + * @param string $prefix The prefix you want added to the routes. + * @param callable $routes A function that defines routes. + * @return void + */ + public static function prefix(string $prefix, callable $routes) + { + self::$currentPrefix = $prefix; + + $routes(); + + self::$currentPrefix = ''; + } + /** * Define a constraint for a route parameter. If only passing one parameter, * provide the parameter name as first argument and constraint as second. If From d0bef18c59fb360c30fa1528b4f6f926ad6ef415 Mon Sep 17 00:00:00 2001 From: Splashsky Date: Fri, 16 Jul 2021 07:14:33 -0500 Subject: [PATCH 3/5] =?UTF-8?q?=F0=9F=A7=BC=20Clean=20up=20routing=20behav?= =?UTF-8?q?ior=20with=20new=20trim=20method?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Splashsky/Router.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Splashsky/Router.php b/src/Splashsky/Router.php index f77b83c..7ebfe93 100644 --- a/src/Splashsky/Router.php +++ b/src/Splashsky/Router.php @@ -27,7 +27,7 @@ class Router } self::$routes[] = [ - 'route' => $route, + 'route' => self::trimRoute($route), 'action' => $action, 'methods' => $methods ]; @@ -102,6 +102,12 @@ class Router self::$defaultConstraint = $constraint; } + private static function trimRoute(string $route): string + { + $route = trim(trim($route), '/'); + return "/$route"; + } + /** * Accepts a callable that defines routes, and adds a prefix to them. * @@ -182,12 +188,7 @@ class Router $basePath = rtrim($basePath, '/'); $method = $_SERVER['REQUEST_METHOD']; $uri = parse_url($_SERVER['REQUEST_URI'])['path']; - $path = urldecode(rtrim($uri, '/')); - - // If the path is empty (no slash in URI) place one to satisfy the root route ('/') - if (empty($path)) { - $path = '/'; - } + $path = urldecode(self::trimRoute($uri)); $pathMatchFound = false; $routeMatchFound = false; From 3dd5ddc83e2a2ab5797cc5dfa4c594d78ea3d5f3 Mon Sep 17 00:00:00 2001 From: Splashsky Date: Fri, 16 Jul 2021 07:17:40 -0500 Subject: [PATCH 4/5] =?UTF-8?q?=F0=9F=A7=B4=20Make=20constraint=20definiti?= =?UTF-8?q?ons=20smoother=20via=20trim?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Splashsky/Router.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Splashsky/Router.php b/src/Splashsky/Router.php index 7ebfe93..898b2bf 100644 --- a/src/Splashsky/Router.php +++ b/src/Splashsky/Router.php @@ -166,7 +166,10 @@ class Router $pattern = '{'.$match.'}'; if (in_array($match, $constraints)) { - $uri = str_replace($pattern, '('.self::$constraints[$match].')', $uri); + // Do some voodoo to allow users to use parentheses in their constraints if they want + $constraint = '('.rtrim(ltrim(trim(self::$constraints[$match]), '('), ')').')'; + + $uri = str_replace($pattern, $constraint, $uri); } else { $uri = str_replace($pattern, self::$defaultConstraint, $uri); } From 227e5edc06c110dc349e1ebda9b03cd83993e6a4 Mon Sep 17 00:00:00 2001 From: Splashsky Date: Fri, 16 Jul 2021 07:20:58 -0500 Subject: [PATCH 5/5] =?UTF-8?q?=F0=9F=A7=BC=20Clean=20up=20request=20handl?= =?UTF-8?q?ing=20by=20treating=20basePath=20similar=20to=20other=20routes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Splashsky/Router.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Splashsky/Router.php b/src/Splashsky/Router.php index 898b2bf..5dc6139 100644 --- a/src/Splashsky/Router.php +++ b/src/Splashsky/Router.php @@ -188,7 +188,7 @@ class Router */ public static function run(string $basePath = '', bool $multimatch = false) { - $basePath = rtrim($basePath, '/'); + $basePath = self::trimRoute($basePath); $method = $_SERVER['REQUEST_METHOD']; $uri = parse_url($_SERVER['REQUEST_URI'])['path']; $path = urldecode(self::trimRoute($uri)); @@ -198,7 +198,8 @@ class Router // Begin looking through routes foreach (self::$routes as $route) { - if ($basePath != '' && $basePath != '/') { + // If the basePath isn't just "root" + if ($basePath != '/') { $route['route'] = $basePath.$route['route']; }