PluginProbe
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution / 2.0.0
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution v2.0.0
2.0.1 trunk 1.0.0 1.1.0 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.5.0 1.6.0 1.6.1 1.7.0 1.7.1 1.8.0 1.9.0 2.0.0
solid-performance / src / Performance / Config / Config_Loader.php

Config_Loader.php in Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution 2.0.0, at src/Performance/Config/Config_Loader.php

128 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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
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 * @var Cache_Path
49 */
50 private Cache_Path $cache_path;
51
52 /**
53 * @var Config_Resolver
54 */
55 private Config_Resolver $resolver;
56
57 /**
58 * @var Config_Filter
59 */
60 private Config_Filter $filter;
61
62 /**
63 * @param Default_Config $default_config The default config items.
64 * @param Config_File $config_file The config file dir/location.
65 * @param Cache_Path $cache_path The cache path object.
66 * @param Config_Resolver $resolver The configuration resolver.
67 * @param Config_Filter $filter The config filter.
68 */
69 public function __construct(
70 Default_Config $default_config,
71 Config_File $config_file,
72 Cache_Path $cache_path,
73 Config_Resolver $resolver,
74 Config_Filter $filter
75 ) {
76 $this->default_config = $default_config;
77 $this->config_file = $config_file;
78 $this->cache_path = $cache_path;
79 $this->resolver = $resolver;
80 $this->filter = $filter;
81 }
82
83 /**
84 * Fetches config items from multiple sources, merging them based on precedence and then
85 * filters them to ensure they only match the keys from the default config.
86 *
87 * @return array<string, mixed>
88 */
89 public function load(): array {
90 $merge_items = [];
91
92 // This file is the only source available when advanced-cache.php is loaded.
93 if ( file_exists( $this->config_file->get() ) ) {
94 $merge_items[] = require $this->config_file->get();
95 }
96
97 // Fetch the network configuration. Note: we currently have no true MS support/settings page etc...this is just for the future.
98 if ( function_exists( 'get_network_option' ) && is_multisite() ) {
99 $merge_items[] = (array) get_network_option( get_main_network_id(), self::OPTION_KEY, [] );
100 }
101
102 // Allow local sites to override the network configuration.
103 if ( function_exists( 'get_option' ) ) {
104 $merge_items[] = (array) get_option( self::OPTION_KEY, [] );
105 }
106
107 // Filter out all keys that don't exist in the default config from the merged items.
108 return $this->filter->filter( $this->merge_configs( ...$merge_items ) );
109 }
110
111 /**
112 * Merge configuration items, starting with the default configuration.
113 *
114 * @param array<string, mixed> ...$configs The different config sources.
115 *
116 * @return array
117 */
118 private function merge_configs( array ...$configs ): array {
119 $merged = $this->resolver->merge_configs( $this->default_config->get(), ...$configs );
120
121 // Force a dynamic cache_dir every time so it's always current.
122 // The saved cache_dir path could now be invalid and cause all kinds of issues.
123 $merged['page_cache']['cache_dir'] = $this->cache_path->get_cache_dir();
124
125 return $merged;
126 }
127 }
128