| Total Complexity | 57 |
| Total Lines | 298 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Merger 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 Merger, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class Merger implements MergerInterface |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * @var RouterInterface |
||
| 27 | */ |
||
| 28 | private $router; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var ZikulaHttpKernelInterface |
||
| 32 | */ |
||
| 33 | private $kernel; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | private $rootDir; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var integer |
||
| 42 | */ |
||
| 43 | private $lifetime; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var boolean |
||
| 47 | */ |
||
| 48 | private $minify; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var boolean |
||
| 52 | */ |
||
| 53 | private $compress; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var string[] |
||
| 57 | */ |
||
| 58 | private $skipFiles; |
||
| 59 | |||
| 60 | public function __construct( |
||
| 61 | RouterInterface $router, |
||
| 62 | ZikulaHttpKernelInterface $kernel, |
||
| 63 | string $lifetime = '1 day', |
||
| 64 | bool $minify = false, |
||
| 65 | bool $compress = false, |
||
| 66 | array $skipFiles = [] |
||
| 67 | ) { |
||
| 68 | $this->router = $router; |
||
| 69 | $this->kernel = $kernel; |
||
| 70 | $publicDir = realpath($kernel->getProjectDir() . '/public'); |
||
| 71 | $basePath = $router->getContext()->getBaseUrl(); |
||
| 72 | $this->rootDir = str_replace($basePath, '', $publicDir); |
||
| 73 | $this->lifetime = abs((new DateTime($lifetime))->getTimestamp() - (new DateTime())->getTimestamp()); |
||
| 74 | $this->minify = $minify; |
||
| 75 | $this->compress = $compress; |
||
| 76 | |||
| 77 | $this->skipFiles = []; |
||
| 78 | foreach ($skipFiles as $path) { |
||
| 79 | $this->skipFiles[] = $basePath . $path; |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | public function merge(array $assets, $type = 'js'): array |
||
| 84 | { |
||
| 85 | if (!in_array($type, ['js', 'css'])) { |
||
| 86 | return []; |
||
| 87 | } |
||
| 88 | |||
| 89 | $preCachedFiles = []; |
||
| 90 | $cachedFiles = []; |
||
| 91 | $outputFiles = []; |
||
| 92 | $postCachedFiles = []; |
||
| 93 | foreach ($assets as $asset => $weight) { |
||
| 94 | $path = realpath($this->rootDir . $asset); |
||
| 95 | // skip remote files and specific unwanted ones from combining |
||
| 96 | if ( |
||
| 97 | false !== $path |
||
| 98 | && is_file($path) |
||
| 99 | && !in_array($asset, $this->skipFiles) |
||
| 100 | && null === s($asset)->indexOf('/public/bootswatch') |
||
| 101 | && !in_array($weight, [AssetBag::WEIGHT_ROUTER_JS, AssetBag::WEIGHT_ROUTES_JS]) |
||
| 102 | ) { |
||
| 103 | $cachedFiles[] = $path; |
||
| 104 | } elseif (0 > $weight) { |
||
| 105 | $preCachedFiles[$asset] = $weight; |
||
| 106 | } elseif (AssetBag::WEIGHT_DEFAULT < $weight) { |
||
| 107 | $postCachedFiles[$asset] = $weight; |
||
| 108 | } else { |
||
| 109 | $outputFiles[$asset] = $weight; |
||
| 110 | } |
||
| 111 | } |
||
| 112 | $cacheService = new FilesystemAdapter( |
||
| 113 | 'combined_assets', |
||
| 114 | $this->lifetime, |
||
| 115 | $this->kernel->getCacheDir() . '/assets/' . $type |
||
| 116 | ); |
||
| 117 | $key = md5(serialize($assets)) . (int) $this->minify . (int) $this->compress . $this->lifetime . '.combined.' . $type; |
||
| 118 | $cacheService->get($key, function() use ($cachedFiles, $type) { |
||
| 119 | $data = []; |
||
| 120 | foreach ($cachedFiles as $k => $file) { |
||
| 121 | $this->readFile($data, $file, $type); |
||
| 122 | // avoid exposure of absolute server path |
||
| 123 | $pathParts = explode($this->rootDir, $file); |
||
| 124 | $cachedFiles[$k] = end($pathParts); |
||
| 125 | } |
||
| 126 | $now = new DateTime(); |
||
| 127 | array_unshift($data, sprintf("/* --- Combined file written: %s */\n\n", $now->format('c'))); |
||
| 128 | array_unshift($data, sprintf("/* --- Combined files:\n%s\n*/\n\n", implode("\n", $cachedFiles))); |
||
| 129 | $data = implode('', $data); |
||
| 130 | if ('css' === $type && $this->minify) { |
||
| 131 | $data = $this->minify($data); |
||
| 132 | } |
||
| 133 | |||
| 134 | return $data; |
||
| 135 | }); |
||
| 136 | |||
| 137 | $route = $this->router->generate('zikulathememodule_combinedasset_asset', ['type' => $type, 'key' => $key]); |
||
| 138 | $outputFiles[$route] = AssetBag::WEIGHT_DEFAULT; |
||
| 139 | |||
| 140 | $outputFiles = array_merge($preCachedFiles, $outputFiles, $postCachedFiles); |
||
| 141 | |||
| 142 | return $outputFiles; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Read a file and add its contents to the $contents array. |
||
| 147 | * This function includes the content of all "@import" statements (recursive). |
||
| 148 | */ |
||
| 149 | private function readFile(array &$contents, string $file, string $ext): void |
||
| 150 | { |
||
| 151 | if (!file_exists($file)) { |
||
| 152 | return; |
||
| 153 | } |
||
| 154 | $source = fopen($file, 'r'); |
||
| 155 | if (false === $source) { |
||
| 156 | return; |
||
| 157 | } |
||
| 158 | |||
| 159 | // avoid exposure of absolute server path |
||
| 160 | $pathParts = explode($this->rootDir, $file); |
||
| 161 | $relativePath = end($pathParts); |
||
| 162 | $contents[] = "/* --- Source file: {$relativePath} */\n\n"; |
||
| 163 | $inMultilineComment = false; |
||
| 164 | $importsAllowed = true; |
||
| 165 | $wasCommentHack = false; |
||
| 166 | while (!feof($source)) { |
||
| 167 | if ('css' === $ext) { |
||
| 168 | $line = fgets($source, 4096); |
||
| 169 | $lineParse = s(false !== $line ? trim($line) : '')->toString(); |
||
| 170 | $lineParseLength = mb_strlen($lineParse, 'UTF-8'); |
||
| 171 | $newLine = ''; |
||
| 172 | // parse line char by char |
||
| 173 | for ($i = 0; $i < $lineParseLength; $i++) { |
||
| 174 | $char = $lineParse[$i]; |
||
| 175 | $nextchar = $i < ($lineParseLength - 1) ? $lineParse[$i + 1] : ''; |
||
| 176 | if (!$inMultilineComment && '/' === $char && '*' === $nextchar) { |
||
| 177 | // a multiline comment starts here |
||
| 178 | $inMultilineComment = true; |
||
| 179 | $wasCommentHack = false; |
||
| 180 | $newLine .= $char . $nextchar; |
||
| 181 | $i++; |
||
| 182 | } elseif ($inMultilineComment && '*' === $char && '/' === $nextchar) { |
||
| 183 | // a multiline comment stops here |
||
| 184 | $inMultilineComment = false; |
||
| 185 | $newLine .= $char . $nextchar; |
||
| 186 | if ('/*\*//*/' === s($lineParse)->slice($i - 3, 8)) { |
||
| 187 | $wasCommentHack = true; |
||
| 188 | $i += 3; // move to end of hack process hack as it where |
||
| 189 | $newLine .= '/*/'; // fix hack comment because we lost some chars with $i += 3 |
||
| 190 | } |
||
| 191 | $i++; |
||
| 192 | } elseif ($importsAllowed && '@' === $char && '@import' === s($lineParse)->slice($i, 7)) { |
||
| 193 | // an @import starts here |
||
| 194 | $lineParseRest = s($lineParse)->slice($i + 7)->trim(); |
||
| 195 | if ($lineParseRest->ignoreCase()->startsWith('url')) { |
||
| 196 | // the @import uses url to specify the path |
||
| 197 | $posEnd = s($lineParse)->indexOf(';', $i); |
||
| 198 | $charsEnd = s($lineParse)->slice($posEnd - 1, 2); |
||
| 199 | if (');' === $charsEnd) { |
||
| 200 | // used url() without media |
||
| 201 | $start = $lineParseRest->indexOf('(') + 1; |
||
| 202 | $end = $lineParseRest->indexOf(')'); |
||
| 203 | $url = $lineParseRest->slice($start, $end - $start); |
||
| 204 | $url = $url->trimStart('"')->trimStart('"'); |
||
| 205 | // fix url |
||
| 206 | if ($url->startsWith('http')) { |
||
| 207 | $newLine .= '@import url("' . $url . '");'; |
||
| 208 | } else { |
||
| 209 | $url = dirname($file) . '/' . $url; |
||
| 210 | if (!$wasCommentHack) { |
||
| 211 | // clear buffer |
||
| 212 | $contents[] = $newLine; |
||
| 213 | $newLine = ''; |
||
| 214 | // process include |
||
| 215 | $this->readFile($contents, $url, $ext); |
||
| 216 | } else { |
||
| 217 | $newLine .= '@import url("' . $url . '");'; |
||
| 218 | } |
||
| 219 | } |
||
| 220 | // skip @import statement |
||
| 221 | $i += $posEnd - $i; |
||
| 222 | } else { |
||
| 223 | // @import contains media type so we can't include its contents. |
||
| 224 | // We need to fix the url instead. |
||
| 225 | $start = $lineParseRest->indexOf('(') + 1; |
||
| 226 | $end = $lineParseRest->indexOf(')'); |
||
| 227 | $url = $lineParseRest->slice($start, $end - $start); |
||
| 228 | $url = $url->trimStart('"')->trimStart('"'); |
||
| 229 | // fix url |
||
| 230 | $url = dirname($file) . '/' . $url; |
||
| 231 | // readd @import with fixed url |
||
| 232 | $newLine .= '@import url("' . $url . '")' . $lineParseRest->slice($end + 1, $lineParseRest->indexOf(';') - $end - 1) . ';'; |
||
| 233 | // skip @import statement |
||
| 234 | $i += $posEnd - $i; |
||
| 235 | } |
||
| 236 | } elseif ($lineParseRest->startsWith('"') || $lineParseRest->startsWith('\'')) { |
||
| 237 | // the @import uses an normal string to specify the path |
||
| 238 | $posEnd = $lineParseRest->indexOf(';'); |
||
| 239 | $url = $lineParseRest->slice(1, $posEnd - 2); |
||
| 240 | $posEnd = s($lineParse)->indexOf(';', $i); |
||
| 241 | // fix url |
||
| 242 | $url = dirname($file) . '/' . $url; |
||
| 243 | if (!$wasCommentHack) { |
||
| 244 | // clear buffer |
||
| 245 | $contents[] = $newLine; |
||
| 246 | $newLine = ''; |
||
| 247 | // process include |
||
| 248 | self::readFile($contents, $url, $ext); |
||
|
|
|||
| 249 | } else { |
||
| 250 | $newLine .= '@import url("' . $url . '");'; |
||
| 251 | } |
||
| 252 | // skip @import statement |
||
| 253 | $i += $posEnd - $i; |
||
| 254 | } |
||
| 255 | } elseif (!$inMultilineComment && ' ' !== $char && "\n" !== $char && "\r\n" !== $char && "\r" !== $char) { |
||
| 256 | // css rule found -> stop processing of @import statements |
||
| 257 | $importsAllowed = false; |
||
| 258 | $newLine .= $char; |
||
| 259 | } else { |
||
| 260 | $newLine .= $char; |
||
| 261 | } |
||
| 262 | } |
||
| 263 | // fix other paths after @import processing |
||
| 264 | if (!$importsAllowed) { |
||
| 265 | $relativePath = str_replace(realpath($this->rootDir), '', $file); |
||
| 266 | $newLine = $this->cssFixPath($newLine, explode('/', dirname($relativePath))); |
||
| 267 | } |
||
| 268 | $contents[] = $newLine; |
||
| 269 | } else { |
||
| 270 | $line = fgets($source, 4096); |
||
| 271 | if (false === $line || 0 === mb_strpos($line, '//# sourceMappingURL=')) { |
||
| 272 | continue; |
||
| 273 | } |
||
| 274 | $contents[] = $line; |
||
| 275 | } |
||
| 276 | } |
||
| 277 | fclose($source); |
||
| 278 | if ('js' === $ext) { |
||
| 279 | $contents[] = "\n;\n"; |
||
| 280 | } else { |
||
| 281 | $contents[] = "\n\n"; |
||
| 282 | } |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Fix paths in CSS files. |
||
| 287 | */ |
||
| 288 | private function cssFixPath(string $line, array $filePathSegments = []): string |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Remove comments, whitespace and spaces from css files. |
||
| 310 | */ |
||
| 311 | private function minify(string $input): string |
||
| 321 | } |
||
| 322 | } |
||
| 323 |