• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Cecilapp / Cecil / 26636045955

29 May 2026 12:00PM UTC coverage: 82.309% (-0.001%) from 82.31%
26636045955

push

github

ArnaudLigny
refactor: use single Cache instance in Asset

Add a protected Cache $cache property and instantiate it in the Asset constructor, replacing multiple local new Cache(...) calls across the class. Update usages to call $this->cache for has/createKey/get/set/getContentFile, and pull assets.images.quality into a single $quality variable earlier. This centralizes cache handling and avoids repeatedly constructing Cache objects.

23 of 27 new or added lines in 1 file covered. (85.19%)

1 existing line in 1 file now uncovered.

3494 of 4245 relevant lines covered (82.31%)

0.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.04
/src/Asset.php
1
<?php
2

3
/**
4
 * This file is part of Cecil.
5
 *
6
 * (c) Arnaud Ligny <arnaud@ligny.fr>
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\Compiler as AssetCompiler;
17
use Cecil\Asset\Image;
18
use Cecil\Asset\Locator;
19
use Cecil\Asset\Optimizer as AssetOptimizer;
20
use Cecil\Builder;
21
use Cecil\Cache;
22
use Cecil\Config;
23
use Cecil\Exception\RuntimeException;
24
use Cecil\Url;
25
use Cecil\Util;
26
use wapmorgan\Mp3Info\Mp3Info;
27

28
/**
29
 * Asset class.
30
 *
31
 * Represents an asset (file) in the Cecil project.
32
 * Handles file locating, content reading, compiling, minifying, fingerprinting,
33
 * resizing images, and more.
34
 */
