| Total Complexity | 54 |
| Total Lines | 536 |
| Duplicated Lines | 0 % |
| Changes | 27 | ||
| 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 | /** |
||
| 92 | * @var bool Whether the modulepreload-polyfill shim should be included |
||
| 93 | */ |
||
| 94 | public $includeModulePreloadShim = true; |
||
| 95 | |||
| 96 | // Protected Properties |
||
| 97 | // ========================================================================= |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var bool Whether the manifest shims has been included yet or not |
||
| 101 | */ |
||
| 102 | protected $manifestShimsIncluded = false; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var bool Whether the dev server shims has been included yet or not |
||
| 106 | */ |
||
| 107 | protected $devServerShimsIncluded = false; |
||
| 108 | |||
| 109 | // Public Methods |
||
| 110 | // ========================================================================= |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @inheritDoc |
||
| 114 | */ |
||
| 115 | public function init() |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Return the appropriate tags to load the Vite script, either via the dev server or |
||
| 131 | * extracting it from the manifest.json file |
||
| 132 | * |
||
| 133 | * @param string $path |
||
| 134 | * @param bool $asyncCss |
||
| 135 | * @param array $scriptTagAttrs |
||
| 136 | * @param array $cssTagAttrs |
||
| 137 | * |
||
| 138 | * @return string |
||
| 139 | */ |
||
| 140 | public function script(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []): string |
||
| 141 | { |
||
| 142 | if ($this->devServerRunning()) { |
||
| 143 | return $this->devServerScript($path, $scriptTagAttrs); |
||
| 144 | } |
||
| 145 | |||
| 146 | return $this->manifestScript($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs); |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Return the script tag to load the script from the Vite dev server |
||
| 151 | * |
||
| 152 | * @param string $path |
||
| 153 | * @param array $scriptTagAttrs |
||
| 154 | * |
||
| 155 | * @return string |
||
| 156 | */ |
||
| 157 | public function devServerScript(string $path, array $scriptTagAttrs = []): string |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Return the script, module link, and CSS link tags for the script from the manifest.json file |
||
| 191 | * |
||
| 192 | * @param string $path |
||
| 193 | * @param bool $asyncCss |
||
| 194 | * @param array $scriptTagAttrs |
||
| 195 | * @param array $cssTagAttrs |
||
| 196 | * |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | public function manifestScript(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []): string |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Register the appropriate tags to the Craft View to load the Vite script, either via the dev server or |
||
| 232 | * extracting it from the manifest.json file |
||
| 233 | * |
||
| 234 | * @param string $path |
||
| 235 | * @param bool $asyncCss |
||
| 236 | * @param array $scriptTagAttrs |
||
| 237 | * @param array $cssTagAttrs |
||
| 238 | * |
||
| 239 | * @return void |
||
| 240 | * @throws InvalidConfigException |
||
| 241 | */ |
||
| 242 | public function register(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []) |
||
| 243 | { |
||
| 244 | if ($this->devServerRunning()) { |
||
| 245 | $this->devServerRegister($path, $scriptTagAttrs); |
||
| 246 | |||
| 247 | return; |
||
| 248 | } |
||
| 249 | |||
| 250 | $this->manifestRegister($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs); |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Register the script tag to the Craft View to load the script from the Vite dev server |
||
| 255 | * |
||
| 256 | * @param string $path |
||
| 257 | * @param array $scriptTagAttrs |
||
| 258 | * |
||
| 259 | * @return void |
||
| 260 | * @throws InvalidConfigException |
||
| 261 | */ |
||
| 262 | public function devServerRegister(string $path, array $scriptTagAttrs = []) |
||
| 293 | ); |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Register the script, module link, and CSS link tags to the Craft View for the script from the manifest.json file |
||
| 298 | * |
||
| 299 | * @param string $path |
||
| 300 | * @param bool $asyncCss |
||
| 301 | * @param array $scriptTagAttrs |
||
| 302 | * @param array $cssTagAttrs |
||
| 303 | * |
||
| 304 | * @return void |
||
| 305 | * @throws InvalidConfigException |
||
| 306 | */ |
||
| 307 | public function manifestRegister(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []) |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Return the URL for the given entry |
||
| 342 | * |
||
| 343 | * @param string $path |
||
| 344 | * |
||
| 345 | * @return string |
||
| 346 | */ |
||
| 347 | public function entry(string $path): string |
||
| 348 | { |
||
| 349 | ManifestHelper::fetchManifest($this->manifestPath); |
||
| 350 | $entry = ManifestHelper::extractEntry($path); |
||
| 351 | |||
| 352 | return FileHelper::createUrl($this->serverPublic, $entry); |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Return the URL for the given asset |
||
| 357 | * |
||
| 358 | * @param string $path |
||
| 359 | * |
||
| 360 | * @return string |
||
| 361 | */ |
||
| 362 | public function asset(string $path): string |
||
| 363 | { |
||
| 364 | if ($this->devServerRunning()) { |
||
| 365 | return $this->devServerAsset($path); |
||
| 366 | } |
||
| 367 | |||
| 368 | return $this->manifestAsset($path); |
||
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Return the URL for the asset from the Vite dev server |
||
| 373 | * |
||
| 374 | * @param string $path |
||
| 375 | * |
||
| 376 | * @return string |
||
| 377 | */ |
||
| 378 | public function devServerAsset(string $path): string |
||
| 379 | { |
||
| 380 | // Return a URL to the given asset |
||
| 381 | return FileHelper::createUrl($this->devServerPublic, $path); |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Return the URL for the asset from the manifest.json file |
||
| 386 | * |
||
| 387 | * @param string $path |
||
| 388 | * |
||
| 389 | * @return string |
||
| 390 | */ |
||
| 391 | public function manifestAsset(string $path): string |
||
| 392 | { |
||
| 393 | ManifestHelper::fetchManifest($this->manifestPath); |
||
| 394 | $assets = ManifestHelper::extractAssetFiles(); |
||
| 395 | // Get just the file name |
||
| 396 | $assetKeyParts = explode('/', $path); |
||
| 397 | $assetKey = end($assetKeyParts); |
||
| 398 | foreach($assets as $key => $value) { |
||
| 399 | if ($key === $assetKey) { |
||
| 400 | return FileHelper::createUrl($this->serverPublic, $value); |
||
| 401 | } |
||
| 402 | } |
||
| 403 | |||
| 404 | return ''; |
||
| 405 | } |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Return the contents of a local file (via path) or remote file (via URL), |
||
| 409 | * or null if the file doesn't exist or couldn't be fetched |
||
| 410 | * Yii2 aliases and/or environment variables may be used |
||
| 411 | * |
||
| 412 | * @param string $pathOrUrl |
||
| 413 | * @param callable|null $callback |
||
| 414 | * |
||
| 415 | * @return string|array|null |
||
| 416 | */ |
||
| 417 | public function fetch(string $pathOrUrl, callable $callback = null) |
||
| 420 | } |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Determine whether the Vite dev server is running |
||
| 424 | * |
||
| 425 | * @return bool |
||
| 426 | */ |
||
| 427 | public function devServerRunning(): bool |
||
| 428 | { |
||
| 429 | // If the dev server is turned off via config, say it's not running |
||
| 430 | if (!$this->useDevServer) { |
||
| 431 | return false; |
||
| 432 | } |
||
| 433 | // If we're not supposed to check that the dev server is actually running, just assume it is |
||
| 434 | if (!$this->checkDevServer) { |
||
| 435 | return true; |
||
| 436 | } |
||
| 437 | // Check to see if the dev server is actually running by pinging it |
||
| 438 | $url = FileHelper::createUrl($this->devServerInternal, self::VITE_CLIENT); |
||
| 439 | |||
| 440 | return !($this->fetch($url) === null); |
||
| 441 | } |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Invalidate all of the Vite caches |
||
| 445 | */ |
||
| 446 | public function invalidateCaches() |
||
| 447 | { |
||
| 448 | $cache = Craft::$app->getCache(); |
||
| 449 | TagDependency::invalidate($cache, FileHelper::CACHE_TAG . $this->cacheKeySuffix); |
||
| 450 | Craft::info('All Vite caches cleared', __METHOD__); |
||
| 451 | } |
||
| 452 | |||
| 453 | // Protected Methods |
||
| 454 | // ========================================================================= |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Inject the error entry point JavaScript for auto-reloading of Twig error |
||
| 458 | * pages |
||
| 459 | */ |
||
| 460 | protected function injectErrorEntry() |
||
| 488 | // That's okay, Vite will have already logged the error |
||
| 489 | } |
||
| 490 | } |
||
| 491 | |||
| 492 | |||
| 493 | /** |
||
| 494 | * Iterate through all the tags, and return them |
||
| 495 | * |
||
| 496 | * @param array $tags |
||
| 497 | * @param array $legacyTags |
||
| 498 | * @return array |
||
| 499 | */ |
||
| 500 | protected function manifestScriptTags(array $tags, array $legacyTags): array |
||
| 501 | { |
||
| 502 | $lines = []; |
||
| 503 | foreach (array_merge($tags, $legacyTags) as $tag) { |
||
| 504 | if (!empty($tag)) { |
||
| 505 | $url = FileHelper::createUrl($this->serverPublic, $tag['url']); |
||
| 506 | switch ($tag['type']) { |
||
| 507 | case 'file': |
||
| 508 | $lines[] = HtmlHelper::jsFile($url, $tag['options']); |
||
| 509 | break; |
||
| 510 | case 'css': |
||
| 511 | $lines[] = HtmlHelper::cssFile($url, $tag['options']); |
||
| 512 | break; |
||
| 513 | case 'import': |
||
| 514 | $lines[] = HtmlHelper::tag('link', '', [ |
||
| 515 | 'crossorigin' => $tag['crossorigin'], |
||
| 516 | 'href' => $url, |
||
| 517 | 'rel' => 'modulepreload', |
||
| 518 | ]); |
||
| 519 | break; |
||
| 520 | default: |
||
| 521 | break; |
||
| 522 | } |
||
| 523 | } |
||
| 524 | } |
||
| 525 | |||
| 526 | return $lines; |
||
| 527 | } |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Iterate through all the tags, and register them |
||
| 531 | * |
||
| 532 | * @param array $tags |
||
| 533 | * @param array $legacyTags |
||
| 534 | * @throws InvalidConfigException |
||
| 535 | */ |
||
| 536 | protected function manifestRegisterTags(array $tags, array $legacyTags) |
||
| 568 | } |
||
| 569 | } |
||
| 570 | } |
||
| 571 | } |
||
| 572 | } |
||
| 573 |