PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.11.0
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.11.0
2.11.0 2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 All 78 releases
← All changes | vendor/wpfluent/framework/src/WPFluent/Foundation/Config.php +138 -11 1.0.99 → 2.11.0 View file →
@@ -6,48 +6,175 @@
6 6
7 7 class Config
8 8 {
9 9 /**
10 - * The config data
10 + * Eagerly-set or lazily-loaded config data, keyed by top-level config name.
11 11 * @var array
12 12 */
13 13 protected $data = [];
14 14
15 15 /**
16 - * Construct the Config instance
17 - * @param null
16 + * Map of top-level key (e.g. 'i18n', 'app') → absolute file path.
17 + * Files are required on first access to their top-level key. Defers
18 + * any side-effecting code in config files (notably `__()` calls in
19 + * config/i18n.php) until consumers actually read them — which always
20 + * happens after `init`, so WP 6.7+'s `_doing_it_wrong` notice on
21 + * early textdomain load is no longer triggered by the framework.
22 + *
23 + * Eagerly-supplied $data still works (back-compat for tests / direct
24 + * construction without a file map).
25 + *
26 + * @var array<string, string>
18 27 */
19 - public function __construct($data)
28 + protected $files = [];
29 +
30 + /**
31 + * Top-level keys whose backing file has been required. Tracks loaded
32 + * state separately from $data because a config file may legitimately
33 + * `return null` / `return []`, and we must not re-require those.
34 + *
35 + * @var array<string, bool>
36 + */
37 + protected $loaded = [];
38 +
39 + /**
40 + * Construct the Config instance.
41 + *
42 + * @param array $data Eagerly-populated data (keyed by top-level name).
43 + * @param array $files Map of top-level name → file path for lazy load.
44 + */
45 + public function __construct($data, $files = [])
20 46 {
21 47 $this->data = $data;
48 + $this->files = $files;
49 +
50 + // Anything passed in $data is already resolved.
51 + foreach (array_keys($data) as $top) {
52 + $this->loaded[$top] = true;
53 + }
22 54 }
23 55
24 56 /**
25 - * Retrieve all config data
57 + * Retrieve all config data. Force-loads every lazy file.
58 + *
26 59 * @return array
27 60 */
28 61 public function all()
29 62 {
30 - return $this->get();
63 + foreach (array_keys($this->files) as $top) {
64 + $this->ensureLoaded($top);
65 + }
66 +
67 + return $this->data;
31 68 }
32 69
33 70 /**
34 - * Retrieve specific item from config array
71 + * Retrieve specific item from config array.
72 + *
35 73 * @param string $key
36 - * @param string $default
74 + * @param mixed $default
37 75 * @return mixed
38 76 */
39 77 public function get($key = null, $default = null)
40 78 {
41 - return $key ? Arr::get($this->data, $key, $default) : $this->data;
79 + if ($key === null || $key === '') {
80 + return $this->all();
81 + }
82 +
83 + $key = $this->resolveKey($key);
84 +
85 + $this->ensureLoaded($this->topKey($key));
86 +
87 + return Arr::get($this->data, $key, $default);
42 88 }
43 89
44 90 /**
45 - * Set an item into the config array on the fly.
91 + * Get only specific items from the config array.
92 + *
93 + * @param string|array $key ($key1, $key2 or [$key1, $key2])
94 + * @return array
95 + */
96 + public function only($key)
97 + {
98 + $keys = array_map(function ($key) {
99 + return $this->resolveKey($key);
100 + }, is_array($key) ? $key : func_get_args());
101 +
102 + $result = [];
103 +
104 + foreach ($keys as $key) {
105 + $this->ensureLoaded($this->topKey($key));
106 + $result[] = Arr::get($this->data, $key);
107 + }
108 +
109 + return array_values(array_filter($result));
110 + }
111 +
112 + /**
113 + * Set an item into the config array on the fly. Forces the underlying
114 + * file to load first, so the write isn't clobbered by a later lazy load.
115 + *
46 116 * @param string $key
47 - * @param mkixed $value
117 + * @param mixed $value
48 118 */
49 119 public function set($key, $value)
50 120 {
121 + $key = $this->resolveKey($key);
122 + $this->ensureLoaded($this->topKey($key));
51 123 Arr::set($this->data, $key, $value);
124 + }
125 +
126 + /**
127 + * Lazy-load a config file the first time its top-level key is touched.
128 + * Idempotent: subsequent calls are no-ops.
129 + *
130 + * @param string $topKey
131 + * @return void
132 + */
133 + protected function ensureLoaded($topKey)
134 + {
135 + if (!$topKey || isset($this->loaded[$topKey])) {
136 + return;
137 + }
138 +
139 + if (isset($this->files[$topKey])) {
140 + $this->data[$topKey] = require $this->files[$topKey];
141 + }
142 +
143 + $this->loaded[$topKey] = true;
144 + }
145 +
146 + /**
147 + * Extract the top-level key from a dotted path. 'app.text_domain' → 'app'.
148 + *
149 + * @param string $key
150 + * @return string
151 + */
152 + protected function topKey($key)
153 + {
154 + $dot = strpos($key, '.');
155 + return $dot === false ? $key : substr($key, 0, $dot);
156 + }
157 +
158 + /**
159 + * Resolve the config key, add `app.` prefix when the key is short and
160 + * not a known top-level config name (matches eagerly-loaded data OR a
161 + * deferred file). Without the file lookup, `get('i18n')` would resolve
162 + * to `app.i18n` before the i18n config file had been touched.
163 + *
164 + * @param string $key
165 + * @return string
166 + */
167 + protected function resolveKey($key)
168 + {
169 + if (!$key) return $key;
170 +
171 + if (
172 + array_key_exists($key, $this->data)
173 + || array_key_exists($key, $this->files)
174 + ) {
175 + return $key;
176 + }
177 +
178 + return str_contains($key, '.') ? $key : "app.{$key}";
52 179 }
53 180 }