35
class Asset implements \ArrayAccess
36
{
37
    public const IMAGE_THUMB = 'thumbnails';
38

39
    /** @var Builder */
40
    protected $builder;
41

42
    /** @var Config */
43
    protected $config;
44

45
    protected Cache $cache;
46

47
    /** @var array */
48
    protected $data = [];
49

50
    /** @var array Cache tags */
51
    protected $cacheTags = [];
52

53
    /**
54
     * Creates an Asset from a file path, an array of files path or an URL.
55
     * Options:
56
     * [
57
     *     'filename' => <string>,
58
     *     'ignore_missing' => <bool>,
59
     *     'fingerprint' => <bool>,
60
     *     'minify' => <bool>,
61
     *     'optimize' => <bool>,
62
     *     'fallback' => <string>,
63
     *     'useragent' => <string>,
64
     *     'language' => <string|null>,
65
     * ]
66
     *
67
     * @param Builder      $builder
68
     * @param string|array $paths
69
     * @param array|null   $options
70
     *
71
     * @throws RuntimeException
72
     */
73
    public function __construct(Builder $builder, string|array $paths, array|null $options = null)
74
    {
75
        $this->builder = $builder;
1✔
76
        $this->config = $builder->getConfig();
1✔
77
        $this->cache = new Cache($this->builder, 'assets');
1✔
78
        $paths = \is_array($paths) ? $paths : [$paths];
1✔
79
        // checks path(s)
80
        array_walk($paths, function ($path) {
1✔
81
            // must be a string
82
            if (!\is_string($path)) {
1✔
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)) {
1✔
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) == '..') {
1✔
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
        });
1✔
94
        $this->data = [
1✔
95
            'file'     => '',    // absolute file path
1✔
96
            'files'    => [],    // array of absolute files path
1✔
97
            'missing'  => false, // if file not found but missing allowed: 'missing' is true
1✔
98
            '_path'    => '',    // original path
1✔
99
            'path'     => '',    // public path
1✔
100
            'url'      => null,  // URL if it's a remote file
1✔
101
            'ext'      => '',    // file extension
1✔
102
            'type'     => '',    // file type (e.g.: image, audio, video, etc.)
1✔
103
            'subtype'  => '',    // file media type (e.g.: image/png, audio/mp3, etc.)
1✔
104
            'size'     => 0,     // file size (in bytes)
1✔
105
            'width'    => null,  // width (in pixels)
1✔
106
            'height'   => null,  // height (in pixels)
1✔
107
            'exif'     => [],    // image exif data
1✔
108
            'duration' => null,  // audio or video duration
1✔
109
            'content'  => '',    // file content
1✔
110
            'hash'     => '',    // file content hash
1✔
111
        ];
1✔
112

113
        // handles options
114
        $options = array_merge(
1✔
115
            [
1✔
116
                'filename'       => '',
1✔
117
                'ignore_missing' => false,
1✔
118
                'fingerprint'    => $this->config->isEnabled('assets.fingerprint'),
1✔
119
                'minify'         => $this->config->isEnabled('assets.minify'),
1✔
120
                'optimize'       => $this->config->isEnabled('assets.images.optimize'),
1✔
121
                'fallback'       => '',
1✔
122
                'useragent'      => (string) $this->config->get('assets.remote.useragent.default'),
1✔
123
                'language'       => null,
1✔
124
            ],
1✔
125
            \is_array($options) ? $options : []
1✔
126
        );
1✔
127
        $language = null;
1✔
128
        if (isset($options['language']) && \is_scalar($options['language'])) {
1✔
129
            $language = (string) $options['language'];
1✔
130
            $language = $language === '' ? null : $language;
1✔
131
        }
132
        unset($options['language']);
1✔
133

134
        // cache for "locate file(s)"
135
        $locateCacheKey = \sprintf(
1✔
136
            '%s_locate%s__%s__%s',
1✔
137
            $options['filename'] ?: implode('_', $paths),
1✔
138
            $language ? '_' . $language : '',
1✔
139
            Builder::getBuildId(),
1✔
140
            Builder::getVersion()
1✔
141
        );
1✔
142

143
        // locate file(s) and get content
144
        if (!$this->cache->has($locateCacheKey)) {
1✔
145
            $pathsCount = \count($paths);
1✔
146
            for ($i = 0; $i < $pathsCount; $i++) {
1✔
147
                try {
148
                    $this->data['missing'] = false;
1✔
149
                    $locate = (new Locator($this->builder))->locate(
1✔
150
                        $paths[$i],
1✔
151
                        $options['fallback'],
1✔
152
                        $options['useragent'],
1✔
153
                        $language
1✔
154
                    );
1✔
155
                    $file = $locate['file'];
1✔
156
                    $path = $locate['path'];
1✔
157
                    $type = Util\File::getMediaType($file)[0];
1✔
158
                    if ($i > 0) { // bundle
1✔
159
                        if ($type != $this->data['type']) {
1✔
160
                            throw new RuntimeException(\sprintf('Asset bundle type error (%s != %s).', $type, $this->data['type']));
×
161
                        }
162
                    }
163
                    $this->data['file'] = $file;
1✔
164
                    $this->data['files'][] = $file;
1✔
165
                    $this->data['path'] = $path;
1✔
166
                    $this->data['url'] = Util\File::isRemote($paths[$i]) ? $paths[$i] : null;
1✔
167
                    $this->data['ext'] = Util\File::getExtension($file);
1✔
168
                    $this->data['type'] = $type;
1✔
169
                    $this->data['subtype'] = Util\File::getMediaType($file)[1];
1✔
170
                    $this->data['size'] += filesize($file) ?: 0;
1✔
171
                    $this->data['content'] .= Util\File::fileGetContents($file);
1✔
172
                    $this->data['hash'] = hash('xxh128', $this->data['content']);
1✔
173
                    // bundle default filename
174
                    $filename = $options['filename'];
1✔
175
                    if ($pathsCount > 1 && empty($filename)) {
1✔
176
                        switch ($this->data['ext']) {
1✔
177
                            case 'scss':
1✔
178
                            case 'css':
1✔
179
                                $filename = 'styles.css';
1✔
180
                                break;
1✔
181
                            case 'js':
1✔
182
                                $filename = 'scripts.js';
1✔
183
                                break;
1✔
184
                            default:
185
                                throw new RuntimeException(\sprintf('Asset bundle supports %s files only.', '.scss, .css and .js'));
×
186
                        }
187
                    }
188
                    // apply bundle filename to path
189
                    if (!empty($filename)) {
1✔
190
                        $this->data['path'] = $filename;
1✔
191
                    }
192
                    // add leading slash
193
                    $this->data['path'] = '/' . ltrim($this->data['path'], '/');
1✔
194
                    $this->data['_path'] = $this->data['path'];
1✔
195
                } catch (RuntimeException $e) {
1✔
196
                    if ($options['ignore_missing']) {
1✔
197
                        $this->data['missing'] = true;
1✔
198
                        continue;
1✔
199
                    }
200
                    throw new RuntimeException(\sprintf('Unable to handle asset "%s".', $paths[$i]), previous: $e);
×
201
                }
202
            }
203
            $this->cache->set($locateCacheKey, $this->data);
1✔
204
        }
205
        $this->data = $this->cache->get($locateCacheKey);
1✔
206

207
        // missing
208
        if ($this->isMissing()) {
1✔
209
            return;
1✔
210
        }
211

212
        // create cache tags from options
213
        $this->cacheTags = $options;
1✔
214
        // remove unnecessary cache tags
215
        unset($this->cacheTags['optimize'], $this->cacheTags['ignore_missing'], $this->cacheTags['fallback'], $this->cacheTags['useragent']);
1✔
216
        if (!\in_array($this->data['ext'], ['css', 'js', 'scss'])) {
1✔
217
            unset($this->cacheTags['minify']);
1✔
218
        }
219
        // optimize image?
220
        $optimize = false;
1✔
221
        $quality = (int) $this->config->get('assets.images.quality');
1✔
222
        if ($options['optimize'] && $this->data['type'] == 'image' && !$this->isImageInCdn()) {
1✔
223
            $optimize = true;
1✔
224
            $this->cacheTags['quality'] = $quality;
1✔
225
        }
226
        $cacheKey = $this->cache->createKey($this, tags: $this->cacheTags);
1✔
227
        if (!$this->cache->has($cacheKey)) {
1✔
228
            // fingerprinting
229
            if ($options['fingerprint']) {
1✔
230
                $this->doFingerprint();
×
231
            }
232
            // compiling Sass files
233
            $this->doCompile();
1✔
234
            // minifying (CSS and JavaScript files)
235
            if ($options['minify']) {
1✔
236
                $this->doMinify();
×
237
            }
238
            // get width and height
239
            $this->data['width'] = $this->getWidth();
1✔
240
            $this->data['height'] = $this->getHeight();
1✔
241
            // get image exif
242
            if ($this->data['subtype'] == 'image/jpeg') {
1✔
243
                $this->data['exif'] = Util\File::readExif($this->data['file']);
1✔
244
            }
245
            // get duration
246
            if ($this->data['type'] == 'audio') {
1✔
247
                $this->data['duration'] = $this->getAudio()['duration'];
1✔
248
            }
249
            if ($this->data['type'] == 'video') {
1✔
250
                $this->data['duration'] = $this->getVideo()['duration'];
1✔
251
            }
252
            $this->cache->set($cacheKey, $this->data, $this->config->get('cache.assets.ttl'));
1✔
253
            $this->builder->getLogger()->debug(\sprintf('Asset cached: "%s"', $this->data['path']));
1✔
254
            // optimizing images files (in cache directory)
255
            if ($optimize) {
1✔
256
                $this->optimizeImage($this->cache->getContentFile($this->data['path']), $this->data['path'], $quality);
1✔
257
            }
258
        }
259
        $this->data = $this->cache->get($cacheKey);
1✔
260
    }
