Skip to content

Commit

Permalink
implemented case insensitive function for route
Browse files Browse the repository at this point in the history
  • Loading branch information
dconco committed Dec 26, 2024
1 parent 8191cc6 commit 807596c
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 98 deletions.
36 changes: 27 additions & 9 deletions Router/MapRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,13 @@ public function match(string $method, string|array $route): bool|array
* | $_REQUEST['uri'] will be empty if req uri is /
* ----------------------------------------------
*/
self::$request_uri = strtolower(
preg_replace("/(^\/)|(\/$)/", '', Application::$request_uri),
);
self::$request_uri =
preg_replace("/(^\/)|(\/$)/", '', Application::$request_uri);
self::$request_uri = empty(self::$request_uri) ? '/' : self::$request_uri;

self::$route = is_array($route)
? $route
: strtolower(preg_replace("/(^\/)|(\/$)/", '', $route));
: preg_replace("/(^\/)|(\/$)/", '', $route);

// Firstly, resolve route with pattern
if (is_array(self::$route))
Expand Down Expand Up @@ -191,9 +190,15 @@ public function match(string $method, string|array $route): bool|array
* -----------------------------------
*/
$reqUri = str_replace('/', '\\/', $reqUri);
$route = self::$route;

if (Application::$caseInSensitive === true) {
$reqUri = strtolower($reqUri);
$route = strtolower($route);
}

// now matching route with regex
if (preg_match("/$reqUri/", self::$route . '$'))
if (preg_match("/$reqUri/", $route . '$'))
{
// checks if the requested method is of the given route
if (
Expand Down Expand Up @@ -247,6 +252,7 @@ private function match_routing (): bool|array
{
$uri = [];
$str_route = '';
$request_uri = self::$request_uri;

if (is_array(self::$route))
{
Expand All @@ -255,14 +261,20 @@ private function match_routing (): bool|array
$each_route = preg_replace("/(^\/)|(\/$)/", '', self::$route[$i]);

empty($each_route)
? array_push($uri, strtolower('/'))
: array_push($uri, strtolower($each_route));
? array_push($uri, '/')
: array_push($uri, Application::$caseInSensitive === true ? strtolower($each_route) : $each_route);
}
}
else
{
$str_route = empty(self::$route) ? '/' : self::$route;
}


if (Application::$caseInSensitive === true) {
$request_uri = strtolower($request_uri);
$str_route = strtolower($str_route);
}

if (
in_array(self::$request_uri, $uri) ||
Expand Down Expand Up @@ -329,9 +341,15 @@ private function pattern (): array|bool
*/
private function validatePattern (string $pattern): array|bool
{
$request_uri = self::$request_uri;
$pattern = preg_replace("/(^\/)|(\/$)/", '', trim(substr($pattern, 8)));

if (fnmatch($pattern, self::$request_uri))
if (Application::$caseInSensitive === true) {
$request_uri = strtolower($request_uri);
$pattern = strtolower($pattern);
}

if (fnmatch($pattern, $request_uri))
{
if (
!in_array($_SERVER['REQUEST_METHOD'], self::$method) &&
Expand All @@ -349,4 +367,4 @@ private function validatePattern (string $pattern): array|bool
}
return false;
}
}
}
12 changes: 12 additions & 0 deletions Router/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class Route extends Controller implements RouteInterface

private ?array $mapRoute = null;

private bool $caseSensitive = false;

private ?Closure $handleInvalidParameterType = null;

private static array $routes;
Expand Down Expand Up @@ -224,6 +226,12 @@ public function handleInvalidParameterType (Closure $closure): self
$this->handleInvalidParameterType = $closure;
return $this;
}

public function caseSensitive (): self
{
$this->caseSensitive = true;
return $this;
}

/**
* Applies Authentication Guard to the current route.
Expand Down Expand Up @@ -404,6 +412,10 @@ public function __destruct ()
$route_index = end(self::$route);
$route_index = is_array($route_index) ? $route_index[0] : $route_index;

$GLOBALS['__registered_routes'][$route_index][
'caseSensitive'
] = $this->caseSensitive;

if (self::$map !== null)
{
$GLOBALS['__registered_routes'][$route_index]['map'] = self::$map;
Expand Down
4 changes: 3 additions & 1 deletion src/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Application extends Controller implements ApplicationInterface
/**
* The version of the PhpSlides application.
*/
public const PHPSLIDES_VERSION = '1.4.3';
public const PHPSLIDES_VERSION = '1.4.4';

/**
* @var string $REMOTE_ADDR The remote address of the client making the request.
Expand Down Expand Up @@ -89,6 +89,8 @@ class Application extends Controller implements ApplicationInterface
* The directory path for script resources (e.g., JavaScript files).
*/
public static string $scriptsDir;

public static bool $caseInSensitive = true;

public static ?Closure $handleInvalidParameterType;

Expand Down
15 changes: 5 additions & 10 deletions src/Foundation/Render.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ public static function WebRoute ()

foreach ($reg_route as $route)
{
self::$handleInvalidParameterType =
$route['handleInvalidParameterType'] ?? null;
$caseSensitive = $route['caseSensitive'];
$handleInvalidParameterType = $route['handleInvalidParameterType'] ?? null;

self::$redirect = $route['redirect'] ?? null;
self::$method = $route['method'] ?? null;
self::$guards = $route['guards'] ?? null;
Expand All @@ -45,14 +46,8 @@ public static function WebRoute ()
self::$use = $route['use'] ?? null;
self::$map = $route['map'] ?? null;

if (self::$handleInvalidParameterType)
{
self::__handleInvalidParameterType();
}
else
{
Application::$handleInvalidParameterType = null;
}
Application::$handleInvalidParameterType = $handleInvalidParameterType;
Application::$caseInSensitive = !$caseSensitive;

if (self::$map)
{
Expand Down
8 changes: 0 additions & 8 deletions src/Traits/Resources/RouteResources.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ trait RouteResources

protected static ?string $file = null;

protected static ?Closure $handleInvalidParameterType = null;

protected static array|bool $map_info = false;

/**
Expand Down Expand Up @@ -73,12 +71,6 @@ protected static function __map (): void
}
}

protected static function __handleInvalidParameterType (): void
{
Application::$handleInvalidParameterType =
self::$handleInvalidParameterType;
}

protected static function __any (?Request $request = null): void
{
$route = self::$any['route'] ?? self::$method['route'];
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/Routes/Exception/InvalidTypesException.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static function catchInvalidParameterTypes (array $typeRequested, string
$requested = implode(', ', $typeRequested);
$requested = preg_replace('/<[^<>]*>/', '', $requested);

return new self(htmlspecialchars("Invalid request parameter type. {{$requested}} requested, but got {{$typeGotten}}"));
return new self(htmlspecialchars("Invalid request parameter type: Expected {{$requested}}, but received {{$typeGotten}}."));
}
else
{
Expand Down
Loading

0 comments on commit 807596c

Please sign in to comment.