| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* JCH Optimize - Performs several front-end optimizations for fast downloads |
| 5 |
* |
| 6 |
* @package jchoptimize/joomla-platform |
| 7 |
* @author Samuel Marshall <samuel@jch-optimize.net> |
| 8 |
* @copyright Copyright (c) 2024 Samuel Marshall / JCH Optimize |
| 9 |
* @license GNU/GPLv3, or later. See LICENSE file |
| 10 |
* |
| 11 |
* If LICENSE file missing, see <http://www.gnu.org/licenses/>. |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace JchOptimize\Core; |
| 15 |
|
| 16 |
use _JchOptimizeVendor\V91\Psr\Http\Message\UriInterface; |
| 17 |
use JchOptimize\Core\Css\Components\CssSelectorList; |
| 18 |
|
| 19 |
use function array_merge; |
| 20 |
use function md5; |
| 21 |
use function time; |
| 22 |
|
| 23 |
class CacheObject |
| 24 |
{ |
| 25 |
private ?CacheObject $importedContents = null; |
| 26 |
|
| 27 |
private int $filemtime; |
| 28 |
|
| 29 |
private string $etag; |
| 30 |
|
| 31 |
private string $contents = ''; |
| 32 |
|
| 33 |
private string $imports = ''; |
| 34 |
|
| 35 |
private string $criticalCss = ''; |
| 36 |
|
| 37 |
private string $dynamicCriticalCss = ''; |
| 38 |
|
| 39 |
private string $potentialCriticalCssAtRules = ''; |
| 40 |
|
| 41 |
private string $belowFoldFontsKeyFrame = ''; |
| 42 |
|
| 43 |
private array $images = []; |
| 44 |
|
| 45 |
private array $fontFace = []; |
| 46 |
|
| 47 |
private array $gFonts = []; |
| 48 |
|
| 49 |
private array $prefetches = []; |
| 50 |
|
| 51 |
private array $bgSelectors = []; |
| 52 |
|
| 53 |
private array $lcpImages = []; |
| 54 |
|
| 55 |
private string $criticalCssId = ''; |
| 56 |
|
| 57 |
private array $http2Preloads = []; |
| 58 |
|
| 59 |
public function getEtag(): string |
| 60 |
{ |
| 61 |
return $this->etag; |
| 62 |
} |
| 63 |
|
| 64 |
public function setFilemtime(?int $filemtime = null): CacheObject |
| 65 |
{ |
| 66 |
$this->filemtime = $filemtime ?? time(); |
| 67 |
|
| 68 |
return $this; |
| 69 |
} |
| 70 |
|
| 71 |
public function getFilemtime(): int |
| 72 |
{ |
| 73 |
return $this->filemtime; |
| 74 |
} |
| 75 |
|
| 76 |
public function setImports(string $imports): static |
| 77 |
{ |
| 78 |
$this->imports = $imports; |
| 79 |
|
| 80 |
return $this; |
| 81 |
} |
| 82 |
|
| 83 |
public function addImports(string $imports): static |
| 84 |
{ |
| 85 |
$this->imports .= $imports; |
| 86 |
|
| 87 |
return $this; |
| 88 |
} |
| 89 |
|
| 90 |
public function setImages(array $images): static |
| 91 |
{ |
| 92 |
$this->images = $images; |
| 93 |
|
| 94 |
return $this; |
| 95 |
} |
| 96 |
|
| 97 |
public function addImages(string|UriInterface $images): static |
| 98 |
{ |
| 99 |
$this->images[] = $images; |
| 100 |
|
| 101 |
return $this; |
| 102 |
} |
| 103 |
|
| 104 |
public function setFontFace(array $fontFace): static |
| 105 |
{ |
| 106 |
$this->fontFace = $fontFace; |
| 107 |
|
| 108 |
return $this; |
| 109 |
} |
| 110 |
|
| 111 |
public function addFontFace(array $fontFace): static |
| 112 |
{ |
| 113 |
$this->fontFace[] = $fontFace; |
| 114 |
|
| 115 |
return $this; |
| 116 |
} |
| 117 |
|
| 118 |
public function setGFonts(array $gFonts): static |
| 119 |
{ |
| 120 |
$this->gFonts = $gFonts; |
| 121 |
|
| 122 |
return $this; |
| 123 |
} |
| 124 |
|
| 125 |
public function addGFonts(array $gFont): static |
| 126 |
{ |
| 127 |
$this->gFonts[] = $gFont; |
| 128 |
|
| 129 |
return $this; |
| 130 |
} |
| 131 |
|
| 132 |
public function setPrefetches(array $prefetches): static |
| 133 |
{ |
| 134 |
$this->prefetches = $prefetches; |
| 135 |
|
| 136 |
return $this; |
| 137 |
} |
| 138 |
|
| 139 |
public function addPrefetches(UriInterface $prefetch): static |
| 140 |
{ |
| 141 |
$this->prefetches[] = $prefetch; |
| 142 |
|
| 143 |
return $this; |
| 144 |
} |
| 145 |
|
| 146 |
public function setBgSelectors(array $bgSelectors): static |
| 147 |
{ |
| 148 |
$this->bgSelectors = $bgSelectors; |
| 149 |
|
| 150 |
return $this; |
| 151 |
} |
| 152 |
|
| 153 |
public function addBgSelectors(CssSelectorList $bgSelectors): static |
| 154 |
{ |
| 155 |
$this->bgSelectors[] = $bgSelectors; |
| 156 |
|
| 157 |
return $this; |
| 158 |
} |
| 159 |
|
| 160 |
public function setLcpImages(array $lcpImages): static |
| 161 |
{ |
| 162 |
$this->lcpImages = $lcpImages; |
| 163 |
|
| 164 |
return $this; |
| 165 |
} |
| 166 |
|
| 167 |
public function addLcpImages(array $lcpImage): static |
| 168 |
{ |
| 169 |
$this->lcpImages[] = $lcpImage; |
| 170 |
|
| 171 |
return $this; |
| 172 |
} |
| 173 |
|
| 174 |
public function getGFonts(): array |
| 175 |
{ |
| 176 |
return $this->gFonts; |
| 177 |
} |
| 178 |
|
| 179 |
public function getImages(): array |
| 180 |
{ |
| 181 |
return $this->images; |
| 182 |
} |
| 183 |
|
| 184 |
public function getFontFace(): array |
| 185 |
{ |
| 186 |
return $this->fontFace; |
| 187 |
} |
| 188 |
|
| 189 |
public function getPrefetches(): array |
| 190 |
{ |
| 191 |
return $this->prefetches; |
| 192 |
} |
| 193 |
|
| 194 |
public function getBgSelectors(): array |
| 195 |
{ |
| 196 |
return $this->bgSelectors; |
| 197 |
} |
| 198 |
|
| 199 |
public function getLcpImages(): array |
| 200 |
{ |
| 201 |
return $this->lcpImages; |
| 202 |
} |
| 203 |
|
| 204 |
public function merge(CacheObject $object): static |
| 205 |
{ |
| 206 |
$this->internalMergeImportedContents($object); |
| 207 |
$this->contents .= $object->getContents(); |
| 208 |
$this->imports .= $object->getImports(); |
| 209 |
$this->criticalCss .= $object->getCriticalCss(); |
| 210 |
$this->dynamicCriticalCss .= $object->getDynamicCriticalCss(); |
| 211 |
$this->potentialCriticalCssAtRules .= $object->getPotentialCriticalCssAtRules(); |
| 212 |
$this->belowFoldFontsKeyFrame .= $object->getBelowFoldFontsKeyFrame(); |
| 213 |
$this->images = array_merge($this->images, $object->getImages()); |
| 214 |
$this->fontFace = array_merge($this->fontFace, $object->getFontFace()); |
| 215 |
$this->gFonts = array_merge($this->gFonts, $object->getGFonts()); |
| 216 |
$this->prefetches = array_merge($this->prefetches, $object->getPrefetches()); |
| 217 |
$this->bgSelectors = array_merge($this->bgSelectors, $object->getBgSelectors()); |
| 218 |
$this->lcpImages = array_merge($this->lcpImages, $object->getLcpImages()); |
| 219 |
$this->http2Preloads = array_merge($this->http2Preloads, $object->getHttp2Preloads()); |
| 220 |
|
| 221 |
return $this; |
| 222 |
} |
| 223 |
|
| 224 |
public function getContents(): string |
| 225 |
{ |
| 226 |
return $this->contents; |
| 227 |
} |
| 228 |
|
| 229 |
public function setContents(string $contents): static |
| 230 |
{ |
| 231 |
$this->contents = $contents; |
| 232 |
|
| 233 |
return $this; |
| 234 |
} |
| 235 |
|
| 236 |
public function appendContents(string $contents): static |
| 237 |
{ |
| 238 |
$this->contents .= $contents; |
| 239 |
|
| 240 |
return $this; |
| 241 |
} |
| 242 |
|
| 243 |
public function prependContents(string $contents): static |
| 244 |
{ |
| 245 |
$this->contents = $contents . $this->contents; |
| 246 |
|
| 247 |
return $this; |
| 248 |
} |
| 249 |
|
| 250 |
public function getImports(): string |
| 251 |
{ |
| 252 |
return $this->imports; |
| 253 |
} |
| 254 |
|
| 255 |
public function calculateEtag(): CacheObject |
| 256 |
{ |
| 257 |
$this->etag = md5($this->contents); |
| 258 |
|
| 259 |
return $this; |
| 260 |
} |
| 261 |
|
| 262 |
public function setCriticalCss(string $criticalCss): CacheObject |
| 263 |
{ |
| 264 |
$this->criticalCss = $criticalCss; |
| 265 |
|
| 266 |
return $this; |
| 267 |
} |
| 268 |
|
| 269 |
public function setDynamicCriticalCss(string $dynamicCriticalCss): CacheObject |
| 270 |
{ |
| 271 |
$this->dynamicCriticalCss = $dynamicCriticalCss; |
| 272 |
|
| 273 |
return $this; |
| 274 |
} |
| 275 |
|
| 276 |
public function appendCriticalCss(string $criticalCss): CacheObject |
| 277 |
{ |
| 278 |
$this->criticalCss .= $criticalCss; |
| 279 |
|
| 280 |
return $this; |
| 281 |
} |
| 282 |
|
| 283 |
public function appendDynamicCriticalCss(string $dynamicCriticalCss): CacheObject |
| 284 |
{ |
| 285 |
$this->dynamicCriticalCss .= $dynamicCriticalCss; |
| 286 |
|
| 287 |
return $this; |
| 288 |
} |
| 289 |
|
| 290 |
public function setPotentialCriticalCssAtRules(string $potentialCriticalCssAtRules): CacheObject |
| 291 |
{ |
| 292 |
$this->potentialCriticalCssAtRules = $potentialCriticalCssAtRules; |
| 293 |
|
| 294 |
return $this; |
| 295 |
} |
| 296 |
|
| 297 |
public function getPotentialCriticalCssAtRules(): string |
| 298 |
{ |
| 299 |
return $this->potentialCriticalCssAtRules; |
| 300 |
} |
| 301 |
|
| 302 |
public function getCriticalCss(): string |
| 303 |
{ |
| 304 |
return $this->criticalCss; |
| 305 |
} |
| 306 |
|
| 307 |
public function getDynamicCriticalCss(): string |
| 308 |
{ |
| 309 |
return $this->dynamicCriticalCss; |
| 310 |
} |
| 311 |
|
| 312 |
public function getBelowFoldFontsKeyFrame(): string |
| 313 |
{ |
| 314 |
return $this->belowFoldFontsKeyFrame; |
| 315 |
} |
| 316 |
|
| 317 |
public function setBelowFoldFontsKeyFrame(string $belowFoldFontsKeyFrame): static |
| 318 |
{ |
| 319 |
$this->belowFoldFontsKeyFrame = $belowFoldFontsKeyFrame; |
| 320 |
|
| 321 |
return $this; |
| 322 |
} |
| 323 |
|
| 324 |
public function prepareForCaching(): void |
| 325 |
{ |
| 326 |
$this->calculateEtag(); |
| 327 |
$this->setFilemtime(); |
| 328 |
} |
| 329 |
|
| 330 |
public function setCriticalCssId(string $id): static |
| 331 |
{ |
| 332 |
$this->criticalCssId = $id; |
| 333 |
|
| 334 |
return $this; |
| 335 |
} |
| 336 |
|
| 337 |
public function getCriticalCssId(): string |
| 338 |
{ |
| 339 |
return $this->criticalCssId; |
| 340 |
} |
| 341 |
|
| 342 |
public function getHttp2Preloads(): array |
| 343 |
{ |
| 344 |
return $this->http2Preloads; |
| 345 |
} |
| 346 |
|
| 347 |
public function setHttp2Preloads(array $http2Preloads): static |
| 348 |
{ |
| 349 |
$this->http2Preloads = $http2Preloads; |
| 350 |
|
| 351 |
return $this; |
| 352 |
} |
| 353 |
|
| 354 |
public function addHttp2Preloads(array $http2Preload): static |
| 355 |
{ |
| 356 |
$this->http2Preloads[] = $http2Preload; |
| 357 |
|
| 358 |
return $this; |
| 359 |
} |
| 360 |
|
| 361 |
public function setImportedContents(CacheObject $importedContents): CacheObject |
| 362 |
{ |
| 363 |
$this->importedContents = $importedContents; |
| 364 |
|
| 365 |
return $this; |
| 366 |
} |
| 367 |
|
| 368 |
public function getMergedImportedContents(): CacheObject |
| 369 |
{ |
| 370 |
if ($this->importedContents instanceof CacheObject) { |
| 371 |
$mergedImportObj = $this->importedContents->getMergedImportedContents(); |
| 372 |
$mergedImportObj->merge($this); |
| 373 |
$mergedImportObj->importedContents = null; |
| 374 |
|
| 375 |
return $mergedImportObj; |
| 376 |
} |
| 377 |
|
| 378 |
return $this; |
| 379 |
} |
| 380 |
|
| 381 |
private function internalMergeImportedContents(CacheObject $object): void |
| 382 |
{ |
| 383 |
if ($object->importedContents instanceof self) { |
| 384 |
if ($this->importedContents instanceof self) { |
| 385 |
$this->importedContents->merge($object->importedContents); |
| 386 |
} elseif ($this->importedContents === null) { |
| 387 |
$this->importedContents = $object->importedContents; |
| 388 |
} |
| 389 |
} |
| 390 |
} |
| 391 |
} |
| 392 |
|