From 660a642e8a63fe537fa221a178d0094f551e0331 Mon Sep 17 00:00:00 2001 From: Austin Collier Date: Tue, 6 Feb 2024 07:47:17 -0700 Subject: [PATCH 1/3] fixed bug with grouped routes --- flight/Engine.php | 1 + flight/core/Dispatcher.php | 2 +- flight/net/Router.php | 28 ++++++++++++++++++++++++++-- tests/RouterTest.php | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 3 deletions(-) diff --git a/flight/Engine.php b/flight/Engine.php index 58b0859..398061e 100644 --- a/flight/Engine.php +++ b/flight/Engine.php @@ -64,6 +64,7 @@ use flight\net\Route; * @method void etag(string $id, ('strong'|'weak') $type = 'strong') Handles ETag HTTP caching. * @method void lastModified(int $time) Handles last modified HTTP caching. */ +// phpcs:ignoreFile PSR2.Methods.MethodDeclaration.Underscore class Engine { /** @var array Stored variables. */ diff --git a/flight/core/Dispatcher.php b/flight/core/Dispatcher.php index d91b4b2..9fd8d1e 100644 --- a/flight/core/Dispatcher.php +++ b/flight/core/Dispatcher.php @@ -78,7 +78,7 @@ class Dispatcher * Assigns a callback to an event. * * @param string $name Event name - * @param Closure(): (void|mixed) $callback Callback function + * @param Closure(): (void|mixed)|callable $callback Callback function * * @return $this */ diff --git a/flight/net/Router.php b/flight/net/Router.php index 5d067ad..895b337 100644 --- a/flight/net/Router.php +++ b/flight/net/Router.php @@ -46,6 +46,13 @@ class Router */ protected array $group_middlewares = []; + /** + * Allowed HTTP methods + * + * @var array + */ + protected array $allowed_methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS']; + /** * Gets mapped routes. * @@ -74,7 +81,19 @@ class Router */ public function map(string $pattern, callable $callback, bool $pass_route = false, string $route_alias = ''): Route { - $url = trim($pattern); + + // This means that the route ies defined in a group, but the defined route is the base + // url path. Note the '' in route() + // Ex: Flight::group('/api', function() { + // Flight::route('', function() {}); + // } + // Keep the space so that it can execute the below code normally + if ($this->group_prefix !== '') { + $url = ltrim($pattern); + } else { + $url = trim($pattern); + } + $methods = ['*']; if (false !== strpos($url, ' ')) { @@ -83,7 +102,12 @@ class Router $methods = explode('|', $method); } - $route = new Route($this->group_prefix . $url, $callback, $methods, $pass_route, $route_alias); + // And this finishes it off. + if ($this->group_prefix !== '') { + $url = rtrim($this->group_prefix . $url); + } + + $route = new Route($url, $callback, $methods, $pass_route, $route_alias); // to handle group middleware foreach ($this->group_middlewares as $gm) { diff --git a/tests/RouterTest.php b/tests/RouterTest.php index f67759f..1b52ae1 100644 --- a/tests/RouterTest.php +++ b/tests/RouterTest.php @@ -459,6 +459,41 @@ class RouterTest extends TestCase $this->check('123'); } + public function testGroupRouteWithEmptyMapPath() + { + $this->router->group('/user', function (Router $router) { + $router->map('', function () { + echo "I'm a little teapot"; + }); + }); + $this->request->url = '/user'; + $this->check('I\'m a little teapot'); + } + + public function testGroupRouteWithEmptyGetPath() + { + $this->router->group('/user', function (Router $router) { + $router->get('', function () { + echo "I'm a little teapot"; + }); + }); + $this->request->url = '/user'; + $this->request->method = 'GET'; + $this->check('I\'m a little teapot'); + } + + public function testGroupRouteWithEmptyMultipleMethodsPath() + { + $this->router->group('/user', function (Router $router) { + $router->map('GET|POST ', function () { + echo "I'm a little teapot"; + }); + }); + $this->request->url = '/user'; + $this->request->method = 'GET'; + $this->check('I\'m a little teapot'); + } + public function testGroupRoutesMultiParams() { $this->router->group('/user', function (Router $router) { From 84dbed0dc076938de7be6ff27dbfda950e0b492c Mon Sep 17 00:00:00 2001 From: fadrian06 Date: Tue, 6 Feb 2024 20:32:09 -0400 Subject: [PATCH 2/3] What's sense of using phpcs with ignored rules :\ --- flight/Engine.php | 1 - 1 file changed, 1 deletion(-) diff --git a/flight/Engine.php b/flight/Engine.php index 398061e..58b0859 100644 --- a/flight/Engine.php +++ b/flight/Engine.php @@ -64,7 +64,6 @@ use flight\net\Route; * @method void etag(string $id, ('strong'|'weak') $type = 'strong') Handles ETag HTTP caching. * @method void lastModified(int $time) Handles last modified HTTP caching. */ -// phpcs:ignoreFile PSR2.Methods.MethodDeclaration.Underscore class Engine { /** @var array Stored variables. */ From 72882b24deae6c37e6b5d0ff593d625432d5e36d Mon Sep 17 00:00:00 2001 From: fadrian06 Date: Tue, 6 Feb 2024 20:33:08 -0400 Subject: [PATCH 3/3] Without parentheses, it was interpreting Closure(): (void|mixed|callable) --- flight/core/Dispatcher.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flight/core/Dispatcher.php b/flight/core/Dispatcher.php index 9fd8d1e..d91b4b2 100644 --- a/flight/core/Dispatcher.php +++ b/flight/core/Dispatcher.php @@ -78,7 +78,7 @@ class Dispatcher * Assigns a callback to an event. * * @param string $name Event name - * @param Closure(): (void|mixed)|callable $callback Callback function + * @param Closure(): (void|mixed) $callback Callback function * * @return $this */