From: Tim Otten Date: Fri, 21 Jul 2023 23:48:56 +0000 (-0700) Subject: Civi::url() - Add option for resCacheCode X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=6437be7022f83558dc1e246032522e53e81898fa;p=civicrm-core.git Civi::url() - Add option for resCacheCode --- diff --git a/Civi.php b/Civi.php index 1233dfbb53..e9d0dcab55 100644 --- a/Civi.php +++ b/Civi.php @@ -267,6 +267,7 @@ class Civi { * - 'h': html (aka `setHtmlEscape(TRUE)`) * - 't': text (aka `setHtmlEscape(FALSE)`) * - 's': ssl (aka `setSsl(TRUE)`) + * - 'c': cache code for resources (aka Civi::resources()->addCacheCode()) * FIXME: Should we have a flag for appending 'resCacheCode'? * @return \Civi\Core\Url * URL object which may be modified or rendered as text. diff --git a/Civi/Core/Url.php b/Civi/Core/Url.php index 5a4a081c3d..bedea2aaa2 100644 --- a/Civi/Core/Url.php +++ b/Civi/Core/Url.php @@ -39,6 +39,14 @@ final class Url { */ private $fragment; + /** + * Whether to auto-append the cache-busting resource code. + * + * @var bool|null + * NULL definition TBD (either "off" or "automatic"?) + */ + private $cacheCode; + /** * Preferred format. * @@ -203,6 +211,26 @@ final class Url { return $this; } + /** + * @return bool|null + */ + public function getCacheCode(): ?bool { + return $this->cacheCode; + } + + /** + * Specify whether to append a cache-busting code. + * + * @param bool|null $cacheCode + * TRUE: Do append + * FALSE: Do not append + * @return $this; + */ + public function setCacheCode(?bool $cacheCode) { + $this->cacheCode = $cacheCode; + return $this; + } + /** * @return string|null * 'relative' or 'absolute' @@ -339,6 +367,11 @@ final class Url { case 's'; $this->ssl = TRUE; break; + + // (c)ache code for resources + case 'c': + $this->cacheCode = TRUE; + break; } } return $this; @@ -395,6 +428,10 @@ final class Url { throw new \RuntimeException("Unknown URL scheme: {$this->getScheme()}"); } + if ($this->cacheCode) { + $result = \Civi::resources()->addCacheCode($result); + } + // TODO decide if the current default is good enough for future $ssl = $this->getSsl() ?: \CRM_Utils_System::isSSL(); if ($ssl && str_starts_with($result, 'http:')) {