| Total Complexity | 47 |
| Total Lines | 444 |
| Duplicated Lines | 0 % |
| Changes | 25 | ||
| 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 | /** |
||
| 105 | * @var bool Whether the modulepreload polyfill has been included yet or not |
||
| 106 | */ |
||
| 107 | protected $modulepreloadPolyfillIncluded = 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 |
||
| 200 | { |
||
| 201 | $lines = []; |
||
| 202 | ManifestHelper::fetchManifest($this->manifestPath); |
||
| 203 | $tags = ManifestHelper::manifestTags($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs); |
||
| 204 | $legacyTags = ManifestHelper::legacyManifestTags($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs); |
||
| 205 | // Include any manifest shims |
||
| 206 | if (!$this->manifestShimsIncluded) { |
||
| 207 | // Handle any legacy polyfills |
||
| 208 | if (!empty($legacyTags)) { |
||
| 209 | $lines[] = HtmlHelper::script( |
||
| 210 | FileHelper::fetchScript('safari-nomodule-fix.min.js', $this->cacheKeySuffix), |
||
| 211 | [] |
||
| 212 | ); |
||
| 213 | $legacyPolyfillTags = ManifestHelper::extractManifestTags(self::LEGACY_POLYFILLS, $asyncCss, $scriptTagAttrs, $cssTagAttrs, true); |
||
| 214 | $tags = array_merge($legacyPolyfillTags, $tags); |
||
| 215 | } |
||
| 216 | $this->manifestShimsIncluded = true; |
||
| 217 | } |
||
| 218 | |||
| 219 | foreach (array_merge($tags, $legacyTags) as $tag) { |
||
| 220 | if (!empty($tag)) { |
||
| 221 | $url = FileHelper::createUrl($this->serverPublic, $tag['url']); |
||
| 222 | switch ($tag['type']) { |
||
| 223 | case 'file': |
||
| 224 | $lines[] = HtmlHelper::jsFile($url, $tag['options']); |
||
| 225 | break; |
||
| 226 | case 'css': |
||
| 227 | $lines[] = HtmlHelper::cssFile($url, $tag['options']); |
||
| 228 | break; |
||
| 229 | case 'import': |
||
| 230 | $lines[] = HtmlHelper::tag('link', '', [ |
||
| 231 | 'crossorigin' => $tag['crossorigin'], |
||
| 232 | 'href' => $url, |
||
| 233 | 'rel' => 'modulepreload', |
||
| 234 | ]); |
||
| 235 | break; |
||
| 236 | default: |
||
| 237 | break; |
||
| 238 | } |
||
| 239 | } |
||
| 240 | } |
||
| 241 | |||
| 242 | if ($modulepreloadPolyfillRequired && !$this->modulepreloadPolyfillIncluded) { |
||
| 243 | $this->modulepreloadPolyfillIncluded = true; |
||
| 244 | $lines[] = HtmlHelper::script( |
||
| 245 | FileHelper::fetchScript('modulepreload-polyfill.min.js', $this->cacheKeySuffix), |
||
| 246 | ['type' => 'module'] |
||
| 247 | ); |
||
| 248 | } |
||
| 249 | |||
| 250 | return implode("\r\n", $lines); |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Register the appropriate tags to the Craft View to load the Vite script, either via the dev server or |
||
| 255 | * extracting it from the manifest.json file |
||
| 256 | * |
||
| 257 | * @param string $path |
||
| 258 | * @param bool $asyncCss |
||
| 259 | * @param array $scriptTagAttrs |
||
| 260 | * @param array $cssTagAttrs |
||
| 261 | * |
||
| 262 | * @return void |
||
| 263 | * @throws InvalidConfigException |
||
| 264 | */ |
||
| 265 | public function register(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []) |
||
| 266 | { |
||
| 267 | if ($this->devServerRunning()) { |
||
| 268 | $this->devServerRegister($path, $scriptTagAttrs); |
||
| 269 | |||
| 270 | return; |
||
| 271 | } |
||
| 272 | |||
| 273 | $this->manifestRegister($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs); |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Register the script tag to the Craft View to load the script from the Vite dev server |
||
| 278 | * |
||
| 279 | * @param string $path |
||
| 280 | * @param array $scriptTagAttrs |
||
| 281 | * |
||
| 282 | * @return void |
||
| 283 | * @throws InvalidConfigException |
||
| 284 | */ |
||
| 285 | public function devServerRegister(string $path, array $scriptTagAttrs = []) |
||
| 286 | { |
||
| 287 | $view = Craft::$app->getView(); |
||
| 288 | // Include any dev server shims |
||
| 289 | if (!$this->devServerShimsIncluded) { |
||
| 290 | // Include the react-refresh-shim |
||
| 291 | if ($this->includeReactRefreshShim) { |
||
| 292 | $script = FileHelper::fetchScript('react-refresh-shim.min.js', $this->cacheKeySuffix); |
||
| 293 | // Replace the hard-coded dev server URL with whatever they have theirs set to |
||
| 294 | $script = str_replace( |
||
| 295 | 'http://localhost:3000/', |
||
| 296 | rtrim($this->devServerPublic, '/') . '/', |
||
| 297 | $script |
||
| 298 | ); |
||
| 299 | $view->registerScript( |
||
| 300 | $script, |
||
| 301 | $view::POS_HEAD, |
||
| 302 | [ |
||
| 303 | 'type' => 'module', |
||
| 304 | ], |
||
| 305 | 'REACT_REFRESH_SHIM' |
||
| 306 | ); |
||
| 307 | } |
||
| 308 | $this->devServerShimsIncluded = true; |
||
| 309 | } |
||
| 310 | // Include the entry script |
||
| 311 | $url = FileHelper::createUrl($this->devServerPublic, $path); |
||
| 312 | $view->registerJsFile( |
||
| 313 | $url, |
||
| 314 | array_merge(['type' => 'module'], $scriptTagAttrs), |
||
| 315 | md5($url . JsonHelper::encode($scriptTagAttrs)) |
||
| 316 | ); |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Register the script, module link, and CSS link tags to the Craft View for the script from the manifest.json file |
||
| 321 | * |
||
| 322 | * @param string $path |
||
| 323 | * @param bool $asyncCss |
||
| 324 | * @param array $scriptTagAttrs |
||
| 325 | * @param array $cssTagAttrs |
||
| 326 | * |
||
| 327 | * @return void |
||
| 328 | * @throws InvalidConfigException |
||
| 329 | */ |
||
| 330 | public function manifestRegister(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []) |
||
| 331 | { |
||
| 332 | $view = Craft::$app->getView(); |
||
| 333 | ManifestHelper::fetchManifest($this->manifestPath); |
||
| 334 | $tags = ManifestHelper::manifestTags($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs); |
||
| 335 | $legacyTags = ManifestHelper::legacyManifestTags($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs); |
||
| 336 | // Include any manifest shims |
||
| 337 | if (!$this->manifestShimsIncluded) { |
||
| 338 | // Handle any legacy polyfills |
||
| 339 | if (!empty($legacyTags)) { |
||
| 340 | $view->registerScript( |
||
| 341 | FileHelper::fetchScript('safari-nomodule-fix.min.js', $this->cacheKeySuffix), |
||
| 342 | $view::POS_HEAD, |
||
| 343 | [], |
||
| 344 | 'SAFARI_NOMODULE_FIX' |
||
| 345 | ); |
||
| 346 | $legacyPolyfillTags = ManifestHelper::extractManifestTags(self::LEGACY_POLYFILLS, $asyncCss, $scriptTagAttrs, $cssTagAttrs, true); |
||
| 347 | $tags = array_merge($legacyPolyfillTags, $tags); |
||
| 348 | } |
||
| 349 | $this->manifestShimsIncluded = true; |
||
| 350 | } |
||
| 351 | foreach (array_merge($tags, $legacyTags) as $tag) { |
||
| 352 | if (!empty($tag)) { |
||
| 353 | $url = FileHelper::createUrl($this->serverPublic, $tag['url']); |
||
| 354 | switch ($tag['type']) { |
||
| 355 | case 'file': |
||
| 356 | $view->registerJsFile( |
||
| 357 | $url, |
||
| 358 | $tag['options'], |
||
| 359 | md5($url . JsonHelper::encode($tag['options'])) |
||
| 360 | ); |
||
| 361 | break; |
||
| 362 | case 'css': |
||
| 363 | $view->registerCssFile( |
||
| 364 | $url, |
||
| 365 | $tag['options'] |
||
| 366 | ); |
||
| 367 | break; |
||
| 368 | case 'import': |
||
| 369 | $view->registerLinkTag( |
||
| 370 | [ |
||
| 371 | 'crossorigin' => $tag['crossorigin'], |
||
| 372 | 'href' => $url, |
||
| 373 | 'rel' => 'modulepreload', |
||
| 374 | ], |
||
| 375 | md5($url) |
||
| 376 | ); |
||
| 377 | break; |
||
| 378 | default: |
||
| 379 | break; |
||
| 380 | } |
||
| 381 | } |
||
| 382 | } |
||
| 383 | |||
| 384 | if ($modulepreloadPolyfillRequired && !$this->modulepreloadPolyfillIncluded) { |
||
| 385 | $this->modulepreloadPolyfillIncluded = true; |
||
| 386 | $view->registerScript( |
||
| 387 | FileHelper::fetchScript('modulepreload-polyfill.min.js', $this->cacheKeySuffix), |
||
| 388 | $view::POS_HEAD, |
||
| 389 | ['type' => 'module'], |
||
| 390 | 'MODULEPRELOAD_POLYFILL' |
||
| 391 | ); |
||
| 392 | } |
||
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Return the contents of a local file (via path) or remote file (via URL), |
||
| 397 | * or null if the file doesn't exist or couldn't be fetched |
||
| 398 | * Yii2 aliases and/or environment variables may be used |
||
| 399 | * |
||
| 400 | * @param string $pathOrUrl |
||
| 401 | * @param callable|null $callback |
||
| 402 | * |
||
| 403 | * @return string|array|null |
||
| 404 | */ |
||
| 405 | public function fetch(string $pathOrUrl, callable $callback = null) |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Determine whether the Vite dev server is running |
||
| 412 | * |
||
| 413 | * @return bool |
||
| 414 | */ |
||
| 415 | public function devServerRunning(): bool |
||
| 416 | { |
||
| 417 | // If the dev server is turned off via config, say it's not running |
||
| 418 | if (!$this->useDevServer) { |
||
| 419 | return false; |
||
| 420 | } |
||
| 421 | // If we're not supposed to check that the dev server is actually running, just assume it is |
||
| 422 | if (!$this->checkDevServer) { |
||
| 423 | return true; |
||
| 424 | } |
||
| 425 | // Check to see if the dev server is actually running by pinging it |
||
| 426 | $url = FileHelper::createUrl($this->devServerInternal, self::VITE_CLIENT); |
||
| 427 | |||
| 428 | return !($this->fetch($url) === null); |
||
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Invalidate all of the Vite caches |
||
| 433 | */ |
||
| 434 | public function invalidateCaches() |
||
| 435 | { |
||
| 436 | $cache = Craft::$app->getCache(); |
||
| 437 | TagDependency::invalidate($cache, FileHelper::CACHE_TAG . $this->cacheKeySuffix); |
||
| 438 | Craft::info('All Vite caches cleared', __METHOD__); |
||
| 439 | } |
||
| 440 | |||
| 441 | // Protected Methods |
||
| 442 | // ========================================================================= |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Inject the error entry point JavaScript for auto-reloading of Twig error |
||
| 446 | * pages |
||
| 447 | */ |
||
| 448 | protected function injectErrorEntry() |
||
| 476 | // That's okay, Vite will have already logged the error |
||
| 477 | } |
||
| 478 | } |
||
| 479 | } |
||
| 480 |