1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Twigpack plugin for Craft CMS 3.x |
4
|
|
|
* |
5
|
|
|
* Twigpack is the conduit between Twig and webpack, with manifest.json & |
6
|
|
|
* webpack-dev-server HMR support |
7
|
|
|
* |
8
|
|
|
* @link https://nystudio107.com/ |
|
|
|
|
9
|
|
|
* @copyright Copyright (c) 2018 nystudio107 |
|
|
|
|
10
|
|
|
*/ |
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace nystudio107\twigpack\services; |
13
|
|
|
|
14
|
|
|
use craft\base\Component; |
15
|
|
|
use nystudio107\twigpack\helpers\Manifest as ManifestHelper; |
16
|
|
|
use nystudio107\twigpack\Twigpack; |
17
|
|
|
use Twig\Error\LoaderError; |
18
|
|
|
use yii\web\NotFoundHttpException; |
19
|
|
|
|
20
|
|
|
/** @noinspection MissingPropertyAnnotationsInspection */ |
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
|
|
|
|
23
|
|
|
* @author nystudio107 |
|
|
|
|
24
|
|
|
* @package Twigpack |
|
|
|
|
25
|
|
|
* @since 1.0.0 |
|
|
|
|
26
|
|
|
*/ |
|
|
|
|
27
|
|
|
class Manifest extends Component |
28
|
|
|
{ |
29
|
|
|
// Public Methods |
30
|
|
|
// ========================================================================= |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Return the HTML tags to include the CSS |
34
|
|
|
* |
35
|
|
|
* @param string $moduleName |
|
|
|
|
36
|
|
|
* @param bool $async |
|
|
|
|
37
|
|
|
* @param null|array $config |
|
|
|
|
38
|
|
|
* @param array $attributes additional HTML key/value pair attributes to add to the resulting tag |
|
|
|
|
39
|
|
|
* |
40
|
|
|
* @return string |
41
|
|
|
* @throws NotFoundHttpException |
42
|
|
|
*/ |
43
|
|
|
public function getCssModuleTags(string $moduleName, bool $async = false, $config = null, array $attributes = []): string |
44
|
|
|
{ |
45
|
|
|
$settings = Twigpack::$plugin->getSettings(); |
|
|
|
|
46
|
|
|
$config = $config ?? $settings->getAttributes(); |
47
|
|
|
|
48
|
|
|
return ManifestHelper::getCssModuleTags($config, $moduleName, $async, $attributes); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Returns the CSS file in $path wrapped in <style></style> tags |
53
|
|
|
* |
54
|
|
|
* @param string $path |
|
|
|
|
55
|
|
|
* @param array $attributes additional HTML key/value pair attributes to add to the resulting tag |
|
|
|
|
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function getCssInlineTags(string $path, array $attributes = []): string |
60
|
|
|
{ |
61
|
|
|
return ManifestHelper::getCssInlineTags($path, $attributes); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
|
|
|
|
65
|
|
|
* @param array $config |
|
|
|
|
66
|
|
|
* @param null|string $name |
|
|
|
|
67
|
|
|
* @param array $attributes additional HTML key/value pair attributes to add to the resulting tag |
|
|
|
|
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
* @throws LoaderError |
71
|
|
|
*/ |
72
|
|
|
public function getCriticalCssTags(?string $name = null, ?array $config = null, array $attributes = []): string |
73
|
|
|
{ |
74
|
|
|
$settings = Twigpack::$plugin->getSettings(); |
75
|
|
|
$config = $config ?? $settings->getAttributes(); |
76
|
|
|
|
77
|
|
|
return ManifestHelper::getCriticalCssTags($config, $name, $attributes); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns the uglified loadCSS rel=preload Polyfill as per: |
82
|
|
|
* https://github.com/filamentgroup/loadCSS#how-to-use-loadcss-recommended-example |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public function getCssRelPreloadPolyfill(): string |
87
|
|
|
{ |
88
|
|
|
return ManifestHelper::getCssRelPreloadPolyfill(); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Return the HTML tags to include the JavaScript module |
93
|
|
|
* |
94
|
|
|
* @param string $moduleName |
|
|
|
|
95
|
|
|
* @param bool $async |
|
|
|
|
96
|
|
|
* @param null|array $config |
|
|
|
|
97
|
|
|
* @param array $attributes additional HTML key/value pair attributes to add to the resulting tag |
|
|
|
|
98
|
|
|
* |
99
|
|
|
* @return null|string |
100
|
|
|
* @throws NotFoundHttpException |
101
|
|
|
*/ |
102
|
|
|
public function getJsModuleTags(string $moduleName, bool $async = false, ?array $config = null, array $attributes = []) |
103
|
|
|
{ |
104
|
|
|
$settings = Twigpack::$plugin->getSettings(); |
105
|
|
|
$config = $config ?? $settings->getAttributes(); |
106
|
|
|
|
107
|
|
|
return ManifestHelper::getJsModuleTags($config, $moduleName, $async, $attributes); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Return the Safari 10.1 nomodule JavaScript fix |
112
|
|
|
* |
113
|
|
|
* @param array $attributes additional HTML key/value pair attributes to add to the resulting tag |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
public function getSafariNomoduleFix(array $attributes = []): string |
118
|
|
|
{ |
119
|
|
|
return ManifestHelper::getSafariNomoduleFix($attributes); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Return the URI to a module |
124
|
|
|
* |
125
|
|
|
* @param string $moduleName |
|
|
|
|
126
|
|
|
* @param string $type |
|
|
|
|
127
|
|
|
* @param ?array $config |
|
|
|
|
128
|
|
|
* |
129
|
|
|
* @return null|string |
130
|
|
|
* @throws NotFoundHttpException |
131
|
|
|
*/ |
132
|
|
|
public function getModule(string $moduleName, string $type = 'modern', $config = null): ?string |
133
|
|
|
{ |
134
|
|
|
$settings = Twigpack::$plugin->getSettings(); |
135
|
|
|
$config = $config ?? $settings->getAttributes(); |
136
|
|
|
|
137
|
|
|
return ManifestHelper::getModule($config, $moduleName, $type); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Return the HASH value from a module |
142
|
|
|
* |
143
|
|
|
* @param string $moduleName |
|
|
|
|
144
|
|
|
* @param string $type |
|
|
|
|
145
|
|
|
* @param ?array $config |
|
|
|
|
146
|
|
|
* |
147
|
|
|
* @return null|string |
148
|
|
|
* @throws NotFoundHttpException |
149
|
|
|
*/ |
150
|
|
|
public function getModuleHash(string $moduleName, string $type = 'modern', $config = null): ?string |
151
|
|
|
{ |
152
|
|
|
$settings = Twigpack::$plugin->getSettings(); |
153
|
|
|
$config = $config ?? $settings->getAttributes(); |
154
|
|
|
|
155
|
|
|
return ManifestHelper::getModuleHash($config, $moduleName, $type); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Returns the contents of a file from a URI path |
160
|
|
|
* |
161
|
|
|
* @param string $path |
|
|
|
|
162
|
|
|
* |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
|
public function getFile(string $path): string |
166
|
|
|
{ |
167
|
|
|
return ManifestHelper::getFile($path); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Returns the contents of a file from the $fileName in the manifest |
172
|
|
|
* |
173
|
|
|
* @param string $fileName |
|
|
|
|
174
|
|
|
* @param string $type |
|
|
|
|
175
|
|
|
* @param ?array $config |
|
|
|
|
176
|
|
|
* |
177
|
|
|
* @return string |
178
|
|
|
*/ |
179
|
|
|
public function getFileFromManifest(string $fileName, string $type = 'legacy', $config = null): string |
180
|
|
|
{ |
181
|
|
|
$settings = Twigpack::$plugin->getSettings(); |
182
|
|
|
$config = $config ?? $settings->getAttributes(); |
183
|
|
|
|
184
|
|
|
return ManifestHelper::getFileFromManifest($config, $fileName, $type); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Invalidate the manifest cache |
189
|
|
|
*/ |
|
|
|
|
190
|
|
|
public function invalidateCaches() |
191
|
|
|
{ |
192
|
|
|
ManifestHelper::invalidateCaches(); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|