| Total Complexity | 44 |
| Total Lines | 387 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 30 | class Manifest |
||
| 31 | { |
||
| 32 | // Constants |
||
| 33 | // ========================================================================= |
||
| 34 | |||
| 35 | const ASSET_CLASS = InstantAnalyticsAsset::class; |
||
| 36 | |||
| 37 | const CACHE_KEY = 'twigpack-' . self::ASSET_CLASS; |
||
| 38 | const CACHE_TAG = 'twigpack-' . self::ASSET_CLASS; |
||
| 39 | |||
| 40 | const DEVMODE_CACHE_DURATION = 1; |
||
| 41 | |||
| 42 | const SUPPRESS_ERRORS_FOR_MODULES = [ |
||
| 43 | 'styles.js', |
||
| 44 | 'commons.js', |
||
| 45 | 'vendors.js', |
||
| 46 | 'vendors.css', |
||
| 47 | 'styles.css', |
||
| 48 | ]; |
||
| 49 | |||
| 50 | // Protected Static Properties |
||
| 51 | // ========================================================================= |
||
| 52 | |||
| 53 | protected static $config = [ |
||
| 54 | // If `devMode` is on, use webpack-dev-server to all for HMR (hot module reloading) |
||
| 55 | 'useDevServer' => false, |
||
| 56 | // Manifest names |
||
| 57 | 'manifest' => [ |
||
| 58 | 'legacy' => 'manifest.json', |
||
| 59 | 'modern' => 'manifest.json', |
||
| 60 | ], |
||
| 61 | // Public server config |
||
| 62 | 'server' => [ |
||
| 63 | 'manifestPath' => '/', |
||
| 64 | 'publicPath' => '/', |
||
| 65 | ], |
||
| 66 | // webpack-dev-server config |
||
| 67 | 'devServer' => [ |
||
| 68 | 'manifestPath' => 'http://127.0.0.1:8080', |
||
| 69 | 'publicPath' => '/', |
||
| 70 | ], |
||
| 71 | ]; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | protected static $files; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var bool |
||
| 80 | */ |
||
| 81 | protected static $isHot = false; |
||
| 82 | |||
| 83 | // Public Static Methods |
||
| 84 | // ========================================================================= |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Simulate a static constructor |
||
| 88 | * |
||
| 89 | * ManifestVariable constructor. |
||
| 90 | * @noinspection MagicMethodsValidityInspection |
||
| 91 | */ |
||
| 92 | public static function __constructStatic() |
||
| 93 | { |
||
| 94 | ManifestHelper::invalidateCaches(); |
||
| 95 | $assetClass = self::ASSET_CLASS; |
||
| 96 | $bundle = new $assetClass; |
||
| 97 | $baseAssetsUrl = Craft::$app->assetManager->getPublishedUrl( |
||
| 98 | $bundle->sourcePath, |
||
| 99 | true |
||
| 100 | ); |
||
| 101 | self::$config['server']['manifestPath'] = Craft::getAlias($bundle->sourcePath); |
||
| 102 | self::$config['server']['publicPath'] = $baseAssetsUrl; |
||
| 103 | $useDevServer = getenv('NYS_PLUGIN_DEVSERVER'); |
||
| 104 | if ($useDevServer !== false) { |
||
| 105 | self::$config['useDevServer'] = (bool)$useDevServer; |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Get the passed in JS modules from the manifest, and register them in the current Craft view |
||
| 111 | * |
||
| 112 | * @param array $modules |
||
| 113 | * @throws NotFoundHttpException |
||
| 114 | * @throws \yii\base\InvalidConfigException |
||
| 115 | */ |
||
| 116 | public static function registerJsModules(array $modules) |
||
| 117 | { |
||
| 118 | $view = Craft::$app->getView(); |
||
| 119 | foreach($modules as $module) { |
||
| 120 | $jsModule = self::getModule(self::$config, $module, 'modern'); |
||
| 121 | if ($jsModule) { |
||
| 122 | $view->registerJsFile($jsModule, [ |
||
| 123 | 'depends' => self::ASSET_CLASS |
||
| 124 | ]); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Get the passed in CS modules from the manifest, and register them in the current Craft view |
||
| 131 | * |
||
| 132 | * @param array $modules |
||
| 133 | * @throws NotFoundHttpException |
||
| 134 | * @throws \yii\base\InvalidConfigException |
||
| 135 | */ |
||
| 136 | public static function registerCssModules(array $modules) |
||
| 137 | { |
||
| 138 | $view = Craft::$app->getView(); |
||
| 139 | foreach($modules as $module) { |
||
| 140 | $cssModule = self::getModule(self::$config, $module, 'legacy'); |
||
| 141 | if ($cssModule) { |
||
| 142 | $view->registerCssFile($cssModule, [ |
||
| 143 | 'depends' => self::ASSET_CLASS |
||
| 144 | ]); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | // Protected Static Methods |
||
| 150 | // ========================================================================= |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Return the URI to a module |
||
| 154 | * |
||
| 155 | * @param array $config |
||
| 156 | * @param string $moduleName |
||
| 157 | * @param string $type |
||
| 158 | * @param bool $soft |
||
| 159 | * |
||
| 160 | * @return null|string |
||
| 161 | * @throws NotFoundHttpException |
||
| 162 | */ |
||
| 163 | protected static function getModule(array $config, string $moduleName, string $type = 'modern', bool $soft = false) |
||
| 164 | { |
||
| 165 | // Get the module entry |
||
| 166 | $module = self::getModuleEntry($config, $moduleName, $type, $soft); |
||
| 167 | if ($module !== null) { |
||
| 168 | $prefix = self::$isHot |
||
| 169 | ? $config['devServer']['publicPath'] |
||
| 170 | : $config['server']['publicPath']; |
||
| 171 | // If the module isn't a full URL, prefix it |
||
| 172 | if (!UrlHelper::isAbsoluteUrl($module)) { |
||
| 173 | $module = self::combinePaths($prefix, $module); |
||
| 174 | } |
||
| 175 | // Resolve any aliases |
||
| 176 | $alias = Craft::getAlias($module, false); |
||
| 177 | if ($alias) { |
||
| 178 | $module = $alias; |
||
| 179 | } |
||
| 180 | // Make sure it's a full URL |
||
| 181 | if (!UrlHelper::isAbsoluteUrl($module) && !is_file($module)) { |
||
| 182 | try { |
||
| 183 | $module = UrlHelper::siteUrl($module); |
||
| 184 | } catch (Exception $e) { |
||
| 185 | Craft::error($e->getMessage(), __METHOD__); |
||
| 186 | } |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | return $module; |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Return a module's raw entry from the manifest |
||
| 195 | * |
||
| 196 | * @param array $config |
||
| 197 | * @param string $moduleName |
||
| 198 | * @param string $type |
||
| 199 | * @param bool $soft |
||
| 200 | * |
||
| 201 | * @return null|string |
||
| 202 | * @throws NotFoundHttpException |
||
| 203 | */ |
||
| 204 | protected static function getModuleEntry(array $config, string $moduleName, string $type = 'modern', bool $soft = false) |
||
| 205 | { |
||
| 206 | $module = null; |
||
| 207 | // Get the manifest file |
||
| 208 | $manifest = self::getManifestFile($config, $type); |
||
| 209 | if ($manifest !== null) { |
||
| 210 | // Make sure it exists in the manifest |
||
| 211 | if (empty($manifest[$moduleName])) { |
||
| 212 | // Don't report errors for any files in SUPPRESS_ERRORS_FOR_MODULES |
||
| 213 | if (!in_array($moduleName, self::SUPPRESS_ERRORS_FOR_MODULES)) { |
||
| 214 | self::reportError(Craft::t( |
||
| 215 | 'instant-analytics', |
||
| 216 | 'Module does not exist in the manifest: {moduleName}', |
||
| 217 | ['moduleName' => $moduleName] |
||
| 218 | ), $soft); |
||
| 219 | } |
||
| 220 | |||
| 221 | return null; |
||
| 222 | } |
||
| 223 | $module = $manifest[$moduleName]; |
||
| 224 | } |
||
| 225 | |||
| 226 | return $module; |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Return a JSON-decoded manifest file |
||
| 231 | * |
||
| 232 | * @param array $config |
||
| 233 | * @param string $type |
||
| 234 | * |
||
| 235 | * @return null|array |
||
| 236 | * @throws NotFoundHttpException |
||
| 237 | */ |
||
| 238 | protected static function getManifestFile(array $config, string $type = 'modern') |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Return the contents of a JSON file from a URI path |
||
| 275 | * |
||
| 276 | * @param string $path |
||
| 277 | * |
||
| 278 | * @return null|array |
||
| 279 | */ |
||
| 280 | protected static function getJsonFile(string $path) |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Invalidate all of the manifest caches |
||
| 287 | */ |
||
| 288 | public static function invalidateCaches() |
||
| 289 | { |
||
| 290 | $cache = Craft::$app->getCache(); |
||
| 291 | TagDependency::invalidate($cache, self::CACHE_TAG); |
||
| 292 | Craft::info('All manifest caches cleared', __METHOD__); |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Return the contents of a file from a URI path |
||
| 297 | * |
||
| 298 | * @param string $path |
||
| 299 | * @param callable|null $callback |
||
| 300 | * |
||
| 301 | * @return null|mixed |
||
| 302 | */ |
||
| 303 | protected static function getFileFromUri(string $path, callable $callback = null) |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Return the contents of a file from the passed in path |
||
| 324 | * |
||
| 325 | * @param string $path |
||
| 326 | * @param callable $callback |
||
| 327 | * |
||
| 328 | * @return null|mixed |
||
| 329 | */ |
||
| 330 | protected static function getFileContents(string $path, callable $callback = null) |
||
| 331 | { |
||
| 332 | // Return the memoized manifest if it exists |
||
| 333 | if (!empty(self::$files[$path])) { |
||
| 334 | return self::$files[$path]; |
||
| 335 | } |
||
| 336 | // Create the dependency tags |
||
| 337 | $dependency = new TagDependency([ |
||
| 338 | 'tags' => [ |
||
| 339 | self::CACHE_TAG, |
||
| 340 | self::CACHE_TAG.$path, |
||
| 341 | ], |
||
| 342 | ]); |
||
| 343 | // Set the cache duration based on devMode |
||
| 344 | $cacheDuration = Craft::$app->getConfig()->getGeneral()->devMode |
||
| 345 | ? self::DEVMODE_CACHE_DURATION |
||
| 346 | : null; |
||
| 347 | // Get the result from the cache, or parse the file |
||
| 348 | $cache = Craft::$app->getCache(); |
||
| 349 | $file = $cache->getOrSet( |
||
| 350 | self::CACHE_KEY.$path, |
||
| 351 | function () use ($path, $callback) { |
||
| 352 | $result = null; |
||
| 353 | $contents = @file_get_contents($path); |
||
| 354 | if ($contents) { |
||
| 355 | $result = $contents; |
||
| 356 | if ($callback) { |
||
| 357 | $result = $callback($result); |
||
| 358 | } |
||
| 359 | } |
||
| 360 | |||
| 361 | return $result; |
||
| 362 | }, |
||
| 363 | $cacheDuration, |
||
| 364 | $dependency |
||
| 365 | ); |
||
| 366 | self::$files[$path] = $file; |
||
| 367 | |||
| 368 | return $file; |
||
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Combined the passed in paths, whether file system or URL |
||
| 373 | * |
||
| 374 | * @param string ...$paths |
||
| 375 | * |
||
| 376 | * @return string |
||
| 377 | */ |
||
| 378 | protected static function combinePaths(string ...$paths): string |
||
| 379 | { |
||
| 380 | $last_key = \count($paths) - 1; |
||
| 381 | array_walk($paths, function (&$val, $key) use ($last_key) { |
||
| 382 | switch ($key) { |
||
| 383 | case 0: |
||
| 384 | $val = rtrim($val, '/ '); |
||
| 385 | break; |
||
| 386 | case $last_key: |
||
| 387 | $val = ltrim($val, '/ '); |
||
| 388 | break; |
||
| 389 | default: |
||
| 390 | $val = trim($val, '/ '); |
||
| 391 | break; |
||
| 392 | } |
||
| 393 | }); |
||
| 394 | |||
| 395 | $first = array_shift($paths); |
||
| 396 | $last = array_pop($paths); |
||
| 397 | $paths = array_filter($paths); |
||
| 398 | array_unshift($paths, $first); |
||
| 399 | $paths[] = $last; |
||
| 400 | |||
| 401 | return implode('/', $paths); |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @param string $error |
||
| 406 | * @param bool $soft |
||
| 407 | * |
||
| 408 | * @throws NotFoundHttpException |
||
| 409 | */ |
||
| 410 | protected static function reportError(string $error, $soft = false) |
||
| 417 | } |
||
| 418 | } |
||
| 419 | |||
| 420 | // Simulate a static constructor |
||
| 421 | Manifest::__constructStatic(); |
||
| 422 |