Skip to content

Commit

Permalink
Improved code for getting filtered route list
Browse files Browse the repository at this point in the history
  • Loading branch information
realodix committed Jun 19, 2024
1 parent f30fda9 commit 46b1b46
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 22 deletions.
26 changes: 6 additions & 20 deletions app/Helpers/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,30 +67,16 @@ public static function urlFormat(string $value, ?int $limit = null, bool $scheme
}

/**
* List of potentially colliding routes with shortened link keywords
* List of potentially colliding routes with shortened link keywords.
*
* @return array<string>
*/
public static function routeList(): array
public static function routeCollisionList(): array
{
$route = array_map(
fn (\Illuminate\Routing\Route $route) => $route->uri,
\Illuminate\Support\Facades\Route::getRoutes()->get()
);

return collect($route)
// ex. foobar/{route_param?} => foobar
->map(fn ($value) => preg_replace('/(\/{)([a-zA-Z]+)(\?})$/', '', $value))
// Remove foo/bar
->map(fn ($value) => preg_replace('/^([a-zA-Z-_]+)\/([a-zA-Z-\/{}\.]+)/', '', $value))
// Remove '{route_param}' or '+{route_param}'
->map(fn ($value) => preg_replace('/^(\+?)({)([a-zA-Z]+)(})/', '', $value))
// Remove '/'
->map(fn ($value) => preg_replace('/\//', '', $value))
// Remove empty value
->reject(fn ($value) => empty($value))
->unique()
->sort()
return collect(\Illuminate\Support\Facades\Route::getRoutes()->get())
->map(fn (\Illuminate\Routing\Route $route) => $route->uri)
->reject(fn ($value) => ! preg_match('/^[a-zA-Z\-]+$/', $value))
->unique()->sort()
->toArray();
}

Expand Down
2 changes: 1 addition & 1 deletion app/Services/KeyGeneratorService.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function verify(string $value): bool
{
$alreadyInUse = Url::whereKeyword($value)->exists();
$isReservedKeyword = in_array($value, config('urlhub.reserved_keyword'));
$isRoute = in_array($value, \App\Helpers\Helper::routeList());
$isRoute = in_array($value, \App\Helpers\Helper::routeCollisionList());
$isPublicPath = in_array($value, \App\Helpers\Helper::publicPathCollisionList());

if ($alreadyInUse || $isReservedKeyword || $isRoute || $isPublicPath) {
Expand Down
2 changes: 1 addition & 1 deletion resources/views/backend/about.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
<br> <br>

<p><b>Registered routes</b></p>
<code>{{ implode(", ", \App\Helpers\Helper::routeList()) }}</code>
<code>{{ implode(", ", \App\Helpers\Helper::routeCollisionList()) }}</code>

<br> <br>

Expand Down

0 comments on commit 46b1b46

Please sign in to comment.