Total Complexity | 148 |
Total Lines | 998 |
Duplicated Lines | 0 % |
Changes | 22 | ||
Bugs | 10 | Features | 0 |
Complex classes like Asset often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Asset, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | class Asset implements \ArrayAccess |
||
37 | { |
||
38 | public const IMAGE_THUMB = 'thumbnails'; |
||
39 | |||
40 | /** @var Builder */ |
||
41 | protected $builder; |
||
42 | |||
43 | /** @var Config */ |
||
44 | protected $config; |
||
45 | |||
46 | /** @var array */ |
||
47 | protected $data = []; |
||
48 | |||
49 | /** @var array Cache tags */ |
||
50 | protected $cacheTags = []; |
||
51 | |||
52 | /** |
||
53 | * Creates an Asset from a file path, an array of files path or an URL. |
||
54 | * Options: |
||
55 | * [ |
||
56 | * 'filename' => <string>, |
||
57 | * 'leading_slash' => <bool> |
||
58 | * 'ignore_missing' => <bool>, |
||
59 | * 'fingerprint' => <bool>, |
||
60 | * 'minify' => <bool>, |
||
61 | * 'optimize' => <bool>, |
||
62 | * 'fallback' => <string>, |
||
63 | * 'useragent' => <string>, |
||
64 | * ] |
||
65 | * |
||
66 | * @param Builder $builder |
||
67 | * @param string|array $paths |
||
68 | * @param array|null $options |
||
69 | * |
||
70 | * @throws RuntimeException |
||
71 | */ |
||
72 | public function __construct(Builder $builder, string|array $paths, array|null $options = null) |
||
73 | { |
||
74 | $this->builder = $builder; |
||
75 | $this->config = $builder->getConfig(); |
||
76 | $paths = \is_array($paths) ? $paths : [$paths]; |
||
77 | // checks path(s) |
||
78 | array_walk($paths, function ($path) { |
||
79 | // must be a string |
||
80 | if (!\is_string($path)) { |
||
81 | throw new RuntimeException(\sprintf('The path of an asset must be a string ("%s" given).', \gettype($path))); |
||
82 | } |
||
83 | // can't be empty |
||
84 | if (empty($path)) { |
||
85 | throw new RuntimeException('The path of an asset can\'t be empty.'); |
||
86 | } |
||
87 | // can't be relative |
||
88 | if (substr($path, 0, 2) == '..') { |
||
89 | throw new RuntimeException(\sprintf('The path of asset "%s" is wrong: it must be directly relative to `assets` or `static` directory, or a remote URL.', $path)); |
||
90 | } |
||
91 | }); |
||
92 | $this->data = [ |
||
93 | 'file' => '', // absolute file path |
||
94 | 'files' => [], // array of absolute files path |
||
95 | 'missing' => false, // if file not found but missing allowed: 'missing' is true |
||
96 | '_path' => '', // original path |
||
97 | 'path' => '', // public path |
||
98 | 'url' => null, // URL if it's a remote file |
||
99 | 'ext' => '', // file extension |
||
100 | 'type' => '', // file type (e.g.: image, audio, video, etc.) |
||
101 | 'subtype' => '', // file media type (e.g.: image/png, audio/mp3, etc.) |
||
102 | 'size' => 0, // file size (in bytes) |
||
103 | 'width' => 0, // image width (in pixels) |
||
104 | 'height' => 0, // image height (in pixels) |
||
105 | 'exif' => [], // image exif data |
||
106 | 'content' => '', // file content |
||
107 | 'hash' => '', // file content hash (md5) |
||
108 | ]; |
||
109 | |||
110 | // handles options |
||
111 | $options = array_merge( |
||
112 | [ |
||
113 | 'filename' => '', |
||
114 | 'leading_slash' => true, |
||
115 | 'ignore_missing' => false, |
||
116 | 'fingerprint' => $this->config->isEnabled('assets.fingerprint'), |
||
117 | 'minify' => $this->config->isEnabled('assets.minify'), |
||
118 | 'optimize' => $this->config->isEnabled('assets.images.optimize'), |
||
119 | 'fallback' => '', |
||
120 | 'useragent' => (string) $this->config->get('assets.remote.useragent.default'), |
||
121 | ], |
||
122 | \is_array($options) ? $options : [] |
||
123 | ); |
||
124 | |||
125 | // cache |
||
126 | $cache = new Cache($this->builder, 'assets'); |
||
127 | $locateCacheKey = \sprintf('%s_locate__%s__%s', $options['filename'] ?: implode('_', $paths), $this->builder->getBuildId(), $this->builder->getVersion()); |
||
128 | |||
129 | // locate file(s) and get content |
||
130 | if (!$cache->has($locateCacheKey)) { |
||
131 | $pathsCount = \count($paths); |
||
132 | for ($i = 0; $i < $pathsCount; $i++) { |
||
133 | try { |
||
134 | $this->data['missing'] = false; |
||
135 | $locate = $this->locateFile($paths[$i], $options['fallback'], $options['useragent']); |
||
136 | $file = $locate['file']; |
||
137 | $path = $locate['path']; |
||
138 | $type = Util\File::getMediaType($file)[0]; |
||
139 | if ($i > 0) { // bundle |
||
140 | if ($type != $this->data['type']) { |
||
141 | throw new RuntimeException(\sprintf('Asset bundle type error (%s != %s).', $type, $this->data['type'])); |
||
142 | } |
||
143 | } |
||
144 | $this->data['file'] = $file; |
||
145 | $this->data['files'][] = $file; |
||
146 | $this->data['path'] = $path; |
||
147 | $this->data['url'] = Util\File::isRemote($paths[$i]) ? $paths[$i] : null; |
||
148 | $this->data['ext'] = Util\File::getExtension($file); |
||
149 | $this->data['type'] = $type; |
||
150 | $this->data['subtype'] = Util\File::getMediaType($file)[1]; |
||
151 | $this->data['size'] += filesize($file); |
||
152 | $this->data['content'] .= Util\File::fileGetContents($file); |
||
153 | $this->data['hash'] = hash('md5', $this->data['content']); |
||
154 | // bundle default filename |
||
155 | $filename = $options['filename']; |
||
156 | if ($pathsCount > 1 && empty($filename)) { |
||
157 | switch ($this->data['ext']) { |
||
158 | case 'scss': |
||
159 | case 'css': |
||
160 | $filename = 'styles.css'; |
||
161 | break; |
||
162 | case 'js': |
||
163 | $filename = 'scripts.js'; |
||
164 | break; |
||
165 | default: |
||
166 | throw new RuntimeException(\sprintf('Asset bundle supports %s files only.', '.scss, .css and .js')); |
||
167 | } |
||
168 | } |
||
169 | // apply bundle filename to path |
||
170 | if (!empty($filename)) { |
||
171 | $this->data['path'] = $filename; |
||
172 | } |
||
173 | // add leading slash |
||
174 | if ($options['leading_slash']) { |
||
175 | $this->data['path'] = '/' . ltrim($this->data['path'], '/'); |
||
176 | } |
||
177 | $this->data['_path'] = $this->data['path']; |
||
178 | } catch (RuntimeException $e) { |
||
179 | if ($options['ignore_missing']) { |
||
180 | $this->data['missing'] = true; |
||
181 | continue; |
||
182 | } |
||
183 | throw new RuntimeException( |
||
184 | \sprintf('Can\'t handle asset "%s".', $paths[$i]), |
||
185 | previous: $e |
||
186 | ); |
||
187 | } |
||
188 | } |
||
189 | $cache->set($locateCacheKey, $this->data); |
||
190 | } |
||
191 | $this->data = $cache->get($locateCacheKey); |
||
192 | |||
193 | // missing |
||
194 | if ($this->data['missing']) { |
||
195 | return; |
||
196 | } |
||
197 | |||
198 | // cache |
||
199 | $cache = new Cache($this->builder, 'assets'); |
||
200 | // create cache tags from options |
||
201 | $this->cacheTags = $options; |
||
202 | // remove unnecessary cache tags |
||
203 | unset($this->cacheTags['optimize'], $this->cacheTags['ignore_missing'], $this->cacheTags['fallback'], $this->cacheTags['useragent']); |
||
204 | if (!\in_array($this->data['ext'], ['css', 'js', 'scss'])) { |
||
205 | unset($this->cacheTags['minify']); |
||
206 | } |
||
207 | // optimize image? |
||
208 | $optimize = false; |
||
209 | if ($options['optimize'] && $this->data['type'] == 'image' && !$this->isImageInCdn()) { |
||
210 | $optimize = true; |
||
211 | $quality = (int) $this->config->get('assets.images.quality'); |
||
212 | $this->cacheTags['quality'] = $quality; |
||
213 | } |
||
214 | $cacheKey = $cache->createKeyFromAsset($this, $this->cacheTags); |
||
215 | if (!$cache->has($cacheKey)) { |
||
216 | // fingerprinting |
||
217 | if ($options['fingerprint']) { |
||
218 | $this->doFingerprint(); |
||
219 | } |
||
220 | // compiling Sass files |
||
221 | $this->doCompile(); |
||
222 | // minifying (CSS and JavaScript files) |
||
223 | if ($options['minify']) { |
||
224 | $this->doMinify(); |
||
225 | } |
||
226 | // get image width, height and exif |
||
227 | if ($this->data['type'] == 'image') { |
||
228 | $this->data['width'] = $this->getWidth(); |
||
229 | $this->data['height'] = $this->getHeight(); |
||
230 | if ($this->data['subtype'] == 'image/jpeg') { |
||
231 | $this->data['exif'] = Util\File::readExif($this->data['file']); |
||
232 | } |
||
233 | } |
||
234 | $cache->set($cacheKey, $this->data, $this->config->get('cache.assets.ttl')); |
||
235 | $this->builder->getLogger()->debug(\sprintf('Asset cached: "%s"', $this->data['path'])); |
||
236 | // optimizing images files (in cache directory) |
||
237 | if ($optimize) { |
||
238 | $this->optimize($cache->getContentFilePathname($this->data['path']), $this->data['path'], $quality); |
||
239 | } |
||
240 | } |
||
241 | $this->data = $cache->get($cacheKey); |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * Returns path. |
||
246 | */ |
||
247 | public function __toString(): string |
||
248 | { |
||
249 | $this->save(); |
||
250 | |||
251 | if ($this->isImageInCdn()) { |
||
252 | return $this->buildImageCdnUrl(); |
||
253 | } |
||
254 | |||
255 | if ($this->builder->getConfig()->isEnabled('canonicalurl')) { |
||
256 | return (string) new Url($this->builder, $this->data['path'], ['canonical' => true]); |
||
257 | } |
||
258 | |||
259 | return $this->data['path']; |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * Compiles a SCSS + cache. |
||
264 | * |
||
265 | * @throws RuntimeException |
||
266 | */ |
||
267 | public function compile(): self |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * Minifying a CSS or a JS. |
||
283 | */ |
||
284 | public function minify(): self |
||
285 | { |
||
286 | $this->cacheTags['minify'] = true; |
||
287 | $cache = new Cache($this->builder, 'assets'); |
||
288 | $cacheKey = $cache->createKeyFromAsset($this, $this->cacheTags); |
||
289 | if (!$cache->has($cacheKey)) { |
||
290 | $this->doMinify(); |
||
291 | $cache->set($cacheKey, $this->data, $this->config->get('cache.assets.ttl')); |
||
292 | } |
||
293 | $this->data = $cache->get($cacheKey); |
||
294 | |||
295 | return $this; |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Add hash to the file name + cache. |
||
300 | */ |
||
301 | public function fingerprint(): self |
||
302 | { |
||
303 | $this->cacheTags['fingerprint'] = true; |
||
304 | $cache = new Cache($this->builder, 'assets'); |
||
305 | $cacheKey = $cache->createKeyFromAsset($this, $this->cacheTags); |
||
306 | if (!$cache->has($cacheKey)) { |
||
307 | $this->doFingerprint(); |
||
308 | $cache->set($cacheKey, $this->data, $this->config->get('cache.assets.ttl')); |
||
309 | } |
||
310 | $this->data = $cache->get($cacheKey); |
||
311 | |||
312 | return $this; |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * Scales down an image to a new $width. |
||
317 | * |
||
318 | * @throws RuntimeException |
||
319 | */ |
||
320 | public function resize(int $width): self |
||
321 | { |
||
322 | $this->checkImage(); |
||
323 | |||
324 | // if the image is already smaller than the requested width, return it |
||
325 | if ($width >= $this->data['width']) { |
||
326 | return $this; |
||
327 | } |
||
328 | |||
329 | $assetResized = clone $this; |
||
330 | $assetResized->data['width'] = $width; |
||
331 | |||
332 | if ($this->isImageInCdn()) { |
||
333 | $assetResized->data['height'] = round($this->data['height'] / ($this->data['width'] / $width)); |
||
334 | |||
335 | return $assetResized; // returns asset with the new dimensions only: CDN do the rest of the job |
||
336 | } |
||
337 | |||
338 | $quality = (int) $this->config->get('assets.images.quality'); |
||
339 | |||
340 | $cache = new Cache($this->builder, 'assets'); |
||
341 | $assetResized->cacheTags['quality'] = $quality; |
||
342 | $assetResized->cacheTags['width'] = $width; |
||
343 | $cacheKey = $cache->createKeyFromAsset($assetResized, $assetResized->cacheTags); |
||
344 | if (!$cache->has($cacheKey)) { |
||
345 | $assetResized->data['content'] = Image::resize($assetResized, $width, $quality); |
||
346 | $assetResized->data['path'] = '/' . Util::joinPath( |
||
347 | (string) $this->config->get('assets.target'), |
||
348 | self::IMAGE_THUMB, |
||
349 | (string) $width, |
||
350 | $assetResized->data['path'] |
||
351 | ); |
||
352 | $assetResized->data['path'] = $this->deduplicateThumbPath($assetResized->data['path']); |
||
353 | $assetResized->data['height'] = $assetResized->getHeight(); |
||
354 | $assetResized->data['size'] = \strlen($assetResized->data['content']); |
||
355 | |||
356 | $cache->set($cacheKey, $assetResized->data, $this->config->get('cache.assets.ttl')); |
||
357 | $this->builder->getLogger()->debug(\sprintf('Asset resized: "%s" (%sx)', $assetResized->data['path'], $width)); |
||
358 | } |
||
359 | $assetResized->data = $cache->get($cacheKey); |
||
360 | |||
361 | return $assetResized; |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * Crops the image to the specified width and height, keeping the specified position. |
||
366 | * |
||
367 | * @throws RuntimeException |
||
368 | */ |
||
369 | public function cover(int $width, int $height): self |
||
370 | { |
||
371 | $this->checkImage(); |
||
372 | |||
373 | $assetResized = clone $this; |
||
374 | $assetResized->data['width'] = $width; |
||
375 | $assetResized->data['height'] = $height; |
||
376 | |||
377 | $quality = (int) $this->config->get('assets.images.quality'); |
||
378 | |||
379 | $cache = new Cache($this->builder, 'assets'); |
||
380 | $assetResized->cacheTags['quality'] = $quality; |
||
381 | $assetResized->cacheTags['width'] = $width; |
||
382 | $assetResized->cacheTags['height'] = $height; |
||
383 | $cacheKey = $cache->createKeyFromAsset($assetResized, $assetResized->cacheTags); |
||
384 | if (!$cache->has($cacheKey)) { |
||
385 | $assetResized->data['content'] = Image::cover($assetResized, $width, $height, $quality); |
||
386 | $assetResized->data['path'] = '/' . Util::joinPath( |
||
387 | (string) $this->config->get('assets.target'), |
||
388 | self::IMAGE_THUMB, |
||
389 | (string) $width . 'x' . (string) $height, |
||
390 | $assetResized->data['path'] |
||
391 | ); |
||
392 | $assetResized->data['path'] = $this->deduplicateThumbPath($assetResized->data['path']); |
||
393 | $assetResized->data['size'] = \strlen($assetResized->data['content']); |
||
394 | |||
395 | $cache->set($cacheKey, $assetResized->data, $this->config->get('cache.assets.ttl')); |
||
396 | $this->builder->getLogger()->debug(\sprintf('Asset resized: "%s" (%sx%s)', $assetResized->data['path'], $width, $height)); |
||
397 | } |
||
398 | $assetResized->data = $cache->get($cacheKey); |
||
399 | |||
400 | return $assetResized; |
||
401 | } |
||
402 | |||
403 | /** |
||
404 | * Creates a maskable image (with a padding = 20%). |
||
405 | * |
||
406 | * @throws RuntimeException |
||
407 | */ |
||
408 | public function maskable(?int $padding = null): self |
||
409 | { |
||
410 | $this->checkImage(); |
||
411 | |||
412 | if ($padding === null) { |
||
413 | $padding = 20; // default padding |
||
414 | } |
||
415 | |||
416 | $assetMaskable = clone $this; |
||
417 | |||
418 | $quality = (int) $this->config->get('assets.images.quality'); |
||
419 | |||
420 | $cache = new Cache($this->builder, 'assets'); |
||
421 | $assetMaskable->cacheTags['maskable'] = true; |
||
422 | $cacheKey = $cache->createKeyFromAsset($assetMaskable, $assetMaskable->cacheTags); |
||
423 | if (!$cache->has($cacheKey)) { |
||
424 | $assetMaskable->data['content'] = Image::maskable($assetMaskable, $quality, $padding); |
||
425 | $assetMaskable->data['path'] = '/' . Util::joinPath( |
||
426 | (string) $this->config->get('assets.target'), |
||
427 | 'maskable', |
||
428 | $assetMaskable->data['path'] |
||
429 | ); |
||
430 | $assetMaskable->data['size'] = \strlen($assetMaskable->data['content']); |
||
431 | |||
432 | $cache->set($cacheKey, $assetMaskable->data, $this->config->get('cache.assets.ttl')); |
||
433 | $this->builder->getLogger()->debug(\sprintf('Asset maskabled: "%s"', $assetMaskable->data['path'])); |
||
434 | } |
||
435 | $assetMaskable->data = $cache->get($cacheKey); |
||
436 | |||
437 | return $assetMaskable; |
||
438 | } |
||
439 | |||
440 | /** |
||
441 | * Converts an image asset to $format format. |
||
442 | * |
||
443 | * @throws RuntimeException |
||
444 | */ |
||
445 | public function convert(string $format, ?int $quality = null): self |
||
446 | { |
||
447 | if ($this->data['type'] != 'image') { |
||
448 | throw new RuntimeException(\sprintf('Not able to convert "%s" (%s) to %s: not an image.', $this->data['path'], $this->data['type'], $format)); |
||
449 | } |
||
450 | |||
451 | if ($quality === null) { |
||
452 | $quality = (int) $this->config->get('assets.images.quality'); |
||
453 | } |
||
454 | |||
455 | $asset = clone $this; |
||
456 | $asset['ext'] = $format; |
||
457 | $asset->data['subtype'] = "image/$format"; |
||
458 | |||
459 | if ($this->isImageInCdn()) { |
||
460 | return $asset; // returns the asset with the new extension only: CDN do the rest of the job |
||
461 | } |
||
462 | |||
463 | $cache = new Cache($this->builder, 'assets'); |
||
464 | $this->cacheTags['quality'] = $quality; |
||
465 | if ($this->data['width']) { |
||
466 | $this->cacheTags['width'] = $this->data['width']; |
||
467 | } |
||
468 | $cacheKey = $cache->createKeyFromAsset($asset, $this->cacheTags); |
||
469 | if (!$cache->has($cacheKey)) { |
||
470 | $asset->data['content'] = Image::convert($asset, $format, $quality); |
||
471 | $asset->data['path'] = preg_replace('/\.' . $this->data['ext'] . '$/m', ".$format", $this->data['path']); |
||
472 | $asset->data['size'] = \strlen($asset->data['content']); |
||
473 | $cache->set($cacheKey, $asset->data, $this->config->get('cache.assets.ttl')); |
||
474 | $this->builder->getLogger()->debug(\sprintf('Asset converted: "%s" (%s -> %s)', $asset->data['path'], $this->data['ext'], $format)); |
||
475 | } |
||
476 | $asset->data = $cache->get($cacheKey); |
||
477 | |||
478 | return $asset; |
||
479 | } |
||
480 | |||
481 | /** |
||
482 | * Converts an image asset to WebP format. |
||
483 | * |
||
484 | * @throws RuntimeException |
||
485 | */ |
||
486 | public function webp(?int $quality = null): self |
||
487 | { |
||
488 | return $this->convert('webp', $quality); |
||
489 | } |
||
490 | |||
491 | /** |
||
492 | * Converts an image asset to AVIF format. |
||
493 | * |
||
494 | * @throws RuntimeException |
||
495 | */ |
||
496 | public function avif(?int $quality = null): self |
||
497 | { |
||
498 | return $this->convert('avif', $quality); |
||
499 | } |
||
500 | |||
501 | /** |
||
502 | * Implements \ArrayAccess. |
||
503 | */ |
||
504 | #[\ReturnTypeWillChange] |
||
505 | public function offsetSet($offset, $value): void |
||
506 | { |
||
507 | if (!\is_null($offset)) { |
||
508 | $this->data[$offset] = $value; |
||
509 | } |
||
510 | } |
||
511 | |||
512 | /** |
||
513 | * Implements \ArrayAccess. |
||
514 | */ |
||
515 | #[\ReturnTypeWillChange] |
||
516 | public function offsetExists($offset): bool |
||
517 | { |
||
518 | return isset($this->data[$offset]); |
||
519 | } |
||
520 | |||
521 | /** |
||
522 | * Implements \ArrayAccess. |
||
523 | */ |
||
524 | #[\ReturnTypeWillChange] |
||
525 | public function offsetUnset($offset): void |
||
526 | { |
||
527 | unset($this->data[$offset]); |
||
528 | } |
||
529 | |||
530 | /** |
||
531 | * Implements \ArrayAccess. |
||
532 | */ |
||
533 | #[\ReturnTypeWillChange] |
||
534 | public function offsetGet($offset) |
||
535 | { |
||
536 | return isset($this->data[$offset]) ? $this->data[$offset] : null; |
||
537 | } |
||
538 | |||
539 | /** |
||
540 | * Hashing content of an asset with the specified algo, sha384 by default. |
||
541 | * Used for SRI (Subresource Integrity). |
||
542 | * |
||
543 | * @see https://developer.mozilla.org/fr/docs/Web/Security/Subresource_Integrity |
||
544 | */ |
||
545 | public function getIntegrity(string $algo = 'sha384'): string |
||
546 | { |
||
547 | return \sprintf('%s-%s', $algo, base64_encode(hash($algo, $this->data['content'], true))); |
||
548 | } |
||
549 | |||
550 | /** |
||
551 | * Returns MP3 file infos. |
||
552 | * |
||
553 | * @see https://github.com/wapmorgan/Mp3Info |
||
554 | */ |
||
555 | public function getAudio(): Mp3Info |
||
556 | { |
||
557 | if ($this->data['type'] !== 'audio') { |
||
558 | throw new RuntimeException(\sprintf('Not able to get audio infos of "%s".', $this->data['path'])); |
||
559 | } |
||
560 | |||
561 | return new Mp3Info($this->data['file']); |
||
562 | } |
||
563 | |||
564 | /** |
||
565 | * Returns MP4 file infos. |
||
566 | * |
||
567 | * @see https://github.com/clwu88/php-read-mp4info |
||
568 | */ |
||
569 | public function getVideo(): array |
||
570 | { |
||
571 | if ($this->data['type'] !== 'video') { |
||
572 | throw new RuntimeException(\sprintf('Not able to get video infos of "%s".', $this->data['path'])); |
||
573 | } |
||
574 | |||
575 | return (array) \Clwu\Mp4::getInfo($this->data['file']); |
||
576 | } |
||
577 | |||
578 | /** |
||
579 | * Returns the Data URL (encoded in Base64). |
||
580 | * |
||
581 | * @throws RuntimeException |
||
582 | */ |
||
583 | public function dataurl(): string |
||
584 | { |
||
585 | if ($this->data['type'] == 'image' && !Image::isSVG($this)) { |
||
586 | return Image::getDataUrl($this, (int) $this->config->get('assets.images.quality')); |
||
587 | } |
||
588 | |||
589 | return \sprintf('data:%s;base64,%s', $this->data['subtype'], base64_encode($this->data['content'])); |
||
590 | } |
||
591 | |||
592 | /** |
||
593 | * Adds asset path to the list of assets to save. |
||
594 | * |
||
595 | * @throws RuntimeException |
||
596 | */ |
||
597 | public function save(): void |
||
598 | { |
||
599 | if ($this->data['missing']) { |
||
600 | return; |
||
601 | } |
||
602 | |||
603 | $cache = new Cache($this->builder, 'assets'); |
||
604 | if (empty($this->data['path']) || !Util\File::getFS()->exists($cache->getContentFilePathname($this->data['path']))) { |
||
605 | throw new RuntimeException( |
||
606 | \sprintf('Can\'t add "%s" to assets list. Please clear cache and retry.', $this->data['path']) |
||
607 | ); |
||
608 | } |
||
609 | |||
610 | $this->builder->addAsset($this->data['path']); |
||
611 | } |
||
612 | |||
613 | /** |
||
614 | * Is the asset an image and is it in CDN? |
||
615 | */ |
||
616 | public function isImageInCdn(): bool |
||
617 | { |
||
618 | if ( |
||
619 | $this->data['type'] == 'image' |
||
620 | && $this->config->isEnabled('assets.images.cdn') |
||
621 | && $this->data['ext'] != 'ico' |
||
622 | && (Image::isSVG($this) && $this->config->isEnabled('assets.images.cdn.svg')) |
||
623 | ) { |
||
624 | return true; |
||
625 | } |
||
626 | // handle remote image? |
||
627 | if ($this->data['url'] !== null && $this->config->isEnabled('assets.images.cdn.remote')) { |
||
628 | return true; |
||
629 | } |
||
630 | |||
631 | return false; |
||
632 | } |
||
633 | |||
634 | /** |
||
635 | * Builds a relative path from a URL. |
||
636 | * Used for remote files. |
||
637 | */ |
||
638 | public static function buildPathFromUrl(string $url): string |
||
639 | { |
||
640 | $host = parse_url($url, PHP_URL_HOST); |
||
641 | $path = parse_url($url, PHP_URL_PATH); |
||
642 | $query = parse_url($url, PHP_URL_QUERY); |
||
643 | $ext = pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_EXTENSION); |
||
644 | |||
645 | // Google Fonts hack |
||
646 | if (Util\Str::endsWith($path, '/css') || Util\Str::endsWith($path, '/css2')) { |
||
647 | $ext = 'css'; |
||
648 | } |
||
649 | |||
650 | return Page::slugify(\sprintf('%s%s%s%s', $host, self::sanitize($path), $query ? "-$query" : '', $query && $ext ? ".$ext" : '')); |
||
651 | } |
||
652 | |||
653 | /** |
||
654 | * Replaces some characters by '_'. |
||
655 | */ |
||
656 | public static function sanitize(string $string): string |
||
657 | { |
||
658 | return str_replace(['<', '>', ':', '"', '\\', '|', '?', '*'], '_', $string); |
||
659 | } |
||
660 | |||
661 | /** |
||
662 | * Compiles a SCSS. |
||
663 | * |
||
664 | * @throws RuntimeException |
||
665 | */ |
||
666 | protected function doCompile(): self |
||
667 | { |
||
668 | // abort if not a SCSS file |
||
669 | if ($this->data['ext'] != 'scss') { |
||
670 | return $this; |
||
671 | } |
||
672 | $scssPhp = new Compiler(); |
||
673 | // import paths |
||
674 | $importDir = []; |
||
675 | $importDir[] = Util::joinPath($this->config->getStaticPath()); |
||
676 | $importDir[] = Util::joinPath($this->config->getAssetsPath()); |
||
677 | $scssDir = (array) $this->config->get('assets.compile.import'); |
||
678 | $themes = $this->config->getTheme() ?? []; |
||
679 | foreach ($scssDir as $dir) { |
||
680 | $importDir[] = Util::joinPath($this->config->getStaticPath(), $dir); |
||
681 | $importDir[] = Util::joinPath($this->config->getAssetsPath(), $dir); |
||
682 | $importDir[] = Util::joinPath(\dirname($this->data['file']), $dir); |
||
683 | foreach ($themes as $theme) { |
||
684 | $importDir[] = Util::joinPath($this->config->getThemeDirPath($theme, "static/$dir")); |
||
685 | $importDir[] = Util::joinPath($this->config->getThemeDirPath($theme, "assets/$dir")); |
||
686 | } |
||
687 | } |
||
688 | $scssPhp->setQuietDeps(true); |
||
689 | $scssPhp->setImportPaths(array_unique($importDir)); |
||
690 | // adds source map |
||
691 | if ($this->builder->isDebug() && $this->config->isEnabled('assets.compile.sourcemap')) { |
||
692 | $importDir = []; |
||
693 | $assetDir = (string) $this->config->get('assets.dir'); |
||
694 | $assetDirPos = strrpos($this->data['file'], DIRECTORY_SEPARATOR . $assetDir . DIRECTORY_SEPARATOR); |
||
695 | $fileRelPath = substr($this->data['file'], $assetDirPos + 8); |
||
696 | $filePath = Util::joinFile($this->config->getOutputPath(), $fileRelPath); |
||
697 | $importDir[] = \dirname($filePath); |
||
698 | foreach ($scssDir as $dir) { |
||
699 | $importDir[] = Util::joinFile($this->config->getOutputPath(), $dir); |
||
700 | } |
||
701 | $scssPhp->setImportPaths(array_unique($importDir)); |
||
702 | $scssPhp->setSourceMap(Compiler::SOURCE_MAP_INLINE); |
||
703 | $scssPhp->setSourceMapOptions([ |
||
704 | 'sourceMapBasepath' => Util::joinPath($this->config->getOutputPath()), |
||
705 | 'sourceRoot' => '/', |
||
706 | ]); |
||
707 | } |
||
708 | // defines output style |
||
709 | $outputStyles = ['expanded', 'compressed']; |
||
710 | $outputStyle = strtolower((string) $this->config->get('assets.compile.style')); |
||
711 | if (!\in_array($outputStyle, $outputStyles)) { |
||
712 | throw new ConfigException(\sprintf('"%s" value must be "%s".', 'assets.compile.style', implode('" or "', $outputStyles))); |
||
713 | } |
||
714 | $scssPhp->setOutputStyle($outputStyle == 'compressed' ? OutputStyle::COMPRESSED : OutputStyle::EXPANDED); |
||
715 | // set variables |
||
716 | $variables = $this->config->get('assets.compile.variables'); |
||
717 | if (!empty($variables)) { |
||
718 | $variables = array_map('ScssPhp\ScssPhp\ValueConverter::parseValue', $variables); |
||
719 | $scssPhp->replaceVariables($variables); |
||
720 | } |
||
721 | // debug |
||
722 | if ($this->builder->isDebug()) { |
||
723 | $scssPhp->setQuietDeps(false); |
||
724 | $this->builder->getLogger()->debug(\sprintf("SCSS compiler imported paths:\n%s", Util\Str::arrayToList(array_unique($importDir)))); |
||
725 | } |
||
726 | // update data |
||
727 | $this->data['path'] = preg_replace('/sass|scss/m', 'css', $this->data['path']); |
||
728 | $this->data['ext'] = 'css'; |
||
729 | $this->data['type'] = 'text'; |
||
730 | $this->data['subtype'] = 'text/css'; |
||
731 | $this->data['content'] = $scssPhp->compileString($this->data['content'])->getCss(); |
||
732 | $this->data['size'] = \strlen($this->data['content']); |
||
733 | |||
734 | $this->builder->getLogger()->debug(\sprintf('Asset compiled: "%s"', $this->data['path'])); |
||
735 | |||
736 | return $this; |
||
737 | } |
||
738 | |||
739 | /** |
||
740 | * Minifying a CSS or a JS + cache. |
||
741 | * |
||
742 | * @throws RuntimeException |
||
743 | */ |
||
744 | protected function doMinify(): self |
||
745 | { |
||
746 | // compile SCSS files |
||
747 | if ($this->data['ext'] == 'scss') { |
||
748 | $this->doCompile(); |
||
749 | } |
||
750 | // abort if already minified |
||
751 | if (substr($this->data['path'], -8) == '.min.css' || substr($this->data['path'], -7) == '.min.js') { |
||
752 | return $this; |
||
753 | } |
||
754 | // abord if not a CSS or JS file |
||
755 | if (!\in_array($this->data['ext'], ['css', 'js'])) { |
||
756 | return $this; |
||
757 | } |
||
758 | // in debug mode: disable minify to preserve inline source map |
||
759 | if ($this->builder->isDebug() && $this->config->isEnabled('assets.compile.sourcemap')) { |
||
760 | return $this; |
||
761 | } |
||
762 | switch ($this->data['ext']) { |
||
763 | case 'css': |
||
764 | $minifier = new Minify\CSS($this->data['content']); |
||
765 | break; |
||
766 | case 'js': |
||
767 | $minifier = new Minify\JS($this->data['content']); |
||
768 | break; |
||
769 | default: |
||
770 | throw new RuntimeException(\sprintf('Not able to minify "%s".', $this->data['path'])); |
||
771 | } |
||
772 | $this->data['content'] = $minifier->minify(); |
||
773 | $this->data['size'] = \strlen($this->data['content']); |
||
774 | |||
775 | $this->builder->getLogger()->debug(\sprintf('Asset minified: "%s"', $this->data['path'])); |
||
776 | |||
777 | return $this; |
||
778 | } |
||
779 | |||
780 | /** |
||
781 | * Add hash to the file name. |
||
782 | */ |
||
783 | protected function doFingerprint(): self |
||
784 | { |
||
785 | $hash = hash('md5', $this->data['content']); |
||
786 | $this->data['path'] = preg_replace( |
||
787 | '/\.' . $this->data['ext'] . '$/m', |
||
788 | ".$hash." . $this->data['ext'], |
||
789 | $this->data['path'] |
||
790 | ); |
||
791 | $this->builder->getLogger()->debug(\sprintf('Asset fingerprinted: "%s"', $this->data['path'])); |
||
792 | |||
793 | return $this; |
||
794 | } |
||
795 | |||
796 | /** |
||
797 | * Returns local file path and updated path, or throw an exception. |
||
798 | * If $fallback path is set, it will be used if the remote file is not found. |
||
799 | * |
||
800 | * Try to locate the file in: |
||
801 | * (1. remote file) |
||
802 | * 1. assets |
||
803 | * 2. themes/<theme>/assets |
||
804 | * 3. static |
||
805 | * 4. themes/<theme>/static |
||
806 | * |
||
807 | * @throws RuntimeException |
||
808 | */ |
||
809 | private function locateFile(string $path, ?string $fallback = null, ?string $userAgent = null): array |
||
810 | { |
||
811 | // remote file |
||
812 | if (Util\File::isRemote($path)) { |
||
813 | try { |
||
814 | $content = $this->getRemoteFileContent($path, $userAgent); |
||
815 | $path = self::buildPathFromUrl($path); |
||
816 | $cache = new Cache($this->builder, 'assets/remote'); |
||
817 | if (!$cache->has($path)) { |
||
818 | $cache->set($path, [ |
||
819 | 'content' => $content, |
||
820 | 'path' => $path, |
||
821 | ], $this->config->get('cache.assets.remote.ttl')); |
||
822 | } |
||
823 | return [ |
||
824 | 'file' => $cache->getContentFilePathname($path), |
||
825 | 'path' => $path, |
||
826 | ]; |
||
827 | } catch (RuntimeException $e) { |
||
828 | if (empty($fallback)) { |
||
829 | throw new RuntimeException($e->getMessage()); |
||
830 | } |
||
831 | $path = $fallback; |
||
832 | } |
||
833 | } |
||
834 | |||
835 | // checks in assets/ |
||
836 | $file = Util::joinFile($this->config->getAssetsPath(), $path); |
||
837 | if (Util\File::getFS()->exists($file)) { |
||
838 | return [ |
||
839 | 'file' => $file, |
||
840 | 'path' => $path, |
||
841 | ]; |
||
842 | } |
||
843 | |||
844 | // checks in each themes/<theme>/assets/ |
||
845 | foreach ($this->config->getTheme() ?? [] as $theme) { |
||
846 | $file = Util::joinFile($this->config->getThemeDirPath($theme, 'assets'), $path); |
||
847 | if (Util\File::getFS()->exists($file)) { |
||
848 | return [ |
||
849 | 'file' => $file, |
||
850 | 'path' => $path, |
||
851 | ]; |
||
852 | } |
||
853 | } |
||
854 | |||
855 | // checks in static/ |
||
856 | $file = Util::joinFile($this->config->getStaticPath(), $path); |
||
857 | if (Util\File::getFS()->exists($file)) { |
||
858 | return [ |
||
859 | 'file' => $file, |
||
860 | 'path' => $path, |
||
861 | ]; |
||
862 | } |
||
863 | |||
864 | // checks in each themes/<theme>/static/ |
||
865 | foreach ($this->config->getTheme() ?? [] as $theme) { |
||
866 | $file = Util::joinFile($this->config->getThemeDirPath($theme, 'static'), $path); |
||
867 | if (Util\File::getFS()->exists($file)) { |
||
868 | return [ |
||
869 | 'file' => $file, |
||
870 | 'path' => $path, |
||
871 | ]; |
||
872 | } |
||
873 | } |
||
874 | |||
875 | throw new RuntimeException(\sprintf('Can\'t locate file "%s".', $path)); |
||
876 | } |
||
877 | |||
878 | /** |
||
879 | * Try to get remote file content. |
||
880 | * Returns file content or throw an exception. |
||
881 | * |
||
882 | * @throws RuntimeException |
||
883 | */ |
||
884 | private function getRemoteFileContent(string $path, ?string $userAgent = null): string |
||
885 | { |
||
886 | if (!Util\File::isRemoteExists($path)) { |
||
887 | throw new RuntimeException(\sprintf('Can\'t get remote file "%s".', $path)); |
||
888 | } |
||
889 | if (false === $content = Util\File::fileGetContents($path, $userAgent)) { |
||
890 | throw new RuntimeException(\sprintf('Can\'t get content of remote file "%s".', $path)); |
||
891 | } |
||
892 | if (\strlen($content) <= 1) { |
||
893 | throw new RuntimeException(\sprintf('Remote file "%s" is empty.', $path)); |
||
894 | } |
||
895 | |||
896 | return $content; |
||
897 | } |
||
898 | |||
899 | /** |
||
900 | * Optimizing $filepath image. |
||
901 | * Returns the new file size. |
||
902 | */ |
||
903 | private function optimize(string $filepath, string $path, int $quality): int |
||
904 | { |
||
905 | $message = \sprintf('Asset not optimized: "%s"', $path); |
||
906 | $sizeBefore = filesize($filepath); |
||
907 | Optimizer::create($quality)->optimize($filepath); |
||
908 | $sizeAfter = filesize($filepath); |
||
909 | if ($sizeAfter < $sizeBefore) { |
||
910 | $message = \sprintf( |
||
911 | 'Asset optimized: "%s" (%s Ko -> %s Ko)', |
||
912 | $path, |
||
913 | ceil($sizeBefore / 1000), |
||
914 | ceil($sizeAfter / 1000) |
||
915 | ); |
||
916 | } |
||
917 | $this->builder->getLogger()->debug($message); |
||
918 | |||
919 | return $sizeAfter; |
||
920 | } |
||
921 | |||
922 | /** |
||
923 | * Returns the width of an image/SVG. |
||
924 | * |
||
925 | * @throws RuntimeException |
||
926 | */ |
||
927 | private function getWidth(): int |
||
928 | { |
||
929 | if ($this->data['type'] != 'image') { |
||
930 | return 0; |
||
931 | } |
||
932 | if (Image::isSVG($this) && false !== $svg = Image::getSvgAttributes($this)) { |
||
933 | return (int) $svg->width; |
||
934 | } |
||
935 | if (false === $size = $this->getImageSize()) { |
||
936 | throw new RuntimeException(\sprintf('Not able to get width of "%s".', $this->data['path'])); |
||
937 | } |
||
938 | |||
939 | return $size[0]; |
||
940 | } |
||
941 | |||
942 | /** |
||
943 | * Returns the height of an image/SVG. |
||
944 | * |
||
945 | * @throws RuntimeException |
||
946 | */ |
||
947 | private function getHeight(): int |
||
948 | { |
||
949 | if ($this->data['type'] != 'image') { |
||
950 | return 0; |
||
951 | } |
||
952 | if (Image::isSVG($this) && false !== $svg = Image::getSvgAttributes($this)) { |
||
953 | return (int) $svg->height; |
||
954 | } |
||
955 | if (false === $size = $this->getImageSize()) { |
||
956 | throw new RuntimeException(\sprintf('Not able to get height of "%s".', $this->data['path'])); |
||
957 | } |
||
958 | |||
959 | return $size[1]; |
||
960 | } |
||
961 | |||
962 | /** |
||
963 | * Returns image size informations. |
||
964 | * |
||
965 | * @see https://www.php.net/manual/function.getimagesize.php |
||
966 | * |
||
967 | * @return array|false |
||
968 | */ |
||
969 | private function getImageSize() |
||
970 | { |
||
971 | if (!$this->data['type'] == 'image') { |
||
972 | return false; |
||
973 | } |
||
974 | |||
975 | try { |
||
976 | if (false === $size = getimagesizefromstring($this->data['content'])) { |
||
977 | return false; |
||
978 | } |
||
979 | } catch (\Exception $e) { |
||
980 | throw new RuntimeException(\sprintf('Handling asset "%s" failed: "%s"', $this->data['path'], $e->getMessage())); |
||
981 | } |
||
982 | |||
983 | return $size; |
||
984 | } |
||
985 | |||
986 | /** |
||
987 | * Builds CDN image URL. |
||
988 | */ |
||
989 | private function buildImageCdnUrl(): string |
||
990 | { |
||
991 | return str_replace( |
||
992 | [ |
||
993 | '%account%', |
||
994 | '%image_url%', |
||
995 | '%width%', |
||
996 | '%quality%', |
||
997 | '%format%', |
||
998 | ], |
||
999 | [ |
||
1000 | $this->config->get('assets.images.cdn.account') ?? '', |
||
1001 | ltrim($this->data['url'] ?? (string) new Url($this->builder, $this->data['path'], ['canonical' => $this->config->get('assets.images.cdn.canonical') ?? true]), '/'), |
||
1002 | $this->data['width'], |
||
1003 | (int) $this->config->get('assets.images.quality'), |
||
1004 | $this->data['ext'], |
||
1005 | ], |
||
1006 | (string) $this->config->get('assets.images.cdn.url') |
||
1007 | ); |
||
1008 | } |
||
1009 | |||
1010 | /** |
||
1011 | * Checks if the asset is not missing and is typed as an image. |
||
1012 | * |
||
1013 | * @throws RuntimeException |
||
1014 | */ |
||
1015 | private function checkImage(): void |
||
1022 | } |
||
1023 | } |
||
1024 | |||
1025 | /** |
||
1026 | * Remove redondant '/thumbnails/<width>/' in the path. |
||
1027 | */ |
||
1028 | private function deduplicateThumbPath(string $path): string |
||
1034 | } |
||
1035 | } |
||
1036 |