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