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\instantanalytics\helpers; |
13
|
|
|
|
14
|
|
|
use nystudio107\instantanalytics\assetbundles\instantanalytics\InstantAnalyticsAsset; |
15
|
|
|
use nystudio107\instantanalytics\helpers\Manifest as ManifestHelper; |
16
|
|
|
|
17
|
|
|
use Craft; |
18
|
|
|
use craft\helpers\Json as JsonHelper; |
19
|
|
|
use craft\helpers\UrlHelper; |
20
|
|
|
|
21
|
|
|
use yii\base\Exception; |
22
|
|
|
use yii\caching\TagDependency; |
23
|
|
|
use yii\web\NotFoundHttpException; |
24
|
|
|
|
25
|
|
|
/** |
|
|
|
|
26
|
|
|
* @author nystudio107 |
|
|
|
|
27
|
|
|
* @package Twigpack |
|
|
|
|
28
|
|
|
* @since 1.0.0 |
|
|
|
|
29
|
|
|
*/ |
|
|
|
|
30
|
|
|
class Manifest |
31
|
|
|
{ |
32
|
|
|
// Constants |
33
|
|
|
// ========================================================================= |
34
|
|
|
|
35
|
|
|
const ASSET_CLASS = InstantAnalyticsAsset::class; |
36
|
|
|
|
37
|
|
|
const CACHE_KEY = 'twigpack-' . self::ASSET_CLASS; |
38
|
|
|
const CACHE_TAG = 'twigpack-' . self::ASSET_CLASS; |
39
|
|
|
|
40
|
|
|
const DEVMODE_CACHE_DURATION = 1; |
41
|
|
|
|
42
|
|
|
const SUPPRESS_ERRORS_FOR_MODULES = [ |
43
|
|
|
'styles.js', |
44
|
|
|
'commons.js', |
45
|
|
|
'vendors.js', |
46
|
|
|
'vendors.css', |
47
|
|
|
'styles.css', |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
// Protected Static Properties |
51
|
|
|
// ========================================================================= |
52
|
|
|
|
53
|
|
|
protected static $config = [ |
54
|
|
|
// If `devMode` is on, use webpack-dev-server to all for HMR (hot module reloading) |
55
|
|
|
'useDevServer' => false, |
56
|
|
|
// Manifest names |
57
|
|
|
'manifest' => [ |
58
|
|
|
'legacy' => 'manifest.json', |
59
|
|
|
'modern' => 'manifest.json', |
60
|
|
|
], |
61
|
|
|
// Public server config |
62
|
|
|
'server' => [ |
63
|
|
|
'manifestPath' => '/', |
64
|
|
|
'publicPath' => '/', |
65
|
|
|
], |
66
|
|
|
// webpack-dev-server config |
67
|
|
|
'devServer' => [ |
68
|
|
|
'manifestPath' => 'http://127.0.0.1:8080', |
69
|
|
|
'publicPath' => '/', |
70
|
|
|
], |
71
|
|
|
]; |
72
|
|
|
|
73
|
|
|
/** |
|
|
|
|
74
|
|
|
* @var array |
75
|
|
|
*/ |
76
|
|
|
protected static $files; |
77
|
|
|
|
78
|
|
|
/** |
|
|
|
|
79
|
|
|
* @var bool |
80
|
|
|
*/ |
81
|
|
|
protected static $isHot = false; |
82
|
|
|
|
83
|
|
|
// Public Static Methods |
84
|
|
|
// ========================================================================= |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Simulate a static constructor |
88
|
|
|
* |
89
|
|
|
* ManifestVariable constructor. |
90
|
|
|
* @noinspection MagicMethodsValidityInspection |
|
|
|
|
91
|
|
|
*/ |
|
|
|
|
92
|
|
|
public static function __constructStatic() |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
ManifestHelper::invalidateCaches(); |
95
|
|
|
$assetClass = self::ASSET_CLASS; |
96
|
|
|
$bundle = new $assetClass; |
97
|
|
|
$baseAssetsUrl = Craft::$app->assetManager->getPublishedUrl( |
98
|
|
|
$bundle->sourcePath, |
99
|
|
|
true |
|
|
|
|
100
|
|
|
); |
101
|
|
|
self::$config['server']['manifestPath'] = Craft::getAlias($bundle->sourcePath); |
102
|
|
|
self::$config['server']['publicPath'] = $baseAssetsUrl; |
103
|
|
|
$useDevServer = getenv('NYS_PLUGIN_DEVSERVER'); |
104
|
|
|
if ($useDevServer !== false) { |
105
|
|
|
self::$config['useDevServer'] = (bool)$useDevServer; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get the passed in JS modules from the manifest, and register them in the current Craft view |
111
|
|
|
* |
112
|
|
|
* @param array $modules |
|
|
|
|
113
|
|
|
* @throws NotFoundHttpException |
|
|
|
|
114
|
|
|
* @throws \yii\base\InvalidConfigException |
|
|
|
|
115
|
|
|
*/ |
|
|
|
|
116
|
|
|
public static function registerJsModules(array $modules) |
117
|
|
|
{ |
118
|
|
|
$view = Craft::$app->getView(); |
119
|
|
|
foreach($modules as $module) { |
|
|
|
|
120
|
|
|
$jsModule = self::getModule(self::$config, $module, 'modern'); |
121
|
|
|
if ($jsModule) { |
122
|
|
|
$view->registerJsFile($jsModule, [ |
|
|
|
|
123
|
|
|
'depends' => self::ASSET_CLASS |
124
|
|
|
]); |
|
|
|
|
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get the passed in CS modules from the manifest, and register them in the current Craft view |
131
|
|
|
* |
132
|
|
|
* @param array $modules |
|
|
|
|
133
|
|
|
* @throws NotFoundHttpException |
|
|
|
|
134
|
|
|
* @throws \yii\base\InvalidConfigException |
|
|
|
|
135
|
|
|
*/ |
|
|
|
|
136
|
|
|
public static function registerCssModules(array $modules) |
137
|
|
|
{ |
138
|
|
|
$view = Craft::$app->getView(); |
139
|
|
|
foreach($modules as $module) { |
|
|
|
|
140
|
|
|
$cssModule = self::getModule(self::$config, $module, 'legacy'); |
141
|
|
|
if ($cssModule) { |
142
|
|
|
$view->registerCssFile($cssModule, [ |
|
|
|
|
143
|
|
|
'depends' => self::ASSET_CLASS |
144
|
|
|
]); |
|
|
|
|
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
// Protected Static Methods |
150
|
|
|
// ========================================================================= |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Return the URI to a module |
154
|
|
|
* |
155
|
|
|
* @param array $config |
|
|
|
|
156
|
|
|
* @param string $moduleName |
|
|
|
|
157
|
|
|
* @param string $type |
|
|
|
|
158
|
|
|
* @param bool $soft |
|
|
|
|
159
|
|
|
* |
160
|
|
|
* @return null|string |
161
|
|
|
* @throws NotFoundHttpException |
162
|
|
|
*/ |
163
|
|
|
protected static function getModule(array $config, string $moduleName, string $type = 'modern', bool $soft = false) |
164
|
|
|
{ |
165
|
|
|
// Get the module entry |
166
|
|
|
$module = self::getModuleEntry($config, $moduleName, $type, $soft); |
167
|
|
|
if ($module !== null) { |
168
|
|
|
$prefix = self::$isHot |
169
|
|
|
? $config['devServer']['publicPath'] |
170
|
|
|
: $config['server']['publicPath']; |
171
|
|
|
// If the module isn't a full URL, prefix it |
172
|
|
|
if (!UrlHelper::isAbsoluteUrl($module)) { |
173
|
|
|
$module = self::combinePaths($prefix, $module); |
174
|
|
|
} |
175
|
|
|
// Resolve any aliases |
176
|
|
|
$alias = Craft::getAlias($module, false); |
177
|
|
|
if ($alias) { |
178
|
|
|
$module = $alias; |
179
|
|
|
} |
180
|
|
|
// Make sure it's a full URL |
181
|
|
|
if (!UrlHelper::isAbsoluteUrl($module) && !is_file($module)) { |
|
|
|
|
182
|
|
|
try { |
183
|
|
|
$module = UrlHelper::siteUrl($module); |
|
|
|
|
184
|
|
|
} catch (Exception $e) { |
185
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return $module; |
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Return a module's raw entry from the manifest |
195
|
|
|
* |
196
|
|
|
* @param array $config |
|
|
|
|
197
|
|
|
* @param string $moduleName |
|
|
|
|
198
|
|
|
* @param string $type |
|
|
|
|
199
|
|
|
* @param bool $soft |
|
|
|
|
200
|
|
|
* |
201
|
|
|
* @return null|string |
202
|
|
|
* @throws NotFoundHttpException |
203
|
|
|
*/ |
204
|
|
|
protected static function getModuleEntry(array $config, string $moduleName, string $type = 'modern', bool $soft = false) |
205
|
|
|
{ |
206
|
|
|
$module = null; |
207
|
|
|
// Get the manifest file |
208
|
|
|
$manifest = self::getManifestFile($config, $type); |
209
|
|
|
if ($manifest !== null) { |
210
|
|
|
// Make sure it exists in the manifest |
211
|
|
|
if (empty($manifest[$moduleName])) { |
212
|
|
|
// Don't report errors for any files in SUPPRESS_ERRORS_FOR_MODULES |
213
|
|
|
if (!in_array($moduleName, self::SUPPRESS_ERRORS_FOR_MODULES)) { |
214
|
|
|
self::reportError(Craft::t( |
|
|
|
|
215
|
|
|
'instant-analytics', |
216
|
|
|
'Module does not exist in the manifest: {moduleName}', |
217
|
|
|
['moduleName' => $moduleName] |
218
|
|
|
), $soft); |
|
|
|
|
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return null; |
222
|
|
|
} |
223
|
|
|
$module = $manifest[$moduleName]; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return $module; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Return a JSON-decoded manifest file |
231
|
|
|
* |
232
|
|
|
* @param array $config |
|
|
|
|
233
|
|
|
* @param string $type |
|
|
|
|
234
|
|
|
* |
235
|
|
|
* @return null|array |
236
|
|
|
* @throws NotFoundHttpException |
237
|
|
|
*/ |
238
|
|
|
protected static function getManifestFile(array $config, string $type = 'modern') |
239
|
|
|
{ |
240
|
|
|
$manifest = null; |
241
|
|
|
// Determine whether we should use the devServer for HMR or not |
242
|
|
|
$devMode = Craft::$app->getConfig()->getGeneral()->devMode; |
243
|
|
|
self::$isHot = ($devMode && $config['useDevServer']); |
244
|
|
|
// Try to get the manifest |
245
|
|
|
while ($manifest === null) { |
246
|
|
|
$manifestPath = self::$isHot |
247
|
|
|
? $config['devServer']['manifestPath'] |
248
|
|
|
: $config['server']['manifestPath']; |
249
|
|
|
// Normalize the path |
250
|
|
|
$path = self::combinePaths($manifestPath, $config['manifest'][$type]); |
251
|
|
|
$manifest = self::getJsonFile($path); |
252
|
|
|
// If the manifest isn't found, and it was hot, fall back on non-hot |
253
|
|
|
if ($manifest === null) { |
254
|
|
|
// We couldn't find a manifest; throw an error |
255
|
|
|
self::reportError(Craft::t( |
|
|
|
|
256
|
|
|
'instant-analytics', |
257
|
|
|
'Manifest file not found at: {manifestPath}', |
258
|
|
|
['manifestPath' => $manifestPath] |
259
|
|
|
), true); |
|
|
|
|
260
|
|
|
if (self::$isHot) { |
261
|
|
|
// Try again, but not with home module replacement |
262
|
|
|
self::$isHot = false; |
263
|
|
|
} else { |
264
|
|
|
// Give up and return null |
265
|
|
|
return null; |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
return $manifest; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Return the contents of a JSON file from a URI path |
275
|
|
|
* |
276
|
|
|
* @param string $path |
|
|
|
|
277
|
|
|
* |
278
|
|
|
* @return null|array |
279
|
|
|
*/ |
280
|
|
|
protected static function getJsonFile(string $path) |
281
|
|
|
{ |
282
|
|
|
return self::getFileFromUri($path, [self::class, 'jsonFileDecode']); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Invalidate all of the manifest caches |
287
|
|
|
*/ |
|
|
|
|
288
|
|
|
public static function invalidateCaches() |
289
|
|
|
{ |
290
|
|
|
$cache = Craft::$app->getCache(); |
291
|
|
|
TagDependency::invalidate($cache, self::CACHE_TAG); |
292
|
|
|
Craft::info('All manifest caches cleared', __METHOD__); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Return the contents of a file from a URI path |
297
|
|
|
* |
298
|
|
|
* @param string $path |
|
|
|
|
299
|
|
|
* @param callable|null $callback |
|
|
|
|
300
|
|
|
* |
301
|
|
|
* @return null|mixed |
302
|
|
|
*/ |
303
|
|
|
protected static function getFileFromUri(string $path, callable $callback = null) |
304
|
|
|
{ |
305
|
|
|
// Resolve any aliases |
306
|
|
|
$alias = Craft::getAlias($path, false); |
307
|
|
|
if ($alias) { |
308
|
|
|
$path = $alias; |
309
|
|
|
} |
310
|
|
|
// Make sure it's a full URL |
311
|
|
|
if (!UrlHelper::isAbsoluteUrl($path) && !is_file($path)) { |
|
|
|
|
312
|
|
|
try { |
313
|
|
|
$path = UrlHelper::siteUrl($path); |
|
|
|
|
314
|
|
|
} catch (Exception $e) { |
315
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
return self::getFileContents($path, $callback); |
|
|
|
|
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Return the contents of a file from the passed in path |
324
|
|
|
* |
325
|
|
|
* @param string $path |
|
|
|
|
326
|
|
|
* @param callable $callback |
|
|
|
|
327
|
|
|
* |
328
|
|
|
* @return null|mixed |
329
|
|
|
*/ |
330
|
|
|
protected static function getFileContents(string $path, callable $callback = null) |
331
|
|
|
{ |
332
|
|
|
// Return the memoized manifest if it exists |
333
|
|
|
if (!empty(self::$files[$path])) { |
334
|
|
|
return self::$files[$path]; |
335
|
|
|
} |
336
|
|
|
// Create the dependency tags |
337
|
|
|
$dependency = new TagDependency([ |
|
|
|
|
338
|
|
|
'tags' => [ |
339
|
|
|
self::CACHE_TAG, |
340
|
|
|
self::CACHE_TAG.$path, |
341
|
|
|
], |
342
|
|
|
]); |
|
|
|
|
343
|
|
|
// Set the cache duration based on devMode |
344
|
|
|
$cacheDuration = Craft::$app->getConfig()->getGeneral()->devMode |
345
|
|
|
? self::DEVMODE_CACHE_DURATION |
346
|
|
|
: null; |
347
|
|
|
// Get the result from the cache, or parse the file |
348
|
|
|
$cache = Craft::$app->getCache(); |
349
|
|
|
$file = $cache->getOrSet( |
350
|
|
|
self::CACHE_KEY.$path, |
351
|
|
|
function () use ($path, $callback) { |
352
|
|
|
$result = null; |
353
|
|
|
$contents = @file_get_contents($path); |
354
|
|
|
if ($contents) { |
355
|
|
|
$result = $contents; |
356
|
|
|
if ($callback) { |
357
|
|
|
$result = $callback($result); |
358
|
|
|
} |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
return $result; |
362
|
|
|
}, |
363
|
|
|
$cacheDuration, |
364
|
|
|
$dependency |
365
|
|
|
); |
366
|
|
|
self::$files[$path] = $file; |
367
|
|
|
|
368
|
|
|
return $file; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* Combined the passed in paths, whether file system or URL |
373
|
|
|
* |
374
|
|
|
* @param string ...$paths |
|
|
|
|
375
|
|
|
* |
376
|
|
|
* @return string |
377
|
|
|
*/ |
378
|
|
|
protected static function combinePaths(string ...$paths): string |
379
|
|
|
{ |
380
|
|
|
$last_key = \count($paths) - 1; |
381
|
|
|
array_walk($paths, function (&$val, $key) use ($last_key) { |
|
|
|
|
382
|
|
|
switch ($key) { |
383
|
|
|
case 0: |
|
|
|
|
384
|
|
|
$val = rtrim($val, '/ '); |
385
|
|
|
break; |
386
|
|
|
case $last_key: |
|
|
|
|
387
|
|
|
$val = ltrim($val, '/ '); |
388
|
|
|
break; |
389
|
|
|
default: |
|
|
|
|
390
|
|
|
$val = trim($val, '/ '); |
391
|
|
|
break; |
392
|
|
|
} |
393
|
|
|
}); |
|
|
|
|
394
|
|
|
|
395
|
|
|
$first = array_shift($paths); |
396
|
|
|
$last = array_pop($paths); |
397
|
|
|
$paths = array_filter($paths); |
398
|
|
|
array_unshift($paths, $first); |
399
|
|
|
$paths[] = $last; |
400
|
|
|
|
401
|
|
|
return implode('/', $paths); |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
/** |
|
|
|
|
405
|
|
|
* @param string $error |
|
|
|
|
406
|
|
|
* @param bool $soft |
|
|
|
|
407
|
|
|
* |
408
|
|
|
* @throws NotFoundHttpException |
409
|
|
|
*/ |
|
|
|
|
410
|
|
|
protected static function reportError(string $error, $soft = false) |
411
|
|
|
{ |
412
|
|
|
$devMode = Craft::$app->getConfig()->getGeneral()->devMode; |
413
|
|
|
if ($devMode && !$soft) { |
414
|
|
|
throw new NotFoundHttpException($error); |
415
|
|
|
} |
416
|
|
|
Craft::error($error, __METHOD__); |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
// Private Static Methods |
420
|
|
|
// ========================================================================= |
421
|
|
|
|
422
|
|
|
/** |
|
|
|
|
423
|
|
|
* @param $string |
|
|
|
|
424
|
|
|
* |
425
|
|
|
* @return mixed |
426
|
|
|
*/ |
427
|
|
|
private static function jsonFileDecode($string) |
|
|
|
|
428
|
|
|
{ |
429
|
|
|
return JsonHelper::decodeIfJson($string); |
430
|
|
|
} |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
// Simulate a static constructor |
434
|
|
|
Manifest::__constructStatic(); |
435
|
|
|
|