Total Complexity | 42 |
Total Lines | 377 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Manifest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Manifest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class Manifest |
||
28 | { |
||
29 | // Constants |
||
30 | // ========================================================================= |
||
31 | |||
32 | const CACHE_KEY = 'twigpack'; |
||
33 | const CACHE_TAG = 'twigpack'; |
||
34 | |||
35 | const DEVMODE_CACHE_DURATION = 1; |
||
36 | |||
37 | // Protected Static Properties |
||
38 | // ========================================================================= |
||
39 | |||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | protected static $files; |
||
44 | |||
45 | // Public Static Methods |
||
46 | // ========================================================================= |
||
47 | |||
48 | /** |
||
49 | * @param array $config |
||
50 | * @param string $moduleName |
||
51 | * @param bool $async |
||
52 | * |
||
53 | * @return null|string |
||
54 | * @throws NotFoundHttpException |
||
55 | */ |
||
56 | public static function getCssModuleTags(array $config, string $moduleName, bool $async) |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Returns the uglified loadCSS rel=preload Polyfill as per: |
||
75 | * https://github.com/filamentgroup/loadCSS#how-to-use-loadcss-recommended-example |
||
76 | * |
||
77 | * @return string |
||
78 | */ |
||
79 | public static function getCssRelPreloadPolyfill(): string |
||
82 | <script> |
||
83 | /*! loadCSS. [c]2017 Filament Group, Inc. MIT License */ |
||
84 | !function(t){"use strict";t.loadCSS||(t.loadCSS=function(){});var e=loadCSS.relpreload={};if(e.support=function(){var e;try{e=t.document.createElement("link").relList.supports("preload")}catch(t){e=!1}return function(){return e}}(),e.bindMediaToggle=function(t){var e=t.media||"all";function a(){t.media=e}t.addEventListener?t.addEventListener("load",a):t.attachEvent&&t.attachEvent("onload",a),setTimeout(function(){t.rel="stylesheet",t.media="only x"}),setTimeout(a,3e3)},e.poly=function(){if(!e.support())for(var a=t.document.getElementsByTagName("link"),n=0;n<a.length;n++){var o=a[n];"preload"!==o.rel||"style"!==o.getAttribute("as")||o.getAttribute("data-loadcss")||(o.setAttribute("data-loadcss",!0),e.bindMediaToggle(o))}},!e.support()){e.poly();var a=t.setInterval(e.poly,500);t.addEventListener?t.addEventListener("load",function(){e.poly(),t.clearInterval(a)}):t.attachEvent&&t.attachEvent("onload",function(){e.poly(),t.clearInterval(a)})}"undefined"!=typeof exports?exports.loadCSS=loadCSS:t.loadCSS=loadCSS}("undefined"!=typeof global?global:this); |
||
85 | </script> |
||
86 | EOT; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @param array $config |
||
91 | * @param string $moduleName |
||
92 | * @param bool $async |
||
93 | * |
||
94 | * @return null|string |
||
95 | * @throws NotFoundHttpException |
||
96 | */ |
||
97 | public static function getJsModuleTags(array $config, string $moduleName, bool $async) |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Safari 10.1 supports modules, but does not support the `nomodule` |
||
122 | * attribute - it will load <script nomodule> anyway. This snippet solve |
||
123 | * this problem, but only for script tags that load external code, e.g.: |
||
124 | * <script nomodule src="nomodule.js"></script> |
||
125 | * |
||
126 | * Again: this will **not* # prevent inline script, e.g.: |
||
127 | * <script nomodule>alert('no modules');</script>. |
||
128 | * |
||
129 | * This workaround is possible because Safari supports the non-standard |
||
130 | * 'beforeload' event. This allows us to trap the module and nomodule load. |
||
131 | * |
||
132 | * Note also that `nomodule` is supported in later versions of Safari - |
||
133 | * it's just 10.1 that omits this attribute. |
||
134 | * |
||
135 | * c.f.: https://gist.github.com/samthor/64b114e4a4f539915a95b91ffd340acc |
||
136 | * |
||
137 | * @return string |
||
138 | */ |
||
139 | public static function getSafariNomoduleFix(): string |
||
142 | <script> |
||
143 | !function(){var e=document,t=e.createElement("script");if(!("noModule"in t)&&"onbeforeload"in t){var n=!1;e.addEventListener("beforeload",function(e){if(e.target===t)n=!0;else if(!e.target.hasAttribute("nomodule")||!n)return;e.preventDefault()},!0),t.type="module",t.src=".",e.head.appendChild(t),t.remove()}}(); |
||
144 | </script> |
||
145 | EOT; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Return the URI to a module |
||
150 | * |
||
151 | * @param array $config |
||
152 | * @param string $moduleName |
||
153 | * @param string $type |
||
154 | * @param bool $soft |
||
155 | * |
||
156 | * @return null|string |
||
157 | * @throws NotFoundHttpException |
||
158 | */ |
||
159 | public static function getModule(array $config, string $moduleName, string $type = 'modern', bool $soft = false) |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Return a JSON-decoded manifest file |
||
201 | * |
||
202 | * @param array $config |
||
203 | * @param bool $isHot |
||
204 | * @param string $type |
||
205 | * |
||
206 | * @return null|array |
||
207 | * @throws NotFoundHttpException |
||
208 | */ |
||
209 | public static function getManifestFile(array $config, bool &$isHot, string $type = 'modern') |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Returns the contents of a file from a URI path |
||
243 | * |
||
244 | * @param string $path |
||
245 | * |
||
246 | * @return mixed |
||
247 | */ |
||
248 | public static function getFile($path) |
||
249 | { |
||
250 | return self::getFileFromUri($path, null); |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * Invalidate all of the manifest caches |
||
255 | */ |
||
256 | public static function invalidateCaches() |
||
257 | { |
||
258 | $cache = Craft::$app->getCache(); |
||
259 | TagDependency::invalidate($cache, self::CACHE_TAG); |
||
260 | Craft::info('All manifest caches cleared', __METHOD__); |
||
261 | } |
||
262 | |||
263 | // Protected Static Methods |
||
264 | // ========================================================================= |
||
265 | |||
266 | /** |
||
267 | * Return the contents of a JSON file from a URI path |
||
268 | * |
||
269 | * @param string $path |
||
270 | * |
||
271 | * @return mixed |
||
272 | */ |
||
273 | protected static function getJsonFileFromUri(string $path) |
||
274 | { |
||
275 | return self::getFileFromUri($path, [self::class, 'jsonFileDecode']); |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Return the contents of a file from a URI path |
||
280 | * |
||
281 | * @param string $path |
||
282 | * @param callable|null $callback |
||
283 | * |
||
284 | * @return mixed |
||
285 | */ |
||
286 | protected static function getFileFromUri(string $path, callable $callback = null) |
||
287 | { |
||
288 | // Make sure it's a full URL |
||
289 | if (!UrlHelper::isAbsoluteUrl($path) && !is_file($path)) { |
||
290 | try { |
||
291 | $path = UrlHelper::siteUrl($path); |
||
292 | } catch (Exception $e) { |
||
293 | Craft::error($e->getMessage(), __METHOD__); |
||
294 | } |
||
295 | } |
||
296 | |||
297 | return self::getFileContents($path, $callback); |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * Return the contents of a file from the passed in path |
||
302 | * |
||
303 | * @param string $path |
||
304 | * @param callable $callback |
||
305 | * |
||
306 | * @return mixed |
||
307 | */ |
||
308 | protected static function getFileContents(string $path, callable $callback = null) |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * Combined the passed in paths, whether file system or URL |
||
347 | * |
||
348 | * @param string ...$paths |
||
349 | * |
||
350 | * @return string |
||
351 | */ |
||
352 | protected static function combinePaths(string ...$paths): string |
||
353 | { |
||
354 | $last_key = \count($paths) - 1; |
||
355 | array_walk($paths, function (&$val, $key) use ($last_key) { |
||
356 | switch ($key) { |
||
357 | case 0: |
||
358 | $val = rtrim($val, '/ '); |
||
359 | break; |
||
360 | case $last_key: |
||
361 | $val = ltrim($val, '/ '); |
||
362 | break; |
||
363 | default: |
||
364 | $val = trim($val, '/ '); |
||
365 | break; |
||
366 | } |
||
367 | }); |
||
368 | |||
369 | $first = array_shift($paths); |
||
370 | $last = array_pop($paths); |
||
371 | $paths = array_filter($paths); |
||
372 | array_unshift($paths, $first); |
||
373 | $paths[] = $last; |
||
374 | |||
375 | return implode('/', $paths); |
||
376 | } |
||
377 | |||
378 | /** |
||
379 | * @param string $error |
||
380 | * @param bool $soft |
||
381 | * |
||
382 | * @throws NotFoundHttpException |
||
383 | */ |
||
384 | protected static function reportError(string $error, $soft = false) |
||
391 | } |
||
392 | |||
393 | // Private Static Methods |
||
394 | // ========================================================================= |
||
395 | |||
396 | /** |
||
397 | * @param $string |
||
398 | * |
||
399 | * @return mixed |
||
400 | */ |
||
401 | private function jsonFileDecode($string) |
||
404 | } |
||
405 | } |
||
406 |