Skip to content

Commit 702c945

Browse files
authored
Merge pull request #108 from utopia-php/feat-route-override
allow overriding routes
2 parents 207f773 + 9ac646a commit 702c945

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed

src/App.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,29 @@ public static function setMode(string $value): void
298298
self::$mode = $value;
299299
}
300300

301+
/**
302+
* Get allow override
303+
*
304+
*
305+
* @return bool
306+
*/
307+
public static function getAllowOverride(): bool
308+
{
309+
return Router::getAllowOverride();
310+
}
311+
312+
/**
313+
* Set Allow override
314+
*
315+
*
316+
* @param bool $value
317+
* @return void
318+
*/
319+
public static function setAllowOverride(bool $value): void
320+
{
321+
Router::setAllowOverride($value);
322+
}
323+
301324
/**
302325
* If a resource has been created return it, otherwise create it and then return it
303326
*

src/Router.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class Router
1212
public const PLACEHOLDER_TOKEN = ':::';
1313
public const WILDCARD_TOKEN = '*';
1414

15+
protected static bool $allowOverride = false;
16+
1517
/**
1618
* @var array<string,Route[]>
1719
*/
@@ -40,6 +42,30 @@ public static function getRoutes(): array
4042
return self::$routes;
4143
}
4244

45+
/**
46+
* Get allow override
47+
*
48+
*
49+
* @return bool
50+
*/
51+
public static function getAllowOverride(): bool
52+
{
53+
return self::$allowOverride;
54+
}
55+
56+
/**
57+
* Set Allow override
58+
*
59+
*
60+
* @param bool $value
61+
* @return void
62+
*/
63+
public static function setAllowOverride(bool $value): void
64+
{
65+
self::$allowOverride = $value;
66+
}
67+
68+
4369
/**
4470
* Add route to router.
4571
*
@@ -55,7 +81,7 @@ public static function addRoute(Route $route): void
5581
throw new Exception("Method ({$route->getMethod()}) not supported.");
5682
}
5783

58-
if (array_key_exists($path, self::$routes[$route->getMethod()])) {
84+
if (array_key_exists($path, self::$routes[$route->getMethod()]) && !self::$allowOverride) {
5985
throw new Exception("Route for ({$route->getMethod()}:{$path}) already registered.");
6086
}
6187

@@ -77,7 +103,7 @@ public static function addRouteAlias(string $path, Route $route): void
77103
{
78104
[$alias] = self::preparePath($path);
79105

80-
if (array_key_exists($alias, self::$routes[$route->getMethod()])) {
106+
if (array_key_exists($alias, self::$routes[$route->getMethod()]) && !self::$allowOverride) {
81107
throw new Exception("Route for ({$route->getMethod()}:{$alias}) already registered.");
82108
}
83109

tests/AppTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPUnit\Framework\TestCase;
66
use Utopia\Tests\UtopiaRequestTest;
77
use Utopia\Validator\Text;
8+
use Exception;
89

910
class AppTest extends TestCase
1011
{
@@ -315,6 +316,36 @@ public function testCanAddAndExecuteHooks()
315316
$this->assertEquals('x-def', $result);
316317
}
317318

319+
public function testAllowRouteOverrides()
320+
{
321+
App::setAllowOverride(false);
322+
$this->assertFalse(App::getAllowOverride());
323+
App::get('/')->action(function () {
324+
echo 'Hello first';
325+
});
326+
327+
try {
328+
App::get('/')->action(function () {
329+
echo 'Hello second';
330+
});
331+
$this->fail('Failed to throw exception');
332+
} catch (Exception $e) {
333+
// Threw exception as expected
334+
$this->assertEquals('Route for (GET:) already registered.', $e->getMessage());
335+
}
336+
337+
// Test success
338+
App::setAllowOverride(true);
339+
$this->assertTrue(App::getAllowOverride());
340+
App::get('/')->action(function () {
341+
echo 'Hello first';
342+
});
343+
344+
App::get('/')->action(function () {
345+
echo 'Hello second';
346+
});
347+
}
348+
318349
public function testCanHookThrowExceptions()
319350
{
320351
$this->app

0 commit comments

Comments
 (0)