| 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\Page_Cache\Cache_Path; |
| 16 |
use SolidWP\Performance\StellarWP\Arrays\Arr; |
| 17 |
|
| 18 |
if ( ! defined( 'ABSPATH' ) ) { |
| 19 |
exit; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Responsible for loading configuration items from different locations. |
| 24 |
* |
| 25 |
* @note When the request begins at advanced-cache.php, the database is not yet available, and we rely on the file based |
| 26 |
* configuration. |
| 27 |
* |
| 28 |
* @package SolidWP\Performance |
| 29 |
*/ |
| 30 |
final class Config_Loader { |
| 31 |
|
| 32 |
public const OPTION_KEY = 'solid_performance_settings'; |
| 33 |
|
| 34 |
/** |
| 35 |
* The default config items. |
| 36 |
* |
| 37 |
* @var Default_Config |
| 38 |
*/ |
| 39 |
private Default_Config $default_config; |
| 40 |
|
| 41 |
/** |
| 42 |
* The config file dir/location. |
| 43 |
* |
| 44 |
* @var Config_File |
| 45 |
*/ |
| 46 |
private Config_File $config_file; |
| 47 |
|
| 48 |
/** |
| 49 |
* @var Cache_Path |
| 50 |
*/ |
| 51 |
private Cache_Path $cache_path; |
| 52 |
|
| 53 |
/** |
| 54 |
* @param Default_Config $default_config The default config items. |
| 55 |
* @param Config_File $config_file The config file dir/location. |
| 56 |
* @param Cache_Path $cache_path The cache path object. |
| 57 |
*/ |
| 58 |
public function __construct( Default_Config $default_config, Config_File $config_file, Cache_Path $cache_path ) { |
| 59 |
$this->default_config = $default_config; |
| 60 |
$this->config_file = $config_file; |
| 61 |
$this->cache_path = $cache_path; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Fetches config items from multiple sources, merging them based on precedence. |
| 66 |
* |
| 67 |
* @return array<string, mixed> |
| 68 |
*/ |
| 69 |
public function load(): array { |
| 70 |
$merge_items = []; |
| 71 |
|
| 72 |
// This file is the only source available when advanced-cache.php is loaded. |
| 73 |
if ( file_exists( $this->config_file->get() ) ) { |
| 74 |
$merge_items[] = require $this->config_file->get(); |
| 75 |
} |
| 76 |
|
| 77 |
// Fetch the network configuration. Note: we currently have no true MS support/settings page etc...this is just for the future. |
| 78 |
if ( function_exists( 'get_network_option' ) && is_multisite() ) { |
| 79 |
$merge_items[] = (array) get_network_option( get_main_network_id(), self::OPTION_KEY, [] ); |
| 80 |
} |
| 81 |
|
| 82 |
// Allow local sites to override the network configuration. |
| 83 |
if ( function_exists( 'get_option' ) ) { |
| 84 |
$merge_items[] = (array) get_option( self::OPTION_KEY, [] ); |
| 85 |
} |
| 86 |
|
| 87 |
return $this->merge_configs( ...$merge_items ); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Merge configuration items, starting with the default configuration. |
| 92 |
* |
| 93 |
* @param array<string, mixed> ...$configs The different config sources. |
| 94 |
* |
| 95 |
* @return array |
| 96 |
*/ |
| 97 |
private function merge_configs( array ...$configs ): array { |
| 98 |
$merged = $this->default_config->get(); |
| 99 |
|
| 100 |
foreach ( $configs as $config ) { |
| 101 |
$merged = Arr::merge_recursive( $merged, $config ); |
| 102 |
} |
| 103 |
|
| 104 |
// Force a dynamic cache_dir every time so it's always current. |
| 105 |
// The saved cache_dir path could now be invalid and cause all kinds of issues. |
| 106 |
$merged['page_cache']['cache_dir'] = $this->cache_path->get_cache_dir(); |
| 107 |
|
| 108 |
return $merged; |
| 109 |
} |
| 110 |
} |
| 111 |
|