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

Added two new utility functions #101

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fleetbase/core-api",
"version": "1.4.28",
"version": "1.4.29",
"description": "Core Framework and Resources for Fleetbase API",
"keywords": [
"fleetbase",
Expand Down
52 changes: 52 additions & 0 deletions src/Support/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,58 @@ public static function consoleUrl(string $path = '', ?array $queryParams = [], $
return $url;
}

/**
* Get the full path to the console directory.
*
* This function retrieves the base path for the console directory from the configuration
* and appends an optional subpath if provided. The resulting path is normalized to
* remove any trailing slashes.
*
* @param string|null $path optional subpath to append to the base console path
*
* @return string the full path to the console directory, optionally including the subpath
*/
public static function consolePath(?string $path = null): string
{
$basePath = rtrim(config('fleetbase.console.path', ''), DIRECTORY_SEPARATOR);
$path = trim($path ?? '', DIRECTORY_SEPARATOR);

return $path ? $basePath . DIRECTORY_SEPARATOR . $path : $basePath;
}

/**
* Extract the domain from a URL, optionally including the port.
*
* This function parses the given URL to extract the domain name. If the URL does not
* include a scheme (e.g., http or https), it prepends 'http://' to the URL before parsing.
* Optionally, the port can be included in the returned domain string.
*
* @param string $url the URL to extract the domain from
* @param bool $withPort whether to include the port in the returned domain string
*
* @return string the extracted domain, optionally including the port
*/
public static function getDomainFromUrl(string $url, bool $withPort = false): string
{
// Parse the URL and get the host
$parsedUrl = parse_url($url);

if (!isset($parsedUrl['host'])) {
// Handle cases where only the hostname is provided without a scheme (e.g., domain:8000)
$url = 'http://' . ltrim($url, '/');
$parsedUrl = parse_url($url);
}

$domain = $parsedUrl['host'];

// Include the port if it's set
if ($withPort === true && isset($parsedUrl['port'])) {
$domain .= ':' . $parsedUrl['port'];
}

return $domain;
}

/**
* Return asset URL from s3.
*
Expand Down
Loading