1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace ExoUNX\Vasri; |
5
|
|
|
|
6
|
|
|
use Illuminate\Support\Facades\File; |
7
|
|
|
use Exception; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Constructs Manifest |
11
|
|
|
* Class ManifestBuilder |
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 ManifestBuilder |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $vasriConfig; |
25
|
|
|
/** |
26
|
|
|
* @var bool |
27
|
|
|
*/ |
28
|
|
|
private $isMixManifestEnabled; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $mixManifestPath; |
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private $mixManifest; |
38
|
|
|
/** |
39
|
|
|
* @var ManifestReader |
40
|
|
|
*/ |
41
|
|
|
private $manifestReader; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var Builder |
45
|
|
|
*/ |
46
|
|
|
private $builder; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* ManifestBuilder constructor. |
50
|
|
|
* @throws Exception |
51
|
|
|
*/ |
52
|
|
|
public function __construct() |
53
|
|
|
{ |
54
|
|
|
$this->manifestReader = new ManifestReader(); |
55
|
|
|
$this->builder = new Builder(); |
56
|
|
|
$this->vasriConfig = config('vasri'); |
57
|
|
|
$this->isMixManifestEnabled = $this->vasriConfig['mix-manifest']; |
58
|
|
|
$this->isMixManifestAltEnabled = $this->vasriConfig['mix-manifest-alt']; |
|
|
|
|
59
|
|
|
$this->mixManifestPath = public_path('mix-manifest.json'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Builds the basic asset list |
64
|
|
|
* |
65
|
|
|
* @param array $mixManifest |
66
|
|
|
* @param array $vasriConfigAssets |
67
|
|
|
* |
68
|
|
|
* @return array |
69
|
|
|
* @throws Exception |
70
|
|
|
*/ |
71
|
|
|
private function buildAssets(array $mixManifest = [], array $vasriConfigAssets = []): array |
72
|
|
|
{ |
73
|
|
|
$vasriManifest = []; |
74
|
|
|
|
75
|
|
|
if ($this->isMixManifestEnabled && File::exists($this->mixManifestPath)) { |
76
|
|
|
|
77
|
|
|
if ($this->isMixManifestAltEnabled) { |
|
|
|
|
78
|
|
|
|
79
|
|
|
foreach ($mixManifest as $key => $val) { |
80
|
|
|
$vasriManifest[] = $val; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
} else { |
84
|
|
|
|
85
|
|
|
foreach ($mixManifest as $key => $val) { |
86
|
|
|
$vasriManifest[] = $key; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
} elseif ( ! empty($vasriConfigAssets)) { |
92
|
|
|
|
93
|
|
|
$vasriManifest = $this->vasriConfig['assets']; |
94
|
|
|
|
95
|
|
|
} else { |
96
|
|
|
|
97
|
|
|
throw new Exception('No manifest or valid assets found'); |
98
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $vasriManifest; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Builds the manifest based off the asset list |
106
|
|
|
* |
107
|
|
|
* @return array |
108
|
|
|
* @throws Exception |
109
|
|
|
*/ |
110
|
|
|
private function buildManifest(): array |
111
|
|
|
{ |
112
|
|
|
$manifest = []; |
113
|
|
|
|
114
|
|
|
foreach ( |
115
|
|
|
$this->buildAssets( |
116
|
|
|
$this->manifestReader->getManifest($this->mixManifestPath), |
117
|
|
|
$this->vasriConfig['assets'] |
118
|
|
|
) as $asset |
119
|
|
|
) { |
120
|
|
|
$manifest[$asset] = [ |
121
|
|
|
'sri' => $this->builder->sri($asset), |
122
|
|
|
'version' => $this->builder->versioning($asset) |
123
|
|
|
]; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $manifest; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Deploys the manifest as json file in the Laravel base directory |
131
|
|
|
* |
132
|
|
|
* @throws Exception |
133
|
|
|
*/ |
134
|
|
|
public function deployManifest(): void |
135
|
|
|
{ |
136
|
|
|
file_put_contents(base_path('vasri-manifest.json'), |
137
|
|
|
stripslashes(json_encode($this->buildManifest(), JSON_PRETTY_PRINT))); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.