|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace ExoUNX\Vasri; |
|
5
|
|
|
|
|
6
|
|
|
use Illuminate\Support\Facades\File; |
|
7
|
|
|
use Exception; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Access class for Vasri |
|
11
|
|
|
* Class Vasri |
|
12
|
|
|
* |
|
13
|
|
|
* @package ExoUNX\Vasri |
|
14
|
|
|
* @author Gaige Lama <[email protected]> |
|
15
|
|
|
* @license MIT License |
|
16
|
|
|
* @link https://github.com/ExoUNX/Vasri |
|
17
|
|
|
*/ |
|
18
|
|
|
class Vasri |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var Builder |
|
24
|
|
|
*/ |
|
25
|
|
|
private $builder; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var ManifestReader |
|
29
|
|
|
*/ |
|
30
|
|
|
private $manifestReader; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
private $vasriManifest; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var mixed |
|
39
|
|
|
*/ |
|
40
|
|
|
private $appEnvironment; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var |
|
44
|
|
|
*/ |
|
45
|
|
|
private $vasriConfig; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var |
|
49
|
|
|
*/ |
|
50
|
|
|
private $isMixManifestAltEnabled; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Vasri constructor. |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __construct() |
|
56
|
|
|
{ |
|
57
|
|
|
$this->builder = new Builder(); |
|
58
|
|
|
$this->manifestReader = new ManifestReader(); |
|
59
|
|
|
$this->vasriConfig = config('vasri'); |
|
60
|
|
|
$this->isMixManifestAltEnabled = $this->vasriConfig['mix-manifest-alt']; |
|
61
|
|
|
$this->vasriManifest = $this->manifestReader->getManifest(base_path('vasri-manifest.json')); |
|
62
|
|
|
$this->appEnvironment = env('APP_ENV', 'production'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* The Vasri helper function |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $file |
|
69
|
|
|
* @param bool $enableVersioning |
|
70
|
|
|
* @param bool $enableSRI |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $keyword |
|
73
|
|
|
* |
|
74
|
|
|
* @return string |
|
75
|
|
|
* @throws Exception |
|
76
|
|
|
*/ |
|
77
|
|
|
public function vasri( |
|
78
|
|
|
string $file, |
|
79
|
|
|
bool $enableVersioning = true, |
|
80
|
|
|
bool $enableSRI = true, |
|
81
|
|
|
string $keyword = 'anonymous' |
|
82
|
|
|
): string { |
|
83
|
|
|
|
|
84
|
|
|
$altFile = ''; |
|
85
|
|
|
|
|
86
|
|
|
if ($this->isMixManifestAltEnabled) { |
|
87
|
|
|
|
|
88
|
|
|
$altFile = $this->vasriManifest[$file]['alt']; |
|
89
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if (self::isPublicFile($file) || self::isPublicFile($altFile)) { |
|
93
|
|
|
|
|
94
|
|
|
return $this->addAttributes($file, $altFile, $enableVersioning, $enableSRI, $keyword); |
|
95
|
|
|
|
|
96
|
|
|
} else { |
|
97
|
|
|
|
|
98
|
|
|
throw new Exception('Incorrect file path or file does not exist for local asset'); |
|
99
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Fetches the SRI hash from the Vasri Manifest and adds the crossorigin attribute |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $file |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $keyword |
|
109
|
|
|
* |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
|
|
private function getSRI(string $file, string $keyword): string |
|
113
|
|
|
{ |
|
114
|
|
|
|
|
115
|
|
|
return sprintf('integrity="%s" %s', $this->vasriManifest[$file]['sri'], $this->builder->crossOrigin($keyword)); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Builds all the attributes |
|
120
|
|
|
* |
|
121
|
|
|
* @param string $file |
|
122
|
|
|
* @param string $altFile |
|
123
|
|
|
* @param bool $enableVersioning |
|
124
|
|
|
* |
|
125
|
|
|
* @param bool $enableSRI |
|
126
|
|
|
* @param string $keyword |
|
127
|
|
|
* |
|
128
|
|
|
* @return string |
|
129
|
|
|
* @throws Exception |
|
130
|
|
|
*/ |
|
131
|
|
|
private function addAttributes( |
|
132
|
|
|
string $file, |
|
133
|
|
|
string $altFile, |
|
134
|
|
|
bool $enableVersioning, |
|
135
|
|
|
bool $enableSRI, |
|
136
|
|
|
string $keyword |
|
137
|
|
|
): string { |
|
138
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
$option = $this->getOptions($enableVersioning, $enableSRI); |
|
141
|
|
|
$output = $this->getSourceAttribute($file); |
|
142
|
|
|
|
|
143
|
|
|
if ($option['versioning']) { |
|
144
|
|
|
|
|
145
|
|
|
$path = $file; |
|
146
|
|
|
|
|
147
|
|
|
if ($this->isMixManifestAltEnabled) { |
|
148
|
|
|
$path = $altFile; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
$output = $this->getSourceAttribute($path, $this->getVersioning($file)); |
|
152
|
|
|
|
|
153
|
|
|
} |
|
154
|
|
|
if ($option['sri']) { |
|
155
|
|
|
|
|
156
|
|
|
$output = sprintf('%s %s', $output, $this->getSRI($file, $keyword)); |
|
157
|
|
|
|
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
return $output; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Fetches the version query string from the Vasri Manifest |
|
165
|
|
|
* |
|
166
|
|
|
* @param string $file |
|
167
|
|
|
* |
|
168
|
|
|
* @return string |
|
169
|
|
|
*/ |
|
170
|
|
|
private function getVersioning(string $file): string |
|
171
|
|
|
{ |
|
172
|
|
|
return $this->vasriManifest[$file]['version']; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Figures out whether or not to toggle versioning and sri |
|
177
|
|
|
* |
|
178
|
|
|
* @param bool $enableVersioning |
|
179
|
|
|
* @param bool $enableSRI |
|
180
|
|
|
* |
|
181
|
|
|
* @return array |
|
182
|
|
|
*/ |
|
183
|
|
|
private function getOptions(bool $enableVersioning, bool $enableSRI): array |
|
184
|
|
|
{ |
|
185
|
|
|
return [ |
|
186
|
|
|
'versioning' => ! ($this->appEnvironment === 'local' |
|
187
|
|
|
&& ! config('vasri.local-versioning') |
|
188
|
|
|
|| ! $enableVersioning |
|
189
|
|
|
|| ! $this->vasriConfig['versioning'] |
|
190
|
|
|
), |
|
191
|
|
|
'sri' => $enableSRI && $this->vasriConfig['sri'], |
|
192
|
|
|
]; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Gets source attribute based on the extension, adds file path and version |
|
197
|
|
|
* |
|
198
|
|
|
* @param string $file |
|
199
|
|
|
* @param string $version |
|
200
|
|
|
* |
|
201
|
|
|
* @return string |
|
202
|
|
|
* @throws Exception |
|
203
|
|
|
*/ |
|
204
|
|
|
private function getSourceAttribute(string $file, string $version = ''): string |
|
205
|
|
|
{ |
|
206
|
|
|
return sprintf('%s="%s%s"', $this->builder->attribute($file), $file, $version); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Checks if the file is in the Laravel public path |
|
211
|
|
|
* |
|
212
|
|
|
* @param string $file |
|
213
|
|
|
* |
|
214
|
|
|
* @return bool |
|
215
|
|
|
*/ |
|
216
|
|
|
private static function isPublicFile(string $file): bool |
|
217
|
|
|
{ |
|
218
|
|
|
return File::exists(public_path($file)); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
} |
|
222
|
|
|
|