Total Complexity | 43 |
Total Lines | 421 |
Duplicated Lines | 0 % |
Changes | 23 | ||
Bugs | 0 | Features | 2 |
Complex classes like ViteService 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 ViteService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class ViteService extends Component |
||
33 | { |
||
34 | // Constants |
||
35 | // ========================================================================= |
||
36 | |||
37 | const VITE_CLIENT = '@vite/client'; |
||
38 | const LEGACY_POLYFILLS = 'vite/legacy-polyfills'; |
||
39 | |||
40 | // Public Properties |
||
41 | // ========================================================================= |
||
42 | |||
43 | /** |
||
44 | * @var bool Should the dev server be used for? |
||
45 | */ |
||
46 | public $useDevServer; |
||
47 | |||
48 | /** |
||
49 | * @var string File system path (or URL) to the Vite-built manifest.json |
||
50 | */ |
||
51 | public $manifestPath; |
||
52 | |||
53 | /** |
||
54 | * @var string The public URL to the dev server (what appears in `<script src="">` tags |
||
55 | */ |
||
56 | public $devServerPublic; |
||
57 | |||
58 | /** |
||
59 | * @var string The public URL to use when not using the dev server |
||
60 | */ |
||
61 | public $serverPublic; |
||
62 | |||
63 | /** |
||
64 | * @var string The JavaScript entry from the manifest.json to inject on Twig error pages |
||
65 | * This can be a string or an array of strings |
||
66 | */ |
||
67 | public $errorEntry = ''; |
||
68 | |||
69 | /** |
||
70 | * @var string String to be appended to the cache key |
||
71 | */ |
||
72 | public $cacheKeySuffix = ''; |
||
73 | |||
74 | /** |
||
75 | * @var string The internal URL to the dev server, when accessed from the environment in which PHP is executing |
||
76 | * This can be the same as `$devServerPublic`, but may be different in containerized or VM setups. |
||
77 | * ONLY used if $checkDevServer = true |
||
78 | */ |
||
79 | public $devServerInternal; |
||
80 | |||
81 | /** |
||
82 | * @var bool Should we check for the presence of the dev server by pinging $devServerInternal to make sure it's running? |
||
83 | */ |
||
84 | public $checkDevServer = false; |
||
85 | |||
86 | /** |
||
87 | * @var bool Whether the react-refresh-shim should be included |
||
88 | */ |
||
89 | public $includeReactRefreshShim = false; |
||
90 | |||
91 | // Protected Properties |
||
92 | // ========================================================================= |
||
93 | |||
94 | /** |
||
95 | * @var bool Whether the manifest shims has been included yet or not |
||
96 | */ |
||
97 | protected $manifestShimsIncluded = false; |
||
98 | |||
99 | /** |
||
100 | * @var bool Whether the dev server shims has been included yet or not |
||
101 | */ |
||
102 | protected $devServerShimsIncluded = false; |
||
103 | |||
104 | // Public Methods |
||
105 | // ========================================================================= |
||
106 | |||
107 | /** |
||
108 | * @inheritDoc |
||
109 | */ |
||
110 | public function init() |
||
121 | } |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Return the appropriate tags to load the Vite script, either via the dev server or |
||
126 | * extracting it from the manifest.json file |
||
127 | * |
||
128 | * @param string $path |
||
129 | * @param bool $asyncCss |
||
130 | * @param array $scriptTagAttrs |
||
131 | * @param array $cssTagAttrs |
||
132 | * |
||
133 | * @return string |
||
134 | */ |
||
135 | public function script(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []): string |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Return the script tag to load the script from the Vite dev server |
||
146 | * |
||
147 | * @param string $path |
||
148 | * @param array $scriptTagAttrs |
||
149 | * |
||
150 | * @return string |
||
151 | */ |
||
152 | public function devServerScript(string $path, array $scriptTagAttrs = []): string |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Return the script, module link, and CSS link tags for the script from the manifest.json file |
||
186 | * |
||
187 | * @param string $path |
||
188 | * @param bool $asyncCss |
||
189 | * @param array $scriptTagAttrs |
||
190 | * @param array $cssTagAttrs |
||
191 | * |
||
192 | * @return string |
||
193 | */ |
||
194 | public function manifestScript(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []): string |
||
195 | { |
||
196 | $lines = []; |
||
197 | ManifestHelper::fetchManifest($this->manifestPath); |
||
198 | $tags = ManifestHelper::manifestTags($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs); |
||
199 | $legacyTags = ManifestHelper::legacyManifestTags($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs); |
||
200 | // Include any manifest shims |
||
201 | if (!$this->manifestShimsIncluded) { |
||
202 | // Handle any legacy polyfills |
||
203 | if (!empty($legacyTags)) { |
||
204 | $lines[] = HtmlHelper::script( |
||
205 | FileHelper::fetchScript('safari-nomodule-fix.min.js', $this->cacheKeySuffix), |
||
206 | [] |
||
207 | ); |
||
208 | $legacyPolyfillTags = ManifestHelper::extractManifestTags(self::LEGACY_POLYFILLS, $asyncCss, $scriptTagAttrs, $cssTagAttrs, true); |
||
209 | $tags = array_merge($legacyPolyfillTags, $tags); |
||
210 | } |
||
211 | $this->manifestShimsIncluded = true; |
||
212 | } |
||
213 | |||
214 | foreach(array_merge($tags, $legacyTags) as $tag) { |
||
215 | if (!empty($tag)) { |
||
216 | $url = FileHelper::createUrl($this->serverPublic, $tag['url']); |
||
217 | switch ($tag['type']) { |
||
218 | case 'import': |
||
219 | $lines[] = HtmlHelper::tag('link', '', [ |
||
220 | 'crossorigin' => $tag['crossorigin'], |
||
221 | 'href' => $url, |
||
222 | 'rel' => 'modulepreload', |
||
223 | ]); |
||
224 | break; |
||
225 | case 'file': |
||
226 | $lines[] = HtmlHelper::jsFile($url, $tag['options']); |
||
227 | break; |
||
228 | case 'css': |
||
229 | $lines[] = HtmlHelper::cssFile($url, $tag['options']); |
||
230 | break; |
||
231 | default: |
||
232 | break; |
||
233 | } |
||
234 | } |
||
235 | } |
||
236 | |||
237 | return implode("\r\n", $lines); |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * Register the appropriate tags to the Craft View to load the Vite script, either via the dev server or |
||
242 | * extracting it from the manifest.json file |
||
243 | * |
||
244 | * @param string $path |
||
245 | * @param bool $asyncCss |
||
246 | * @param array $scriptTagAttrs |
||
247 | * @param array $cssTagAttrs |
||
248 | * |
||
249 | * @return void |
||
250 | * @throws InvalidConfigException |
||
251 | */ |
||
252 | public function register(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []) |
||
253 | { |
||
254 | if ($this->devServerRunning()) { |
||
255 | $this->devServerRegister($path, $scriptTagAttrs); |
||
256 | |||
257 | return; |
||
258 | } |
||
259 | |||
260 | $this->manifestRegister($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs); |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * Register the script tag to the Craft View to load the script from the Vite dev server |
||
265 | * |
||
266 | * @param string $path |
||
267 | * @param array $scriptTagAttrs |
||
268 | * |
||
269 | * @return void |
||
270 | * @throws InvalidConfigException |
||
271 | */ |
||
272 | public function devServerRegister(string $path, array $scriptTagAttrs = []) |
||
303 | ); |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * Register the script, module link, and CSS link tags to the Craft View for the script from the manifest.json file |
||
308 | * |
||
309 | * @param string $path |
||
310 | * @param bool $asyncCss |
||
311 | * @param array $scriptTagAttrs |
||
312 | * @param array $cssTagAttrs |
||
313 | * |
||
314 | * @return void |
||
315 | * @throws InvalidConfigException |
||
316 | */ |
||
317 | public function manifestRegister(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []) |
||
318 | { |
||
319 | $view = Craft::$app->getView(); |
||
320 | ManifestHelper::fetchManifest($this->manifestPath); |
||
321 | $tags = ManifestHelper::manifestTags($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs); |
||
322 | $legacyTags = ManifestHelper::legacyManifestTags($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs); |
||
323 | // Include any manifest shims |
||
324 | if (!$this->manifestShimsIncluded) { |
||
325 | // Handle any legacy polyfills |
||
326 | if (!empty($legacyTags)) { |
||
327 | $view->registerScript( |
||
328 | FileHelper::fetchScript('safari-nomodule-fix.min.js', $this->cacheKeySuffix), |
||
329 | $view::POS_HEAD, |
||
330 | [], |
||
331 | 'SAFARI_NOMODULE_FIX' |
||
332 | ); |
||
333 | $legacyPolyfillTags = ManifestHelper::extractManifestTags(self::LEGACY_POLYFILLS, $asyncCss, $scriptTagAttrs, $cssTagAttrs, true); |
||
334 | $tags = array_merge($legacyPolyfillTags, $tags); |
||
335 | } |
||
336 | $this->manifestShimsIncluded = true; |
||
337 | } |
||
338 | foreach(array_merge($tags, $legacyTags) as $tag) { |
||
339 | if (!empty($tag)) { |
||
340 | $url = FileHelper::createUrl($this->serverPublic, $tag['url']); |
||
341 | switch ($tag['type']) { |
||
342 | case 'import': |
||
343 | $view->registerLinkTag( |
||
344 | [ |
||
345 | 'crossorigin' => $tag['crossorigin'], |
||
346 | 'href' => $url, |
||
347 | 'rel' => 'modulepreload', |
||
348 | ], |
||
349 | md5($url) |
||
350 | ); |
||
351 | break; |
||
352 | case 'file': |
||
353 | $view->registerJsFile( |
||
354 | $url, |
||
355 | $tag['options'], |
||
356 | md5($url . JsonHelper::encode($tag['options'])) |
||
357 | ); |
||
358 | break; |
||
359 | case 'css': |
||
360 | $view->registerCssFile( |
||
361 | $url, |
||
362 | $tag['options'] |
||
363 | ); |
||
364 | break; |
||
365 | default: |
||
366 | break; |
||
367 | } |
||
368 | } |
||
369 | } |
||
370 | } |
||
371 | |||
372 | /** |
||
373 | * Return the contents of a local file (via path) or remote file (via URL), |
||
374 | * or null if the file doesn't exist or couldn't be fetched |
||
375 | * Yii2 aliases and/or environment variables may be used |
||
376 | * |
||
377 | * @param string $pathOrUrl |
||
378 | * @param callable|null $callback |
||
379 | * |
||
380 | * @return string|array|null |
||
381 | */ |
||
382 | public function fetch(string $pathOrUrl, callable $callback = null) |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * Determine whether the Vite dev server is running |
||
389 | * |
||
390 | * @return bool |
||
391 | */ |
||
392 | public function devServerRunning(): bool |
||
393 | { |
||
394 | // If the dev server is turned off via config, say it's not running |
||
395 | if (!$this->useDevServer) { |
||
396 | return false; |
||
397 | } |
||
398 | // If we're not supposed to check that the dev server is actually running, just assume it is |
||
399 | if (!$this->checkDevServer) { |
||
400 | return true; |
||
401 | } |
||
402 | // Check to see if the dev server is actually running by pinging it |
||
403 | $url = FileHelper::createUrl($this->devServerInternal, self::VITE_CLIENT); |
||
404 | |||
405 | return !($this->fetch($url) === null); |
||
406 | } |
||
407 | |||
408 | /** |
||
409 | * Invalidate all of the Vite caches |
||
410 | */ |
||
411 | public function invalidateCaches() |
||
412 | { |
||
413 | $cache = Craft::$app->getCache(); |
||
414 | TagDependency::invalidate($cache, FileHelper::CACHE_TAG . $this->cacheKeySuffix); |
||
415 | Craft::info('All Vite caches cleared', __METHOD__); |
||
416 | } |
||
417 | |||
418 | // Protected Methods |
||
419 | // ========================================================================= |
||
420 | |||
421 | /** |
||
422 | * Inject the error entry point JavaScript for auto-reloading of Twig error |
||
423 | * pages |
||
424 | */ |
||
425 | protected function injectErrorEntry() |
||
453 | // That's okay, Vite will have already logged the error |
||
454 | } |
||
455 | } |
||
456 | } |
||
457 |