Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow overriding routes #108

Merged
merged 6 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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