@@ -54,12 +54,12 @@ discard block |
||
54 | 54 | */ |
55 | 55 | public static function fetch(string $pathOrUrl, callable $callback = null, string $cacheKeySuffix = '') |
56 | 56 | { |
57 | - $pathOrUrl = (string)Craft::parseEnv($pathOrUrl); |
|
57 | + $pathOrUrl = (string) Craft::parseEnv($pathOrUrl); |
|
58 | 58 | // Create the dependency tags |
59 | 59 | $dependency = new TagDependency([ |
60 | 60 | 'tags' => [ |
61 | - self::CACHE_TAG . $cacheKeySuffix, |
|
62 | - self::CACHE_TAG . $cacheKeySuffix . $pathOrUrl, |
|
61 | + self::CACHE_TAG.$cacheKeySuffix, |
|
62 | + self::CACHE_TAG.$cacheKeySuffix.$pathOrUrl, |
|
63 | 63 | ], |
64 | 64 | ]); |
65 | 65 | // If this is a file path such as for the `manifest.json`, add a FileDependency so it's cache bust if the file changes |
@@ -80,8 +80,8 @@ discard block |
||
80 | 80 | // Get the result from the cache, or parse the file |
81 | 81 | $cache = Craft::$app->getCache(); |
82 | 82 | return $cache->getOrSet( |
83 | - self::CACHE_KEY . $cacheKeySuffix . $pathOrUrl, |
|
84 | - function () use ($pathOrUrl, $callback) { |
|
83 | + self::CACHE_KEY.$cacheKeySuffix.$pathOrUrl, |
|
84 | + function() use ($pathOrUrl, $callback) { |
|
85 | 85 | $contents = null; |
86 | 86 | $result = null; |
87 | 87 | if (UrlHelper::isAbsoluteUrl($pathOrUrl)) { |
@@ -133,8 +133,8 @@ discard block |
||
133 | 133 | */ |
134 | 134 | public static function createUrl(string $url, string $path): string |
135 | 135 | { |
136 | - $url = (string)Craft::parseEnv($url); |
|
137 | - return rtrim($url, '/') . '/' . trim($path, '/'); |
|
136 | + $url = (string) Craft::parseEnv($url); |
|
137 | + return rtrim($url, '/').'/'.trim($path, '/'); |
|
138 | 138 | } |
139 | 139 | |
140 | 140 | /** |
@@ -152,15 +152,15 @@ discard block |
||
152 | 152 | 'options' => $tagOptions |
153 | 153 | ]; |
154 | 154 | // Include any imports |
155 | - $importFiles = []; |
|
156 | - self::extractImportFiles(self::$manifest, $manifestKey, $importFiles); |
|
157 | - foreach ($importFiles as $importFile) { |
|
158 | - $tags[] = [ |
|
159 | - 'crossorigin' => $tagOptions['crossorigin'] ?? true, |
|
160 | - 'type' => 'import', |
|
161 | - 'url' => $importFile, |
|
162 | - ]; |
|
163 | - } |
|
155 | + $importFiles = []; |
|
156 | + self::extractImportFiles(self::$manifest, $manifestKey, $importFiles); |
|
157 | + foreach ($importFiles as $importFile) { |
|
158 | + $tags[] = [ |
|
159 | + 'crossorigin' => $tagOptions['crossorigin'] ?? true, |
|
160 | + 'type' => 'import', |
|
161 | + 'url' => $importFile, |
|
162 | + ]; |
|
163 | + } |
|
164 | 164 | // Include any CSS tags |
165 | 165 | $cssFiles = []; |
166 | 166 | self::extractCssFiles(self::$manifest, $manifestKey, $cssFiles); |
@@ -178,32 +178,32 @@ discard block |
||
178 | 178 | return $tags; |
179 | 179 | } |
180 | 180 | |
181 | - /** |
|
182 | - * Extract any import files from entries recursively |
|
183 | - * |
|
184 | - * @param array $manifest |
|
185 | - * @param string $manifestKey |
|
186 | - * @param array $importFiles |
|
187 | - * |
|
188 | - * @return array |
|
189 | - */ |
|
190 | - protected static function extractImportFiles(array $manifest, string $manifestKey, array &$importFiles): array |
|
191 | - { |
|
192 | - $entry = $manifest[$manifestKey] ?? null; |
|
193 | - if (!$entry) { |
|
194 | - return []; |
|
195 | - } |
|
196 | - |
|
197 | - $imports = $entry['imports'] ?? []; |
|
198 | - foreach($imports as $import) { |
|
199 | - $importFiles[] = $manifest[$import]['file']; |
|
200 | - self::extractImportFiles($manifest, $import, $importFiles); |
|
201 | - } |
|
202 | - |
|
203 | - return $importFiles; |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
181 | + /** |
|
182 | + * Extract any import files from entries recursively |
|
183 | + * |
|
184 | + * @param array $manifest |
|
185 | + * @param string $manifestKey |
|
186 | + * @param array $importFiles |
|
187 | + * |
|
188 | + * @return array |
|
189 | + */ |
|
190 | + protected static function extractImportFiles(array $manifest, string $manifestKey, array &$importFiles): array |
|
191 | + { |
|
192 | + $entry = $manifest[$manifestKey] ?? null; |
|
193 | + if (!$entry) { |
|
194 | + return []; |
|
195 | + } |
|
196 | + |
|
197 | + $imports = $entry['imports'] ?? []; |
|
198 | + foreach($imports as $import) { |
|
199 | + $importFiles[] = $manifest[$import]['file']; |
|
200 | + self::extractImportFiles($manifest, $import, $importFiles); |
|
201 | + } |
|
202 | + |
|
203 | + return $importFiles; |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | 207 | * Extract any CSS files from entries recursively |
208 | 208 | * |
209 | 209 | * @param array $manifest |
@@ -45,14 +45,14 @@ discard block |
||
45 | 45 | public static function fetchManifest(string $manifestPath) |
46 | 46 | { |
47 | 47 | // Grab the manifest |
48 | - $pathOrUrl = (string)Craft::parseEnv($manifestPath); |
|
48 | + $pathOrUrl = (string) Craft::parseEnv($manifestPath); |
|
49 | 49 | $manifest = FileHelper::fetch($pathOrUrl, [JsonHelper::class, 'decodeIfJson']); |
50 | 50 | // If no manifest file is found, log it |
51 | 51 | if ($manifest === null) { |
52 | - Craft::error('Manifest not found at ' . $manifestPath, __METHOD__); |
|
52 | + Craft::error('Manifest not found at '.$manifestPath, __METHOD__); |
|
53 | 53 | } |
54 | 54 | // Ensure we're dealing with an array |
55 | - self::$manifest = (array)$manifest; |
|
55 | + self::$manifest = (array) $manifest; |
|
56 | 56 | } |
57 | 57 | |
58 | 58 | /** |
@@ -195,7 +195,7 @@ discard block |
||
195 | 195 | } |
196 | 196 | |
197 | 197 | $imports = $entry['imports'] ?? []; |
198 | - foreach($imports as $import) { |
|
198 | + foreach ($imports as $import) { |
|
199 | 199 | $importFiles[] = $manifest[$import]['file']; |
200 | 200 | self::extractImportFiles($manifest, $import, $importFiles); |
201 | 201 | } |
@@ -233,30 +233,30 @@ discard block |
||
233 | 233 | } |
234 | 234 | |
235 | 235 | // modulepreload any imports from modern tags |
236 | - $modulepreloadPolyfillRequired = false; |
|
237 | - foreach($tags as $tag) { |
|
238 | - if (!empty($tag)) { |
|
239 | - $url = FileHelper::createUrl($this->serverPublic, $tag['url']); |
|
240 | - switch ($tag['type']) { |
|
241 | - case 'import': |
|
242 | - $lines[] = HtmlHelper::tag('link', '', [ |
|
243 | - 'crossorigin' => $tag['crossorigin'], |
|
244 | - 'href' => $url, |
|
245 | - 'rel' => 'modulepreload', |
|
246 | - ]); |
|
247 | - $modulepreloadPolyfillRequired = true; |
|
248 | - break; |
|
249 | - default: |
|
250 | - break; |
|
251 | - } |
|
252 | - } |
|
253 | - } |
|
254 | - if($modulepreloadPolyfillRequired && ! $this->modulepreloadPolyfillIncluded) { |
|
255 | - $this->modulepreloadPolyfillIncluded = true; |
|
256 | - $lines[] = HtmlHelper::script( |
|
257 | - FileHelper::fetchScript('modulepreload-polyfill.min.js', $this->cacheKeySuffix), |
|
258 | - ['type' => 'module'] |
|
259 | - ); |
|
236 | + $modulepreloadPolyfillRequired = false; |
|
237 | + foreach($tags as $tag) { |
|
238 | + if (!empty($tag)) { |
|
239 | + $url = FileHelper::createUrl($this->serverPublic, $tag['url']); |
|
240 | + switch ($tag['type']) { |
|
241 | + case 'import': |
|
242 | + $lines[] = HtmlHelper::tag('link', '', [ |
|
243 | + 'crossorigin' => $tag['crossorigin'], |
|
244 | + 'href' => $url, |
|
245 | + 'rel' => 'modulepreload', |
|
246 | + ]); |
|
247 | + $modulepreloadPolyfillRequired = true; |
|
248 | + break; |
|
249 | + default: |
|
250 | + break; |
|
251 | + } |
|
252 | + } |
|
253 | + } |
|
254 | + if($modulepreloadPolyfillRequired && ! $this->modulepreloadPolyfillIncluded) { |
|
255 | + $this->modulepreloadPolyfillIncluded = true; |
|
256 | + $lines[] = HtmlHelper::script( |
|
257 | + FileHelper::fetchScript('modulepreload-polyfill.min.js', $this->cacheKeySuffix), |
|
258 | + ['type' => 'module'] |
|
259 | + ); |
|
260 | 260 | } |
261 | 261 | |
262 | 262 | return implode("\r\n", $lines); |
@@ -383,37 +383,37 @@ discard block |
||
383 | 383 | } |
384 | 384 | } |
385 | 385 | |
386 | - // modulepreload any imports from modern tags |
|
387 | - $modulepreloadPolyfillRequired = false; |
|
388 | - foreach($tags as $tag) { |
|
389 | - if (!empty($tag)) { |
|
390 | - $url = FileHelper::createUrl($this->serverPublic, $tag['url']); |
|
391 | - switch ($tag['type']) { |
|
392 | - case 'import': |
|
393 | - $view->registerLinkTag( |
|
394 | - [ |
|
395 | - 'crossorigin' => $tag['crossorigin'], |
|
396 | - 'href' => $url, |
|
397 | - 'rel' => 'modulepreload', |
|
398 | - ], |
|
399 | - md5($url) |
|
400 | - ); |
|
401 | - $modulepreloadPolyfillRequired = true; |
|
402 | - break; |
|
403 | - default: |
|
404 | - break; |
|
405 | - } |
|
406 | - } |
|
407 | - } |
|
408 | - if($modulepreloadPolyfillRequired && ! $this->modulepreloadPolyfillIncluded) { |
|
409 | - $this->modulepreloadPolyfillIncluded = true; |
|
410 | - $view->registerScript( |
|
411 | - FileHelper::fetchScript('modulepreload-polyfill.min.js', $this->cacheKeySuffix), |
|
412 | - $view::POS_HEAD, |
|
413 | - ['type' => 'module'], |
|
414 | - 'MODULEPRELOAD_POLYFILL' |
|
415 | - ); |
|
416 | - } |
|
386 | + // modulepreload any imports from modern tags |
|
387 | + $modulepreloadPolyfillRequired = false; |
|
388 | + foreach($tags as $tag) { |
|
389 | + if (!empty($tag)) { |
|
390 | + $url = FileHelper::createUrl($this->serverPublic, $tag['url']); |
|
391 | + switch ($tag['type']) { |
|
392 | + case 'import': |
|
393 | + $view->registerLinkTag( |
|
394 | + [ |
|
395 | + 'crossorigin' => $tag['crossorigin'], |
|
396 | + 'href' => $url, |
|
397 | + 'rel' => 'modulepreload', |
|
398 | + ], |
|
399 | + md5($url) |
|
400 | + ); |
|
401 | + $modulepreloadPolyfillRequired = true; |
|
402 | + break; |
|
403 | + default: |
|
404 | + break; |
|
405 | + } |
|
406 | + } |
|
407 | + } |
|
408 | + if($modulepreloadPolyfillRequired && ! $this->modulepreloadPolyfillIncluded) { |
|
409 | + $this->modulepreloadPolyfillIncluded = true; |
|
410 | + $view->registerScript( |
|
411 | + FileHelper::fetchScript('modulepreload-polyfill.min.js', $this->cacheKeySuffix), |
|
412 | + $view::POS_HEAD, |
|
413 | + ['type' => 'module'], |
|
414 | + 'MODULEPRELOAD_POLYFILL' |
|
415 | + ); |
|
416 | + } |
|
417 | 417 | } |
418 | 418 | |
419 | 419 | /** |
@@ -216,7 +216,7 @@ discard block |
||
216 | 216 | $this->manifestShimsIncluded = true; |
217 | 217 | } |
218 | 218 | |
219 | - foreach(array_merge($tags, $legacyTags) as $tag) { |
|
219 | + foreach (array_merge($tags, $legacyTags) as $tag) { |
|
220 | 220 | if (!empty($tag)) { |
221 | 221 | $url = FileHelper::createUrl($this->serverPublic, $tag['url']); |
222 | 222 | switch ($tag['type']) { |
@@ -234,7 +234,7 @@ discard block |
||
234 | 234 | |
235 | 235 | // modulepreload any imports from modern tags |
236 | 236 | $modulepreloadPolyfillRequired = false; |
237 | - foreach($tags as $tag) { |
|
237 | + foreach ($tags as $tag) { |
|
238 | 238 | if (!empty($tag)) { |
239 | 239 | $url = FileHelper::createUrl($this->serverPublic, $tag['url']); |
240 | 240 | switch ($tag['type']) { |
@@ -251,7 +251,7 @@ discard block |
||
251 | 251 | } |
252 | 252 | } |
253 | 253 | } |
254 | - if($modulepreloadPolyfillRequired && ! $this->modulepreloadPolyfillIncluded) { |
|
254 | + if ($modulepreloadPolyfillRequired && !$this->modulepreloadPolyfillIncluded) { |
|
255 | 255 | $this->modulepreloadPolyfillIncluded = true; |
256 | 256 | $lines[] = HtmlHelper::script( |
257 | 257 | FileHelper::fetchScript('modulepreload-polyfill.min.js', $this->cacheKeySuffix), |
@@ -324,7 +324,7 @@ discard block |
||
324 | 324 | $view->registerJsFile( |
325 | 325 | $url, |
326 | 326 | array_merge(['type' => 'module'], $scriptTagAttrs), |
327 | - md5($url . JsonHelper::encode($scriptTagAttrs)) |
|
327 | + md5($url.JsonHelper::encode($scriptTagAttrs)) |
|
328 | 328 | ); |
329 | 329 | } |
330 | 330 | |
@@ -360,7 +360,7 @@ discard block |
||
360 | 360 | } |
361 | 361 | $this->manifestShimsIncluded = true; |
362 | 362 | } |
363 | - foreach(array_merge($tags, $legacyTags) as $tag) { |
|
363 | + foreach (array_merge($tags, $legacyTags) as $tag) { |
|
364 | 364 | if (!empty($tag)) { |
365 | 365 | $url = FileHelper::createUrl($this->serverPublic, $tag['url']); |
366 | 366 | switch ($tag['type']) { |
@@ -368,7 +368,7 @@ discard block |
||
368 | 368 | $view->registerJsFile( |
369 | 369 | $url, |
370 | 370 | $tag['options'], |
371 | - md5($url . JsonHelper::encode($tag['options'])) |
|
371 | + md5($url.JsonHelper::encode($tag['options'])) |
|
372 | 372 | ); |
373 | 373 | break; |
374 | 374 | case 'css': |
@@ -385,7 +385,7 @@ discard block |
||
385 | 385 | |
386 | 386 | // modulepreload any imports from modern tags |
387 | 387 | $modulepreloadPolyfillRequired = false; |
388 | - foreach($tags as $tag) { |
|
388 | + foreach ($tags as $tag) { |
|
389 | 389 | if (!empty($tag)) { |
390 | 390 | $url = FileHelper::createUrl($this->serverPublic, $tag['url']); |
391 | 391 | switch ($tag['type']) { |
@@ -405,7 +405,7 @@ discard block |
||
405 | 405 | } |
406 | 406 | } |
407 | 407 | } |
408 | - if($modulepreloadPolyfillRequired && ! $this->modulepreloadPolyfillIncluded) { |
|
408 | + if ($modulepreloadPolyfillRequired && !$this->modulepreloadPolyfillIncluded) { |
|
409 | 409 | $this->modulepreloadPolyfillIncluded = true; |
410 | 410 | $view->registerScript( |
411 | 411 | FileHelper::fetchScript('modulepreload-polyfill.min.js', $this->cacheKeySuffix), |
@@ -458,7 +458,7 @@ discard block |
||
458 | 458 | public function invalidateCaches() |
459 | 459 | { |
460 | 460 | $cache = Craft::$app->getCache(); |
461 | - TagDependency::invalidate($cache, FileHelper::CACHE_TAG . $this->cacheKeySuffix); |
|
461 | + TagDependency::invalidate($cache, FileHelper::CACHE_TAG.$this->cacheKeySuffix); |
|
462 | 462 | Craft::info('All Vite caches cleared', __METHOD__); |
463 | 463 | } |
464 | 464 |