| Total Complexity | 50 |
| Total Lines | 466 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 1 |
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.js'; |
||
| 38 | const LEGACY_EXTENSION = '-legacy.'; |
||
| 39 | const LEGACY_POLYFILLS = 'vite/legacy-polyfills'; |
||
| 40 | |||
| 41 | const CACHE_KEY = 'vite'; |
||
| 42 | const CACHE_TAG = 'vite'; |
||
| 43 | |||
| 44 | const DEVMODE_CACHE_DURATION = 1; |
||
| 45 | |||
| 46 | const USER_AGENT_STRING = 'User-Agent:Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'; |
||
| 47 | |||
| 48 | const SAFARI_NOMODULE_FIX = '!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()}}();'; |
||
| 49 | |||
| 50 | // Public Properties |
||
| 51 | // ========================================================================= |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var bool Should the dev server be used for? |
||
| 55 | */ |
||
| 56 | public $useDevServer; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string File system path (or URL) to the Vite-built manifest.json |
||
| 60 | */ |
||
| 61 | public $manifestPath; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string The public URL to the dev server (what appears in `<script src="">` tags |
||
| 65 | */ |
||
| 66 | public $devServerPublic; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var string The public URL to use when not using the dev server |
||
| 70 | */ |
||
| 71 | public $serverPublic; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var string String to be appended to the cache key |
||
| 75 | */ |
||
| 76 | public $cacheKeySuffix = ''; |
||
| 77 | |||
| 78 | // Protected Properties |
||
| 79 | // ========================================================================= |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var bool Whether any legacy tags were found in this request |
||
| 83 | */ |
||
| 84 | protected $hasLegacyTags = false; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var bool Whether the legacy polyfill has been included yet or not |
||
| 88 | */ |
||
| 89 | protected $legacyPolyfillIncluded = false; |
||
| 90 | |||
| 91 | // Public Methods |
||
| 92 | // ========================================================================= |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Return the appropriate tags to load the Vite script, either via the dev server or |
||
| 96 | * extracting it from the manifest.json file |
||
| 97 | * |
||
| 98 | * @param string $path |
||
| 99 | * @param bool $asyncCss |
||
| 100 | * @param array $scriptTagAttrs |
||
| 101 | * @param array $cssTagAttrs |
||
| 102 | * |
||
| 103 | * @return string |
||
| 104 | */ |
||
| 105 | public function script(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []): string |
||
| 106 | { |
||
| 107 | if ($this->useDevServer) { |
||
| 108 | return $this->devServerScript($path, $scriptTagAttrs); |
||
| 109 | } |
||
| 110 | |||
| 111 | return $this->manifestScript($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Return the script tag to load the script from the Vite dev server |
||
| 116 | * |
||
| 117 | * @param string $path |
||
| 118 | * @param array $scriptTagAttrs |
||
| 119 | * |
||
| 120 | * @return string |
||
| 121 | */ |
||
| 122 | public function devServerScript(string $path, array $scriptTagAttrs = []): string |
||
| 123 | { |
||
| 124 | $lines = []; |
||
| 125 | // Include the entry script |
||
| 126 | $url = $this->createUrl($this->devServerPublic, $path); |
||
| 127 | $lines[] = HtmlHelper::jsFile($url, array_merge([ |
||
| 128 | 'type' => 'module', |
||
| 129 | ], $scriptTagAttrs)); |
||
| 130 | |||
| 131 | return implode("\r\n", $lines); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Return the script, module link, and CSS link tags for the script from the manifest.json file |
||
| 136 | * |
||
| 137 | * @param string $path |
||
| 138 | * @param bool $asyncCss |
||
| 139 | * @param array $scriptTagAttrs |
||
| 140 | * @param array $cssTagAttrs |
||
| 141 | * |
||
| 142 | * @return string |
||
| 143 | */ |
||
| 144 | public function manifestScript(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []): string |
||
| 145 | { |
||
| 146 | $lines = []; |
||
| 147 | $tags = $this->manifestTags($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs); |
||
| 148 | // Handle any legacy polyfills |
||
| 149 | if ($this->hasLegacyTags && !$this->legacyPolyfillIncluded) { |
||
| 150 | $lines[] = HtmlHelper::script(self::SAFARI_NOMODULE_FIX, []); |
||
| 151 | $legacyPolyfillTags = $this->extractManifestTags(self::LEGACY_POLYFILLS, $asyncCss, $scriptTagAttrs, $cssTagAttrs, true); |
||
| 152 | $tags = array_merge($legacyPolyfillTags, $tags); |
||
| 153 | $this->legacyPolyfillIncluded = true; |
||
| 154 | } |
||
| 155 | foreach($tags as $tag) { |
||
| 156 | if (!empty($tag)) { |
||
| 157 | switch ($tag['type']) { |
||
| 158 | case 'file': |
||
| 159 | $lines[] = HtmlHelper::jsFile($tag['url'], $tag['options']); |
||
| 160 | break; |
||
| 161 | case 'css': |
||
| 162 | $lines[] = HtmlHelper::cssFile($tag['url'], $tag['options']); |
||
| 163 | break; |
||
| 164 | default: |
||
| 165 | break; |
||
| 166 | } |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | return implode("\r\n", $lines); |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Register the appropriate tags to the Craft View to load the Vite script, either via the dev server or |
||
| 175 | * extracting it from the manifest.json file |
||
| 176 | * |
||
| 177 | * @param string $path |
||
| 178 | * @param bool $asyncCss |
||
| 179 | * @param array $scriptTagAttrs |
||
| 180 | * @param array $cssTagAttrs |
||
| 181 | * |
||
| 182 | * @return void |
||
| 183 | * @throws InvalidConfigException |
||
| 184 | */ |
||
| 185 | public function register(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []) |
||
| 186 | { |
||
| 187 | if ($this->useDevServer) { |
||
| 188 | $this->devServerRegister($path, $scriptTagAttrs); |
||
| 189 | |||
| 190 | return; |
||
| 191 | } |
||
| 192 | |||
| 193 | $this->manifestRegister($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs); |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Register the script tag to the Craft View to load the script from the Vite dev server |
||
| 198 | * |
||
| 199 | * @param string $path |
||
| 200 | * @param array $scriptTagAttrs |
||
| 201 | * |
||
| 202 | * @return void |
||
| 203 | */ |
||
| 204 | public function devServerRegister(string $path, array $scriptTagAttrs = []) |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Register the script, module link, and CSS link tags to the Craft View for the script from the manifest.json file |
||
| 217 | * |
||
| 218 | * @param string $path |
||
| 219 | * @param bool $asyncCss |
||
| 220 | * @param array $scriptTagAttrs |
||
| 221 | * @param array $cssTagAttrs |
||
| 222 | * |
||
| 223 | * @return void |
||
| 224 | * @throws InvalidConfigException |
||
| 225 | */ |
||
| 226 | public function manifestRegister(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []) |
||
| 227 | { |
||
| 228 | $view = Craft::$app->getView(); |
||
| 229 | $tags = $this->manifestTags($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs); |
||
| 230 | // Handle any legacy polyfills |
||
| 231 | if ($this->hasLegacyTags && !$this->legacyPolyfillIncluded) { |
||
| 232 | $view->registerScript(self::SAFARI_NOMODULE_FIX, $view::POS_HEAD, [], 'SAFARI_NOMODULE_FIX'); |
||
| 233 | $legacyPolyfillTags = $this->extractManifestTags(self::LEGACY_POLYFILLS, $asyncCss, $scriptTagAttrs, $cssTagAttrs, true); |
||
| 234 | $tags = array_merge($legacyPolyfillTags, $tags); |
||
| 235 | $this->legacyPolyfillIncluded = true; |
||
| 236 | } |
||
| 237 | foreach($tags as $tag) { |
||
| 238 | if (!empty($tag)) { |
||
| 239 | switch ($tag['type']) { |
||
| 240 | case 'file': |
||
| 241 | $view->registerScript( |
||
| 242 | '', |
||
| 243 | $view::POS_HEAD, |
||
| 244 | array_merge(['src' => $tag['url']], $tag['options']), |
||
| 245 | md5($tag['url'] . json_encode($tag['options'])) |
||
| 246 | ); |
||
| 247 | break; |
||
| 248 | case 'css': |
||
| 249 | $view->registerCssFile($tag['url'], $tag['options']); |
||
| 250 | break; |
||
| 251 | default: |
||
| 252 | break; |
||
| 253 | } |
||
| 254 | } |
||
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Invalidate all of the Vite caches |
||
| 260 | */ |
||
| 261 | public function invalidateCaches() |
||
| 266 | } |
||
| 267 | |||
| 268 | // Protected Methods |
||
| 269 | // ========================================================================= |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Return an array of tags from the manifest, for both modern and legacy builds |
||
| 273 | * |
||
| 274 | * @param string $path |
||
| 275 | * @param bool $asyncCss |
||
| 276 | * @param array $scriptTagAttrs |
||
| 277 | * @param array $cssTagAttrs |
||
| 278 | * |
||
| 279 | * @return array |
||
| 280 | */ |
||
| 281 | protected function manifestTags(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []): array |
||
| 282 | { |
||
| 283 | // Get the modern tags for this $path |
||
| 284 | $tags = $this->extractManifestTags($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs, false); |
||
| 285 | // Look for a legacy version of this $path too |
||
| 286 | $parts = pathinfo($path); |
||
| 287 | $legacyPath = $parts['dirname'] |
||
| 288 | . '/' |
||
| 289 | . $parts['filename'] |
||
| 290 | . self::LEGACY_EXTENSION |
||
| 291 | . $parts['extension']; |
||
| 292 | $legacyTags = $this->extractManifestTags($legacyPath, $asyncCss, $scriptTagAttrs, $cssTagAttrs, true); |
||
| 293 | // Set a flag to indicate the some legacy gets were found |
||
| 294 | $legacyPolyfillTags = []; |
||
| 295 | if (!empty($legacyTags)) { |
||
| 296 | $this->hasLegacyTags = true; |
||
| 297 | } |
||
| 298 | return array_merge( |
||
| 299 | $legacyPolyfillTags, |
||
| 300 | $tags, |
||
| 301 | $legacyTags |
||
| 302 | ); |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Return an array of data describing the script, module link, and CSS link tags for the |
||
| 307 | * 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 array |
||
| 315 | */ |
||
| 316 | protected function extractManifestTags(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = [], $legacy = false): array |
||
| 317 | { |
||
| 318 | $tags = []; |
||
| 319 | // Grab the manifest |
||
| 320 | $pathOrUrl = (string)Craft::parseEnv($this->manifestPath); |
||
| 321 | $manifest = $this->fetchFile($pathOrUrl, [JsonHelper::class, 'decodeIfJson']); |
||
| 322 | // If no manifest file is found, bail |
||
| 323 | if ($manifest === null) { |
||
| 324 | Craft::error('Manifest not found at ' . $this->manifestPath, __METHOD__); |
||
| 325 | |||
| 326 | return []; |
||
| 327 | } |
||
| 328 | // Set the async CSS args |
||
| 329 | $asyncCssOptions = []; |
||
| 330 | if ($asyncCss) { |
||
| 331 | $asyncCssOptions = [ |
||
| 332 | 'media' => 'print', |
||
| 333 | 'onload' => "this.media='all'", |
||
| 334 | ]; |
||
| 335 | } |
||
| 336 | // Set the script args |
||
| 337 | $scriptOptions = [ |
||
| 338 | 'type' => 'module', |
||
| 339 | 'crossorigin' => true, |
||
| 340 | ]; |
||
| 341 | if ($legacy) { |
||
| 342 | $scriptOptions = [ |
||
| 343 | 'type' => 'nomodule', |
||
| 344 | ]; |
||
| 345 | } |
||
| 346 | // Iterate through the manifest |
||
| 347 | foreach ($manifest as $manifestKey => $entry) { |
||
| 348 | if (isset($entry['isEntry']) && $entry['isEntry']) { |
||
| 349 | // Include the entry script |
||
| 350 | if (isset($entry['file']) && strpos($path, $manifestKey) !== false) { |
||
| 351 | $tags[] = [ |
||
| 352 | 'type' => 'file', |
||
| 353 | 'url' => $this->createUrl($this->serverPublic, $entry['file']), |
||
| 354 | 'options' => array_merge($scriptOptions, $scriptTagAttrs) |
||
| 355 | ]; |
||
| 356 | // Include any CSS tags |
||
| 357 | $cssFiles = []; |
||
| 358 | $this->extractCssFiles($manifest, $manifestKey, $cssFiles); |
||
| 359 | foreach ($cssFiles as $cssFile) { |
||
| 360 | $tags[] = [ |
||
| 361 | 'type' => 'css', |
||
| 362 | 'url' => $this->createUrl($this->serverPublic, $cssFile), |
||
| 363 | 'options' => array_merge([ |
||
| 364 | 'rel' => 'stylesheet', |
||
| 365 | ], $asyncCssOptions, $cssTagAttrs) |
||
| 366 | ]; |
||
| 367 | } |
||
| 368 | } |
||
| 369 | } |
||
| 370 | } |
||
| 371 | |||
| 372 | return $tags; |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Extract any CSS files from entries recursively |
||
| 377 | * |
||
| 378 | * @param array $manifest |
||
| 379 | * @param string $manifestKey |
||
| 380 | * @param array $cssFiles |
||
| 381 | */ |
||
| 382 | protected function extractCssFiles(array $manifest, string $manifestKey, array &$cssFiles) |
||
| 383 | { |
||
| 384 | if (isset($manifest[$manifestKey])) { |
||
| 385 | $entry = $manifest[$manifestKey]; |
||
| 386 | // Handle any CSS files |
||
| 387 | if (isset($entry['css'])) { |
||
| 388 | foreach ($entry['css'] as $css) { |
||
| 389 | $cssFiles[] = $css; |
||
| 390 | } |
||
| 391 | } |
||
| 392 | // Handle imports recursively |
||
| 393 | if (isset($entry['imports'])) { |
||
| 394 | foreach ($entry['imports'] as $import) { |
||
| 395 | $this->extractCssFiles($manifest, $import, $cssFiles); |
||
| 396 | } |
||
| 397 | } |
||
| 398 | // Handle dynamic imports recursively |
||
| 399 | if (isset($entry['dynamicImports'])) { |
||
| 400 | foreach ($entry['dynamicImports'] as $dynamicImport) { |
||
| 401 | $this->extractCssFiles($manifest, $dynamicImport, $cssFiles); |
||
| 402 | } |
||
| 403 | } |
||
| 404 | } |
||
| 405 | } |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Combine a path with a URL to create a URL |
||
| 409 | * |
||
| 410 | * @param string $url |
||
| 411 | * @param string $path |
||
| 412 | * |
||
| 413 | * @return string |
||
| 414 | */ |
||
| 415 | protected function createUrl(string $url, string $path): string |
||
| 416 | { |
||
| 417 | $url = (string)Craft::parseEnv($url); |
||
| 418 | return rtrim($url, '/') . '/' . trim($path, '/'); |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Return the contents of a local or remote file, or null |
||
| 423 | * |
||
| 424 | * @param string $pathOrUrl |
||
| 425 | * @param callable|null $callback |
||
| 426 | * @return mixed |
||
| 427 | */ |
||
| 428 | protected function fetchFile(string $pathOrUrl, callable $callback = null) |
||
| 498 | } |
||
| 499 | } |
||
| 500 |