1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Cecil/Cecil package. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Cecil\Assets; |
12
|
|
|
|
13
|
|
|
use Cecil\Builder; |
14
|
|
|
use Cecil\Collection\Page\Page; |
15
|
|
|
use Cecil\Config; |
16
|
|
|
use Cecil\Exception\Exception; |
17
|
|
|
use Cecil\Util; |
18
|
|
|
use Intervention\Image\ImageManagerStatic as ImageManager; |
19
|
|
|
use MatthiasMullie\Minify; |
20
|
|
|
use ScssPhp\ScssPhp\Compiler; |
21
|
|
|
use wapmorgan\Mp3Info\Mp3Info; |
22
|
|
|
|
23
|
|
|
class Asset implements \ArrayAccess |
24
|
|
|
{ |
25
|
|
|
/** @var Builder */ |
26
|
|
|
protected $builder; |
27
|
|
|
|
28
|
|
|
/** @var Config */ |
29
|
|
|
protected $config; |
30
|
|
|
|
31
|
|
|
/** @var array */ |
32
|
|
|
protected $data = []; |
33
|
|
|
|
34
|
|
|
/** @var bool */ |
35
|
|
|
protected $optimized = false; |
36
|
|
|
|
37
|
|
|
/** @var bool */ |
38
|
|
|
protected $fingerprinted = false; |
39
|
|
|
|
40
|
|
|
/** @var bool */ |
41
|
|
|
protected $compiled = false; |
42
|
|
|
|
43
|
|
|
/** @var bool */ |
44
|
|
|
protected $minified = false; |
45
|
|
|
|
46
|
|
|
/** @var bool */ |
47
|
|
|
protected $ignore_missing = false; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Creates an Asset from file(s) path. |
51
|
|
|
* |
52
|
|
|
* $options[ |
53
|
|
|
* 'fingerprint' => true, |
54
|
|
|
* 'minify' => true, |
55
|
|
|
* 'filename' => '', |
56
|
|
|
* 'ignore_missing' => false, |
57
|
|
|
* ]; |
58
|
|
|
* |
59
|
|
|
* @param Builder $builder |
60
|
|
|
* @param string|array $paths |
61
|
|
|
* @param array|null $options |
62
|
|
|
*/ |
63
|
1 |
|
public function __construct(Builder $builder, $paths, array $options = null) |
64
|
|
|
{ |
65
|
1 |
|
$this->builder = $builder; |
66
|
1 |
|
$this->config = $builder->getConfig(); |
67
|
1 |
|
$paths = is_array($paths) ? $paths : [$paths]; |
68
|
|
|
array_walk($paths, function ($path) { |
69
|
1 |
|
if (empty($path)) { |
70
|
|
|
throw new Exception('The path parameter of "asset() can\'t be empty."'); |
71
|
|
|
} |
72
|
1 |
|
}); |
73
|
1 |
|
$this->data = [ |
74
|
|
|
'file' => '', |
75
|
|
|
'filename' => '', |
76
|
|
|
'path_source' => '', |
77
|
|
|
'path' => '', |
78
|
|
|
'ext' => '', |
79
|
|
|
'type' => '', |
80
|
|
|
'subtype' => '', |
81
|
|
|
'size' => 0, |
82
|
|
|
'content_source' => '', |
83
|
|
|
'content' => '', |
84
|
|
|
]; |
85
|
|
|
|
86
|
|
|
// handles options |
87
|
1 |
|
$optimize = (bool) $this->config->get('assets.images.optimize.enabled'); |
88
|
1 |
|
$fingerprint = (bool) $this->config->get('assets.fingerprint.enabled'); |
89
|
1 |
|
$minify = (bool) $this->config->get('assets.minify.enabled'); |
90
|
1 |
|
$filename = ''; |
91
|
1 |
|
$ignore_missing = false; |
92
|
1 |
|
extract(is_array($options) ? $options : [], EXTR_IF_EXISTS); |
93
|
1 |
|
$this->ignore_missing = $ignore_missing; |
94
|
|
|
|
95
|
|
|
// fill data array with file(s) informations |
96
|
1 |
|
$cache = new Cache($this->builder, 'assets'); |
97
|
1 |
|
$cacheKey = sprintf('%s.ser', implode('_', $paths)); |
98
|
1 |
|
if (!$cache->has($cacheKey)) { |
99
|
1 |
|
$pathsCount = count($paths); |
100
|
1 |
|
$file = []; |
101
|
1 |
|
for ($i = 0; $i < $pathsCount; $i++) { |
102
|
|
|
// loads file(s) |
103
|
1 |
|
$file[$i] = $this->loadFile($paths[$i], $ignore_missing); |
104
|
|
|
// bundle: same type/ext only |
105
|
1 |
|
if ($i > 0) { |
106
|
1 |
|
if ($file[$i]['type'] != $file[$i - 1]['type']) { |
107
|
|
|
throw new Exception(\sprintf('Asset bundle type error (%s != %s).', $file[$i]['type'], $file[$i - 1]['type'])); |
108
|
|
|
} |
109
|
1 |
|
if ($file[$i]['ext'] != $file[$i - 1]['ext']) { |
110
|
|
|
throw new Exception(\sprintf('Asset bundle extension error (%s != %s).', $file[$i]['ext'], $file[$i - 1]['ext'])); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
// missing allowed = empty path |
114
|
1 |
|
if ($file[$i]['missing']) { |
115
|
1 |
|
$this->data['path'] = ''; |
116
|
|
|
|
117
|
1 |
|
continue; |
118
|
|
|
} |
119
|
|
|
// set data |
120
|
1 |
|
if ($i == 0) { |
121
|
1 |
|
$this->data['file'] = $file[$i]['filepath']; // should be an array of files in case of bundle? |
122
|
1 |
|
$this->data['filename'] = $file[$i]['path']; |
123
|
1 |
|
$this->data['path_source'] = $file[$i]['path']; |
124
|
1 |
|
$this->data['path'] = $file[$i]['path']; |
125
|
1 |
|
if (!empty($filename)) { |
126
|
|
|
$this->data['path'] = '/'.ltrim($filename, '/'); |
127
|
|
|
} |
128
|
1 |
|
$this->data['ext'] = $file[$i]['ext']; |
129
|
1 |
|
$this->data['type'] = $file[$i]['type']; |
130
|
1 |
|
$this->data['subtype'] = $file[$i]['subtype']; |
131
|
|
|
} |
132
|
1 |
|
$this->data['size'] += $file[$i]['size']; |
133
|
1 |
|
$this->data['content_source'] .= $file[$i]['content']; |
134
|
1 |
|
$this->data['content'] .= $file[$i]['content']; |
135
|
|
|
} |
136
|
|
|
// bundle: define path |
137
|
1 |
|
if ($pathsCount > 1) { |
138
|
1 |
|
if (empty($filename)) { |
139
|
1 |
|
switch ($this->data['ext']) { |
140
|
1 |
|
case 'scss': |
141
|
1 |
|
case 'css': |
142
|
1 |
|
$this->data['path'] = '/styles.'.$file[0]['ext']; |
143
|
1 |
|
break; |
144
|
|
|
case 'js': |
145
|
|
|
$this->data['path'] = '/scripts.'.$file[0]['ext']; |
146
|
|
|
break; |
147
|
|
|
default: |
148
|
|
|
throw new Exception(\sprintf('Asset bundle supports "%s" files only.', 'scss, css and js')); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
1 |
|
$cache->set($cacheKey, $this->data); |
153
|
|
|
} |
154
|
1 |
|
$this->data = $cache->get($cacheKey); |
155
|
|
|
|
156
|
|
|
// optimizing |
157
|
1 |
|
if ($optimize) { |
158
|
|
|
$this->optimize(); |
159
|
|
|
} |
160
|
|
|
// fingerprinting |
161
|
1 |
|
if ($fingerprint) { |
162
|
1 |
|
$this->fingerprint(); |
163
|
|
|
} |
164
|
|
|
// compiling |
165
|
1 |
|
if ((bool) $this->config->get('assets.compile.enabled')) { |
166
|
1 |
|
$this->compile(); |
167
|
|
|
} |
168
|
|
|
// minifying |
169
|
1 |
|
if ($minify) { |
170
|
1 |
|
$this->minify(); |
171
|
|
|
} |
172
|
1 |
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Returns path. |
176
|
|
|
*/ |
177
|
1 |
|
public function __toString(): string |
178
|
|
|
{ |
179
|
|
|
try { |
180
|
1 |
|
$this->save(); |
181
|
|
|
} catch (Exception $e) { |
182
|
|
|
$this->builder->getLogger()->error($e->getMessage()); |
183
|
|
|
} |
184
|
|
|
|
185
|
1 |
|
return $this->data['path']; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Fingerprints a file. |
190
|
|
|
*/ |
191
|
1 |
|
public function fingerprint(): self |
192
|
|
|
{ |
193
|
1 |
|
if ($this->fingerprinted) { |
194
|
|
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
1 |
|
$fingerprint = hash('md5', $this->data['content_source']); |
198
|
1 |
|
$this->data['path'] = preg_replace( |
199
|
1 |
|
'/\.'.$this->data['ext'].'$/m', |
200
|
1 |
|
".$fingerprint.".$this->data['ext'], |
201
|
1 |
|
$this->data['path'] |
202
|
|
|
); |
203
|
|
|
|
204
|
1 |
|
$this->fingerprinted = true; |
205
|
|
|
|
206
|
1 |
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Compiles a SCSS. |
211
|
|
|
*/ |
212
|
1 |
|
public function compile(): self |
213
|
|
|
{ |
214
|
1 |
|
if ($this->compiled) { |
215
|
1 |
|
return $this; |
216
|
|
|
} |
217
|
|
|
|
218
|
1 |
|
if ($this->data['ext'] != 'scss') { |
219
|
1 |
|
return $this; |
220
|
|
|
} |
221
|
|
|
|
222
|
1 |
|
$cache = new Cache($this->builder, 'assets'); |
223
|
1 |
|
$cacheKey = $cache->createKeyFromAsset($this, 'compiled'); |
224
|
1 |
|
if (!$cache->has($cacheKey)) { |
225
|
1 |
|
$scssPhp = new Compiler(); |
226
|
1 |
|
$importDir = []; |
227
|
1 |
|
$importDir[] = Util::joinPath($this->config->getStaticPath()); |
228
|
1 |
|
$importDir[] = Util::joinPath($this->config->getAssetsPath()); |
229
|
1 |
|
$scssDir = $this->config->get('assets.compile.import') ?? []; |
230
|
1 |
|
$themes = $this->config->getTheme() ?? []; |
231
|
1 |
|
foreach ($scssDir as $dir) { |
232
|
1 |
|
$importDir[] = Util::joinPath($this->config->getStaticPath(), $dir); |
233
|
1 |
|
$importDir[] = Util::joinPath($this->config->getAssetsPath(), $dir); |
234
|
1 |
|
$importDir[] = Util::joinPath(dirname($this->data['file']), $dir); |
235
|
1 |
|
foreach ($themes as $theme) { |
236
|
1 |
|
$importDir[] = Util::joinPath($this->config->getThemeDirPath($theme, "static/$dir")); |
237
|
1 |
|
$importDir[] = Util::joinPath($this->config->getThemeDirPath($theme, "assets/$dir")); |
238
|
|
|
} |
239
|
|
|
} |
240
|
1 |
|
$scssPhp->setImportPaths(array_unique($importDir)); |
241
|
|
|
// source map |
242
|
1 |
|
if ($this->builder->isDebug() && (bool) $this->config->get('assets.compile.sourcemap')) { |
243
|
|
|
$importDir = []; |
244
|
|
|
$assetDir = (string) $this->config->get('assets.dir'); |
245
|
|
|
$assetDirPos = strrpos($this->data['file'], DIRECTORY_SEPARATOR.$assetDir.DIRECTORY_SEPARATOR); |
246
|
|
|
$fileRelPath = substr($this->data['file'], $assetDirPos + 8); |
247
|
|
|
$filePath = Util::joinFile($this->config->getOutputPath(), $fileRelPath); |
248
|
|
|
$importDir[] = dirname($filePath); |
249
|
|
|
foreach ($scssDir as $dir) { |
250
|
|
|
$importDir[] = Util::joinFile($this->config->getOutputPath(), $dir); |
251
|
|
|
} |
252
|
|
|
$scssPhp->setImportPaths(array_unique($importDir)); |
253
|
|
|
$scssPhp->setSourceMap(Compiler::SOURCE_MAP_INLINE); |
254
|
|
|
$scssPhp->setSourceMapOptions([ |
255
|
|
|
'sourceMapBasepath' => Util::joinPath($this->config->getOutputPath()), |
256
|
|
|
'sourceRoot' => '/', |
257
|
|
|
]); |
258
|
|
|
} |
259
|
|
|
// output style |
260
|
1 |
|
$outputStyles = ['expanded', 'compressed']; |
261
|
1 |
|
$outputStyle = strtolower((string) $this->config->get('assets.compile.style')); |
262
|
1 |
|
if (!in_array($outputStyle, $outputStyles)) { |
263
|
|
|
throw new Exception(\sprintf('Scss output style "%s" doesn\'t exists.', $outputStyle)); |
264
|
|
|
} |
265
|
1 |
|
$scssPhp->setOutputStyle($outputStyle); |
266
|
|
|
// variables |
267
|
1 |
|
$variables = $this->config->get('assets.compile.variables') ?? []; |
268
|
1 |
|
if (!empty($variables)) { |
269
|
1 |
|
$variables = array_map('ScssPhp\ScssPhp\ValueConverter::parseValue', $variables); |
270
|
1 |
|
$scssPhp->replaceVariables($variables); |
271
|
|
|
} |
272
|
|
|
// update data |
273
|
1 |
|
$this->data['path'] = preg_replace('/sass|scss/m', 'css', $this->data['path']); |
274
|
1 |
|
$this->data['ext'] = 'css'; |
275
|
1 |
|
$this->data['content'] = $scssPhp->compileString($this->data['content'])->getCss(); |
276
|
1 |
|
$this->compiled = true; |
277
|
1 |
|
$cache->set($cacheKey, $this->data); |
278
|
|
|
} |
279
|
1 |
|
$this->data = $cache->get($cacheKey); |
280
|
|
|
|
281
|
1 |
|
return $this; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Minifying a CSS or a JS. |
286
|
|
|
*/ |
287
|
1 |
|
public function minify(): self |
288
|
|
|
{ |
289
|
|
|
// disable minify to preserve inline source map |
290
|
1 |
|
if ($this->builder->isDebug() && (bool) $this->config->get('assets.compile.sourcemap')) { |
291
|
|
|
return $this; |
292
|
|
|
} |
293
|
|
|
|
294
|
1 |
|
if ($this->minified) { |
295
|
|
|
return $this; |
296
|
|
|
} |
297
|
|
|
|
298
|
1 |
|
if ($this->data['ext'] == 'scss') { |
299
|
|
|
$this->compile(); |
300
|
|
|
} |
301
|
|
|
|
302
|
1 |
|
if ($this->data['ext'] != 'css' && $this->data['ext'] != 'js') { |
303
|
|
|
return $this; |
304
|
|
|
} |
305
|
|
|
|
306
|
1 |
|
if (substr($this->data['path'], -8) == '.min.css' || substr($this->data['path'], -7) == '.min.js') { |
307
|
|
|
$this->minified; |
308
|
|
|
|
309
|
|
|
return $this; |
310
|
|
|
} |
311
|
|
|
|
312
|
1 |
|
$cache = new Cache($this->builder, 'assets'); |
313
|
1 |
|
$cacheKey = $cache->createKeyFromAsset($this, 'minified'); |
314
|
1 |
|
if (!$cache->has($cacheKey)) { |
315
|
1 |
|
switch ($this->data['ext']) { |
316
|
1 |
|
case 'css': |
317
|
1 |
|
$minifier = new Minify\CSS($this->data['content']); |
318
|
1 |
|
break; |
319
|
1 |
|
case 'js': |
320
|
1 |
|
$minifier = new Minify\JS($this->data['content']); |
321
|
1 |
|
break; |
322
|
|
|
default: |
323
|
|
|
throw new Exception(sprintf('Not able to minify "%s"', $this->data['path'])); |
324
|
|
|
} |
325
|
1 |
|
$this->data['path'] = preg_replace( |
326
|
1 |
|
'/\.'.$this->data['ext'].'$/m', |
327
|
1 |
|
'.min.'.$this->data['ext'], |
328
|
1 |
|
$this->data['path'] |
329
|
|
|
); |
330
|
1 |
|
$this->data['content'] = $minifier->minify(); |
331
|
1 |
|
$this->minified = true; |
332
|
1 |
|
$cache->set($cacheKey, $this->data); |
333
|
|
|
} |
334
|
1 |
|
$this->data = $cache->get($cacheKey); |
335
|
|
|
|
336
|
1 |
|
return $this; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Optimizing an image. |
341
|
|
|
*/ |
342
|
|
|
public function optimize(): self |
343
|
|
|
{ |
344
|
|
|
if ($this->optimized) { |
345
|
|
|
return $this; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
if ($this->data['type'] != 'image') { |
349
|
|
|
return $this; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
$cache = new Cache($this->builder, 'assets'); |
353
|
|
|
$cacheKey = $cache->createKeyFromAsset($this, 'optimized'); |
354
|
|
|
if (!$cache->has($cacheKey)) { |
355
|
|
|
$message = $this->data['file']; |
356
|
|
|
$sizeBefore = filesize($this->data['file']); |
357
|
|
|
Util\File::getFS()->copy($this->data['file'], Util::joinFile($this->config->getCachePath(), 'tmp', $this->data['filename'])); |
358
|
|
|
Image::optimizer($this->config->get('assets.images.quality') ?? 85)->optimize( |
359
|
|
|
$this->data['file'], |
360
|
|
|
Util::joinFile($this->config->getCachePath(), 'tmp', $this->data['filename']) |
361
|
|
|
); |
362
|
|
|
$sizeAfter = filesize(Util::joinFile($this->config->getCachePath(), 'tmp', $this->data['filename'])); |
363
|
|
|
if ($sizeAfter < $sizeBefore) { |
364
|
|
|
$message = \sprintf( |
365
|
|
|
'%s (%s Ko -> %s Ko)', |
366
|
|
|
$message, |
367
|
|
|
ceil($sizeBefore / 1000), |
368
|
|
|
ceil($sizeAfter / 1000) |
369
|
|
|
); |
370
|
|
|
} |
371
|
|
|
$this->data['content'] = Util\File::fileGetContents(Util::joinFile($this->config->getCachePath(), 'tmp', $this->data['filename'])); |
372
|
|
|
Util\File::getFS()->remove(Util::joinFile($this->config->getCachePath(), 'tmp')); |
373
|
|
|
$this->optimized = true; |
374
|
|
|
$cache->set($cacheKey, $this->data); |
375
|
|
|
$this->builder->getLogger()->debug(\sprintf('Optimize "%s"', $message)); |
376
|
|
|
} |
377
|
|
|
$this->data = $cache->get($cacheKey); |
378
|
|
|
|
379
|
|
|
return $this; |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* Resizes an image. |
384
|
|
|
*/ |
385
|
1 |
|
public function resize(int $size): self |
386
|
|
|
{ |
387
|
1 |
|
if ($size >= $this->getWidth()) { |
388
|
|
|
return $this; |
389
|
|
|
} |
390
|
|
|
|
391
|
1 |
|
$cache = new Cache($this->builder, 'assets'); |
392
|
1 |
|
$cacheKey = $cache->createKeyFromAsset($this, "{$size}x"); |
393
|
1 |
|
if (!$cache->has($cacheKey)) { |
394
|
1 |
|
if ($this->data['type'] !== 'image') { |
395
|
|
|
throw new Exception(sprintf('Not able to resize "%s"', $this->data['path'])); |
396
|
|
|
} |
397
|
1 |
|
if (!extension_loaded('gd')) { |
398
|
|
|
throw new Exception('GD extension is required to use images resize.'); |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
try { |
402
|
1 |
|
$img = ImageManager::make($this->data['content_source']); |
403
|
|
|
$img->resize($size, null, function (\Intervention\Image\Constraint $constraint) { |
404
|
1 |
|
$constraint->aspectRatio(); |
405
|
1 |
|
$constraint->upsize(); |
406
|
1 |
|
}); |
407
|
|
|
} catch (\Exception $e) { |
408
|
|
|
throw new Exception(sprintf('Not able to resize image "%s": %s', $this->data['path'], $e->getMessage())); |
409
|
|
|
} |
410
|
1 |
|
$this->data['path'] = '/'.Util::joinPath((string) $this->config->get('assets.target'), 'thumbnails', (string) $size, $this->data['path']); |
411
|
|
|
|
412
|
|
|
try { |
413
|
1 |
|
$this->data['content'] = (string) $img->encode($this->data['ext'], $this->config->get('assets.images.quality')); |
414
|
|
|
} catch (\Exception $e) { |
415
|
|
|
throw new Exception(sprintf('Not able to encode image "%s": %s', $this->data['path'], $e->getMessage())); |
416
|
|
|
} |
417
|
|
|
|
418
|
1 |
|
$cache->set($cacheKey, $this->data); |
419
|
|
|
} |
420
|
1 |
|
$this->data = $cache->get($cacheKey); |
421
|
|
|
|
422
|
1 |
|
return $this; |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
/** |
426
|
|
|
* Returns the data URL of an image. |
427
|
|
|
*/ |
428
|
|
|
public function dataurl(): string |
429
|
|
|
{ |
430
|
|
|
if ($this->data['type'] !== 'image') { |
431
|
|
|
throw new Exception(sprintf('Can\'t get data URL of "%s"', $this->data['path'])); |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
return (string) ImageManager::make($this->data['content'])->encode('data-url', $this->config->get('assets.images.quality')); |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
/** |
438
|
|
|
* Implements \ArrayAccess. |
439
|
|
|
*/ |
440
|
|
|
public function offsetSet($offset, $value) |
441
|
|
|
{ |
442
|
|
|
if (!is_null($offset)) { |
443
|
|
|
$this->data[$offset] = $value; |
444
|
|
|
} |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
/** |
448
|
|
|
* Implements \ArrayAccess. |
449
|
|
|
*/ |
450
|
1 |
|
public function offsetExists($offset) |
451
|
|
|
{ |
452
|
1 |
|
return isset($this->data[$offset]); |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
/** |
456
|
|
|
* Implements \ArrayAccess. |
457
|
|
|
*/ |
458
|
|
|
public function offsetUnset($offset) |
459
|
|
|
{ |
460
|
|
|
unset($this->data[$offset]); |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
/** |
464
|
|
|
* Implements \ArrayAccess. |
465
|
|
|
*/ |
466
|
1 |
|
public function offsetGet($offset) |
467
|
|
|
{ |
468
|
1 |
|
return isset($this->data[$offset]) ? $this->data[$offset] : null; |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
/** |
472
|
|
|
* Hashing content of an asset with the specified algo, sha384 by default. |
473
|
|
|
* Used for SRI (Subresource Integrity). |
474
|
|
|
* |
475
|
|
|
* @see https://developer.mozilla.org/fr/docs/Web/Security/Subresource_Integrity |
476
|
|
|
*/ |
477
|
1 |
|
public function getIntegrity(string $algo = 'sha384'): string |
478
|
|
|
{ |
479
|
1 |
|
return \sprintf('%s-%s', $algo, base64_encode(hash($algo, $this->data['content'], true))); |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
/** |
483
|
|
|
* Returns the width of an image. |
484
|
|
|
* |
485
|
|
|
* @return false|int |
486
|
|
|
*/ |
487
|
1 |
|
public function getWidth() |
488
|
|
|
{ |
489
|
1 |
|
if (false === $size = $this->getImageSize()) { |
490
|
|
|
return false; |
491
|
|
|
} |
492
|
|
|
|
493
|
1 |
|
return $size[0]; |
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
/** |
497
|
|
|
* Returns the height of an image. |
498
|
|
|
* |
499
|
|
|
* @return false|int |
500
|
|
|
*/ |
501
|
1 |
|
public function getHeight() |
502
|
|
|
{ |
503
|
1 |
|
if (false === $size = $this->getImageSize()) { |
504
|
|
|
return false; |
505
|
|
|
} |
506
|
|
|
|
507
|
1 |
|
return $size[1]; |
508
|
|
|
} |
509
|
|
|
|
510
|
|
|
/** |
511
|
|
|
* Returns MP3 file infos. |
512
|
|
|
* |
513
|
|
|
* @see https://github.com/wapmorgan/Mp3Info |
514
|
|
|
*/ |
515
|
|
|
public function getAudio(): Mp3Info |
516
|
|
|
{ |
517
|
|
|
return new Mp3Info($this->data['file']); |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
/** |
521
|
|
|
* Saves file. |
522
|
|
|
* Note: a file from `static/` with the same name will NOT be overridden. |
523
|
|
|
* |
524
|
|
|
* @throws Exception |
525
|
|
|
*/ |
526
|
1 |
|
public function save(): void |
527
|
|
|
{ |
528
|
1 |
|
$filepath = Util::joinFile($this->config->getOutputPath(), $this->data['path']); |
529
|
1 |
|
if (!$this->builder->getBuildOptions()['dry-run'] && !Util\File::getFS()->exists($filepath)) { |
530
|
|
|
try { |
531
|
1 |
|
Util\File::getFS()->dumpFile($filepath, $this->data['content']); |
532
|
1 |
|
$this->builder->getLogger()->debug(\sprintf('Save asset "%s"', $this->data['path'])); |
533
|
|
|
} catch (\Symfony\Component\Filesystem\Exception\IOException $e) { |
534
|
|
|
if (!$this->ignore_missing) { |
535
|
|
|
throw new Exception(\sprintf('Can\'t save asset "%s"', $this->data['path'])); |
536
|
|
|
} |
537
|
|
|
} |
538
|
|
|
} |
539
|
1 |
|
} |
540
|
|
|
|
541
|
|
|
/** |
542
|
|
|
* Load file data. |
543
|
|
|
*/ |
544
|
1 |
|
private function loadFile(string $path, bool $ignore_missing = false): array |
545
|
|
|
{ |
546
|
1 |
|
$file = []; |
547
|
|
|
|
548
|
1 |
|
if (false === $filePath = $this->findFile($path)) { |
549
|
1 |
|
if ($ignore_missing) { |
550
|
1 |
|
$file['missing'] = true; |
551
|
|
|
|
552
|
1 |
|
return $file; |
553
|
|
|
} |
554
|
|
|
|
555
|
1 |
|
throw new Exception(sprintf('Asset file "%s" doesn\'t exist.', $path)); |
556
|
|
|
} |
557
|
|
|
|
558
|
1 |
|
if (Util\Url::isUrl($path)) { |
559
|
1 |
|
$urlHost = parse_url($path, PHP_URL_HOST); |
560
|
1 |
|
$urlPath = parse_url($path, PHP_URL_PATH); |
561
|
1 |
|
$urlQuery = parse_url($path, PHP_URL_QUERY); |
562
|
1 |
|
$path = Util::joinPath((string) $this->config->get('assets.target'), $urlHost, $urlPath); |
563
|
1 |
|
if (!empty($urlQuery)) { |
564
|
1 |
|
$path = Util::joinPath($path, Page::slugify($urlQuery)); |
565
|
|
|
// Google Fonts hack |
566
|
1 |
|
if (strpos($urlPath, '/css') !== false) { |
567
|
1 |
|
$path .= '.css'; |
568
|
|
|
} |
569
|
|
|
} |
570
|
|
|
} |
571
|
1 |
|
$path = '/'.ltrim($path, '/'); |
572
|
|
|
|
573
|
1 |
|
$pathinfo = pathinfo($path); |
574
|
1 |
|
list($type, $subtype) = Util\File::getMimeType($filePath); |
575
|
1 |
|
$content = Util\File::fileGetContents($filePath); |
576
|
|
|
|
577
|
1 |
|
$file['filepath'] = $filePath; |
578
|
1 |
|
$file['path'] = $path; |
579
|
1 |
|
$file['ext'] = $pathinfo['extension']; |
580
|
1 |
|
$file['type'] = $type; |
581
|
1 |
|
$file['subtype'] = $subtype; |
582
|
1 |
|
$file['size'] = filesize($filePath); |
583
|
1 |
|
$file['content'] = $content; |
584
|
1 |
|
$file['missing'] = false; |
585
|
|
|
|
586
|
1 |
|
return $file; |
587
|
|
|
} |
588
|
|
|
|
589
|
|
|
/** |
590
|
|
|
* Try to find the file: |
591
|
|
|
* 1. remote (if $path is a valid URL) |
592
|
|
|
* 2. in static/ |
593
|
|
|
* 3. in themes/<theme>/static/ |
594
|
|
|
* Returns local file path or false if file don't exists. |
595
|
|
|
* |
596
|
|
|
* @return string|false |
597
|
|
|
*/ |
598
|
1 |
|
private function findFile(string $path) |
599
|
|
|
{ |
600
|
|
|
// in case of remote file: save it and returns cached file path |
601
|
1 |
|
if (Util\Url::isUrl($path)) { |
602
|
1 |
|
$url = $path; |
603
|
1 |
|
$relativePath = Page::slugify(sprintf('%s%s-%s', parse_url($url, PHP_URL_HOST), parse_url($url, PHP_URL_PATH), parse_url($url, PHP_URL_QUERY))); |
604
|
1 |
|
$filePath = Util::joinFile($this->config->getCacheAssetsPath(), $relativePath); |
605
|
1 |
|
if (!file_exists($filePath)) { |
606
|
1 |
|
if (!Util\Url::isRemoteFileExists($url)) { |
607
|
1 |
|
return false; |
608
|
|
|
} |
609
|
1 |
|
if (false === $content = Util\File::fileGetContents($url, true)) { |
610
|
|
|
return false; |
611
|
|
|
} |
612
|
1 |
|
if (strlen($content) <= 1) { |
613
|
|
|
throw new Exception(sprintf('Asset at "%s" is empty.', $url)); |
614
|
|
|
} |
615
|
1 |
|
Util\File::getFS()->dumpFile($filePath, $content); |
616
|
|
|
} |
617
|
|
|
|
618
|
1 |
|
return $filePath; |
619
|
|
|
} |
620
|
|
|
|
621
|
|
|
// checks in assets/ |
622
|
1 |
|
$filePath = Util::joinFile($this->config->getAssetsPath(), $path); |
623
|
1 |
|
if (Util\File::getFS()->exists($filePath)) { |
624
|
|
|
return $filePath; |
625
|
|
|
} |
626
|
|
|
|
627
|
|
|
// checks in each themes/<theme>/assets/ |
628
|
1 |
|
foreach ($this->config->getTheme() as $theme) { |
629
|
1 |
|
$filePath = Util::joinFile($this->config->getThemeDirPath($theme, 'assets'), $path); |
630
|
1 |
|
if (Util\File::getFS()->exists($filePath)) { |
631
|
1 |
|
return $filePath; |
632
|
|
|
} |
633
|
|
|
} |
634
|
|
|
|
635
|
|
|
// checks in static/ |
636
|
1 |
|
$filePath = Util::joinFile($this->config->getStaticPath(), $path); |
637
|
1 |
|
if (Util\File::getFS()->exists($filePath)) { |
638
|
1 |
|
return $filePath; |
639
|
|
|
} |
640
|
|
|
|
641
|
|
|
// checks in each themes/<theme>/static/ |
642
|
1 |
|
foreach ($this->config->getTheme() as $theme) { |
643
|
1 |
|
$filePath = Util::joinFile($this->config->getThemeDirPath($theme, 'static'), $path); |
644
|
1 |
|
if (Util\File::getFS()->exists($filePath)) { |
645
|
1 |
|
return $filePath; |
646
|
|
|
} |
647
|
|
|
} |
648
|
|
|
|
649
|
1 |
|
return false; |
650
|
|
|
} |
651
|
|
|
|
652
|
|
|
/** |
653
|
|
|
* Returns image size informations. |
654
|
|
|
* |
655
|
|
|
* @see https://www.php.net/manual/function.getimagesize.php |
656
|
|
|
* |
657
|
|
|
* @return false|array |
658
|
|
|
*/ |
659
|
1 |
|
private function getImageSize() |
660
|
|
|
{ |
661
|
1 |
|
if (!$this->data['type'] == 'image') { |
662
|
|
|
return false; |
663
|
|
|
} |
664
|
|
|
|
665
|
1 |
|
if (false === $size = getimagesizefromstring($this->data['content'])) { |
666
|
|
|
return false; |
667
|
|
|
} |
668
|
|
|
|
669
|
1 |
|
return $size; |
670
|
|
|
} |
671
|
|
|
} |
672
|
|
|
|