From 1435aa3fad8c5ce17bc426ae1551aad7f0865aa9 Mon Sep 17 00:00:00 2001 From: "Ronald A. Richardson" Date: Fri, 19 Jul 2024 10:25:27 +0800 Subject: [PATCH] Added two new utility functions --- composer.json | 2 +- src/Support/Utils.php | 52 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 8cf0c7b..c054f2c 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/Support/Utils.php b/src/Support/Utils.php index 5b47d0f..4247838 100644 --- a/src/Support/Utils.php +++ b/src/Support/Utils.php @@ -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. *