From c7f20711f755f622a8ea1fa4fdb0b5936f25651e Mon Sep 17 00:00:00 2001 From: Awilum Date: Sat, 4 Sep 2021 19:22:34 +0300 Subject: [PATCH] feat(helpers): add new helper function `getAbsoluteUrl` and ``getBaseUrl --- src/flextype/helpers/urls.php | 85 +++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/src/flextype/helpers/urls.php b/src/flextype/helpers/urls.php index 31a53195..60bdcef4 100644 --- a/src/flextype/helpers/urls.php +++ b/src/flextype/helpers/urls.php @@ -107,6 +107,91 @@ if (! function_exists('setBasePath')) { } } +if (! function_exists('getBaseUrl')) { + /** + * Get the application base url. + * + * @return string Application base url. + */ + function getBaseUrl(): string + { + $baseUrl = registry()->get('flextype.settings.base_url') ?? ''; + $basePath = registry()->get('flextype.settings.base_path') ?? ''; + + if ($baseUrl != '') { + return $baseUrl . $basePath; + } + + $getAuth = static function (): string { + $result = ''; + if ($user = $_SERVER['PHP_AUTH_USER'] ?? '') { + $result .= $user; + + if ($password = $_SERVER['PHP_AUTH_PW'] ?? '') { + $result .= ':' . $password; + } + + $result .= '@'; + } + + return $result; + }; + + $isHttps = static function (): bool { + if (array_key_exists('HTTPS', $_SERVER)) { + return !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off'; + } + + return false; + }; + + $url = ''; + + $isHttps = $isHttps(); + + $url .= $getAuth(); + + $serverData = arrays($_SERVER); + + $host = (string) $serverData->get('HTTP_HOST'); + $port = (int) $serverData->get('SERVER_PORT'); + $url .= str_replace(':' . $port, '', $host); + + if ($isHttps && $port !== 443) { + $url .= $port ? ":{$port}" : ''; + } elseif (!$isHttps && $port !== 80) { + $url .= $port ? ":{$port}" : ''; + } + + if ($url) { + if ($isHttps) { + $url = 'https://' . $url; + } else { + $url = 'http://' . $url; + } + } + + $url .= $basePath; + + return $url; + } +} + +if (! function_exists('getAbsoluteUrl')) { + /** + * Get the application absolute url. + * + * @return string Application absolute url. + */ + function getAbsoluteUrl(): string + { + $url = getBaseUrl(); + $url .= $_SERVER['REQUEST_URI']; + + return $url; + } +} + if (! function_exists('redirect')) { /** * Redirect.