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