| 1 |
<?php |
| 2 |
/** |
| 3 |
* Responsible for loading configuration items from different locations. |
| 4 |
* |
| 5 |
* @note When the request begins at advanced-cache.php, the database is not yet available, and we rely on the file based |
| 6 |
* configuration. |
| 7 |
* |
| 8 |
* @package SolidWP\Performance |
| 9 |
*/ |
| 10 |
|
| 11 |
declare( strict_types=1 ); |
| 12 |
|
| 13 |
namespace SolidWP\Performance\Config; |
| 14 |
|
| 15 |
use SolidWP\Performance\StellarWP\Arrays\Arr; |
| 16 |
|
| 17 |
if ( ! defined( 'ABSPATH' ) ) { |
| 18 |
exit; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Responsible for loading configuration items from different locations. |
| 23 |
* |
| 24 |
* @note When the request begins at advanced-cache.php, the database is not yet available, and we rely on the file based |
| 25 |
* configuration. |
| 26 |
* |
| 27 |
* @package SolidWP\Performance |
| 28 |
*/ |
| 29 |
final class Config_Loader { |
| 30 |
|
| 31 |
public const OPTION_KEY = 'solid_performance_settings'; |
| 32 |
|
| 33 |
/** |
| 34 |
* The default config items. |
| 35 |
* |
| 36 |
* @var Default_Config |
| 37 |
*/ |
| 38 |
private Default_Config $default_config; |
| 39 |
|
| 40 |
/** |
| 41 |
* The config file dir/location. |
| 42 |
* |
| 43 |
* @var Config_File |
| 44 |
*/ |
| 45 |
private Config_File $config_file; |
| 46 |
|
| 47 |
/** |
| 48 |
* @param Default_Config $default_config The default config items. |
| 49 |
* @param Config_File $config_file The config file dir/location. |
| 50 |
*/ |
| 51 |
public function __construct( Default_Config $default_config, Config_File $config_file ) { |
| 52 |
$this->default_config = $default_config; |
| 53 |
$this->config_file = $config_file; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Fetches config items from multiple sources, merging them based on precedence. |
| 58 |
* |
| 59 |
* @return array<string, mixed> |
| 60 |
*/ |
| 61 |
public function load(): array { |
| 62 |
$merge_items = []; |
| 63 |
|
| 64 |
// This file is the only source available when advanced-cache.php is loaded. |
| 65 |
if ( file_exists( $this->config_file->get() ) ) { |
| 66 |
$merge_items[] = require $this->config_file->get(); |
| 67 |
} |
| 68 |
|
| 69 |
// Fetch the network configuration. Note: we currently have no true MS support/settings page etc...this is just for the future. |
| 70 |
if ( function_exists( 'get_network_option' ) && is_multisite() ) { |
| 71 |
$merge_items[] = (array) get_network_option( get_main_network_id(), self::OPTION_KEY, [] ); |
| 72 |
} |
| 73 |
|
| 74 |
// Allow local sites to override the network configuration. |
| 75 |
if ( function_exists( 'get_option' ) ) { |
| 76 |
$merge_items[] = (array) get_option( self::OPTION_KEY, [] ); |
| 77 |
} |
| 78 |
|
| 79 |
return $this->merge_configs( ...$merge_items ); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Merge configuration items, starting with the default configuration. |
| 84 |
* |
| 85 |
* @param array<string, mixed> ...$configs The different config sources. |
| 86 |
* |
| 87 |
* @return array |
| 88 |
*/ |
| 89 |
private function merge_configs( array ...$configs ): array { |
| 90 |
$merged = $this->default_config->get(); |
| 91 |
|
| 92 |
foreach ( $configs as $config ) { |
| 93 |
$merged = Arr::merge_recursive( $merged, $config ); |
| 94 |
} |
| 95 |
|
| 96 |
return $merged; |
| 97 |
} |
| 98 |
} |
| 99 |
|