|
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\Renderer\Twig; |
|
12
|
|
|
|
|
13
|
|
|
use Cecil\Assets\Image; |
|
14
|
|
|
use Cecil\Builder; |
|
15
|
|
|
use Cecil\Collection\CollectionInterface; |
|
16
|
|
|
use Cecil\Collection\Page\Collection as PagesCollection; |
|
17
|
|
|
use Cecil\Collection\Page\Page; |
|
18
|
|
|
use Cecil\Config; |
|
19
|
|
|
use Cecil\Exception\Exception; |
|
20
|
|
|
use Cecil\Util; |
|
21
|
|
|
use Cocur\Slugify\Bridge\Twig\SlugifyExtension; |
|
22
|
|
|
use Cocur\Slugify\Slugify; |
|
23
|
|
|
use MatthiasMullie\Minify; |
|
24
|
|
|
use ScssPhp\ScssPhp\Compiler; |
|
25
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Class Twig\Extension. |
|
29
|
|
|
*/ |
|
30
|
|
|
class Extension extends SlugifyExtension |
|
31
|
|
|
{ |
|
32
|
|
|
/** @var Builder */ |
|
33
|
|
|
protected $builder; |
|
34
|
|
|
/** @var Config */ |
|
35
|
|
|
protected $config; |
|
36
|
|
|
/** @var string */ |
|
37
|
|
|
protected $outputPath; |
|
38
|
|
|
/** @var Filesystem */ |
|
39
|
|
|
protected $fileSystem; |
|
40
|
|
|
/** @var Slugify */ |
|
41
|
|
|
private static $slugifier; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param Builder $builder |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct(Builder $builder) |
|
47
|
|
|
{ |
|
48
|
|
|
if (!self::$slugifier instanceof Slugify) { |
|
49
|
|
|
self::$slugifier = Slugify::create(['regexp' => Page::SLUGIFY_PATTERN]); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
parent::__construct(self::$slugifier); |
|
53
|
|
|
|
|
54
|
|
|
$this->builder = $builder; |
|
55
|
|
|
$this->config = $this->builder->getConfig(); |
|
56
|
|
|
$this->outputPath = $this->config->getOutputPath(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* {@inheritdoc} |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getName() |
|
63
|
|
|
{ |
|
64
|
|
|
return 'cecil'; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* {@inheritdoc} |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getFilters() |
|
71
|
|
|
{ |
|
72
|
|
|
return [ |
|
73
|
|
|
new \Twig\TwigFilter('filterBySection', [$this, 'filterBySection']), |
|
74
|
|
|
new \Twig\TwigFilter('filterBy', [$this, 'filterBy']), |
|
75
|
|
|
new \Twig\TwigFilter('sortByTitle', [$this, 'sortByTitle']), |
|
76
|
|
|
new \Twig\TwigFilter('sortByWeight', [$this, 'sortByWeight']), |
|
77
|
|
|
new \Twig\TwigFilter('sortByDate', [$this, 'sortByDate']), |
|
78
|
|
|
new \Twig\TwigFilter('urlize', [$this, 'slugifyFilter']), |
|
79
|
|
|
new \Twig\TwigFilter('minifyCSS', [$this, 'minifyCss']), |
|
80
|
|
|
new \Twig\TwigFilter('minifyJS', [$this, 'minifyJs']), |
|
81
|
|
|
new \Twig\TwigFilter('SCSStoCSS', [$this, 'scssToCss']), |
|
82
|
|
|
new \Twig\TwigFilter('excerpt', [$this, 'excerpt']), |
|
83
|
|
|
new \Twig\TwigFilter('excerptHtml', [$this, 'excerptHtml']), |
|
84
|
|
|
new \Twig\TwigFilter('resize', [$this, 'resize']), |
|
85
|
|
|
]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* {@inheritdoc} |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getFunctions() |
|
92
|
|
|
{ |
|
93
|
|
|
return [ |
|
94
|
|
|
new \Twig\TwigFunction('url', [$this, 'createUrl']), |
|
95
|
|
|
new \Twig\TwigFunction('minify', [$this, 'minify']), |
|
96
|
|
|
new \Twig\TwigFunction('readtime', [$this, 'readtime']), |
|
97
|
|
|
new \Twig\TwigFunction('toCSS', [$this, 'toCss']), |
|
98
|
|
|
new \Twig\TwigFunction('hash', [$this, 'hashFile']), |
|
99
|
|
|
new \Twig\TwigFunction('getenv', [$this, 'getEnv']), |
|
100
|
|
|
]; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Filter by section. |
|
105
|
|
|
* |
|
106
|
|
|
* @param PagesCollection $pages |
|
107
|
|
|
* @param string $section |
|
108
|
|
|
* |
|
109
|
|
|
* @return CollectionInterface |
|
110
|
|
|
*/ |
|
111
|
|
|
public function filterBySection(PagesCollection $pages, string $section): CollectionInterface |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->filterBy($pages, 'section', $section); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Filters by variable's name/value. |
|
118
|
|
|
* |
|
119
|
|
|
* @param PagesCollection $pages |
|
120
|
|
|
* @param string $variable |
|
121
|
|
|
* @param string $value |
|
122
|
|
|
* |
|
123
|
|
|
* @return CollectionInterface |
|
124
|
|
|
*/ |
|
125
|
|
|
public function filterBy(PagesCollection $pages, string $variable, string $value): CollectionInterface |
|
126
|
|
|
{ |
|
127
|
|
|
$filteredPages = $pages->filter(function (Page $page) use ($variable, $value) { |
|
128
|
|
|
$notVirtual = false; |
|
129
|
|
|
if (!$page->isVirtual()) { |
|
130
|
|
|
$notVirtual = true; |
|
131
|
|
|
} |
|
132
|
|
|
// is a dedicated getter exists? |
|
133
|
|
|
$method = 'get'.ucfirst($variable); |
|
134
|
|
|
if (method_exists($page, $method) && $page->$method() == $value) { |
|
135
|
|
|
return $notVirtual && true; |
|
136
|
|
|
} |
|
137
|
|
|
if ($page->getVariable($variable) == $value) { |
|
138
|
|
|
return $notVirtual && true; |
|
139
|
|
|
} |
|
140
|
|
|
}); |
|
141
|
|
|
|
|
142
|
|
|
return $filteredPages; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Sorts by title. |
|
147
|
|
|
* |
|
148
|
|
|
* @param \Traversable $collection |
|
149
|
|
|
* |
|
150
|
|
|
* @return array |
|
151
|
|
|
*/ |
|
152
|
|
|
public function sortByTitle(\Traversable $collection): array |
|
153
|
|
|
{ |
|
154
|
|
|
$collection = iterator_to_array($collection); |
|
155
|
|
|
array_multisort(array_keys($collection), SORT_NATURAL | SORT_FLAG_CASE, $collection); |
|
|
|
|
|
|
156
|
|
|
|
|
157
|
|
|
return $collection; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Sorts by weight. |
|
162
|
|
|
* |
|
163
|
|
|
* @param \Traversable $collection |
|
164
|
|
|
* |
|
165
|
|
|
* @return array |
|
166
|
|
|
*/ |
|
167
|
|
|
public function sortByWeight(\Traversable $collection): array |
|
168
|
|
|
{ |
|
169
|
|
|
$callback = function ($a, $b) { |
|
170
|
|
|
if (!isset($a['weight'])) { |
|
171
|
|
|
return 1; |
|
172
|
|
|
} |
|
173
|
|
|
if (!isset($b['weight'])) { |
|
174
|
|
|
return -1; |
|
175
|
|
|
} |
|
176
|
|
|
if ($a['weight'] == $b['weight']) { |
|
177
|
|
|
return 0; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
return ($a['weight'] < $b['weight']) ? -1 : 1; |
|
181
|
|
|
}; |
|
182
|
|
|
|
|
183
|
|
|
$collection = iterator_to_array($collection); |
|
184
|
|
|
usort($collection, $callback); |
|
185
|
|
|
|
|
186
|
|
|
return $collection; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Sorts by date. |
|
191
|
|
|
* |
|
192
|
|
|
* @param \Traversable $collection |
|
193
|
|
|
* |
|
194
|
|
|
* @return array |
|
195
|
|
|
*/ |
|
196
|
|
|
public function sortByDate(\Traversable $collection): array |
|
197
|
|
|
{ |
|
198
|
|
|
$callback = function ($a, $b) { |
|
199
|
|
|
if (!isset($a['date'])) { |
|
200
|
|
|
return -1; |
|
201
|
|
|
} |
|
202
|
|
|
if (!isset($b['date'])) { |
|
203
|
|
|
return 1; |
|
204
|
|
|
} |
|
205
|
|
|
if ($a['date'] == $b['date']) { |
|
206
|
|
|
return 0; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
return ($a['date'] > $b['date']) ? -1 : 1; |
|
210
|
|
|
}; |
|
211
|
|
|
|
|
212
|
|
|
$collection = iterator_to_array($collection); |
|
213
|
|
|
usort($collection, $callback); |
|
214
|
|
|
|
|
215
|
|
|
return $collection; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Creates an URL. |
|
220
|
|
|
* |
|
221
|
|
|
* $options[ |
|
222
|
|
|
* 'canonical' => null, |
|
223
|
|
|
* 'addhash' => true, |
|
224
|
|
|
* 'format' => 'json', |
|
225
|
|
|
* ]; |
|
226
|
|
|
* |
|
227
|
|
|
* @param Page|string|null $value |
|
228
|
|
|
* @param array|bool|null $options |
|
229
|
|
|
* |
|
230
|
|
|
* @return string|null |
|
231
|
|
|
*/ |
|
232
|
|
|
public function createUrl($value = null, $options = null): ?string |
|
233
|
|
|
{ |
|
234
|
|
|
$baseurl = (string) $this->config->get('baseurl'); |
|
235
|
|
|
$hash = md5((string) $this->config->get('time')); |
|
236
|
|
|
$base = ''; |
|
237
|
|
|
// handles options |
|
238
|
|
|
$canonical = null; |
|
239
|
|
|
$addhash = false; |
|
240
|
|
|
$format = null; |
|
241
|
|
|
extract(is_array($options) ? $options : []); |
|
242
|
|
|
|
|
243
|
|
|
// set baseurl |
|
244
|
|
|
if ((bool) $this->config->get('canonicalurl') || $canonical === true) { |
|
245
|
|
|
$base = rtrim($baseurl, '/'); |
|
246
|
|
|
} |
|
247
|
|
|
if ($canonical === false) { |
|
248
|
|
|
$base = ''; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
// value is a Page item |
|
252
|
|
|
if ($value instanceof Page) { |
|
253
|
|
|
if (!$format) { |
|
254
|
|
|
$format = $value->getVariable('output'); |
|
255
|
|
|
if (is_array($value->getVariable('output'))) { |
|
256
|
|
|
$format = $value->getVariable('output')[0]; |
|
257
|
|
|
} |
|
258
|
|
|
if (!$format) { |
|
259
|
|
|
$format = 'html'; |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
$url = $value->getUrl($format, $this->config); |
|
263
|
|
|
$url = $base.'/'.ltrim($url, '/'); |
|
264
|
|
|
|
|
265
|
|
|
return $url; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
// value is an external URL |
|
269
|
|
|
if ($value !== null) { |
|
270
|
|
|
if (Util::isExternalUrl($value)) { |
|
271
|
|
|
$url = $value; |
|
272
|
|
|
|
|
273
|
|
|
return $url; |
|
274
|
|
|
} |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
// value is a string |
|
278
|
|
|
if (!is_null($value)) { |
|
279
|
|
|
// value is an external URL |
|
280
|
|
|
if (Util::isExternalUrl($value)) { |
|
281
|
|
|
$url = $value; |
|
282
|
|
|
|
|
283
|
|
|
return $url; |
|
284
|
|
|
} |
|
285
|
|
|
$value = Util::joinPath($value); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
// value is a ressource URL (ie: 'path/style.css') |
|
289
|
|
|
if (false !== strpos($value, '.')) { |
|
290
|
|
|
$url = $value; |
|
291
|
|
|
if ($addhash) { |
|
292
|
|
|
$url .= '?'.$hash; |
|
293
|
|
|
} |
|
294
|
|
|
$url = $base.'/'.ltrim($url, '/'); |
|
295
|
|
|
|
|
296
|
|
|
return $url; |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
// others cases |
|
300
|
|
|
$url = $base.'/'; |
|
301
|
|
|
if (!empty($value) && $value != '/') { |
|
302
|
|
|
$url = $base.'/'.$value; |
|
303
|
|
|
|
|
304
|
|
|
// value is a page ID (ie: 'path/my-page') |
|
305
|
|
|
try { |
|
306
|
|
|
$pageId = $this->slugifyFilter($value); |
|
307
|
|
|
$page = $this->builder->getPages()->get($pageId); |
|
308
|
|
|
$url = $this->createUrl($page, $options); |
|
309
|
|
|
} catch (\DomainException $e) { |
|
310
|
|
|
// nothing to do |
|
311
|
|
|
} |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
return $url; |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
/** |
|
318
|
|
|
* Minifying a CSS or a JS file. |
|
319
|
|
|
* |
|
320
|
|
|
* ie: minify('css/style.css') |
|
321
|
|
|
* |
|
322
|
|
|
* @param string $path |
|
323
|
|
|
* |
|
324
|
|
|
* @throws Exception |
|
325
|
|
|
* |
|
326
|
|
|
* @return string |
|
327
|
|
|
*/ |
|
328
|
|
|
public function minify(string $path): string |
|
329
|
|
|
{ |
|
330
|
|
|
$filePath = $this->outputPath.'/'.$path; |
|
331
|
|
|
$fileInfo = new \SplFileInfo($filePath); |
|
332
|
|
|
$fileExtension = $fileInfo->getExtension(); |
|
333
|
|
|
// ie: minify('css/style.min.css') |
|
334
|
|
|
$pathMinified = \sprintf('%s.min.%s', substr($path, 0, -strlen(".$fileExtension")), $fileExtension); |
|
335
|
|
|
$filePathMinified = $this->outputPath.'/'.$pathMinified; |
|
336
|
|
|
if (is_file($filePathMinified)) { |
|
337
|
|
|
return $pathMinified; |
|
338
|
|
|
} |
|
339
|
|
|
if (is_file($filePath)) { |
|
340
|
|
|
switch ($fileExtension) { |
|
341
|
|
|
case 'css': |
|
342
|
|
|
$minifier = new Minify\CSS($filePath); |
|
343
|
|
|
break; |
|
344
|
|
|
case 'js': |
|
345
|
|
|
$minifier = new Minify\JS($filePath); |
|
346
|
|
|
break; |
|
347
|
|
|
default: |
|
348
|
|
|
throw new Exception(sprintf("File '%s' should be a '.css' or a '.js'!", $path)); |
|
349
|
|
|
} |
|
350
|
|
|
$minifier->minify($filePathMinified); |
|
351
|
|
|
|
|
352
|
|
|
return $pathMinified; |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
throw new Exception(sprintf("File '%s' doesn't exist!", $path)); |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
|
|
/** |
|
359
|
|
|
* Minifying CSS. |
|
360
|
|
|
* |
|
361
|
|
|
* @param string $value |
|
362
|
|
|
* |
|
363
|
|
|
* @return string |
|
364
|
|
|
*/ |
|
365
|
|
|
public function minifyCss(string $value): string |
|
366
|
|
|
{ |
|
367
|
|
|
$minifier = new Minify\CSS($value); |
|
368
|
|
|
|
|
369
|
|
|
return $minifier->minify(); |
|
370
|
|
|
} |
|
371
|
|
|
|
|
372
|
|
|
/** |
|
373
|
|
|
* Minifying JS. |
|
374
|
|
|
* |
|
375
|
|
|
* @param string $value |
|
376
|
|
|
* |
|
377
|
|
|
* @return string |
|
378
|
|
|
*/ |
|
379
|
|
|
public function minifyJs(string $value): string |
|
380
|
|
|
{ |
|
381
|
|
|
$minifier = new Minify\JS($value); |
|
382
|
|
|
|
|
383
|
|
|
return $minifier->minify(); |
|
384
|
|
|
} |
|
385
|
|
|
|
|
386
|
|
|
/** |
|
387
|
|
|
* Compiles style file to CSS. |
|
388
|
|
|
* |
|
389
|
|
|
* @param string $path |
|
390
|
|
|
* |
|
391
|
|
|
* @throws Exception |
|
392
|
|
|
* |
|
393
|
|
|
* @return string |
|
394
|
|
|
*/ |
|
395
|
|
|
public function toCss(string $path): string |
|
396
|
|
|
{ |
|
397
|
|
|
$filePath = $this->outputPath.'/'.$path; |
|
398
|
|
|
$subPath = substr($path, 0, strrpos($path, '/')); |
|
399
|
|
|
|
|
400
|
|
|
if (is_file($filePath)) { |
|
401
|
|
|
$fileExtension = (new \SplFileInfo($filePath))->getExtension(); |
|
402
|
|
|
switch ($fileExtension) { |
|
403
|
|
|
case 'scss': |
|
404
|
|
|
$scssPhp = new Compiler(); |
|
405
|
|
|
$scssPhp->setImportPaths($this->outputPath.'/'.$subPath); |
|
406
|
|
|
$targetPath = preg_replace('/scss/m', 'css', $path); |
|
407
|
|
|
|
|
408
|
|
|
// compiles if target file doesn't exists |
|
409
|
|
|
if (!Util::getFS()->exists($this->outputPath.'/'.$targetPath)) { |
|
410
|
|
|
$scss = file_get_contents($filePath); |
|
411
|
|
|
$css = $scssPhp->compile($scss); |
|
412
|
|
|
Util::getFS()->dumpFile($this->outputPath.'/'.$targetPath, $css); |
|
413
|
|
|
} |
|
414
|
|
|
|
|
415
|
|
|
return $targetPath; |
|
416
|
|
|
default: |
|
417
|
|
|
throw new Exception(sprintf("File '%s' should be a '.scss'!", $path)); |
|
418
|
|
|
} |
|
419
|
|
|
} |
|
420
|
|
|
|
|
421
|
|
|
throw new Exception(sprintf("File '%s' doesn't exist!", $path)); |
|
422
|
|
|
} |
|
423
|
|
|
|
|
424
|
|
|
/** |
|
425
|
|
|
* Compiles SCSS string to CSS. |
|
426
|
|
|
* |
|
427
|
|
|
* @param string $value |
|
428
|
|
|
* |
|
429
|
|
|
* @return string |
|
430
|
|
|
*/ |
|
431
|
|
|
public function scssToCss(string $value): string |
|
432
|
|
|
{ |
|
433
|
|
|
$scss = new Compiler(); |
|
434
|
|
|
|
|
435
|
|
|
return $scss->compile($value); |
|
436
|
|
|
} |
|
437
|
|
|
|
|
438
|
|
|
/** |
|
439
|
|
|
* Reads $length first characters of a string and adds a suffix. |
|
440
|
|
|
* |
|
441
|
|
|
* @param string|null $string |
|
442
|
|
|
* @param int $length |
|
443
|
|
|
* @param string $suffix |
|
444
|
|
|
* |
|
445
|
|
|
* @return string|null |
|
446
|
|
|
*/ |
|
447
|
|
|
public function excerpt(string $string = null, int $length = 450, string $suffix = ' …'): ?string |
|
448
|
|
|
{ |
|
449
|
|
|
$string = str_replace('</p>', '<br /><br />', $string); |
|
450
|
|
|
$string = trim(strip_tags($string, '<br>'), '<br />'); |
|
451
|
|
|
if (mb_strlen($string) > $length) { |
|
452
|
|
|
$string = mb_substr($string, 0, $length); |
|
453
|
|
|
$string .= $suffix; |
|
454
|
|
|
} |
|
455
|
|
|
|
|
456
|
|
|
return $string; |
|
457
|
|
|
} |
|
458
|
|
|
|
|
459
|
|
|
/** |
|
460
|
|
|
* Reads characters before '<!-- excerpt|break -->'. |
|
461
|
|
|
* |
|
462
|
|
|
* @param string|null $string |
|
463
|
|
|
* |
|
464
|
|
|
* @return string|null |
|
465
|
|
|
*/ |
|
466
|
|
|
public function excerptHtml(string $string = null): ?string |
|
467
|
|
|
{ |
|
468
|
|
|
// https://regex101.com/r/Xl7d5I/3 |
|
469
|
|
|
$pattern = '(.*)(<!--[[:blank:]]?(excerpt|break)[[:blank:]]?-->)(.*)'; |
|
470
|
|
|
preg_match('/'.$pattern.'/is', $string, $matches); |
|
471
|
|
|
if (empty($matches)) { |
|
472
|
|
|
return $string; |
|
473
|
|
|
} |
|
474
|
|
|
|
|
475
|
|
|
return trim($matches[1]); |
|
476
|
|
|
} |
|
477
|
|
|
|
|
478
|
|
|
/** |
|
479
|
|
|
* Calculates estimated time to read a text. |
|
480
|
|
|
* |
|
481
|
|
|
* @param string|null $text |
|
482
|
|
|
* |
|
483
|
|
|
* @return string |
|
484
|
|
|
*/ |
|
485
|
|
|
public function readtime(string $text = null): string |
|
486
|
|
|
{ |
|
487
|
|
|
$words = str_word_count(strip_tags($text)); |
|
488
|
|
|
$min = floor($words / 200); |
|
489
|
|
|
if ($min === 0) { |
|
490
|
|
|
return '1'; |
|
491
|
|
|
} |
|
492
|
|
|
|
|
493
|
|
|
return (string) $min; |
|
494
|
|
|
} |
|
495
|
|
|
|
|
496
|
|
|
/** |
|
497
|
|
|
* Hashing a file with sha384. |
|
498
|
|
|
* |
|
499
|
|
|
* Useful for SRI (Subresource Integrity). |
|
500
|
|
|
* |
|
501
|
|
|
* @see https://developer.mozilla.org/fr/docs/Web/Security/Subresource_Integrity |
|
502
|
|
|
* |
|
503
|
|
|
* @param string $path |
|
504
|
|
|
* |
|
505
|
|
|
* @return string|null |
|
506
|
|
|
*/ |
|
507
|
|
|
public function hashFile(string $path): ?string |
|
508
|
|
|
{ |
|
509
|
|
|
if (is_file($filePath = $this->outputPath.'/'.$path)) { |
|
510
|
|
|
$path = $filePath; |
|
511
|
|
|
} |
|
512
|
|
|
|
|
513
|
|
|
return sprintf('sha384-%s', base64_encode(hash_file('sha384', $path, true))); |
|
514
|
|
|
} |
|
515
|
|
|
|
|
516
|
|
|
/** |
|
517
|
|
|
* Gets the value of an environment variable. |
|
518
|
|
|
* |
|
519
|
|
|
* @param string $var |
|
520
|
|
|
* |
|
521
|
|
|
* @return string|null |
|
522
|
|
|
*/ |
|
523
|
|
|
public function getEnv(string $var): ?string |
|
524
|
|
|
{ |
|
525
|
|
|
return getenv($var) ?: null; |
|
526
|
|
|
} |
|
527
|
|
|
|
|
528
|
|
|
/** |
|
529
|
|
|
* Resizes an image. |
|
530
|
|
|
* |
|
531
|
|
|
* @param string $path Image path (relative from static/ dir or external). |
|
532
|
|
|
* @param int $size Image new size (width). |
|
533
|
|
|
* |
|
534
|
|
|
* @return string |
|
535
|
|
|
*/ |
|
536
|
|
|
public function resize(string $path, int $size): string |
|
537
|
|
|
{ |
|
538
|
|
|
return (new Image($this->builder))->resize($path, $size); |
|
539
|
|
|
} |
|
540
|
|
|
} |
|
541
|
|
|
|