From f8eb68a2af49e266761973f35dde9ed064cdf891 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 20 Jul 2023 18:43:09 -0700 Subject: [PATCH] Civi::url() - Allow environments to change default scheme and format --- CRM/Core/Invoke.php | 3 +++ Civi/Core/Url.php | 52 +++++++++++++++++++++++++++++++-------------- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/CRM/Core/Invoke.php b/CRM/Core/Invoke.php index 51b2d0cb15..f326d3835e 100644 --- a/CRM/Core/Invoke.php +++ b/CRM/Core/Invoke.php @@ -207,6 +207,9 @@ class CRM_Core_Invoke { self::registerPharHandler(); $config = CRM_Core_Config::singleton(); + + // WISHLIST: if $item is a web-service route, swap prepend to $civicrm_url_defaults + if ($config->userFramework == 'Joomla' && $item) { $config->userFrameworkURLVar = 'task'; diff --git a/Civi/Core/Url.php b/Civi/Core/Url.php index 009c94eac8..5bcb72dc5f 100644 --- a/Civi/Core/Url.php +++ b/Civi/Core/Url.php @@ -9,7 +9,7 @@ namespace Civi\Core; * * As output, it provides a *concrete URL* that can be used by a web-browser to make requests. */ -class Url { +final class Url { /** * @var string @@ -286,24 +286,16 @@ class Url { */ public function __toString(): string { $userSystem = \CRM_Core_Config::singleton()->userSystem; + $preferFormat = $this->getPreferFormat() ?: static::detectFormat(); $scheme = $this->getScheme(); - $preferFormat = $this->getPreferFormat(); - // Translate subjective values to real values. - switch ($scheme) { - case 'current': - $preferFormat = $preferFormat ?: 'relative'; - $scheme = $userSystem->isFrontEndPage() ? 'frontend' : 'backend'; - // The current call could actually be a 'service' request, but we treat those as equivalent to 'frontend', so maybe it doesn't matter. - break; - - case 'default': - // $preferFormat = $preferFormat ?: 'absolute'; - // TODO pick $scheme = 'frontend' or 'backend' or 'service'; - throw new \RuntimeException("FIXME: Implement lookup for default "); + if ($scheme === NULL || $scheme === 'current') { + $scheme = static::detectScheme(); + } - default: - $preferFormat = $preferFormat ?: 'absolute'; + if ($scheme === 'default') { + // TODO Use metadata to pick $scheme = 'frontend' or 'backend' or 'service'; + throw new \RuntimeException("FIXME: Implement lookup for default "); } switch ($scheme) { @@ -350,4 +342,32 @@ class Url { return $this->htmlEscape ? htmlentities($result) : $result; } + private static function detectFormat(): string { + // Some environments may override default - e.g. cv-cli prefers absolute URLs + // WISHLIST: If handling `Job.*`, then 'absolute' + // WISHLIST: If active route is a web-service/web-hook/IPN, then 'absolute' + foreach ($GLOBALS['civicrm_url_defaults'] ?? [] as $default) { + if (isset($default['format'])) { + return $default['format']; + } + } + + // Web UI: Most CiviCRM routes (`CRM_Core_Invoke::invoke()`) and CMS blocks + return 'relative'; + } + + private static function detectScheme(): string { + // Some environments may override default - e.g. cv-cli prefers 'default://'. + // WISHLIST: If handling `Job.*`, then `default://' + // WISHLIST: If active route is a web-service/web-hook/IPN, then 'default://' + foreach ($GLOBALS['civicrm_url_defaults'] ?? [] as $default) { + if (isset($default['scheme'])) { + return $default['scheme']; + } + } + + // Web UI: Most CiviCRM routes (`CRM_Core_Invoke::invoke()`) and CMS blocks + return \CRM_Core_Config::singleton()->userSystem->isFrontEndPage() ? 'frontend' : 'backend'; + } + } -- 2.25.1