Skip to content

Commit

Permalink
Merge pull request #108 from utopia-php/feat-route-override
Browse files Browse the repository at this point in the history
allow overriding routes
  • Loading branch information
abnegate authored Nov 24, 2023
2 parents 207f773 + 9ac646a commit 702c945
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
23 changes: 23 additions & 0 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
30 changes: 28 additions & 2 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class Router
public const PLACEHOLDER_TOKEN = ':::';
public const WILDCARD_TOKEN = '*';

protected static bool $allowOverride = false;

/**
* @var array<string,Route[]>
*/
Expand Down Expand Up @@ -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.
*
Expand All @@ -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.");
}

Expand All @@ -77,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.");
}

Expand Down
31 changes: 31 additions & 0 deletions tests/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHPUnit\Framework\TestCase;
use Utopia\Tests\UtopiaRequestTest;
use Utopia\Validator\Text;
use Exception;

class AppTest extends TestCase
{
Expand Down Expand Up @@ -315,6 +316,36 @@ 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';
});

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);
$this->assertTrue(App::getAllowOverride());
App::get('/')->action(function () {
echo 'Hello first';
});

App::get('/')->action(function () {
echo 'Hello second';
});
}

public function testCanHookThrowExceptions()
{
$this->app
Expand Down

0 comments on commit 702c945

Please sign in to comment.