261

262
    /**
263
     * Returns path.
264
     */
265
    public function __toString(): string
266
    {
267
        $this->save();
1✔
268

269
        if ($this->isImageInCdn()) {
1✔
270
            return $this->buildImageCdnUrl();
×
271
        }
272

273
        if ($this->builder->getConfig()->isEnabled('canonicalurl')) {
1✔
274
            return (string) new Url($this->builder, $this->data['path'], ['canonical' => true]);
×
275
        }
276

277
        return $this->data['path'];
1✔
278
    }
279

280
    /**
281
     * Implements \ArrayAccess.
282
     */
283
    #[\ReturnTypeWillChange]
284
    public function offsetSet($offset, $value): void
285
    {
286
        if (!\is_null($offset)) {
1✔
287
            $this->data[$offset] = $value;
1✔
288
        }
289
    }
290

291
    /**
292
     * Implements \ArrayAccess.
293
     */
294
    #[\ReturnTypeWillChange]
295
    public function offsetExists($offset): bool
296
    {
297
        return isset($this->data[$offset]);
1✔
298
    }
299

300
    /**
301
     * Implements \ArrayAccess.
302
     */
303
    #[\ReturnTypeWillChange]
304
    public function offsetUnset($offset): void
305
    {
306
        unset($this->data[$offset]);
×
307
    }
