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\variables; |
13
|
|
|
|
14
|
|
|
use nystudio107\twigpack\Twigpack; |
15
|
|
|
|
16
|
|
|
use craft\helpers\Template; |
17
|
|
|
|
18
|
|
|
use yii\web\NotFoundHttpException; |
19
|
|
|
|
20
|
|
|
use Twig\Markup; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author nystudio107 |
24
|
|
|
* @package Twigpack |
25
|
|
|
* @since 1.0.0 |
26
|
|
|
*/ |
27
|
|
|
class ManifestVariable |
28
|
|
|
{ |
29
|
|
|
// Public Methods |
30
|
|
|
// ========================================================================= |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Returns the uglified loadCSS rel=preload Polyfill as per: |
34
|
|
|
* https://github.com/filamentgroup/loadCSS#how-to-use-loadcss-recommended-example |
35
|
|
|
* |
36
|
|
|
* @return Markup |
37
|
|
|
*/ |
38
|
|
|
public static function includeCssRelPreloadPolyfill(): Markup |
39
|
|
|
{ |
40
|
|
|
return Template::raw( |
41
|
|
|
Twigpack::$plugin->manifest->getCssRelPreloadPolyfill() |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $moduleName |
47
|
|
|
* @param bool $async |
48
|
|
|
* @param array $attributes additional HTML key/value pair attributes to add to the resulting tag |
49
|
|
|
* |
50
|
|
|
* @return Markup |
51
|
|
|
* @throws NotFoundHttpException |
52
|
|
|
*/ |
53
|
|
|
public function includeCssModule(string $moduleName, bool $async = false, array $attributes = []): Markup |
54
|
|
|
{ |
55
|
|
|
return Template::raw( |
56
|
|
|
Twigpack::$plugin->manifest->getCssModuleTags($moduleName, $async, null, $attributes) |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Returns the CSS file in $path wrapped in <style></style> tags |
62
|
|
|
* |
63
|
|
|
* @param string $path |
64
|
|
|
* @param array $attributes additional HTML key/value pair attributes to add to the resulting tag |
65
|
|
|
* |
66
|
|
|
* @return Markup |
67
|
|
|
*/ |
68
|
|
|
public function includeInlineCssTags(string $path, array $attributes = []): Markup |
69
|
|
|
{ |
70
|
|
|
return Template::raw( |
71
|
|
|
Twigpack::$plugin->manifest->getCssInlineTags($path, $attributes) |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Returns the Critical CSS file for $template wrapped in <style></style> |
77
|
|
|
* tags |
78
|
|
|
* |
79
|
|
|
* @param null|string $name |
80
|
|
|
* @param array $attributes additional HTML key/value pair attributes to add to the resulting tag |
81
|
|
|
* |
82
|
|
|
* @return Markup |
83
|
|
|
* @throws \Twig\Error\LoaderError |
84
|
|
|
*/ |
85
|
|
|
public function includeCriticalCssTags($name = null, array $attributes = []): Markup |
86
|
|
|
{ |
87
|
|
|
return Template::raw( |
88
|
|
|
Twigpack::$plugin->manifest->getCriticalCssTags($name, null, $attributes) |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $moduleName |
94
|
|
|
* @param bool $async |
95
|
|
|
* @param array $attributes additional HTML key/value pair attributes to add to the resulting tag |
96
|
|
|
* |
97
|
|
|
* @return null|Markup |
98
|
|
|
* @throws NotFoundHttpException |
99
|
|
|
*/ |
100
|
|
|
public function includeJsModule(string $moduleName, bool $async = false, array $attributes = []) |
101
|
|
|
{ |
102
|
|
|
return Template::raw( |
103
|
|
|
Twigpack::$plugin->manifest->getJsModuleTags($moduleName, $async, null, $attributes) |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Return the URI to a module |
109
|
|
|
* |
110
|
|
|
* @param string $moduleName |
111
|
|
|
* @param string $type |
112
|
|
|
* @param null $config |
|
|
|
|
113
|
|
|
* |
114
|
|
|
* @return null|Markup |
115
|
|
|
* @throws NotFoundHttpException |
116
|
|
|
*/ |
117
|
|
|
public function getModuleUri(string $moduleName, string $type = 'modern', $config = null) |
118
|
|
|
{ |
119
|
|
|
return Template::raw( |
120
|
|
|
Twigpack::$plugin->manifest->getModule($moduleName, $type, $config) |
|
|
|
|
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Return the HASH value from a module |
126
|
|
|
* |
127
|
|
|
* @param string $moduleName |
128
|
|
|
* @param string $type |
129
|
|
|
* @param null $config |
|
|
|
|
130
|
|
|
* |
131
|
|
|
* @return null|Markup |
132
|
|
|
* @throws NotFoundHttpException |
133
|
|
|
*/ |
134
|
|
|
public function getModuleHash(string $moduleName, string $type = 'modern', $config = null) |
135
|
|
|
{ |
136
|
|
|
return Template::raw( |
137
|
|
|
Twigpack::$plugin->manifest->getModuleHash($moduleName, $type, $config) |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Include the Safari 10.1 nomodule fix JavaScript |
143
|
|
|
* |
144
|
|
|
* @param array $attributes additional HTML key/value pair attributes to add to the resulting tag |
145
|
|
|
* |
146
|
|
|
* @return Markup |
147
|
|
|
*/ |
148
|
|
|
public function includeSafariNomoduleFix(array $attributes = []): Markup |
149
|
|
|
{ |
150
|
|
|
return Template::raw( |
151
|
|
|
Twigpack::$plugin->manifest->getSafariNomoduleFix($attributes) |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Returns the contents of a file from a URI path |
157
|
|
|
* |
158
|
|
|
* @param string $path |
159
|
|
|
* |
160
|
|
|
* @return Markup |
161
|
|
|
*/ |
162
|
|
|
public function includeFile(string $path): Markup |
163
|
|
|
{ |
164
|
|
|
return Template::raw( |
165
|
|
|
Twigpack::$plugin->manifest->getFile($path) |
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Returns the contents of a file from the $fileName in the manifest |
171
|
|
|
* |
172
|
|
|
* @param string $fileName |
173
|
|
|
* @param string $type |
174
|
|
|
* @param null $config |
|
|
|
|
175
|
|
|
* |
176
|
|
|
* @return Markup |
177
|
|
|
*/ |
178
|
|
|
public function includeFileFromManifest(string $fileName, string $type = 'legacy', $config = null): Markup |
179
|
|
|
{ |
180
|
|
|
return Template::raw( |
181
|
|
|
Twigpack::$plugin->manifest->getFileFromManifest($fileName, $type, $config) |
182
|
|
|
); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|