Skip to content

Commit

Permalink
allow overriding routes
Browse files Browse the repository at this point in the history
  • Loading branch information
lohanidamodar committed Sep 25, 2023
1 parent 207f773 commit d3ae3a7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
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
28 changes: 27 additions & 1 deletion 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 Down

0 comments on commit d3ae3a7

Please sign in to comment.