308

309
    /**
310
     * Implements \ArrayAccess.
311
     */
312
    #[\ReturnTypeWillChange]
313
    public function offsetGet($offset)
314
    {
315
        return isset($this->data[$offset]) ? $this->data[$offset] : null;
1✔
316
    }
317

318
    /**
319
     * Saves the asset by adding its path to the build assets list.
320
     * Skips assets marked as missing and validates that the asset file exists in cache before adding it.
321
     *
322
     * @throws RuntimeException
323
     */
324
    public function save(): void
325
    {
326
        if ($this->isMissing()) {
1✔
327
            return;
1✔
328
        }
329

330
        if (empty($this->data['path']) || !Util\File::getFS()->exists($this->cache->getContentFile($this->data['path']))) {
1✔
UNCOV
331
            throw new RuntimeException(\sprintf('Unable to add "%s" to assets list. Please clear cache and retry.', $this->data['path']));
×
332
        }
333

334
        $this->builder->addToAssetsList($this->data['path']);
1✔
335
    }
336

337
    /**
338
     * Checks if the asset is missing.
339
     */
340
    public function isMissing(): bool
341
    {
342
        return $this->data['missing'];
1✔
343
    }
344

345
    /**
346
     * Add hash to the file name + cache.
347
     */
348
    public function fingerprint(): self
349
    {
350
        return $this->cached('fingerprint', fn (): self => $this->doFingerprint());
1✔
351
    }
352

353
    /**
354
     * Compiles a SCSS + cache.
355
     *
356
     * @throws RuntimeException
357
     */
358
    public function compile(): self
359
    {
360
        return $this->cached('compile', fn (): self => $this->doCompile());
1✔
361
    }
362

363
    /**
364
     * Minifying a CSS or a JS.
365
     */
366
    public function minify(): self
367
    {
368
        return $this->cached('minify', fn (): self => $this->doMinify());
1✔
369
    }
370

371
    /**
372
     * Runs an asset transformation through the shared cache layer.
373
     */
374
    private function cached(string $tag, callable $action): self
375
    {
376
        $this->cacheTags[$tag] = true;
1✔
377
        $cacheKey = $this->cache->createKey($this, tags: $this->cacheTags);
1✔
378
        if (!$this->cache->has($cacheKey)) {
1✔
379
            $action();
1✔
380
            $this->cache->set($cacheKey, $this->data, $this->config->get('cache.assets.ttl'));
1✔
381
        }
382
        $this->data = $this->cache->get($cacheKey);
1✔
383

384
        return $this;
1✔
385
    }
386

387
    /**
388
     * Returns the Data URL (encoded in Base64).
389
     *
390
     * @throws RuntimeException
391
     */
392
    public function dataurl(): string
393
    {
394
        if ($this->data['type'] == 'image' && !Image::isSVG($this)) {
1✔
395
            return Image::getDataUrl($this, (int) $this->config->get('assets.images.quality'));
1✔
396
        }
397

398
        return \sprintf('data:%s;base64,%s', $this->data['subtype'], base64_encode($this->data['content']));
1✔
399
    }
400

401
    /**
402
     * Hashing content of an asset with the specified algo, sha384 by default.
403
     * Used for SRI (Subresource Integrity).
404
     *
405
     * @see https://developer.mozilla.org/fr/docs/Web/Security/Subresource_Integrity
406
     */
407
    public function integrity(string $algo = 'sha384'): string
408
    {
409
        return \sprintf('%s-%s', $algo, base64_encode(hash($algo, $this->data['content'], true)));
1✔
410
    }
411

