Merge pull request #15412 from demeritcowboy/fly-backwards
[civicrm-core.git] / CRM / Core / Config / MagicMerge.php
CommitLineData
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 */
47class 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'],
be2fb01f
CW
171 'monetaryDecimalPoint' => ['setting'],
172 'monetaryThousandSeparator' => ['setting'],
173 'moneyformat' => ['setting'],
174 'moneyvalueformat' => ['setting'],
175 'provinceLimit' => ['setting'],
176 'recaptchaOptions' => ['setting'],
177 'recaptchaPublicKey' => ['setting'],
178 'recaptchaPrivateKey' => ['setting'],
179 'forceRecaptcha' => ['setting'],
180 'replyTo' => ['setting'],
181 'secondDegRelPermissions' => ['setting'],
182 'smartGroupCacheTimeout' => ['setting'],
183 'timeInputFormat' => ['setting'],
184 'userFrameworkLogging' => ['setting'],
185 'userFrameworkUsersTableName' => ['setting'],
186 'verpSeparator' => ['setting'],
187 'wkhtmltopdfPath' => ['setting'],
188 'wpBasePage' => ['setting'],
189 'wpLoadPhp' => ['setting'],
e3d28c74 190
0bd3f65c
TO
191 // "path" properties are managed via Civi::paths and $civicrm_paths
192 // Option: `mkdir` - auto-create dir
193 // Option: `restrict` - auto-restrict remote access
194 'configAndLogDir' => ['path', 'civicrm.log', ['mkdir', 'restrict']],
195 'templateCompileDir' => ['path', 'civicrm.compile', ['mkdir', 'restrict']],
196
9e1e6020
TO
197 // "setting-path" properties are settings with special filtering
198 // to return normalized file paths.
cdbea577
C
199 // Option: `mkdir` - auto-create dir
200 // Option: `restrict` - auto-restrict remote access
be2fb01f
CW
201 'customFileUploadDir' => ['setting-path', NULL, ['mkdir', 'restrict']],
202 'customPHPPathDir' => ['setting-path'],
203 'customTemplateDir' => ['setting-path'],
204 'extensionsDir' => ['setting-path', NULL, ['mkdir']],
205 'imageUploadDir' => ['setting-path', NULL, ['mkdir']],
206 'uploadDir' => ['setting-path', NULL, ['mkdir', 'restrict']],
e3d28c74 207
cdbea577
C
208 // "setting-url" properties are settings with special filtering
209 // to return normalized URLs.
210 // Option: `noslash` - don't append trailing slash
211 // Option: `rel` - convert to relative URL (if possible)
be2fb01f
CW
212 'customCSSURL' => ['setting-url', NULL, ['noslash']],
213 'extensionsURL' => ['setting-url'],
214 'imageUploadURL' => ['setting-url'],
215 'resourceBase' => ['setting-url', 'userFrameworkResourceURL', ['rel']],
216 'userFrameworkResourceURL' => ['setting-url'],
c0a1f187 217
9e1e6020 218 // "callback" properties are generated on-demand by calling a function.
0d1823d7 219 // @todo remove geocodeMethod. As of Feb 2018, $config->geocodeMethod works but gives a deprecation warning.
be2fb01f
CW
220 'geocodeMethod' => ['callback', 'CRM_Utils_Geocode', 'getProviderClass'],
221 'defaultCurrencySymbol' => ['callback', 'CRM_Core_BAO_Country', 'getDefaultCurrencySymbol'],
222 ];
c0a1f187
TO
223 }
224
8246bca4 225 /**
226 * Get value.
227 *
228 * @param string $k
229 *
230 * @return mixed
231 * @throws \CRM_Core_Exception
232 */
c0a1f187
TO
233 public function __get($k) {
234 if (!isset($this->map[$k])) {
235 throw new \CRM_Core_Exception("Cannot read unrecognized property CRM_Core_Config::\${$k}.");
236 }
fd78a9e5 237 if (isset($this->cache[$k])) {
7dba62da
TO
238 return $this->cache[$k];
239 }
e3d28c74
TO
240
241 $type = $this->map[$k][0];
242 $name = isset($this->map[$k][1]) ? $this->map[$k][1] : $k;
c0a1f187
TO
243
244 switch ($type) {
245 case 'setting':
23bb9c85 246 return $this->getSettings()->get($name);
c0a1f187 247
00f6fb2a
TO
248 // The interpretation of 'path' and 'setting-path' is similar, except
249 // that the latter originates in a stored setting.
250 case 'path':
c0a1f187 251 case 'setting-path':
ac47f7ca 252 // Array(0 => $type, 1 => $setting, 2 => $actions).
00f6fb2a
TO
253 $value = ($type === 'path')
254 ? Civi::paths()->getVariable($name, 'path')
255 : Civi::paths()->getPath($this->getSettings()->get($name));
e3d28c74
TO
256 if ($value) {
257 $value = CRM_Utils_File::addTrailingSlash($value);
ac47f7ca 258 if (isset($this->map[$k][2]) && in_array('mkdir', $this->map[$k][2])) {
f7cc6e55 259 if (!is_dir($value) && !CRM_Utils_File::createDir($value, FALSE)) {
be2fb01f 260 CRM_Core_Session::setStatus(ts('Failed to make directory (%1) at "%2". Please update the settings or file permissions.', [
895c19e7
TO
261 1 => $k,
262 2 => $value,
be2fb01f 263 ]));
895c19e7 264 }
e3d28c74 265 }
ac47f7ca 266 if (isset($this->map[$k][2]) && in_array('restrict', $this->map[$k][2])) {
e3d28c74
TO
267 CRM_Utils_File::restrictAccess($value);
268 }
269 }
7dba62da 270 $this->cache[$k] = $value;
e3d28c74
TO
271 return $value;
272
cdbea577 273 case 'setting-url':
be2fb01f 274 $options = !empty($this->map[$k][2]) ? $this->map[$k][2] : [];
6ec7e639 275 $value = $this->getSettings()->get($name);
cdbea577
C
276 if ($value && !(in_array('noslash', $options))) {
277 $value = CRM_Utils_File::addTrailingSlash($value, '/');
6ec7e639 278 }
cdbea577
C
279 $this->cache[$k] = Civi::paths()->getUrl($value,
280 in_array('rel', $options) ? 'relative' : 'absolute');
7dba62da 281 return $this->cache[$k];
c0a1f187
TO
282
283 case 'runtime':
7f835399 284 return \Civi\Core\Container::getBootService('runtime')->{$name};
c0a1f187 285
d4330c62
TO
286 case 'boot-svc':
287 $this->cache[$k] = \Civi\Core\Container::getBootService($name);
288 return $this->cache[$k];
289
c0a1f187
TO
290 case 'local':
291 $this->initLocals();
292 return $this->locals[$name];
293
d4330c62
TO
294 case 'user-system':
295 $userSystem = \Civi\Core\Container::getBootService('userSystem');
be2fb01f 296 $this->cache[$k] = call_user_func([$userSystem, $name]);
d4330c62
TO
297 return $this->cache[$k];
298
c0a1f187
TO
299 case 'service':
300 return \Civi::service($name);
301
302 case 'callback':
303 // Array(0 => $type, 1 => $obj, 2 => $getter, 3 => $setter, 4 => $unsetter).
304 if (!isset($this->map[$k][1], $this->map[$k][2])) {
305 throw new \CRM_Core_Exception("Cannot find getter for property CRM_Core_Config::\${$k}");
306 }
be2fb01f 307 return \Civi\Core\Resolver::singleton()->call([$this->map[$k][1], $this->map[$k][2]], [$k]);
c0a1f187
TO
308
309 default:
310 throw new \CRM_Core_Exception("Cannot read property CRM_Core_Config::\${$k} ($type)");
311 }
312 }
313
8246bca4 314 /**
315 * Set value.
316 *
317 * @param string $k
318 * @param mixed $v
319 *
320 * @throws \CRM_Core_Exception
321 */
c0a1f187
TO
322 public function __set($k, $v) {
323 if (!isset($this->map[$k])) {
324 throw new \CRM_Core_Exception("Cannot set unrecognized property CRM_Core_Config::\${$k}");
325 }
7dba62da 326 unset($this->cache[$k]);
dc640254 327 $type = $this->map[$k][0];
82eff07b 328
c0a1f187
TO
329 switch ($type) {
330 case 'setting':
dc640254 331 case 'setting-path':
cdbea577 332 case 'setting-url':
00f6fb2a 333 case 'path':
d4330c62 334 case 'user-system':
c0a1f187 335 case 'runtime':
dc640254 336 case 'callback':
6504e96d 337 case 'boot-svc':
dc640254 338 // In the past, changes to $config were not persisted automatically.
cdf3884e 339 $this->cache[$k] = $v;
c0a1f187
TO
340 return;
341
342 case 'local':
343 $this->initLocals();
cdf3884e 344 $this->locals[$k] = $v;
c0a1f187
TO
345 return;
346
c0a1f187
TO
347 default:
348 throw new \CRM_Core_Exception("Cannot set property CRM_Core_Config::\${$k} ($type)");
349 }
350 }
351
8246bca4 352 /**
353 * Is value set.
354 *
355 * @param string $k
356 *
357 * @return bool
358 */
c0a1f187
TO
359 public function __isset($k) {
360 return isset($this->map[$k]);
361 }
362
8246bca4 363 /**
364 * Unset value.
365 *
366 * @param string $k
367 *
368 * @throws \CRM_Core_Exception
369 */
c0a1f187
TO
370 public function __unset($k) {
371 if (!isset($this->map[$k])) {
372 throw new \CRM_Core_Exception("Cannot unset unrecognized property CRM_Core_Config::\${$k}");
373 }
7dba62da 374 unset($this->cache[$k]);
dc640254
TO
375 $type = $this->map[$k][0];
376 $name = isset($this->map[$k][1]) ? $this->map[$k][1] : $k;
c0a1f187
TO
377
378 switch ($type) {
379 case 'setting':
380 case 'setting-path':
cdbea577 381 case 'setting-url':
23bb9c85 382 $this->getSettings()->revert($k);
c0a1f187
TO
383 return;
384
385 case 'local':
386 $this->initLocals();
387 $this->locals[$name] = NULL;
388 return;
389
390 case 'callback':
391 // Array(0 => $type, 1 => $obj, 2 => $getter, 3 => $setter, 4 => $unsetter).
392 if (!isset($this->map[$k][1], $this->map[$k][4])) {
393 throw new \CRM_Core_Exception("Cannot find unsetter for property CRM_Core_Config::\${$k}");
394 }
be2fb01f 395 \Civi\Core\Resolver::singleton()->call([$this->map[$k][1], $this->map[$k][4]], [$k]);
c0a1f187
TO
396 return;
397
398 default:
399 throw new \CRM_Core_Exception("Cannot unset property CRM_Core_Config::\${$k} ($type)");
400 }
401 }
402
23bb9c85
TO
403 /**
404 * @return \Civi\Core\SettingsBag
405 */
406 protected function getSettings() {
407 if ($this->settings === NULL) {
408 $this->settings = Civi::settings();
409 }
410 return $this->settings;
411 }
412
8246bca4 413 /**
414 * Initialise local settings.
415 */
c0a1f187
TO
416 private function initLocals() {
417 if ($this->locals === NULL) {
be2fb01f 418 $this->locals = [
c0a1f187
TO
419 'inCiviCRM' => FALSE,
420 'doNotResetCache' => 0,
5f900b59 421 'keyDisable' => FALSE,
c0a1f187
TO
422 'initialized' => FALSE,
423 'userFrameworkFrontend' => FALSE,
d4330c62 424 'userPermissionTemp' => NULL,
be2fb01f 425 ];
c0a1f187
TO
426 }
427 }
428
429}