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

nette / assets / 15743778999

18 Jun 2025 09:14PM UTC coverage: 94.251% (+2.3%) from 91.984%
15743778999

push

github

dg
simplified detection of dev server

0 of 3 new or added lines in 1 file covered. (0.0%)

459 of 487 relevant lines covered (94.25%)

0.94 hits per line

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

81.25
/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
        /**
28
         * Creates an Asset instance. The asset type is detected by 'mimeType' if provided in $args,
29
         * otherwise is guessed from the file extension of $path or $url.
30
         * @param  mixed[]  $args  parameters passed to the asset constructor
31
         */
32
        public static function createAssetFromUrl(string $url, ?string $path = null, array $args = []): Asset
1✔
33
        {
34
                $args['url'] = $url;
1✔
35
                $args['file'] = $path;
1✔
36
                $argsm = $args;
1✔
37
                $mime = (string) $argsm['mimeType'] ??= self::guessMimeTypeFromExtension($path ?? $url);
1✔
38
                $type = explode('/', (string) $mime, 2)[0];
1✔
39
                return match (true) {
40
                        $mime === 'application/javascript' => new ScriptAsset(...$args),
1✔
41
                        $mime === 'text/css' => new StyleAsset(...$args),
1✔
42
                        $type === 'image' => new ImageAsset(...$args),
1✔
43
                        $type === 'audio' => new AudioAsset(...$argsm),
1✔
44
                        $type === 'video' => new VideoAsset(...$argsm),
1✔
45
                        $type === 'font' => new FontAsset(...$argsm),
1✔
46
                        default => new GenericAsset(...$argsm),
1✔
47
                };
48
        }
49

50

51
        public static function guessMimeTypeFromExtension(string $url): ?string
1✔
52
        {
53
                return preg_match('~\.([a-z0-9]{1,5})([?#]|$)~i', $url, $m)
1✔
54
                        ? self::ExtensionToMime[strtolower($m[1])] ?? null
1✔
55
                        : null;
1✔
56
        }
57

58

59
        /**
60
         * Splits a potentially qualified reference 'mapper:reference' into a [mapper, reference] array.
61
         * @return array{?string, string}
62
         */
63
        public static function parseReference(string $qualifiedRef): array
1✔
64
        {
65
                $parts = explode(':', $qualifiedRef, 2);
1✔
66
                return count($parts) === 1
1✔
67
                        ? [null, $parts[0]]
1✔
68
                        : [$parts[0], $parts[1]];
1✔
69
        }
70

71

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

86

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

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

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

113
                return $fileSize * 8 / $bitrate / 1000;
1✔
114
        }
115

116

117
        public static function detectDevServer(string $infoFile): ?string
118
        {
NEW
119
                return ($info = @file_get_contents($infoFile)) // @ file may not exists
×
NEW
120
                        && ($info = json_decode($info, associative: true))
×
NEW
121
                        && isset($info['devServer'])
×
122
                        && ($url = parse_url($info['devServer']))
×
123
                        ? $info['devServer']
×
124
                        : null;
×
125
        }
126
}
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