412
    /**
413
     * Resizes an image to the given width or/and height.
414
     *
415
     * - If only the width is specified, the height is calculated to preserve the aspect ratio
416
     * - If only the height is specified, the width is calculated to preserve the aspect ratio
417
     * - If both width and height are specified, the image is resized to fit within the given dimensions, image is cropped and centered if necessary
418
     * - If rmAnimation is true, any animation in the image (e.g., GIF) will be removed.
419
     *
420
     * @throws RuntimeException
421
     */
422
    public function resize(?int $width = null, ?int $height = null, bool $rmAnimation = false): self
423
    {
424
        $this->checkImage();
1✔
425

426
        // if no width and no height, return the original image
427
        if ($width === null && $height === null) {
1✔
428
            return $this;
×
429
        }
430

431
        // if equal with and height, return the original image
432
        if ($width == $this->data['width'] && $height == $this->data['height']) {
1✔
433
            return $this;
×
434
        }
435

436
        // if the image width or height is already smaller, return the original image
437
        if ($width !== null && $this->data['width'] <= $width && $height === null) {
1✔
438
            return $this;
1✔
439
        }
440
        if ($height !== null && $this->data['height'] <= $height && $width === null) {
1✔
441
            return $this;
×
442
        }
443

444
        $assetResized = clone $this;
1✔
445
        $assetResized->data['width'] = $width ?? $this->data['width'];
1✔
446
        $assetResized->data['height'] = $height ?? $this->data['height'];
1✔
447

448
        if ($this->isImageInCdn()) {
1✔
449
            if ($width === null) {
×
450
                $assetResized->data['width'] = round($this->data['width'] / ($this->data['height'] / $height));
×
451
            }
452
            if ($height === null) {
×
453
                $assetResized->data['height'] = round($this->data['height'] / ($this->data['width'] / $width));
×
454
            }
455

456
            return $assetResized; // returns asset with the new dimensions only: CDN do the rest of the job
×
457
        }
458

459
        $quality = (int) $this->config->get('assets.images.quality');
1✔
460

461
        $assetResized->cacheTags['quality'] = $quality;
1✔
462
        $assetResized->cacheTags['width'] = $width;
1✔
463
        $assetResized->cacheTags['height'] = $height;
1✔
464
        $cacheKey = $this->cache->createKey($assetResized, tags: $assetResized->cacheTags);
1✔
465
        if (!$this->cache->has($cacheKey)) {
1✔
466
            $assetResized->data['content'] = Image::resize($assetResized, $width, $height, $quality, $rmAnimation);
1✔
467
            $assetResized->data['path'] = '/' . Util::joinPath(
1✔
468
                (string) $this->config->get('assets.target'),
1✔
469
                self::IMAGE_THUMB,
1✔
470
                (string) $width . 'x' . (string) $height,
1✔
471
                $assetResized->data['path']
1✔
472
            );
1✔
473
            $assetResized->data['path'] = $this->deduplicateThumbPath($assetResized->data['path']);
1✔
474
            $assetResized->data['width'] = $assetResized->getWidth();
1✔
475
            $assetResized->data['height'] = $assetResized->getHeight();
1✔
476
            $assetResized->data['size'] = \strlen($assetResized->data['content']);
1✔
477

478
            $this->cache->set($cacheKey, $assetResized->data, $this->config->get('cache.assets.ttl'));
1✔
479
            $this->builder->getLogger()->debug(\sprintf('Asset resized: "%s" (%sx%s)', $assetResized->data['path'], $width, $height));
1✔
480
        }
481
        $assetResized->data = $this->cache->get($cacheKey);
1✔
482

483
        return $assetResized;
1✔
484
    }
485

486
    /**
487
     * Creates a maskable image (with a padding = 20%).
488
     *
489
     * @throws RuntimeException
490
     */
491
    public function maskable(?int $padding = null): self
