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
|
|
|
* Vasri constructor. |
39
|
|
|
*/ |
40
|
|
|
public function __construct() |
41
|
|
|
{ |
42
|
|
|
$this->builder = new Builder(); |
43
|
|
|
$this->manifestReader = new ManifestReader(); |
44
|
|
|
$this->vasriManifest = $this->manifestReader->getVasriManifest(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $file |
49
|
|
|
* @param bool $enableVersioning |
50
|
|
|
* @param bool $enableSRI |
51
|
|
|
* |
52
|
|
|
* @param string $keyword |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
* @throws Exception |
56
|
|
|
*/ |
57
|
|
|
public function vasri( |
58
|
|
|
string $file, |
59
|
|
|
bool $enableVersioning = true, |
60
|
|
|
bool $enableSRI = true, |
61
|
|
|
string $keyword = 'anonymous' |
62
|
|
|
): string { |
63
|
|
|
$output = ''; |
64
|
|
|
|
65
|
|
|
if (self::isFile($file) === true) { |
66
|
|
|
$output .= $this->addAttribute($file, $enableVersioning); |
67
|
|
|
if ($enableSRI === true) { |
68
|
|
|
$output .= $this->addSRI($file, $keyword); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $output; |
72
|
|
|
|
73
|
|
|
} else { |
74
|
|
|
throw new Exception('Incorrect file path or file does not exist for local asset'); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $file |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
private function addVersioning(string $file): string |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
return $this->builder->versioning($file); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $file |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
* @throws Exception |
93
|
|
|
*/ |
94
|
|
|
private function addSRI(string $file, string $keyword): string |
95
|
|
|
{ |
96
|
|
|
return " integrity=\"".$this->vasriManifest[$file]['sri']."\" ".$this->builder->crossOrigin($keyword); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $file |
101
|
|
|
* @param bool $enableVersioning |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
* @throws Exception |
105
|
|
|
*/ |
106
|
|
|
private function addAttribute(string $file, bool $enableVersioning) |
107
|
|
|
{ |
108
|
|
|
try { |
109
|
|
|
if ($enableVersioning === true) { |
110
|
|
|
$output = $this->builder->attribute($file)."=\"".$file.$this->vasriManifest[$file]['version']."\""; |
111
|
|
|
} else { |
112
|
|
|
$output = $this->builder->attribute($file)."=\"".$file."\""; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $output; |
116
|
|
|
} catch (Exception $e) { |
117
|
|
|
throw new Exception($e); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* |
123
|
|
|
* @param string $file |
124
|
|
|
* |
125
|
|
|
* @return bool |
126
|
|
|
*/ |
127
|
|
|
private static function isFile(string $file): bool |
128
|
|
|
{ |
129
|
|
|
return File::exists(public_path($file)); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
} |
133
|
|
|
|