| Total Complexity | 54 |
| Total Lines | 507 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Config 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 Config, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class Config |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * An array containing configuration options. |
||
| 31 | * |
||
| 32 | * @var array $options |
||
| 33 | */ |
||
| 34 | protected $options; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Constructor. |
||
| 38 | * |
||
| 39 | * Depending on the type of the parameter passed to this function, |
||
| 40 | * config array is used directly or it is loaded from a file. |
||
| 41 | * |
||
| 42 | * <b>Important</b>: If you use a PHP file to store your config, remember to use |
||
| 43 | * the <code>return</code> statement inside the file scope to return |
||
| 44 | * the array. |
||
| 45 | * |
||
| 46 | * @param array|string $config |
||
| 47 | * |
||
| 48 | * @throws InvalidConfigException if config was not loaded properly. |
||
| 49 | */ |
||
| 50 | public function __construct($config) |
||
| 51 | { |
||
| 52 | setlocale(LC_ALL, "en_US.utf8"); |
||
| 53 | |||
| 54 | // Check if default timezone was set |
||
| 55 | try { |
||
| 56 | new \DateTime(); |
||
| 57 | } catch (\Exception $e) { |
||
| 58 | date_default_timezone_set('UTC'); |
||
| 59 | } |
||
| 60 | |||
| 61 | if (is_string($config) && is_readable($config)) { |
||
| 62 | $options = require $config; |
||
| 63 | } else { |
||
| 64 | $options = $config; |
||
| 65 | } |
||
| 66 | |||
| 67 | if (!is_array($options)) { |
||
| 68 | throw new InvalidConfigException("Couldn't load configuration. Please check configuration file."); |
||
| 69 | } |
||
| 70 | |||
| 71 | $this->options = $this->mergeDefaultOptions($options); |
||
| 72 | |||
| 73 | $this->validate(); |
||
| 74 | $this->process(); |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Merges default or missing configuration options. |
||
| 79 | * |
||
| 80 | * @param array $options options passed to CKFinder |
||
| 81 | * |
||
| 82 | * @return array |
||
| 83 | */ |
||
| 84 | protected function mergeDefaultOptions($options) |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Returns the configuration node under the path defined in the parameter. |
||
| 238 | * |
||
| 239 | * For easier access to nested configuration options the config `$name` |
||
| 240 | * parameter can be passed also as a dot-separated path. |
||
| 241 | * For example, to check if thumbnails are enabled you can use: |
||
| 242 | * |
||
| 243 | * $config->get('thumbnails.enabled') |
||
| 244 | * |
||
| 245 | * @param string $name config node name |
||
| 246 | * |
||
| 247 | * @return mixed config node value |
||
| 248 | * |
||
| 249 | */ |
||
| 250 | public function get($name) |
||
| 251 | { |
||
| 252 | if (isset($this->options[$name])) { |
||
| 253 | return $this->options[$name]; |
||
| 254 | } |
||
| 255 | |||
| 256 | $keys = explode('.', $name); |
||
| 257 | $array = $this->options; |
||
| 258 | |||
| 259 | do { |
||
| 260 | $key = array_shift($keys); |
||
| 261 | if (isset($array[$key])) { |
||
| 262 | if ($keys) { |
||
| 263 | if (is_array($array[$key])) { |
||
| 264 | $array = $array[$key]; |
||
| 265 | } else { |
||
| 266 | break; |
||
| 267 | } |
||
| 268 | } else { |
||
| 269 | return $array[$key]; |
||
| 270 | } |
||
| 271 | } else { |
||
| 272 | break; |
||
| 273 | } |
||
| 274 | } while ($keys); |
||
| 275 | |||
| 276 | return null; |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Validates the config array structure. |
||
| 281 | * |
||
| 282 | * @throws InvalidConfigException if config structure is invalid. |
||
| 283 | */ |
||
| 284 | protected function validate() |
||
| 285 | { |
||
| 286 | $checkMissingNodes = function (array $required, array $actual, $prefix = '') { |
||
| 287 | $missing = array_keys(array_diff_key(array_flip($required), $actual)); |
||
| 288 | |||
| 289 | if (!empty($missing)) { |
||
| 290 | throw new InvalidConfigException(sprintf( |
||
| 291 | "CKFinder configuration doesn't contain all required fields. " . |
||
| 292 | "Please check configuration file. Missing fields: %s", |
||
| 293 | ($prefix ? "{$prefix}: " : '') . implode(', ', $missing))); |
||
| 294 | } |
||
| 295 | }; |
||
| 296 | |||
| 297 | $requiredRootNodes = array('authentication', 'licenseName', 'licenseKey', 'privateDir', 'images', |
||
| 298 | 'backends', 'defaultResourceTypes', 'resourceTypes', 'roleSessionVar', 'accessControl', |
||
| 299 | 'checkDoubleExtension', 'disallowUnsafeCharacters', 'secureImageUploads', 'checkSizeAfterScaling', |
||
| 300 | 'htmlExtensions', 'hideFolders', 'hideFiles', 'forceAscii', 'xSendfile', 'debug', 'pluginsDirectory', 'plugins'); |
||
| 301 | |||
| 302 | $checkMissingNodes($requiredRootNodes, $this->options); |
||
| 303 | $checkMissingNodes(array('backend', 'tags', 'logs', 'cache', 'thumbs'), $this->options['privateDir'], '[privateDir]'); |
||
| 304 | $checkMissingNodes(array('maxWidth', 'maxHeight', 'quality'), $this->options['images'], '[images]'); |
||
| 305 | |||
| 306 | $backends = array(); |
||
| 307 | |||
| 308 | foreach ($this->options['backends'] as $i => $backendConfig) { |
||
| 309 | $checkMissingNodes(array('name', 'adapter'), $backendConfig, "[backends][{$i}]"); |
||
| 310 | $backends[] = $backendConfig['name']; |
||
| 311 | } |
||
| 312 | |||
| 313 | foreach ($this->options['resourceTypes'] as $i => $resourceTypeConfig) { |
||
| 314 | $checkMissingNodes(array('name', 'directory', 'maxSize', 'allowedExtensions', 'deniedExtensions', 'backend'), |
||
| 315 | $resourceTypeConfig, "[resourceTypes][{$i}]"); |
||
| 316 | |||
| 317 | if (!in_array($resourceTypeConfig['backend'], $backends)) { |
||
| 318 | throw new InvalidConfigException("Backend '{$resourceTypeConfig['backend']}' is not defined: [resourceTypes][{$i}]"); |
||
| 319 | } |
||
| 320 | } |
||
| 321 | |||
| 322 | foreach ($this->options['accessControl'] as $i => $aclConfig) { |
||
| 323 | $checkMissingNodes(array('role', 'resourceType', 'folder'), $aclConfig, "[accessControl][{$i}]"); |
||
| 324 | } |
||
| 325 | |||
| 326 | if (!is_callable($this->options['authentication'])) { |
||
| 327 | throw new InvalidConfigException("CKFinder Authentication config field must be a PHP callable"); |
||
| 328 | } |
||
| 329 | |||
| 330 | if (!is_writable($this->options['tempDirectory'])) { |
||
| 331 | throw new InvalidConfigException("The temporary folder is not writable for CKFinder"); |
||
| 332 | } |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Processes the configuration array. |
||
| 337 | */ |
||
| 338 | protected function process() |
||
| 339 | { |
||
| 340 | $this->options['defaultResourceTypes'] = |
||
| 341 | array_filter( |
||
| 342 | array_map('trim', |
||
| 343 | explode(',', $this->options['defaultResourceTypes']) |
||
| 344 | ), |
||
| 345 | 'strlen'); |
||
| 346 | |||
| 347 | |||
| 348 | $formatToArray = function ($input) { |
||
| 349 | $input = is_array($input) ? $input : explode(',', $input); |
||
| 350 | |||
| 351 | return |
||
| 352 | array_filter( |
||
| 353 | array_map('strtolower', |
||
| 354 | array_map('trim', $input) |
||
| 355 | ), |
||
| 356 | 'strlen'); |
||
| 357 | }; |
||
| 358 | |||
| 359 | foreach ($this->options['resourceTypes'] as $resourceTypeKey => $resourceTypeConfig) { |
||
| 360 | $resourceTypeConfig['allowedExtensions'] = $formatToArray($resourceTypeConfig['allowedExtensions']); |
||
| 361 | $resourceTypeConfig['deniedExtensions'] = $formatToArray($resourceTypeConfig['deniedExtensions']); |
||
| 362 | $resourceTypeConfig['maxSize'] = Utils::returnBytes((string) $resourceTypeConfig['maxSize']); |
||
| 363 | |||
| 364 | $this->options['resourceTypes'][$resourceTypeConfig['name']] = $resourceTypeConfig; |
||
| 365 | |||
| 366 | if ($resourceTypeKey !== $resourceTypeConfig['name']) { |
||
| 367 | unset($this->options['resourceTypes'][$resourceTypeKey]); |
||
| 368 | } |
||
| 369 | } |
||
| 370 | |||
| 371 | foreach ($this->options['backends'] as $backendKey => $backendConfig) { |
||
| 372 | $this->options['backends'][$backendConfig['name']] = $backendConfig; |
||
| 373 | |||
| 374 | if ($backendKey !== $backendConfig['name']) { |
||
| 375 | unset($this->options['backends'][$backendKey]); |
||
| 376 | } |
||
| 377 | } |
||
| 378 | |||
| 379 | $this->options['htmlExtensions'] = $formatToArray($this->options['htmlExtensions']); |
||
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Returns the default resource types names. |
||
| 384 | * |
||
| 385 | * @return array |
||
| 386 | */ |
||
| 387 | public function getDefaultResourceTypes() |
||
| 388 | { |
||
| 389 | return $this->options['defaultResourceTypes']; |
||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Returns all defined resource types names. |
||
| 394 | * |
||
| 395 | * @return array |
||
| 396 | */ |
||
| 397 | public function getResourceTypes() |
||
| 398 | { |
||
| 399 | return array_keys($this->options['resourceTypes']); |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Returns the configuration node for a given resource type. |
||
| 404 | * |
||
| 405 | * @param string $resourceType resource type name |
||
| 406 | * |
||
| 407 | * @return array configuration node for the resource type |
||
| 408 | * |
||
| 409 | * @throws InvalidResourceTypeException if the resource type does not exist |
||
| 410 | */ |
||
| 411 | public function getResourceTypeNode($resourceType) |
||
| 412 | { |
||
| 413 | if (array_key_exists($resourceType, $this->options['resourceTypes'])) { |
||
| 414 | return $this->options['resourceTypes'][$resourceType]; |
||
| 415 | } else { |
||
| 416 | throw new InvalidResourceTypeException("Invalid resource type: {$resourceType}"); |
||
| 417 | } |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Returns the regex used for hidden files check. |
||
| 422 | * @return string |
||
| 423 | */ |
||
| 424 | public function getHideFilesRegex() |
||
| 425 | { |
||
| 426 | static $hideFilesRegex; |
||
| 427 | |||
| 428 | if (!isset($hideFilesRegex)) { |
||
| 429 | $hideFilesConfig = $this->options['hideFiles']; |
||
| 430 | |||
| 431 | if ($hideFilesConfig && is_array($hideFilesConfig)) { |
||
| 432 | $hideFilesRegex = join("|", $hideFilesConfig); |
||
| 433 | $hideFilesRegex = strtr($hideFilesRegex, array("?" => "__QMK__", "*" => "__AST__", "|" => "__PIP__")); |
||
| 434 | $hideFilesRegex = preg_quote($hideFilesRegex, "/"); |
||
| 435 | $hideFilesRegex = strtr($hideFilesRegex, array("__QMK__" => ".", "__AST__" => ".*", "__PIP__" => "|")); |
||
| 436 | $hideFilesRegex = "/^(?:" . $hideFilesRegex . ")$/uim"; |
||
| 437 | } else { |
||
| 438 | $hideFilesRegex = ""; |
||
| 439 | } |
||
| 440 | } |
||
| 441 | |||
| 442 | return $hideFilesRegex; |
||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Returns the regex used for hidden folders check. |
||
| 447 | * @return string |
||
| 448 | */ |
||
| 449 | public function getHideFoldersRegex() |
||
| 450 | { |
||
| 451 | static $hideFoldersRegex; |
||
| 452 | |||
| 453 | if (!isset($hideFoldersRegex)) { |
||
| 454 | $hideFoldersConfig = $this->options['hideFolders']; |
||
| 455 | |||
| 456 | if ($hideFoldersConfig && is_array($hideFoldersConfig)) { |
||
| 457 | $hideFoldersRegex = join("|", $hideFoldersConfig); |
||
| 458 | $hideFoldersRegex = strtr($hideFoldersRegex, array("?" => "__QMK__", "*" => "__AST__", "|" => "__PIP__")); |
||
| 459 | $hideFoldersRegex = preg_quote($hideFoldersRegex, "/"); |
||
| 460 | $hideFoldersRegex = strtr($hideFoldersRegex, array("__QMK__" => ".", "__AST__" => ".*", "__PIP__" => "|")); |
||
| 461 | $hideFoldersRegex = "/^(?:" . $hideFoldersRegex . ")$/uim"; |
||
| 462 | } else { |
||
| 463 | $hideFoldersRegex = ""; |
||
| 464 | } |
||
| 465 | } |
||
| 466 | |||
| 467 | return $hideFoldersRegex; |
||
| 468 | } |
||
| 469 | |||
| 470 | /** |
||
| 471 | * If the config node does not exist, creates the node with a given name and values. |
||
| 472 | * Otherwise extends the config node with additional (default) values. |
||
| 473 | * |
||
| 474 | * @param string $nodeName |
||
| 475 | * @param array $values |
||
| 476 | */ |
||
| 477 | public function extend($nodeName, array $values) |
||
| 478 | { |
||
| 479 | if (!isset($this->options[$nodeName])) { |
||
| 480 | $this->options[$nodeName] = $values; |
||
| 481 | } else { |
||
| 482 | $this->options[$nodeName] = array_replace_recursive($values, $this->options[$nodeName]); |
||
| 483 | } |
||
| 484 | } |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Returns the backend-relative private directory path. |
||
| 488 | * |
||
| 489 | * @param string $privateDirIdentifier |
||
| 490 | * |
||
| 491 | * @return mixed |
||
| 492 | */ |
||
| 493 | public function getPrivateDirPath($privateDirIdentifier) |
||
| 506 | } |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Checks if the debug logger with a given name is enabled. |
||
| 510 | * @param string $loggerName debug logger name |
||
| 511 | * |
||
| 512 | * @return bool `true` if enabled |
||
| 513 | */ |
||
| 514 | public function isDebugLoggerEnabled($loggerName) |
||
| 517 | } |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Returns backend configuration by name. |
||
| 521 | * |
||
| 522 | * @param string $backendName |
||
| 523 | * |
||
| 524 | * @return array backend configuration node |
||
| 525 | * |
||
| 526 | * @throws \InvalidArgumentException |
||
| 527 | */ |
||
| 528 | public function getBackendNode($backendName) |
||
| 534 | } |
||
| 535 | } |
||
| 536 | } |
||
| 537 |