492
    {
493
        $this->checkImage();
×
494

495
        if ($padding === null) {
×
496
            $padding = 20; // default padding
×
497
        }
498

499
        $assetMaskable = clone $this;
×
500

501
        $quality = (int) $this->config->get('assets.images.quality');
×
502

503
        $assetMaskable->cacheTags['maskable'] = true;
×
NEW
504
        $cacheKey = $this->cache->createKey($assetMaskable, tags: $assetMaskable->cacheTags);
×
NEW
505
        if (!$this->cache->has($cacheKey)) {
×
506
            $assetMaskable->data['content'] = Image::maskable($assetMaskable, $quality, $padding);
×
507
            $assetMaskable->data['path'] = '/' . Util::joinPath(
×
508
                (string) $this->config->get('assets.target'),
×
509
                'maskable',
×
510
                $assetMaskable->data['path']
×
511
            );
×
512
            $assetMaskable->data['size'] = \strlen($assetMaskable->data['content']);
×
513

NEW
514
            $this->cache->set($cacheKey, $assetMaskable->data, $this->config->get('cache.assets.ttl'));
×
515
            $this->builder->getLogger()->debug(\sprintf('Asset maskabled: "%s"', $assetMaskable->data['path']));
×
516
        }
NEW
517
        $assetMaskable->data = $this->cache->get($cacheKey);
×
518

519
        return $assetMaskable;
×
520
    }
521

522
    /**
523
     * Converts an image asset to $format format.
524
     *
525
     * @throws RuntimeException
526
     */
527
    public function convert(string $format, ?int $quality = null): self
528
    {
529
        if ($this->data['type'] != 'image') {
1✔
530
            throw new RuntimeException(\sprintf('Unable to convert "%s" (%s) to %s: not an image.', $this->data['path'], $this->data['type'], $format));
×
531
        }
532

533
        if ($quality === null) {
1✔
534
            $quality = (int) $this->config->get('assets.images.quality');
1✔
535
        }
536

537
        $asset = clone $this;
1✔
538
        $asset['ext'] = $format;
1✔
539
        $asset->data['subtype'] = "image/$format";
1✔
540

541
        if ($this->isImageInCdn()) {
1✔
542
            return $asset; // returns the asset with the new extension only: CDN do the rest of the job
×
543
        }
544

545
        $this->cacheTags['quality'] = $quality;
1✔
546
        if ($this->data['width']) {
1✔
547
            $this->cacheTags['width'] = $this->data['width'];
1✔
548
        }
549
        $cacheKey = $this->cache->createKey($asset, tags: $this->cacheTags);
1✔
550
        if (!$this->cache->has($cacheKey)) {
1✔
551
            $asset->data['content'] = Image::convert($asset, $format, $quality);
1✔
552
            $asset->data['path'] = preg_replace('/\.' . $this->data['ext'] . '$/m', ".$format", $this->data['path']);
1✔
553
            $asset->data['size'] = \strlen($asset->data['content']);
1✔
554
            $this->cache->set($cacheKey, $asset->data, $this->config->get('cache.assets.ttl'));
1✔
555
            $this->builder->getLogger()->debug(\sprintf('Asset converted: "%s" (%s -> %s)', $asset->data['path'], $this->data['ext'], $format));
1✔
556
        }
557
        $asset->data = $this->cache->get($cacheKey);
1✔
558

559
        return $asset;
1✔
560
    }
561

562
    /**
563
     * Converts an image asset to WebP format.
564
     *
565
     * @throws RuntimeException
566
     */
567
    public function webp(?int $quality = null): self
568
    {
569
        return $this->convert('webp', $quality);
×
570
    }
571

572
    /**
573
     * Converts an image asset to AVIF format.
574
     *
575
     * @throws RuntimeException
576
     */
577
    public function avif(?int $quality = null): self
578
    {
579
        return $this->convert('avif', $quality);
×
580
    }
581

582
    /**
583
     * Is the asset an image and is it in CDN?
584
     */
585
    public function isImageInCdn(): bool
586
    {
587
        if (
588
            $this->data['type'] == 'image'
1✔
589
            && $this->config->isEnabled('assets.images.cdn')
1✔
590
            && $this->data['ext'] != 'ico'
1✔
591
            && (Image::isSVG($this) && $this->config->isEnabled('assets.images.cdn.svg'))
1✔
592
        ) {
593
            return true;
×
594
        }
595
        // handle remote image?
596
        if ($this->data['url'] !== null && $this->config->isEnabled('assets.images.cdn.remote')) {
1✔
597
            return true;
×
598
        }
599

600
        return false;
1✔
601
    }
