|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\MixPreload; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
|
6
|
|
|
use Illuminate\Support\HtmlString; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
|
|
9
|
|
|
class RenderPreloadLinks |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var array */ |
|
12
|
|
|
protected $manifest; |
|
13
|
|
|
|
|
14
|
|
|
/** @var string|null */ |
|
15
|
|
|
protected $assetUrl; |
|
16
|
|
|
|
|
17
|
|
|
public static function create(string $manifestPath = null, string $assetUrl = null): RenderPreloadLinks |
|
18
|
|
|
{ |
|
19
|
|
|
if (!$manifestPath) { |
|
|
|
|
|
|
20
|
|
|
$manifestPath = public_path('mix-manifest.json'); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
$manifest = json_decode( |
|
24
|
|
|
file_get_contents($manifestPath), |
|
25
|
|
|
true |
|
26
|
|
|
); |
|
27
|
|
|
|
|
28
|
|
|
return new self($manifest, $assetUrl); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function __construct(array $manifest, string $assetUrl = null) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->manifest = $manifest; |
|
34
|
|
|
$this->assetUrl = $assetUrl; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function __invoke(): HtmlString |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->getManifestEntries() |
|
40
|
|
|
->mapSpread(function (string $path, string $name) { |
|
41
|
|
|
$rel = $this->getRelAttribute($name); |
|
42
|
|
|
|
|
43
|
|
|
if (!$rel) { |
|
|
|
|
|
|
44
|
|
|
return null; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$as = $this->getAsAttribute($path); |
|
48
|
|
|
|
|
49
|
|
|
if (!$as) { |
|
|
|
|
|
|
50
|
|
|
return null; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$path = $this->getHrefAttribute($path); |
|
54
|
|
|
|
|
55
|
|
|
return "<link rel=\"{$rel}\" href=\"{$path}\" as=\"{$as}\">"; |
|
56
|
|
|
}) |
|
57
|
|
|
->filter() |
|
58
|
|
|
->pipe(function (Collection $links) { |
|
59
|
|
|
return new HtmlString($links->implode("\n")); |
|
60
|
|
|
}); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
protected function getManifestEntries(): Collection |
|
64
|
|
|
{ |
|
65
|
|
|
return collect($this->manifest) |
|
66
|
|
|
->map(function (string $path, string $name) { |
|
67
|
|
|
return [$path, $name]; |
|
68
|
|
|
}) |
|
69
|
|
|
->values(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
protected function getRelAttribute(string $name): ?string |
|
73
|
|
|
{ |
|
74
|
|
|
if (Str::contains($name, 'preload')) { |
|
75
|
|
|
return 'preload'; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if (Str::contains($name, 'prefetch')) { |
|
79
|
|
|
return 'prefetch'; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return null; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
protected function getAsAttribute(string $path): ?string |
|
86
|
|
|
{ |
|
87
|
|
|
if (Str::contains($path, '.js')) { |
|
88
|
|
|
return 'script'; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if (Str::contains($path, '.css')) { |
|
92
|
|
|
return 'style'; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
if (Str::contains($path, ['.woff', '.woff2', '.ttf', '.eot', '.svg', '.ttc'])) { |
|
96
|
|
|
return 'font'; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return null; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
protected function getHrefAttribute(string $path): ?string |
|
103
|
|
|
{ |
|
104
|
|
|
if ($this->assetUrl !== null) { |
|
105
|
|
|
return $this->assetUrl . $path; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $path; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: