Commit | Line | Data |
---|---|---|
c0a1f187 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
c0a1f187 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
c0a1f187 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * Class CRM_Core_Config_MagicMerge | |
30 | * | |
31 | * Originally, the $config object was based on a single, serialized | |
32 | * data object stored in the database. As the needs for settings | |
9e1e6020 TO |
33 | * grew (with robust metadata, system overrides, extension support, |
34 | * and multi-tenancy), the $config started to store a mix of: | |
c0a1f187 TO |
35 | * (a) canonical config options, |
36 | * (b) dynamically generated runtime data, | |
37 | * (c) cached data derived from other sources (esp civicrm_setting) | |
38 | * (d) instances of service objects | |
39 | * | |
40 | * The config object is now deprecated. Settings and service objects | |
41 | * should generally be accessed via Civi::settings() and Civi::service(). | |
42 | * | |
43 | * MagicMerge provides backward compatibility. You may still access | |
44 | * old properties via $config, but they will be loaded from their | |
45 | * new services. | |
46 | */ | |
47 | class CRM_Core_Config_MagicMerge { | |
48 | ||
49 | /** | |
50 | * Map old config properties to their contemporary counterparts. | |
51 | * | |
52 | * @var array | |
53 | * Array(string $configAlias => Array(string $realType, string $realName)). | |
54 | */ | |
55 | private $map; | |
56 | ||
518fa0ee SL |
57 | private $locals; |
58 | private $settings; | |
c0a1f187 | 59 | |
be2fb01f | 60 | private $cache = []; |
7dba62da | 61 | |
8246bca4 | 62 | /** |
63 | * CRM_Core_Config_MagicMerge constructor. | |
64 | */ | |
c0a1f187 TO |
65 | public function __construct() { |
66 | $this->map = self::getPropertyMap(); | |
67 | } | |
68 | ||
8246bca4 | 69 | /** |
70 | * Set the map to the property map. | |
71 | */ | |
c0a1f187 TO |
72 | public function __wakeup() { |
73 | $this->map = self::getPropertyMap(); | |
74 | } | |
75 | ||
76 | /** | |
f806379b TO |
77 | * Get a list of $config properties and the entities to which they map. |
78 | * | |
79 | * This is used for two purposes: | |
80 | * | |
81 | * 1. Runtime: Provide backward-compatible interface for reading these | |
82 | * properties. | |
83 | * 2. Upgrade: Migrate old properties of config_backend into settings. | |
84 | * | |
c0a1f187 TO |
85 | * @return array |
86 | */ | |
87 | public static function getPropertyMap() { | |
e3d28c74 TO |
88 | // Each mapping: $propertyName => Array(0 => $type, 1 => $foreignName|NULL, ...). |
89 | // If $foreignName is omitted/null, then it's assumed to match the $propertyName. | |
90 | // Other parameters may be specified, depending on the type. | |
be2fb01f | 91 | return [ |
9e1e6020 | 92 | // "local" properties are unique to each instance of CRM_Core_Config (each request). |
be2fb01f CW |
93 | 'doNotResetCache' => ['local'], |
94 | 'inCiviCRM' => ['local'], | |
95 | 'keyDisable' => ['local'], | |
96 | 'userFrameworkFrontend' => ['local'], | |
97 | 'userPermissionTemp' => ['local'], | |
9e1e6020 TO |
98 | |
99 | // "runtime" properties are computed from define()s, $_ENV, etc. | |
100 | // See also: CRM_Core_Config_Runtime. | |
be2fb01f CW |
101 | 'dsn' => ['runtime'], |
102 | 'initialized' => ['runtime'], | |
103 | 'userFramework' => ['runtime'], | |
104 | 'userFrameworkClass' => ['runtime'], | |
105 | 'userFrameworkDSN' => ['runtime'], | |
106 | 'userFrameworkURLVar' => ['runtime'], | |
107 | 'userHookClass' => ['runtime'], | |
108 | 'cleanURL' => ['runtime'], | |
be2fb01f | 109 | 'templateDir' => ['runtime'], |
9e1e6020 | 110 | |
d4330c62 | 111 | // "boot-svc" properties are critical services needed during init. |
7f835399 | 112 | // See also: Civi\Core\Container::getBootService(). |
be2fb01f CW |
113 | 'userSystem' => ['boot-svc'], |
114 | 'userPermissionClass' => ['boot-svc'], | |
d4330c62 | 115 | |
be2fb01f CW |
116 | 'userFrameworkBaseURL' => ['user-system', 'getAbsoluteBaseURL'], |
117 | 'userFrameworkVersion' => ['user-system', 'getVersion'], | |
518fa0ee SL |
118 | // ugh typo. |
119 | 'useFrameworkRelativeBase' => ['user-system', 'getRelativeBaseURL'], | |
d4330c62 | 120 | |
9e1e6020 TO |
121 | // "setting" properties are loaded through the setting layer, esp |
122 | // table "civicrm_setting" and global $civicrm_setting. | |
123 | // See also: Civi::settings(). | |
be2fb01f CW |
124 | 'backtrace' => ['setting'], |
125 | 'contact_default_language' => ['setting'], | |
126 | 'countryLimit' => ['setting'], | |
127 | 'customTranslateFunction' => ['setting'], | |
128 | 'dateInputFormat' => ['setting'], | |
129 | 'dateformatDatetime' => ['setting'], | |
130 | 'dateformatFull' => ['setting'], | |
131 | 'dateformatPartial' => ['setting'], | |
132 | 'dateformatTime' => ['setting'], | |
133 | 'dateformatYear' => ['setting'], | |
134 | 'dateformatFinancialBatch' => ['setting'], | |
135 | 'dateformatshortdate' => ['setting'], | |
518fa0ee SL |
136 | // renamed. |
137 | 'debug' => ['setting', 'debug_enabled'], | |
be2fb01f CW |
138 | 'defaultContactCountry' => ['setting'], |
139 | 'defaultContactStateProvince' => ['setting'], | |
140 | 'defaultCurrency' => ['setting'], | |
141 | 'defaultSearchProfileID' => ['setting'], | |
142 | 'doNotAttachPDFReceipt' => ['setting'], | |
143 | 'empoweredBy' => ['setting'], | |
518fa0ee SL |
144 | // renamed. |
145 | 'enableComponents' => ['setting', 'enable_components'], | |
be2fb01f CW |
146 | 'enableSSL' => ['setting'], |
147 | 'fatalErrorHandler' => ['setting'], | |
148 | 'fieldSeparator' => ['setting'], | |
149 | 'fiscalYearStart' => ['setting'], | |
150 | 'geoAPIKey' => ['setting'], | |
151 | 'geoProvider' => ['setting'], | |
152 | 'includeAlphabeticalPager' => ['setting'], | |
153 | 'includeEmailInName' => ['setting'], | |
154 | 'includeNickNameInName' => ['setting'], | |
155 | 'includeOrderByClause' => ['setting'], | |
156 | 'includeWildCardInName' => ['setting'], | |
157 | 'inheritLocale' => ['setting'], | |
158 | 'languageLimit' => ['setting'], | |
159 | 'lcMessages' => ['setting'], | |
160 | 'legacyEncoding' => ['setting'], | |
161 | 'logging' => ['setting'], | |
162 | 'mailThrottleTime' => ['setting'], | |
163 | 'mailerBatchLimit' => ['setting'], | |
164 | 'mailerJobSize' => ['setting'], | |
165 | 'mailerJobsMax' => ['setting'], | |
166 | 'mapAPIKey' => ['setting'], | |
167 | 'mapProvider' => ['setting'], | |
168 | 'maxFileSize' => ['setting'], | |
518fa0ee SL |
169 | // renamed. |
170 | 'maxAttachments' => ['setting', 'max_attachments'], | |
8913e915 | 171 | 'maxAttachmentsBackend' => ['setting', 'max_attachments_backend'], |
be2fb01f CW |
172 | 'monetaryDecimalPoint' => ['setting'], |
173 | 'monetaryThousandSeparator' => ['setting'], | |
174 | 'moneyformat' => ['setting'], | |
175 | 'moneyvalueformat' => ['setting'], | |
176 | 'provinceLimit' => ['setting'], | |
177 | 'recaptchaOptions' => ['setting'], | |
178 | 'recaptchaPublicKey' => ['setting'], | |
179 | 'recaptchaPrivateKey' => ['setting'], | |
180 | 'forceRecaptcha' => ['setting'], | |
181 | 'replyTo' => ['setting'], | |
182 | 'secondDegRelPermissions' => ['setting'], | |
183 | 'smartGroupCacheTimeout' => ['setting'], | |
184 | 'timeInputFormat' => ['setting'], | |
185 | 'userFrameworkLogging' => ['setting'], | |
186 | 'userFrameworkUsersTableName' => ['setting'], | |
187 | 'verpSeparator' => ['setting'], | |
188 | 'wkhtmltopdfPath' => ['setting'], | |
189 | 'wpBasePage' => ['setting'], | |
190 | 'wpLoadPhp' => ['setting'], | |
e3d28c74 | 191 | |
0bd3f65c TO |
192 | // "path" properties are managed via Civi::paths and $civicrm_paths |
193 | // Option: `mkdir` - auto-create dir | |
194 | // Option: `restrict` - auto-restrict remote access | |
195 | 'configAndLogDir' => ['path', 'civicrm.log', ['mkdir', 'restrict']], | |
196 | 'templateCompileDir' => ['path', 'civicrm.compile', ['mkdir', 'restrict']], | |
197 | ||
9e1e6020 TO |
198 | // "setting-path" properties are settings with special filtering |
199 | // to return normalized file paths. | |
cdbea577 C |
200 | // Option: `mkdir` - auto-create dir |
201 | // Option: `restrict` - auto-restrict remote access | |
be2fb01f CW |
202 | 'customFileUploadDir' => ['setting-path', NULL, ['mkdir', 'restrict']], |
203 | 'customPHPPathDir' => ['setting-path'], | |
204 | 'customTemplateDir' => ['setting-path'], | |
205 | 'extensionsDir' => ['setting-path', NULL, ['mkdir']], | |
206 | 'imageUploadDir' => ['setting-path', NULL, ['mkdir']], | |
207 | 'uploadDir' => ['setting-path', NULL, ['mkdir', 'restrict']], | |
e3d28c74 | 208 | |
cdbea577 C |
209 | // "setting-url" properties are settings with special filtering |
210 | // to return normalized URLs. | |
211 | // Option: `noslash` - don't append trailing slash | |
212 | // Option: `rel` - convert to relative URL (if possible) | |
be2fb01f CW |
213 | 'customCSSURL' => ['setting-url', NULL, ['noslash']], |
214 | 'extensionsURL' => ['setting-url'], | |
215 | 'imageUploadURL' => ['setting-url'], | |
216 | 'resourceBase' => ['setting-url', 'userFrameworkResourceURL', ['rel']], | |
217 | 'userFrameworkResourceURL' => ['setting-url'], | |
c0a1f187 | 218 | |
9e1e6020 | 219 | // "callback" properties are generated on-demand by calling a function. |
0d1823d7 | 220 | // @todo remove geocodeMethod. As of Feb 2018, $config->geocodeMethod works but gives a deprecation warning. |
be2fb01f CW |
221 | 'geocodeMethod' => ['callback', 'CRM_Utils_Geocode', 'getProviderClass'], |
222 | 'defaultCurrencySymbol' => ['callback', 'CRM_Core_BAO_Country', 'getDefaultCurrencySymbol'], | |
223 | ]; | |
c0a1f187 TO |
224 | } |
225 | ||
8246bca4 | 226 | /** |
227 | * Get value. | |
228 | * | |
229 | * @param string $k | |
230 | * | |
231 | * @return mixed | |
232 | * @throws \CRM_Core_Exception | |
233 | */ | |
c0a1f187 TO |
234 | public function __get($k) { |
235 | if (!isset($this->map[$k])) { | |
236 | throw new \CRM_Core_Exception("Cannot read unrecognized property CRM_Core_Config::\${$k}."); | |
237 | } | |
fd78a9e5 | 238 | if (isset($this->cache[$k])) { |
7dba62da TO |
239 | return $this->cache[$k]; |
240 | } | |
e3d28c74 TO |
241 | |
242 | $type = $this->map[$k][0]; | |
243 | $name = isset($this->map[$k][1]) ? $this->map[$k][1] : $k; | |
c0a1f187 TO |
244 | |
245 | switch ($type) { | |
246 | case 'setting': | |
23bb9c85 | 247 | return $this->getSettings()->get($name); |
c0a1f187 | 248 | |
00f6fb2a TO |
249 | // The interpretation of 'path' and 'setting-path' is similar, except |
250 | // that the latter originates in a stored setting. | |
251 | case 'path': | |
c0a1f187 | 252 | case 'setting-path': |
ac47f7ca | 253 | // Array(0 => $type, 1 => $setting, 2 => $actions). |
00f6fb2a TO |
254 | $value = ($type === 'path') |
255 | ? Civi::paths()->getVariable($name, 'path') | |
256 | : Civi::paths()->getPath($this->getSettings()->get($name)); | |
e3d28c74 TO |
257 | if ($value) { |
258 | $value = CRM_Utils_File::addTrailingSlash($value); | |
ac47f7ca | 259 | if (isset($this->map[$k][2]) && in_array('mkdir', $this->map[$k][2])) { |
f7cc6e55 | 260 | if (!is_dir($value) && !CRM_Utils_File::createDir($value, FALSE)) { |
be2fb01f | 261 | CRM_Core_Session::setStatus(ts('Failed to make directory (%1) at "%2". Please update the settings or file permissions.', [ |
895c19e7 TO |
262 | 1 => $k, |
263 | 2 => $value, | |
be2fb01f | 264 | ])); |
895c19e7 | 265 | } |
e3d28c74 | 266 | } |
ac47f7ca | 267 | if (isset($this->map[$k][2]) && in_array('restrict', $this->map[$k][2])) { |
e3d28c74 TO |
268 | CRM_Utils_File::restrictAccess($value); |
269 | } | |
270 | } | |
7dba62da | 271 | $this->cache[$k] = $value; |
e3d28c74 TO |
272 | return $value; |
273 | ||
cdbea577 | 274 | case 'setting-url': |
be2fb01f | 275 | $options = !empty($this->map[$k][2]) ? $this->map[$k][2] : []; |
6ec7e639 | 276 | $value = $this->getSettings()->get($name); |
cdbea577 C |
277 | if ($value && !(in_array('noslash', $options))) { |
278 | $value = CRM_Utils_File::addTrailingSlash($value, '/'); | |
6ec7e639 | 279 | } |
cdbea577 C |
280 | $this->cache[$k] = Civi::paths()->getUrl($value, |
281 | in_array('rel', $options) ? 'relative' : 'absolute'); | |
7dba62da | 282 | return $this->cache[$k]; |
c0a1f187 TO |
283 | |
284 | case 'runtime': | |
7f835399 | 285 | return \Civi\Core\Container::getBootService('runtime')->{$name}; |
c0a1f187 | 286 | |
d4330c62 TO |
287 | case 'boot-svc': |
288 | $this->cache[$k] = \Civi\Core\Container::getBootService($name); | |
289 | return $this->cache[$k]; | |
290 | ||
c0a1f187 TO |
291 | case 'local': |
292 | $this->initLocals(); | |
293 | return $this->locals[$name]; | |
294 | ||
d4330c62 TO |
295 | case 'user-system': |
296 | $userSystem = \Civi\Core\Container::getBootService('userSystem'); | |
be2fb01f | 297 | $this->cache[$k] = call_user_func([$userSystem, $name]); |
d4330c62 TO |
298 | return $this->cache[$k]; |
299 | ||
c0a1f187 TO |
300 | case 'service': |
301 | return \Civi::service($name); | |
302 | ||
303 | case 'callback': | |
304 | // Array(0 => $type, 1 => $obj, 2 => $getter, 3 => $setter, 4 => $unsetter). | |
305 | if (!isset($this->map[$k][1], $this->map[$k][2])) { | |
306 | throw new \CRM_Core_Exception("Cannot find getter for property CRM_Core_Config::\${$k}"); | |
307 | } | |
be2fb01f | 308 | return \Civi\Core\Resolver::singleton()->call([$this->map[$k][1], $this->map[$k][2]], [$k]); |
c0a1f187 TO |
309 | |
310 | default: | |
311 | throw new \CRM_Core_Exception("Cannot read property CRM_Core_Config::\${$k} ($type)"); | |
312 | } | |
313 | } | |
314 | ||
8246bca4 | 315 | /** |
316 | * Set value. | |
317 | * | |
318 | * @param string $k | |
319 | * @param mixed $v | |
320 | * | |
321 | * @throws \CRM_Core_Exception | |
322 | */ | |
c0a1f187 TO |
323 | public function __set($k, $v) { |
324 | if (!isset($this->map[$k])) { | |
325 | throw new \CRM_Core_Exception("Cannot set unrecognized property CRM_Core_Config::\${$k}"); | |
326 | } | |
7dba62da | 327 | unset($this->cache[$k]); |
dc640254 | 328 | $type = $this->map[$k][0]; |
82eff07b | 329 | |
c0a1f187 TO |
330 | switch ($type) { |
331 | case 'setting': | |
dc640254 | 332 | case 'setting-path': |
cdbea577 | 333 | case 'setting-url': |
00f6fb2a | 334 | case 'path': |
d4330c62 | 335 | case 'user-system': |
c0a1f187 | 336 | case 'runtime': |
dc640254 | 337 | case 'callback': |
6504e96d | 338 | case 'boot-svc': |
dc640254 | 339 | // In the past, changes to $config were not persisted automatically. |
cdf3884e | 340 | $this->cache[$k] = $v; |
c0a1f187 TO |
341 | return; |
342 | ||
343 | case 'local': | |
344 | $this->initLocals(); | |
cdf3884e | 345 | $this->locals[$k] = $v; |
c0a1f187 TO |
346 | return; |
347 | ||
c0a1f187 TO |
348 | default: |
349 | throw new \CRM_Core_Exception("Cannot set property CRM_Core_Config::\${$k} ($type)"); | |
350 | } | |
351 | } | |
352 | ||
8246bca4 | 353 | /** |
354 | * Is value set. | |
355 | * | |
356 | * @param string $k | |
357 | * | |
358 | * @return bool | |
359 | */ | |
c0a1f187 TO |
360 | public function __isset($k) { |
361 | return isset($this->map[$k]); | |
362 | } | |
363 | ||
8246bca4 | 364 | /** |
365 | * Unset value. | |
366 | * | |
367 | * @param string $k | |
368 | * | |
369 | * @throws \CRM_Core_Exception | |
370 | */ | |
c0a1f187 TO |
371 | public function __unset($k) { |
372 | if (!isset($this->map[$k])) { | |
373 | throw new \CRM_Core_Exception("Cannot unset unrecognized property CRM_Core_Config::\${$k}"); | |
374 | } | |
7dba62da | 375 | unset($this->cache[$k]); |
dc640254 TO |
376 | $type = $this->map[$k][0]; |
377 | $name = isset($this->map[$k][1]) ? $this->map[$k][1] : $k; | |
c0a1f187 TO |
378 | |
379 | switch ($type) { | |
380 | case 'setting': | |
381 | case 'setting-path': | |
cdbea577 | 382 | case 'setting-url': |
23bb9c85 | 383 | $this->getSettings()->revert($k); |
c0a1f187 TO |
384 | return; |
385 | ||
386 | case 'local': | |
387 | $this->initLocals(); | |
388 | $this->locals[$name] = NULL; | |
389 | return; | |
390 | ||
391 | case 'callback': | |
392 | // Array(0 => $type, 1 => $obj, 2 => $getter, 3 => $setter, 4 => $unsetter). | |
393 | if (!isset($this->map[$k][1], $this->map[$k][4])) { | |
394 | throw new \CRM_Core_Exception("Cannot find unsetter for property CRM_Core_Config::\${$k}"); | |
395 | } | |
be2fb01f | 396 | \Civi\Core\Resolver::singleton()->call([$this->map[$k][1], $this->map[$k][4]], [$k]); |
c0a1f187 TO |
397 | return; |
398 | ||
399 | default: | |
400 | throw new \CRM_Core_Exception("Cannot unset property CRM_Core_Config::\${$k} ($type)"); | |
401 | } | |
402 | } | |
403 | ||
23bb9c85 TO |
404 | /** |
405 | * @return \Civi\Core\SettingsBag | |
406 | */ | |
407 | protected function getSettings() { | |
408 | if ($this->settings === NULL) { | |
409 | $this->settings = Civi::settings(); | |
410 | } | |
411 | return $this->settings; | |
412 | } | |
413 | ||
8246bca4 | 414 | /** |
415 | * Initialise local settings. | |
416 | */ | |
c0a1f187 TO |
417 | private function initLocals() { |
418 | if ($this->locals === NULL) { | |
be2fb01f | 419 | $this->locals = [ |
c0a1f187 TO |
420 | 'inCiviCRM' => FALSE, |
421 | 'doNotResetCache' => 0, | |
5f900b59 | 422 | 'keyDisable' => FALSE, |
c0a1f187 TO |
423 | 'initialized' => FALSE, |
424 | 'userFrameworkFrontend' => FALSE, | |
d4330c62 | 425 | 'userPermissionTemp' => NULL, |
be2fb01f | 426 | ]; |
c0a1f187 TO |
427 | } |
428 | } | |
429 | ||
430 | } |