| Total Complexity | 60 |
| Total Lines | 416 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Assets 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 Assets, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | * @package O2System\Framework\Http\Presenter |
||
| 25 | */ |
||
| 26 | class Assets |
||
| 27 | { |
||
| 28 | use FilePathCollectorTrait; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Assets::$head |
||
| 32 | * |
||
| 33 | * @var \O2System\Framework\Http\Presenter\Assets\Positions\Head |
||
| 34 | */ |
||
| 35 | protected $head; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Assets::$body |
||
| 39 | * |
||
| 40 | * @var \O2System\Framework\Http\Presenter\Assets\Positions\Body |
||
| 41 | */ |
||
| 42 | protected $body; |
||
| 43 | |||
| 44 | // ------------------------------------------------------------------------ |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Assets::__construct |
||
| 48 | */ |
||
| 49 | public function __construct() |
||
| 50 | { |
||
| 51 | $this->addFilePaths([ |
||
| 52 | PATH_RESOURCES, |
||
| 53 | PATH_RESOURCES . 'views' . DIRECTORY_SEPARATOR |
||
| 54 | ]); |
||
| 55 | |||
| 56 | $this->head = new Assets\Positions\Head(); |
||
| 57 | $this->body = new Assets\Positions\Body(); |
||
| 58 | } |
||
| 59 | |||
| 60 | // ------------------------------------------------------------------------ |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Assets::autoload |
||
| 64 | * |
||
| 65 | * @param array $assets |
||
| 66 | * |
||
| 67 | * @return void |
||
| 68 | */ |
||
| 69 | public function autoload($assets) |
||
| 70 | { |
||
| 71 | foreach ($assets as $position => $collections) { |
||
| 72 | if (property_exists($this, $position)) { |
||
| 73 | |||
| 74 | if ($collections instanceof \ArrayObject) { |
||
| 75 | $collections = $collections->getArrayCopy(); |
||
| 76 | } |
||
| 77 | |||
| 78 | $this->{$position}->loadCollections($collections); |
||
| 79 | } elseif ($position === 'packages') { |
||
| 80 | $this->loadPackages($collections); |
||
| 81 | } elseif ($position === 'css') { |
||
| 82 | $this->loadCss($collections); |
||
| 83 | } elseif ($position === 'js') { |
||
| 84 | $this->loadJs($collections); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | // ------------------------------------------------------------------------ |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Assets::loadPackages |
||
| 93 | * |
||
| 94 | * @param array $packages |
||
| 95 | * |
||
| 96 | * @return void |
||
| 97 | */ |
||
| 98 | public function loadPackages($packages) |
||
| 99 | { |
||
| 100 | foreach ($packages as $package => $files) { |
||
| 101 | if (is_string($files)) { |
||
| 102 | $this->loadPackage($files); |
||
| 103 | } elseif (is_array($files)) { |
||
| 104 | $this->loadPackage($package, $files); |
||
| 105 | } elseif (is_object($files)) { |
||
| 106 | $this->loadPackage($package, get_object_vars($files)); |
||
| 107 | } |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | // ------------------------------------------------------------------------ |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Assets::loadPackage |
||
| 115 | * |
||
| 116 | * @param string $package |
||
| 117 | * @param array $subPackages |
||
| 118 | * |
||
| 119 | * @return void |
||
| 120 | */ |
||
| 121 | public function loadPackage($package, $subPackages = []) |
||
| 122 | { |
||
| 123 | $packageDir = implode(DIRECTORY_SEPARATOR, [ |
||
| 124 | 'packages', |
||
| 125 | $package, |
||
| 126 | ]) . DIRECTORY_SEPARATOR; |
||
| 127 | |||
| 128 | if (count($subPackages)) { |
||
| 129 | |||
| 130 | if (array_key_exists('libraries', $subPackages)) { |
||
| 131 | foreach ($subPackages[ 'libraries' ] as $subPackageFile) { |
||
| 132 | $pluginDir = $packageDir . 'libraries' . DIRECTORY_SEPARATOR; |
||
| 133 | $pluginName = $subPackageFile; |
||
| 134 | |||
| 135 | if ($this->body->loadFile($pluginDir . $pluginName . DIRECTORY_SEPARATOR . $pluginName . '.js')) { |
||
| 136 | $this->head->loadFile($pluginDir . $pluginName . DIRECTORY_SEPARATOR . $pluginName . '.css'); |
||
| 137 | } else { |
||
| 138 | $this->body->loadFile($pluginDir . $pluginName . '.js'); |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | unset($subPackages[ 'libraries' ]); |
||
| 143 | } |
||
| 144 | |||
| 145 | $this->head->loadFile($packageDir . $package . '.css'); |
||
| 146 | $this->body->loadFile($packageDir . $package . '.js'); |
||
| 147 | |||
| 148 | foreach ($subPackages as $subPackage => $subPackageFiles) { |
||
| 149 | if ($subPackage === 'theme' or $subPackage === 'themes') { |
||
| 150 | if (is_string($subPackageFiles)) { |
||
| 151 | $subPackageFiles = [$subPackageFiles]; |
||
| 152 | } |
||
| 153 | |||
| 154 | foreach ($subPackageFiles as $themeName) { |
||
| 155 | $themeDir = $packageDir . 'themes' . DIRECTORY_SEPARATOR; |
||
| 156 | |||
| 157 | if ($this->head->loadFile($themeDir . $themeName . DIRECTORY_SEPARATOR . $themeName . '.css')) { |
||
| 158 | $this->body->loadFile($themeDir . $themeName . DIRECTORY_SEPARATOR . $themeName . '.js'); |
||
| 159 | } else { |
||
| 160 | $this->head->loadFile($themeDir . $themeName . '.css'); |
||
| 161 | } |
||
| 162 | } |
||
| 163 | } elseif ($subPackage === 'plugins') { |
||
| 164 | foreach ($subPackageFiles as $subPackageFile) { |
||
| 165 | $pluginDir = $packageDir . 'plugins' . DIRECTORY_SEPARATOR; |
||
| 166 | $pluginName = $subPackageFile; |
||
| 167 | |||
| 168 | $pluginName = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $pluginName); |
||
| 169 | if (strpos($pluginName, DIRECTORY_SEPARATOR) !== false) { |
||
| 170 | $pluginDir .= pathinfo($pluginName, PATHINFO_DIRNAME) . DIRECTORY_SEPARATOR; |
||
| 171 | $pluginName = pathinfo($pluginName, PATHINFO_BASENAME); |
||
| 172 | } |
||
| 173 | |||
| 174 | if ($this->body->loadFile($pluginDir . $pluginName . DIRECTORY_SEPARATOR . $pluginName . '.js')) { |
||
| 175 | $this->head->loadFile($pluginDir . $pluginName . DIRECTORY_SEPARATOR . $pluginName . '.css'); |
||
| 176 | } else { |
||
| 177 | $this->body->loadFile($pluginDir . $pluginName . '.js'); |
||
| 178 | } |
||
| 179 | } |
||
| 180 | } elseif ($subPackage === 'libraries') { |
||
| 181 | foreach ($subPackageFiles as $subPackageFile) { |
||
| 182 | $libraryDir = $packageDir . 'libraries' . DIRECTORY_SEPARATOR; |
||
| 183 | $libraryName = $subPackageFile; |
||
| 184 | |||
| 185 | $libraryName = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $libraryName); |
||
| 186 | if (strpos($libraryName, DIRECTORY_SEPARATOR) !== false) { |
||
| 187 | $libraryDir .= pathinfo($libraryName, PATHINFO_DIRNAME) . DIRECTORY_SEPARATOR; |
||
| 188 | $libraryName = pathinfo($libraryName, PATHINFO_BASENAME); |
||
| 189 | } |
||
| 190 | |||
| 191 | if ($this->body->loadFile($libraryDir . $libraryName . DIRECTORY_SEPARATOR . $libraryName . '.js')) { |
||
| 192 | $this->head->loadFile($libraryDir . $libraryName . DIRECTORY_SEPARATOR . $libraryName . '.css'); |
||
| 193 | } else { |
||
| 194 | $this->body->loadFile($libraryDir . $libraryName . '.js'); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | } |
||
| 198 | } |
||
| 199 | } else { |
||
| 200 | $this->head->loadFile($packageDir . $package . '.css'); |
||
| 201 | $this->body->loadFile($packageDir . $package . '.js'); |
||
| 202 | } |
||
| 203 | } |
||
| 204 | |||
| 205 | // ------------------------------------------------------------------------ |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Assets::loadCss |
||
| 209 | * |
||
| 210 | * @param string|array $files |
||
| 211 | * |
||
| 212 | * @return void |
||
| 213 | */ |
||
| 214 | public function loadCss($files) |
||
| 215 | { |
||
| 216 | $files = is_string($files) ? [$files] : $files; |
||
| 217 | $this->head->loadCollections(['css' => $files]); |
||
| 218 | } |
||
| 219 | |||
| 220 | // ------------------------------------------------------------------------ |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Assets::loadJs |
||
| 224 | * |
||
| 225 | * @param string|array $files |
||
| 226 | * @param string $position |
||
| 227 | * |
||
| 228 | * @return void |
||
| 229 | */ |
||
| 230 | public function loadJs($files, $position = 'body') |
||
| 231 | { |
||
| 232 | $files = is_string($files) ? [$files] : $files; |
||
| 233 | $this->{$position}->loadCollections(['js' => $files]); |
||
| 234 | } |
||
| 235 | |||
| 236 | // ------------------------------------------------------------------------ |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Assets::loadFiles |
||
| 240 | * |
||
| 241 | * @param array $assets |
||
| 242 | * |
||
| 243 | * @return void |
||
| 244 | */ |
||
| 245 | public function loadFiles($assets) |
||
| 246 | { |
||
| 247 | foreach ($assets as $type => $item) { |
||
| 248 | $addMethod = 'load' . ucfirst($type); |
||
| 249 | |||
| 250 | if (method_exists($this, $addMethod)) { |
||
| 251 | call_user_func_array([&$this, $addMethod], [$item]); |
||
| 252 | } |
||
| 253 | } |
||
| 254 | } |
||
| 255 | |||
| 256 | // ------------------------------------------------------------------------ |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Assets::theme |
||
| 260 | * |
||
| 261 | * @param string $path |
||
| 262 | * |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | public function theme($path) |
||
| 266 | { |
||
| 267 | $path = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $path); |
||
| 268 | |||
| 269 | if (is_file($filePath = PATH_THEME . $path)) { |
||
| 270 | return path_to_url($filePath); |
||
| 271 | } |
||
| 272 | } |
||
| 273 | |||
| 274 | // ------------------------------------------------------------------------ |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Assets::file |
||
| 278 | * |
||
| 279 | * @param string $file |
||
| 280 | * |
||
| 281 | * @return string |
||
| 282 | */ |
||
| 283 | public function file($file) |
||
| 284 | { |
||
| 285 | $filePaths = loader()->getPublicDirs(true); |
||
| 286 | |||
| 287 | foreach ($filePaths as $filePath) { |
||
| 288 | if (is_file($filePath . $file)) { |
||
| 289 | return path_to_url($filePath . $file); |
||
| 290 | break; |
||
| 291 | } |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | // ------------------------------------------------------------------------ |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Assets::image |
||
| 299 | * |
||
| 300 | * @param string $image |
||
| 301 | * |
||
| 302 | * @return string |
||
| 303 | */ |
||
| 304 | public function image($image) |
||
| 305 | { |
||
| 306 | $filePaths = loader()->getPublicDirs(true); |
||
| 307 | $image = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $image); |
||
| 308 | |||
| 309 | foreach ($filePaths as $filePath) { |
||
| 310 | $filePath .= 'img' . DIRECTORY_SEPARATOR; |
||
| 311 | |||
| 312 | if (is_file($filePath . $image)) { |
||
| 313 | return path_to_url($filePath . $image); |
||
| 314 | break; |
||
| 315 | } |
||
| 316 | |||
| 317 | unset($filePath); |
||
| 318 | } |
||
| 319 | } |
||
| 320 | |||
| 321 | // ------------------------------------------------------------------------ |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Assets::media |
||
| 325 | * |
||
| 326 | * @param string $media |
||
| 327 | * |
||
| 328 | * @return string |
||
| 329 | */ |
||
| 330 | public function media($media) |
||
| 331 | { |
||
| 332 | $filePaths = loader()->getPublicDirs(true); |
||
| 333 | $media = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $media); |
||
| 334 | |||
| 335 | foreach ($filePaths as $filePath) { |
||
| 336 | $filePath .= 'media' . DIRECTORY_SEPARATOR; |
||
| 337 | |||
| 338 | if (is_file($filePath . $media)) { |
||
| 339 | return path_to_url($filePath . $media); |
||
| 340 | break; |
||
| 341 | } |
||
| 342 | |||
| 343 | unset($filePath); |
||
| 344 | } |
||
| 345 | } |
||
| 346 | |||
| 347 | // ------------------------------------------------------------------------ |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Assets::parseSourceCode |
||
| 351 | * |
||
| 352 | * @param string $sourceCode |
||
| 353 | * |
||
| 354 | * @return string |
||
| 355 | */ |
||
| 356 | public function parseSourceCode($sourceCode) |
||
| 357 | { |
||
| 358 | $sourceCode = str_replace( |
||
| 359 | [ |
||
| 360 | '"../assets/', |
||
| 361 | "'../assets/", |
||
| 362 | "(../assets/", |
||
| 363 | ], |
||
| 364 | [ |
||
| 365 | '"' . base_url() . '/assets/', |
||
| 366 | "'" . base_url() . '/assets/', |
||
| 367 | "(" . base_url() . '/assets/', |
||
| 368 | ], |
||
| 369 | $sourceCode); |
||
| 370 | |||
| 371 | if (presenter()->theme) { |
||
| 372 | $sourceCode = str_replace( |
||
| 373 | [ |
||
| 374 | '"assets/', |
||
| 375 | "'assets/", |
||
| 376 | "(assets/", |
||
| 377 | |||
| 378 | // with dot |
||
| 379 | '"./assets/', |
||
| 380 | "'./assets/", |
||
| 381 | "(./assets/", |
||
| 382 | ], |
||
| 383 | [ |
||
| 384 | '"' . presenter()->theme->getUrl('assets/'), |
||
| 385 | "'" . presenter()->theme->getUrl('assets/'), |
||
| 386 | "(" . presenter()->theme->getUrl('assets/'), |
||
| 387 | |||
| 388 | // with dot |
||
| 389 | '"' . presenter()->theme->getUrl('assets/'), |
||
| 390 | "'" . presenter()->theme->getUrl('assets/'), |
||
| 391 | "(" . presenter()->theme->getUrl('assets/'), |
||
| 392 | ], |
||
| 393 | $sourceCode); |
||
| 394 | } |
||
| 395 | |||
| 396 | // Valet path fixes |
||
| 397 | if (isset($_SERVER[ 'SCRIPT_FILENAME' ])) { |
||
| 398 | $valetPath = dirname($_SERVER[ 'SCRIPT_FILENAME' ]) . DIRECTORY_SEPARATOR; |
||
| 399 | } else { |
||
| 400 | $PATH_ROOT = $_SERVER[ 'DOCUMENT_ROOT' ]; |
||
| 401 | |||
| 402 | if (isset($_SERVER[ 'PHP_SELF' ])) { |
||
| 403 | $valetPath = $PATH_ROOT . dirname($_SERVER[ 'PHP_SELF' ]) . DIRECTORY_SEPARATOR; |
||
| 404 | } elseif (isset($_SERVER[ 'DOCUMENT_URI' ])) { |
||
| 405 | $valetPath = $PATH_ROOT . dirname($_SERVER[ 'DOCUMENT_URI' ]) . DIRECTORY_SEPARATOR; |
||
| 406 | } elseif (isset($_SERVER[ 'REQUEST_URI' ])) { |
||
| 407 | $valetPath = $PATH_ROOT . dirname($_SERVER[ 'REQUEST_URI' ]) . DIRECTORY_SEPARATOR; |
||
| 408 | } elseif (isset($_SERVER[ 'SCRIPT_NAME' ])) { |
||
| 409 | $valetPath = $PATH_ROOT . dirname($_SERVER[ 'SCRIPT_NAME' ]) . DIRECTORY_SEPARATOR; |
||
| 410 | } |
||
| 411 | } |
||
| 412 | |||
| 413 | if (isset($valetPath)) { |
||
| 414 | $sourceCode = str_replace($valetPath, '/', $sourceCode); |
||
| 415 | } |
||
| 416 | |||
| 417 | return $sourceCode; |
||
| 418 | } |
||
| 419 | |||
| 420 | // ------------------------------------------------------------------------ |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Assets::getHead |
||
| 424 | * |
||
| 425 | * @return \O2System\Framework\Http\Presenter\Assets\Positions\Head |
||
| 426 | */ |
||
| 427 | public function getHead() |
||
| 428 | { |
||
| 429 | return $this->head; |
||
| 430 | } |
||
| 431 | |||
| 432 | // ------------------------------------------------------------------------ |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Assets::getBody |
||
| 436 | * |
||
| 437 | * @return \O2System\Framework\Http\Presenter\Assets\Positions\Body |
||
| 438 | */ |
||
| 439 | public function getBody() |
||
| 440 | { |
||
| 444 |