602

603
    /**
604
     * Returns the width of an image/SVG or a video.
605
     *
606
     * @throws RuntimeException
607
     */
608
    public function getWidth(): ?int
609
    {
610
        switch ($this->data['type']) {
1✔
611
            case 'image':
1✔
612
                if (Image::isSVG($this) && false !== $svg = Image::getSvgAttributes($this)) {
1✔
613
                    return (int) $svg->width;
1✔
614
                }
615
                if (false === $size = $this->getImageSize()) {
1✔
616
                    throw new RuntimeException(\sprintf('Unable to get width of "%s".', $this->data['path']));
×
617
                }
618

619
                return $size[0];
1✔
620
            case 'video':
1✔
621
                return $this->getVideo()['width'];
1✔
622
        }
623

624
        return null;
1✔
625
    }
626

627
    /**
628
     * Returns the height of an image/SVG or a video.
629
     *
630
     * @throws RuntimeException
631
     */
632
    public function getHeight(): ?int
633
    {
634
        switch ($this->data['type']) {
1✔
635
            case 'image':
1✔
636
                if (Image::isSVG($this) && false !== $svg = Image::getSvgAttributes($this)) {
1✔
637
                    return (int) $svg->height;
1✔
638
                }
639
                if (false === $size = $this->getImageSize()) {
1✔
640
                    throw new RuntimeException(\sprintf('Unable to get height of "%s".', $this->data['path']));
×
641
                }
642

643
                return $size[1];
1✔
644
            case 'video':
1✔
645
                return $this->getVideo()['height'];
1✔
646
        }
647

648
        return null;
1✔
649
    }
650

651
    /**
652
     * Returns audio file infos:
653
     * - duration (in seconds.microseconds)
654
     * - bitrate (in bps)
655
     * - channel ('stereo', 'dual_mono', 'joint_stereo' or 'mono')
656
     *
657
     * @see https://github.com/wapmorgan/Mp3Info
658
     */
659
    public function getAudio(): array
660
    {
661
        $audio = new Mp3Info($this->data['file']);
1✔
662

663
        return [
1✔
664
            'duration' => $audio->duration,
1✔
665
            'bitrate'  => $audio->bitRate,
1✔
666
            'channel'  => $audio->channel,
1✔
667
        ];
1✔
668
    }
669

670
    /**
671
     * Returns video file infos:
672
     * - duration (in seconds)
673
     * - width (in pixels)
674
     * - height (in pixels)
675
     *
676
     * @see https://github.com/JamesHeinrich/getID3
677
     */
678
    public function getVideo(): array
679
    {
680
        if ($this->data['type'] !== 'video') {
1✔
681
            throw new RuntimeException(\sprintf('Unable to get video infos of "%s".', $this->data['path']));
×
682
        }
683

684
        $video = (new \getID3())->analyze($this->data['file']);
1✔
685

686
        return [
1✔
687
            'duration' => $video['playtime_seconds'],
1✔
688
            'width'    => $video['video']['resolution_x'],
1✔
689
            'height'   => $video['video']['resolution_y'],
1✔
690
        ];
1✔
691
    }
692

693
    /**
694
     * Builds a relative path from a URL.
695
     * Used for remote files.
696
     *
697
     * @deprecated Use Locator::buildPathFromUrl() instead.
698
     */
699
    public static function buildPathFromUrl(string $url): string
700
    {
701
        return Locator::buildPathFromUrl($url);
×
702
    }
703

704
    /**
705
     * Replaces some characters by '_'.
706
     *
707
     * @deprecated Use Locator::sanitize() instead.
708
     */
709
    public static function sanitize(string $string): string
710
    {
711
        return Locator::sanitize($string);
×
712
    }
713

714
    /**
715
     * Add hash to the file name.
716
     */
717
    protected function doFingerprint(): self
