From d3ae3a740aa65d4a320dbc4519109793eebae50a Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Mon, 25 Sep 2023 07:20:22 +0000 Subject: [PATCH 1/6] allow overriding routes --- src/App.php | 23 +++++++++++++++++++++++ src/Router.php | 28 +++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/App.php b/src/App.php index 300f1b01..afb88192 100755 --- a/src/App.php +++ b/src/App.php @@ -298,6 +298,29 @@ public static function setMode(string $value): void self::$mode = $value; } + /** + * Get allow override + * + * + * @return bool + */ + public static function getAllowOverride(): bool + { + return Router::getAllowOverride(); + } + + /** + * Set Allow override + * + * + * @param bool $value + * @return void + */ + public static function setAllowOverride(bool $value): void + { + Router::setAllowOverride($value); + } + /** * If a resource has been created return it, otherwise create it and then return it * diff --git a/src/Router.php b/src/Router.php index d759a293..3bbd274e 100644 --- a/src/Router.php +++ b/src/Router.php @@ -12,6 +12,8 @@ class Router public const PLACEHOLDER_TOKEN = ':::'; public const WILDCARD_TOKEN = '*'; + protected static bool $allowOverride = false; + /** * @var array */ @@ -40,6 +42,30 @@ public static function getRoutes(): array return self::$routes; } + /** + * Get allow override + * + * + * @return bool + */ + public static function getAllowOverride(): bool + { + return self::$allowOverride; + } + + /** + * Set Allow override + * + * + * @param bool $value + * @return void + */ + public static function setAllowOverride(bool $value): void + { + self::$allowOverride = $value; + } + + /** * Add route to router. * @@ -55,7 +81,7 @@ public static function addRoute(Route $route): void throw new Exception("Method ({$route->getMethod()}) not supported."); } - if (array_key_exists($path, self::$routes[$route->getMethod()])) { + if (array_key_exists($path, self::$routes[$route->getMethod()]) && !self::$allowOverride) { throw new Exception("Route for ({$route->getMethod()}:{$path}) already registered."); } From 3d6f732d99726a167e03d89561b20d868d560b36 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Mon, 25 Sep 2023 07:28:06 +0000 Subject: [PATCH 2/6] fix alias --- src/Router.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Router.php b/src/Router.php index 3bbd274e..a2ad9b99 100644 --- a/src/Router.php +++ b/src/Router.php @@ -103,7 +103,7 @@ public static function addRouteAlias(string $path, Route $route): void { [$alias] = self::preparePath($path); - if (array_key_exists($alias, self::$routes[$route->getMethod()])) { + if (array_key_exists($alias, self::$routes[$route->getMethod()]) && !self::$allowOverride) { throw new Exception("Route for ({$route->getMethod()}:{$alias}) already registered."); } From 7a022b792ac44ed7ea717faad4b1adddd2c3c872 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Fri, 24 Nov 2023 00:58:44 +0000 Subject: [PATCH 3/6] test for overrides --- tests/AppTest.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/AppTest.php b/tests/AppTest.php index d3952185..a856f302 100755 --- a/tests/AppTest.php +++ b/tests/AppTest.php @@ -5,6 +5,7 @@ use PHPUnit\Framework\TestCase; use Utopia\Tests\UtopiaRequestTest; use Utopia\Validator\Text; +use Exception; class AppTest extends TestCase { @@ -315,6 +316,31 @@ public function testCanAddAndExecuteHooks() $this->assertEquals('x-def', $result); } + public function testAllowRouteOverrides() { + App::setAllowOverride(false); + $this->assertFalse(App::getAllowOverride()); + App::get('/')->action(function () { + echo 'Hello first'; + }); + + $this->expectException(Exception::class); + App::get('/')->action(function(){ + echo 'Hello second'; + }); + + // Test success + App::setAllowOverride(true); + $this->assertTrue(App::getAllowOverride()); + App::get('/')->action(function () { + echo 'Hello first'; + }); + + App::get('/')->action(function(){ + echo 'Hello second'; + }); + + } + public function testCanHookThrowExceptions() { $this->app From 8a327a5ad6b5ea8c67f5dff1d06f4e3d0caba993 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Fri, 24 Nov 2023 00:58:55 +0000 Subject: [PATCH 4/6] format --- tests/AppTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/AppTest.php b/tests/AppTest.php index a856f302..4547254e 100755 --- a/tests/AppTest.php +++ b/tests/AppTest.php @@ -316,7 +316,8 @@ public function testCanAddAndExecuteHooks() $this->assertEquals('x-def', $result); } - public function testAllowRouteOverrides() { + public function testAllowRouteOverrides() + { App::setAllowOverride(false); $this->assertFalse(App::getAllowOverride()); App::get('/')->action(function () { @@ -324,7 +325,7 @@ public function testAllowRouteOverrides() { }); $this->expectException(Exception::class); - App::get('/')->action(function(){ + App::get('/')->action(function () { echo 'Hello second'; }); @@ -335,10 +336,9 @@ public function testAllowRouteOverrides() { echo 'Hello first'; }); - App::get('/')->action(function(){ + App::get('/')->action(function () { echo 'Hello second'; }); - } public function testCanHookThrowExceptions() From 29ce0d45552001a8209c844774cd1c3795e57b69 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Fri, 24 Nov 2023 02:22:15 +0000 Subject: [PATCH 5/6] fix exception test --- tests/AppTest.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/tests/AppTest.php b/tests/AppTest.php index 4547254e..751d409c 100755 --- a/tests/AppTest.php +++ b/tests/AppTest.php @@ -324,10 +324,16 @@ public function testAllowRouteOverrides() echo 'Hello first'; }); - $this->expectException(Exception::class); - App::get('/')->action(function () { - echo 'Hello second'; - }); + try { + App::get('/')->action(function () { + echo 'Hello second'; + }); + $this->fail('Failed to throw exception'); + } catch (Exception $e) { + // Threw exception as expected + $this->assertEquals('Route for (GET:) already registered.', $e->getMessage()); + + } // Test success App::setAllowOverride(true); From 9ac646a83d7a8c573c64a1ac131b6743519b8df9 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Fri, 24 Nov 2023 02:22:39 +0000 Subject: [PATCH 6/6] format --- tests/AppTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/AppTest.php b/tests/AppTest.php index 751d409c..a5fe1a27 100755 --- a/tests/AppTest.php +++ b/tests/AppTest.php @@ -332,7 +332,6 @@ public function testAllowRouteOverrides() } catch (Exception $e) { // Threw exception as expected $this->assertEquals('Route for (GET:) already registered.', $e->getMessage()); - } // Test success