Commit | Line | Data |
---|---|---|
c0a1f187 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.7 | | |
5 | +--------------------------------------------------------------------+ | |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
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 | ||
7f835399 | 57 | private $locals, $settings; |
c0a1f187 | 58 | |
7dba62da TO |
59 | private $cache = array(); |
60 | ||
c0a1f187 TO |
61 | public function __construct() { |
62 | $this->map = self::getPropertyMap(); | |
63 | } | |
64 | ||
65 | public function __wakeup() { | |
66 | $this->map = self::getPropertyMap(); | |
67 | } | |
68 | ||
69 | /** | |
f806379b TO |
70 | * Get a list of $config properties and the entities to which they map. |
71 | * | |
72 | * This is used for two purposes: | |
73 | * | |
74 | * 1. Runtime: Provide backward-compatible interface for reading these | |
75 | * properties. | |
76 | * 2. Upgrade: Migrate old properties of config_backend into settings. | |
77 | * | |
c0a1f187 TO |
78 | * @return array |
79 | */ | |
80 | public static function getPropertyMap() { | |
e3d28c74 TO |
81 | // Each mapping: $propertyName => Array(0 => $type, 1 => $foreignName|NULL, ...). |
82 | // If $foreignName is omitted/null, then it's assumed to match the $propertyName. | |
83 | // Other parameters may be specified, depending on the type. | |
c0a1f187 | 84 | return array( |
9e1e6020 TO |
85 | // "local" properties are unique to each instance of CRM_Core_Config (each request). |
86 | 'doNotResetCache' => array('local'), | |
87 | 'inCiviCRM' => array('local'), | |
5f900b59 | 88 | 'keyDisable' => array('local'), |
9e1e6020 | 89 | 'userFrameworkFrontend' => array('local'), |
d4330c62 | 90 | 'userPermissionTemp' => array('local'), |
9e1e6020 TO |
91 | |
92 | // "runtime" properties are computed from define()s, $_ENV, etc. | |
93 | // See also: CRM_Core_Config_Runtime. | |
94 | 'dsn' => array('runtime'), | |
95 | 'initialized' => array('runtime'), | |
96 | 'userFramework' => array('runtime'), | |
9e1e6020 TO |
97 | 'userFrameworkClass' => array('runtime'), |
98 | 'userFrameworkDSN' => array('runtime'), | |
9e1e6020 | 99 | 'userFrameworkURLVar' => array('runtime'), |
9e1e6020 TO |
100 | 'userHookClass' => array('runtime'), |
101 | 'cleanURL' => array('runtime'), | |
102 | 'configAndLogDir' => array('runtime'), | |
103 | 'templateCompileDir' => array('runtime'), | |
104 | 'templateDir' => array('runtime'), | |
105 | ||
d4330c62 | 106 | // "boot-svc" properties are critical services needed during init. |
7f835399 | 107 | // See also: Civi\Core\Container::getBootService(). |
d4330c62 TO |
108 | 'userSystem' => array('boot-svc'), |
109 | 'userPermissionClass' => array('boot-svc'), | |
110 | ||
111 | 'userFrameworkBaseURL' => array('user-system', 'getAbsoluteBaseURL'), | |
112 | 'userFrameworkVersion' => array('user-system', 'getVersion'), | |
113 | 'useFrameworkRelativeBase' => array('user-system', 'getRelativeBaseURL'), // ugh typo. | |
114 | ||
9e1e6020 TO |
115 | // "setting" properties are loaded through the setting layer, esp |
116 | // table "civicrm_setting" and global $civicrm_setting. | |
117 | // See also: Civi::settings(). | |
e3d28c74 | 118 | 'backtrace' => array('setting'), |
9e1e6020 | 119 | 'contact_default_language' => array('setting'), |
e3d28c74 | 120 | 'countryLimit' => array('setting'), |
e3d28c74 TO |
121 | 'dateInputFormat' => array('setting'), |
122 | 'dateformatDatetime' => array('setting'), | |
123 | 'dateformatFull' => array('setting'), | |
124 | 'dateformatPartial' => array('setting'), | |
125 | 'dateformatTime' => array('setting'), | |
126 | 'dateformatYear' => array('setting'), | |
37846436 | 127 | 'dateformatFinancialBatch' => array('setting'), |
c0a1f187 | 128 | 'debug' => array('setting', 'debug_enabled'), // renamed. |
e3d28c74 TO |
129 | 'defaultContactCountry' => array('setting'), |
130 | 'defaultContactStateProvince' => array('setting'), | |
131 | 'defaultCurrency' => array('setting'), | |
132 | 'defaultSearchProfileID' => array('setting'), | |
133 | 'doNotAttachPDFReceipt' => array('setting'), | |
134 | 'empoweredBy' => array('setting'), | |
c0a1f187 | 135 | 'enableComponents' => array('setting', 'enable_components'), // renamed. |
e3d28c74 TO |
136 | 'enableSSL' => array('setting'), |
137 | 'fatalErrorHandler' => array('setting'), | |
138 | 'fieldSeparator' => array('setting'), | |
139 | 'fiscalYearStart' => array('setting'), | |
140 | 'geoAPIKey' => array('setting'), | |
141 | 'geoProvider' => array('setting'), | |
142 | 'includeAlphabeticalPager' => array('setting'), | |
143 | 'includeEmailInName' => array('setting'), | |
144 | 'includeNickNameInName' => array('setting'), | |
145 | 'includeOrderByClause' => array('setting'), | |
146 | 'includeWildCardInName' => array('setting'), | |
147 | 'inheritLocale' => array('setting'), | |
148 | 'languageLimit' => array('setting'), | |
149 | 'lcMessages' => array('setting'), | |
150 | 'legacyEncoding' => array('setting'), | |
151 | 'logging' => array('setting'), | |
152 | 'mailThrottleTime' => array('setting'), | |
153 | 'mailerBatchLimit' => array('setting'), | |
154 | 'mailerJobSize' => array('setting'), | |
155 | 'mailerJobsMax' => array('setting'), | |
156 | 'mapAPIKey' => array('setting'), | |
157 | 'mapProvider' => array('setting'), | |
158 | 'maxFileSize' => array('setting'), | |
c0a1f187 | 159 | 'maxAttachments' => array('setting', 'max_attachments'), // renamed. |
e3d28c74 TO |
160 | 'monetaryDecimalPoint' => array('setting'), |
161 | 'monetaryThousandSeparator' => array('setting'), | |
162 | 'moneyformat' => array('setting'), | |
163 | 'moneyvalueformat' => array('setting'), | |
164 | 'provinceLimit' => array('setting'), | |
165 | 'recaptchaOptions' => array('setting'), | |
166 | 'recaptchaPublicKey' => array('setting'), | |
167 | 'recaptchaPrivateKey' => array('setting'), | |
606999ec | 168 | 'replyTo' => array('setting'), |
e3d28c74 TO |
169 | 'secondDegRelPermissions' => array('setting'), |
170 | 'smartGroupCacheTimeout' => array('setting'), | |
171 | 'timeInputFormat' => array('setting'), | |
172 | 'userFrameworkLogging' => array('setting'), | |
173 | 'userFrameworkUsersTableName' => array('setting'), | |
174 | 'verpSeparator' => array('setting'), | |
175 | 'wkhtmltopdfPath' => array('setting'), | |
176 | 'wpBasePage' => array('setting'), | |
177 | 'wpLoadPhp' => array('setting'), | |
178 | ||
9e1e6020 TO |
179 | // "setting-path" properties are settings with special filtering |
180 | // to return normalized file paths. | |
cdbea577 C |
181 | // Option: `mkdir` - auto-create dir |
182 | // Option: `restrict` - auto-restrict remote access | |
ac47f7ca | 183 | 'customFileUploadDir' => array('setting-path', NULL, array('mkdir', 'restrict')), |
e3d28c74 TO |
184 | 'customPHPPathDir' => array('setting-path'), |
185 | 'customTemplateDir' => array('setting-path'), | |
8dac2d39 | 186 | 'extensionsDir' => array('setting-path', NULL, array('mkdir')), |
ac47f7ca TO |
187 | 'imageUploadDir' => array('setting-path', NULL, array('mkdir')), |
188 | 'uploadDir' => array('setting-path', NULL, array('mkdir', 'restrict')), | |
e3d28c74 | 189 | |
cdbea577 C |
190 | // "setting-url" properties are settings with special filtering |
191 | // to return normalized URLs. | |
192 | // Option: `noslash` - don't append trailing slash | |
193 | // Option: `rel` - convert to relative URL (if possible) | |
194 | 'customCSSURL' => array('setting-url', NULL, array('noslash')), | |
195 | 'extensionsURL' => array('setting-url'), | |
196 | 'imageUploadURL' => array('setting-url'), | |
197 | 'resourceBase' => array('setting-url', 'userFrameworkResourceURL', array('rel')), | |
198 | 'userFrameworkResourceURL' => array('setting-url'), | |
c0a1f187 | 199 | |
9e1e6020 | 200 | // "callback" properties are generated on-demand by calling a function. |
c0a1f187 | 201 | 'geocodeMethod' => array('callback', 'CRM_Utils_Geocode', 'getProviderClass'), |
d7c217ae | 202 | 'defaultCurrencySymbol' => array('callback', 'CRM_Core_BAO_Country', 'getDefaultCurrencySymbol'), |
c0a1f187 TO |
203 | ); |
204 | } | |
205 | ||
206 | public function __get($k) { | |
207 | if (!isset($this->map[$k])) { | |
208 | throw new \CRM_Core_Exception("Cannot read unrecognized property CRM_Core_Config::\${$k}."); | |
209 | } | |
7dba62da TO |
210 | if (isset($this->cache[$k])) { |
211 | return $this->cache[$k]; | |
212 | } | |
e3d28c74 TO |
213 | |
214 | $type = $this->map[$k][0]; | |
215 | $name = isset($this->map[$k][1]) ? $this->map[$k][1] : $k; | |
c0a1f187 TO |
216 | |
217 | switch ($type) { | |
218 | case 'setting': | |
23bb9c85 | 219 | return $this->getSettings()->get($name); |
c0a1f187 TO |
220 | |
221 | case 'setting-path': | |
ac47f7ca | 222 | // Array(0 => $type, 1 => $setting, 2 => $actions). |
e3d28c74 | 223 | $value = $this->getSettings()->get($name); |
e3d28c74 TO |
224 | $value = Civi::paths()->getPath($value); |
225 | if ($value) { | |
226 | $value = CRM_Utils_File::addTrailingSlash($value); | |
ac47f7ca | 227 | if (isset($this->map[$k][2]) && in_array('mkdir', $this->map[$k][2])) { |
f7cc6e55 | 228 | if (!is_dir($value) && !CRM_Utils_File::createDir($value, FALSE)) { |
895c19e7 TO |
229 | CRM_Core_Session::setStatus(ts('Failed to make directory (%1) at "%2". Please update the settings or file permissions.', array( |
230 | 1 => $k, | |
231 | 2 => $value, | |
232 | ))); | |
233 | } | |
e3d28c74 | 234 | } |
ac47f7ca | 235 | if (isset($this->map[$k][2]) && in_array('restrict', $this->map[$k][2])) { |
e3d28c74 TO |
236 | CRM_Utils_File::restrictAccess($value); |
237 | } | |
238 | } | |
7dba62da | 239 | $this->cache[$k] = $value; |
e3d28c74 TO |
240 | return $value; |
241 | ||
cdbea577 C |
242 | case 'setting-url': |
243 | $options = !empty($this->map[$k][2]) ? $this->map[$k][2] : array(); | |
6ec7e639 | 244 | $value = $this->getSettings()->get($name); |
cdbea577 C |
245 | if ($value && !(in_array('noslash', $options))) { |
246 | $value = CRM_Utils_File::addTrailingSlash($value, '/'); | |
6ec7e639 | 247 | } |
cdbea577 C |
248 | $this->cache[$k] = Civi::paths()->getUrl($value, |
249 | in_array('rel', $options) ? 'relative' : 'absolute'); | |
7dba62da | 250 | return $this->cache[$k]; |
c0a1f187 TO |
251 | |
252 | case 'runtime': | |
7f835399 | 253 | return \Civi\Core\Container::getBootService('runtime')->{$name}; |
c0a1f187 | 254 | |
d4330c62 TO |
255 | case 'boot-svc': |
256 | $this->cache[$k] = \Civi\Core\Container::getBootService($name); | |
257 | return $this->cache[$k]; | |
258 | ||
c0a1f187 TO |
259 | case 'local': |
260 | $this->initLocals(); | |
261 | return $this->locals[$name]; | |
262 | ||
d4330c62 TO |
263 | case 'user-system': |
264 | $userSystem = \Civi\Core\Container::getBootService('userSystem'); | |
265 | $this->cache[$k] = call_user_func(array($userSystem, $name)); | |
266 | return $this->cache[$k]; | |
267 | ||
c0a1f187 TO |
268 | case 'service': |
269 | return \Civi::service($name); | |
270 | ||
271 | case 'callback': | |
272 | // Array(0 => $type, 1 => $obj, 2 => $getter, 3 => $setter, 4 => $unsetter). | |
273 | if (!isset($this->map[$k][1], $this->map[$k][2])) { | |
274 | throw new \CRM_Core_Exception("Cannot find getter for property CRM_Core_Config::\${$k}"); | |
275 | } | |
276 | return \Civi\Core\Resolver::singleton()->call(array($this->map[$k][1], $this->map[$k][2]), array($k)); | |
277 | ||
278 | default: | |
279 | throw new \CRM_Core_Exception("Cannot read property CRM_Core_Config::\${$k} ($type)"); | |
280 | } | |
281 | } | |
282 | ||
283 | public function __set($k, $v) { | |
284 | if (!isset($this->map[$k])) { | |
285 | throw new \CRM_Core_Exception("Cannot set unrecognized property CRM_Core_Config::\${$k}"); | |
286 | } | |
7dba62da | 287 | unset($this->cache[$k]); |
dc640254 TO |
288 | $type = $this->map[$k][0]; |
289 | $name = isset($this->map[$k][1]) ? $this->map[$k][1] : $k; | |
c0a1f187 TO |
290 | |
291 | switch ($type) { | |
292 | case 'setting': | |
dc640254 | 293 | case 'setting-path': |
cdbea577 | 294 | case 'setting-url': |
d4330c62 | 295 | case 'user-system': |
c0a1f187 | 296 | case 'runtime': |
dc640254 | 297 | case 'callback': |
6504e96d | 298 | case 'boot-svc': |
dc640254 TO |
299 | // In the past, changes to $config were not persisted automatically. |
300 | $this->cache[$name] = $v; | |
c0a1f187 TO |
301 | return; |
302 | ||
303 | case 'local': | |
304 | $this->initLocals(); | |
305 | $this->locals[$name] = $v; | |
306 | return; | |
307 | ||
c0a1f187 TO |
308 | default: |
309 | throw new \CRM_Core_Exception("Cannot set property CRM_Core_Config::\${$k} ($type)"); | |
310 | } | |
311 | } | |
312 | ||
313 | public function __isset($k) { | |
314 | return isset($this->map[$k]); | |
315 | } | |
316 | ||
317 | public function __unset($k) { | |
318 | if (!isset($this->map[$k])) { | |
319 | throw new \CRM_Core_Exception("Cannot unset unrecognized property CRM_Core_Config::\${$k}"); | |
320 | } | |
7dba62da | 321 | unset($this->cache[$k]); |
dc640254 TO |
322 | $type = $this->map[$k][0]; |
323 | $name = isset($this->map[$k][1]) ? $this->map[$k][1] : $k; | |
c0a1f187 TO |
324 | |
325 | switch ($type) { | |
326 | case 'setting': | |
327 | case 'setting-path': | |
cdbea577 | 328 | case 'setting-url': |
23bb9c85 | 329 | $this->getSettings()->revert($k); |
c0a1f187 TO |
330 | return; |
331 | ||
332 | case 'local': | |
333 | $this->initLocals(); | |
334 | $this->locals[$name] = NULL; | |
335 | return; | |
336 | ||
337 | case 'callback': | |
338 | // Array(0 => $type, 1 => $obj, 2 => $getter, 3 => $setter, 4 => $unsetter). | |
339 | if (!isset($this->map[$k][1], $this->map[$k][4])) { | |
340 | throw new \CRM_Core_Exception("Cannot find unsetter for property CRM_Core_Config::\${$k}"); | |
341 | } | |
342 | \Civi\Core\Resolver::singleton()->call(array($this->map[$k][1], $this->map[$k][4]), array($k)); | |
343 | return; | |
344 | ||
345 | default: | |
346 | throw new \CRM_Core_Exception("Cannot unset property CRM_Core_Config::\${$k} ($type)"); | |
347 | } | |
348 | } | |
349 | ||
23bb9c85 TO |
350 | /** |
351 | * @return \Civi\Core\SettingsBag | |
352 | */ | |
353 | protected function getSettings() { | |
354 | if ($this->settings === NULL) { | |
355 | $this->settings = Civi::settings(); | |
356 | } | |
357 | return $this->settings; | |
358 | } | |
359 | ||
c0a1f187 TO |
360 | private function initLocals() { |
361 | if ($this->locals === NULL) { | |
362 | $this->locals = array( | |
363 | 'inCiviCRM' => FALSE, | |
364 | 'doNotResetCache' => 0, | |
5f900b59 | 365 | 'keyDisable' => FALSE, |
c0a1f187 TO |
366 | 'initialized' => FALSE, |
367 | 'userFrameworkFrontend' => FALSE, | |
d4330c62 | 368 | 'userPermissionTemp' => NULL, |
c0a1f187 TO |
369 | ); |
370 | } | |
371 | } | |
372 | ||
373 | } |