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

nette / assets / 15009594452

14 May 2025 12:38AM UTC coverage: 95.411% (+1.1%) from 94.318%
15009594452

push

github

dg
readme: added info about custom mappers

395 of 414 relevant lines covered (95.41%)

0.95 hits per line

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

92.86
/src/Assets/Helpers.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Nette\Assets;
6

7
use Nette;
8

9

10
/**
11
 * Static helper class providing utility functions for working with assets.
12
 */
13
final class Helpers
14
{
15
        use Nette\StaticClass;
16

17
        private const ExtensionToMime = [
18
                'avif' => 'image/avif', 'gif' => 'image/gif', 'ico' => 'image/vnd.microsoft.icon', 'jpeg' => 'image/jpeg', 'jpg' => 'image/jpeg', 'png' => 'image/png', 'svg' => 'image/svg+xml', 'webp' => 'image/webp',
19
                'js' => 'application/javascript', 'mjs' => 'application/javascript',
20
                'css' => 'text/css',
21
                'aac' => 'audio/aac', 'flac' => 'audio/flac', 'm4a' => 'audio/mp4', 'mp3' => 'audio/mpeg', 'ogg' => 'audio/ogg', 'wav' => 'audio/wav',
22
                'avi' => 'video/x-msvideo', 'mkv' => 'video/x-matroska', 'mov' => 'video/quicktime', 'mp4' => 'video/mp4', 'ogv' => 'video/ogg', 'webm' => 'video/webm',
23
                'woff' => 'font/woff', 'woff2' => 'font/woff2', 'ttf' => 'font/ttf',
24
        ];
25

26

27
        public static function createAssetFromUrl(string $url, ?string $path, array $args = []): Asset
1✔
28
        {
29
                $args['url'] = $url;
1✔
30
                $args['sourcePath'] = $path;
1✔
31
                $mime = (string) $args['mimeType'] ??= self::guessMimeTypeFromExtension($url);
1✔
32
                $class = match (true) {
1✔
33
                        $mime === 'application/javascript' => ScriptAsset::class,
1✔
34
                        $mime === 'text/css' => StyleAsset::class,
1✔
35
                        str_starts_with($mime, 'image/') => ImageAsset::class,
1✔
36
                        str_starts_with($mime, 'audio/') => AudioAsset::class,
1✔
37
                        str_starts_with($mime, 'video/') => VideoAsset::class,
1✔
38
                        $mime === 'font/woff' || $mime === 'font/woff2' || $mime === 'font/ttf' => FontAsset::class,
1✔
39
                        default => GenericAsset::class,
1✔
40
                };
41
                return new $class(...$args);
1✔
42
        }
43

44

45
        public static function guessMimeTypeFromExtension(string $url): ?string
1✔
46
        {
47
                return preg_match('~\.([a-z0-9]{1,5})([?#]|$)~i', $url, $m)
1✔
48
                        ? self::ExtensionToMime[strtolower($m[1])] ?? null
1✔
49
                        : null;
1✔
50
        }
51

52

53
        /**
54
         * Splits a potentially qualified reference 'mapper:reference' into a [mapper, reference] array.
55
         * @return array{?string, string}
56
         */
57
        public static function parseReference(string $qualifiedRef): array
1✔
58
        {
59
                $parts = explode(':', $qualifiedRef, 2);
1✔
60
                return count($parts) === 1
1✔
61
                        ? [null, $parts[0]]
1✔
62
                        : [$parts[0], $parts[1]];
1✔
63
        }
64

65

66
        /**
67
         * Validates an array of options against allowed optional and required keys.
68
         * @throws \InvalidArgumentException if there are unsupported or missing options
69
         */
70
        public static function checkOptions(array $array, array $optional = [], array $required = []): void
1✔
71
        {
72
                if ($keys = array_diff(array_keys($array), $optional, $required)) {
1✔
73
                        throw new \InvalidArgumentException('Unsupported asset options: ' . implode(', ', $keys));
1✔
74
                }
75
                if ($keys = array_diff($required, array_keys($array))) {
1✔
76
                        throw new \InvalidArgumentException('Missing asset options: ' . implode(', ', $keys));
×
77
                }
78
        }
1✔
79

80

81
        /**
82
         * Estimates the duration (in seconds) of an MP3 file, assuming constant bitrate (CBR).
83
         * @throws \RuntimeException If the file cannot be opened, MP3 sync bits aren't found, or the bitrate is invalid/unsupported.
84
         */
85
        public static function guessMP3Duration(string $path): float
1✔
86
        {
87
                if (
88
                        ($header = @file_get_contents($path, length: 10000)) === false // @ - file may not exist
1✔
89
                        || ($fileSize = @filesize($path)) === false
1✔
90
                ) {
91
                        throw new \RuntimeException(sprintf("Failed to open file '%s'. %s", $path, Nette\Utils\Helpers::getLastError()));
×
92
                }
93

94
                $frameOffset = strpos($header, "\xFF\xFB"); // 0xFB indicates MPEG Version 1, Layer III, no protection bit.
1✔
95
                if ($frameOffset === false) {
1✔
96
                        throw new \RuntimeException('Failed to find MP3 frame sync bits.');
1✔
97
                }
98

99
                $frameHeader = substr($header, $frameOffset, 4);
1✔
100
                $headerBits = unpack('N', $frameHeader)[1];
1✔
101
                $bitrateIndex = ($headerBits >> 12) & 0xF;
1✔
102
                $bitrate = [null, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320][$bitrateIndex] ?? null;
1✔
103
                if ($bitrate === null) {
1✔
104
                        throw new \RuntimeException('Invalid or unsupported bitrate index.');
×
105
                }
106

107
                return $fileSize * 8 / $bitrate / 1000;
1✔
108
        }
109
}
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