|
3 | 3 |
|
4 | 4 | namespace ViteHelper\Utilities; |
5 | 5 |
|
6 | | -use Nette\Utils\FileSystem; |
7 | | -use Nette\Utils\Json; |
8 | 6 | use ViteHelper\Exception\ManifestNotFoundException; |
9 | 7 |
|
10 | 8 | /** |
11 | 9 | * Reads the information in the manifest.json file provided by ViteJs after running 'vite build' |
12 | 10 | */ |
13 | 11 | class ViteManifest |
14 | 12 | { |
15 | | - /** |
16 | | - * Returns the manifest records as a Collection |
17 | | - * |
18 | | - * @param \ViteHelper\Utilities\ViteHelperConfig $config plugin config instance |
19 | | - * @return \ViteHelper\Utilities\ManifestRecords|\ViteHelper\Utilities\ManifestRecord[] |
20 | | - * @throws \ViteHelper\Exception\ManifestNotFoundException |
21 | | - * @internal |
22 | | - */ |
23 | | - public static function getRecords(ViteHelperConfig $config): ManifestRecords |
24 | | - { |
25 | | - $manifestPath = $config->read('build.manifest', ConfigDefaults::BUILD_MANIFEST); |
| 13 | + /** |
| 14 | + * Returns the manifest records as a Collection |
| 15 | + * |
| 16 | + * @param \ViteHelper\Utilities\ViteHelperConfig $config plugin config instance |
| 17 | + * @return \ViteHelper\Utilities\ManifestRecords|\ViteHelper\Utilities\ManifestRecord[] |
| 18 | + * @throws \ViteHelper\Exception\ManifestNotFoundException |
| 19 | + * @internal |
| 20 | + */ |
| 21 | + public static function getRecords(ViteHelperConfig $config): ManifestRecords |
| 22 | + { |
| 23 | + $manifestPath = $config->read('build.manifest', ConfigDefaults::BUILD_MANIFEST); |
26 | 24 |
|
27 | | - try { |
28 | | - $json = FileSystem::read($manifestPath); |
| 25 | + if (!is_readable($manifestPath)) { |
| 26 | + throw new ManifestNotFoundException( |
| 27 | + "No valid manifest.json found at path {$manifestPath}. Did you build your js?", |
| 28 | + ); |
| 29 | + } |
29 | 30 |
|
30 | | - $json = str_replace([ |
31 | | - "\u0000", |
32 | | - ], '', $json); |
| 31 | + $json = @file_get_contents($manifestPath); |
33 | 32 |
|
34 | | - $manifest = Json::decode($json); |
35 | | - } catch (\Exception $e) { |
36 | | - throw new ManifestNotFoundException( |
37 | | - "No valid manifest.json found at path {$manifestPath}. Did you build your js? Error: {$e->getMessage()}" |
38 | | - ); |
39 | | - } |
| 33 | + if ($json === false) { |
| 34 | + throw new ManifestNotFoundException('Could not parse manifest.json'); |
| 35 | + } |
40 | 36 |
|
41 | | - $manifestArray = []; |
42 | | - foreach (get_object_vars($manifest) as $property => $value) { |
43 | | - $manifestArray[$property] = new ManifestRecord($property, $value, $config); |
44 | | - } |
| 37 | + $json = str_replace( |
| 38 | + [ |
| 39 | + "\u0000", |
| 40 | + ], |
| 41 | + '', |
| 42 | + $json |
| 43 | + ); |
45 | 44 |
|
46 | | - /** |
47 | | - * Legacy Polyfills must come first. |
48 | | - */ |
49 | | - usort($manifestArray, function ($file) { |
50 | | - /** @var \ViteHelper\Utilities\ManifestRecord $file */ |
51 | | - return $file->isPolyfill() ? 0 : 1; |
52 | | - }); |
| 45 | + $manifest = json_decode($json, false, 512, JSON_THROW_ON_ERROR); |
53 | 46 |
|
54 | | - /** |
55 | | - * ES-module scripts must come last. |
56 | | - */ |
57 | | - usort($manifestArray, function ($file) { |
58 | | - /** @var \ViteHelper\Utilities\ManifestRecord $file */ |
59 | | - return !$file->isPolyfill() && !$file->isLegacy() ? 1 : 0; |
60 | | - }); |
| 47 | + $manifestArray = []; |
| 48 | + foreach (get_object_vars($manifest) as $property => $value) { |
| 49 | + $manifestArray[$property] = new ManifestRecord($property, $value, $config); |
| 50 | + } |
61 | 51 |
|
62 | | - return new ManifestRecords($manifestArray, $manifestPath); |
63 | | - } |
| 52 | + /** |
| 53 | + * Legacy Polyfills must come first. |
| 54 | + */ |
| 55 | + usort($manifestArray, function ($file) { |
| 56 | + /** @var \ViteHelper\Utilities\ManifestRecord $file */ |
| 57 | + return $file->isPolyfill() ? 0 : 1; |
| 58 | + }); |
| 59 | + |
| 60 | + /** |
| 61 | + * ES-module scripts must come last. |
| 62 | + */ |
| 63 | + usort($manifestArray, function ($file) { |
| 64 | + /** @var \ViteHelper\Utilities\ManifestRecord $file */ |
| 65 | + return !$file->isPolyfill() && !$file->isLegacy() ? 1 : 0; |
| 66 | + }); |
| 67 | + |
| 68 | + return new ManifestRecords($manifestArray, $manifestPath); |
| 69 | + } |
64 | 70 | } |
0 commit comments