718
    {
719
        $hash = hash('xxh128', $this->data['content']);
1✔
720
        $this->data['path'] = preg_replace(
1✔
721
            '/\.' . $this->data['ext'] . '$/m',
1✔
722
            ".$hash." . $this->data['ext'],
1✔
723
            $this->data['path']
1✔
724
        );
1✔
725
        $this->builder->getLogger()->debug(\sprintf('Asset fingerprinted: "%s"', $this->data['path']));
1✔
726

727
        return $this;
1✔
728
    }
729

730
    /**
731
     * Compiles SCSS to CSS.
732
     */
733
    protected function doCompile(): self
734
    {
735
        $this->data = (new AssetCompiler($this->builder))->compile($this->data);
1✔
736

737
        return $this;
1✔
738
    }
739

740
    /**
741
     * Minifies a CSS or JS asset.
742
     */
743
    protected function doMinify(): self
744
    {
745
        // compile SCSS files first
746
        if ($this->data['ext'] === 'scss') {
1✔
747
            $this->doCompile();
×
748
        }
749
        $this->data = (new AssetOptimizer($this->builder))->minify($this->data);
1✔
750

751
        return $this;
1✔
752
    }
753

754
    /**
755
     * Optimizes an image file in-place.
756
     * Returns the new file size.
757
     */
758
    private function optimizeImage(string $filepath, string $path, int $quality): int
759
    {
760
        return (new AssetOptimizer($this->builder))->optimizeImage($filepath, $path, $quality);
1✔
761
    }
762

763
    /**
764
     * Returns image size informations.
765
     *
766
     * @see https://www.php.net/manual/function.getimagesize.php
767
     *
768
     * @throws RuntimeException
769
     */
770
    private function getImageSize(): array|false
771
    {
772
        if (!$this->data['type'] == 'image') {
1✔
773
            return false;
×
774
        }
775

776
        try {
777
            if (false === $size = getimagesizefromstring($this->data['content'])) {
1✔
778
                return false;
1✔
779
            }
780
        } catch (\Exception $e) {
×
781
            throw new RuntimeException(\sprintf('Handling asset "%s" failed: "%s".', $this->data['path'], $e->getMessage()));
×
782
        }
783

784
        return $size;
1✔
785
    }
786

787
    /**
788
     * Builds CDN image URL.
789
     */
790
    private function buildImageCdnUrl(): string
791
    {
792
        return str_replace(
×
793
            [
×
794
                '%account%',
×
795
                '%image_url%',
×
796
                '%width%',
×
797
                '%quality%',
×
798
                '%format%',
×
799
            ],
×
800
            [
×
801
                $this->config->get('assets.images.cdn.account') ?? '',
×
802
                ltrim($this->data['url'] ?? (string) new Url($this->builder, $this->data['path'], ['canonical' => $this->config->get('assets.images.cdn.canonical') ?? true]), '/'),
×
803
                $this->data['width'],
×
804
                (int) $this->config->get('assets.images.quality'),
×
805
                $this->data['ext'],
×
806
            ],
×
807
            (string) $this->config->get('assets.images.cdn.url')
×
808
        );
×
809
    }
810

811
    /**
812
     * Checks if the asset is not missing and is typed as an image.
813
     *
814
     * @throws RuntimeException
815
     */
816
    private function checkImage(): void
817
    {
818
        if ($this->isMissing()) {
1✔
819
            throw new RuntimeException(\sprintf('Unable to resize "%s": file not found.', $this->data['path']));
×
820
        }
821
        if ($this->data['type'] != 'image') {
1✔
822
            throw new RuntimeException(\sprintf('Unable to resize "%s": not an image.', $this->data['path']));
×
823
        }
824
    }
825

826
    /**
827
     * Remove redondant '/thumbnails/<width(xheight)>/' in the path.
828
     */
829
    private function deduplicateThumbPath(string $path): string
830
    {
831
        // https://regex101.com/r/0r7FMY/1
832
        $pattern = '/(' . self::IMAGE_THUMB . '\/(\d+){0,1}x(\d+){0,1}\/)(' . self::IMAGE_THUMB . '\/(\d+){0,1}x(\d+){0,1}\/)(.*)/i';
1✔
833

834
        if (null === $result = preg_replace($pattern, '$1$7', $path)) {
1✔
835
            return $path;
×
836
        }
837

838
        return $result;
1✔
839
    }
840
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc