CRM-14232, CRM-16373 - Fix saving of contact_default_language
[civicrm-core.git] / CRM / Core / Config / MagicMerge.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
33 * grew (with robust metadata, system overrides, and extension support),
34 * the $config started to store a mix of:
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
57 private $runtime, $locals, $settings;
58
59 private $cache = array();
60
61 public function __construct() {
62 $this->map = self::getPropertyMap();
63 }
64
65 public function __wakeup() {
66 $this->map = self::getPropertyMap();
67 }
68
69 /**
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 *
78 * @return array
79 */
80 public static function getPropertyMap() {
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.
84 return array(
85 'backtrace' => array('setting'),
86 'contact_default_language' => array('contact_default_language'),
87 'countryLimit' => array('setting'),
88 'dashboardCacheTimeout' => array('setting'),
89 'dateInputFormat' => array('setting'),
90 'dateformatDatetime' => array('setting'),
91 'dateformatFull' => array('setting'),
92 'dateformatPartial' => array('setting'),
93 'dateformatTime' => array('setting'),
94 'dateformatYear' => array('setting'),
95 'debug' => array('setting', 'debug_enabled'), // renamed.
96 'defaultContactCountry' => array('setting'),
97 'defaultContactStateProvince' => array('setting'),
98 'defaultCurrency' => array('setting'),
99 'defaultSearchProfileID' => array('setting'),
100 'doNotAttachPDFReceipt' => array('setting'),
101 'empoweredBy' => array('setting'),
102 'enableComponents' => array('setting', 'enable_components'), // renamed.
103 'enableSSL' => array('setting'),
104 'fatalErrorHandler' => array('setting'),
105 'fieldSeparator' => array('setting'),
106 'fiscalYearStart' => array('setting'),
107 'geoAPIKey' => array('setting'),
108 'geoProvider' => array('setting'),
109 'includeAlphabeticalPager' => array('setting'),
110 'includeEmailInName' => array('setting'),
111 'includeNickNameInName' => array('setting'),
112 'includeOrderByClause' => array('setting'),
113 'includeWildCardInName' => array('setting'),
114 'inheritLocale' => array('setting'),
115 'languageLimit' => array('setting'),
116 'lcMessages' => array('setting'),
117 'legacyEncoding' => array('setting'),
118 'logging' => array('setting'),
119 'mailThrottleTime' => array('setting'),
120 'mailerBatchLimit' => array('setting'),
121 'mailerJobSize' => array('setting'),
122 'mailerJobsMax' => array('setting'),
123 'mapAPIKey' => array('setting'),
124 'mapProvider' => array('setting'),
125 'maxFileSize' => array('setting'),
126 'maxAttachments' => array('setting', 'max_attachments'), // renamed.
127 'monetaryDecimalPoint' => array('setting'),
128 'monetaryThousandSeparator' => array('setting'),
129 'moneyformat' => array('setting'),
130 'moneyvalueformat' => array('setting'),
131 'provinceLimit' => array('setting'),
132 'recaptchaOptions' => array('setting'),
133 'recaptchaPublicKey' => array('setting'),
134 'recaptchaPrivateKey' => array('setting'),
135 'replyTo' => array('setting'),
136 'secondDegRelPermissions' => array('setting'),
137 'smartGroupCacheTimeout' => array('setting'),
138 'timeInputFormat' => array('setting'),
139 'userFrameworkLogging' => array('setting'),
140 'userFrameworkUsersTableName' => array('setting'),
141 'verpSeparator' => array('setting'),
142 'versionCheck' => array('setting'),
143 'wkhtmltopdfPath' => array('setting'),
144 'wpBasePage' => array('setting'),
145 'wpLoadPhp' => array('setting'),
146
147 'doNotResetCache' => array('local'),
148 'inCiviCRM' => array('local'),
149 'userFrameworkFrontend' => array('local'),
150
151 'dsn' => array('runtime'),
152 'initialized' => array('runtime'),
153 'userFramework' => array('runtime'),
154 'userFrameworkBaseURL' => array('runtime'),
155 'userFrameworkClass' => array('runtime'),
156 'userFrameworkDSN' => array('runtime'),
157 'useFrameworkRelativeBase' => array('runtime', 'useFrameworkRelativeBase'),
158 'userFrameworkURLVar' => array('runtime'),
159 'userFrameworkVersion' => array('runtime'),
160 'userPermissionClass' => array('runtime'),
161 'userPermissionTemp' => array('runtime'),
162 'userSystem' => array('runtime'),
163 'userHookClass' => array('runtime'),
164 'cleanURL' => array('runtime'),
165 'configAndLogDir' => array('runtime'),
166 'templateCompileDir' => array('runtime'),
167 'templateDir' => array('runtime'),
168
169 'customFileUploadDir' => array('setting-path', NULL, array('mkdir', 'restrict')),
170 'customPHPPathDir' => array('setting-path'),
171 'customTemplateDir' => array('setting-path'),
172 'extensionsDir' => array('setting-path'),
173 'imageUploadDir' => array('setting-path', NULL, array('mkdir')),
174 'uploadDir' => array('setting-path', NULL, array('mkdir', 'restrict')),
175
176 'customCSSURL' => array('setting-url-abs'),
177 'extensionsURL' => array('setting-url-abs'),
178 'imageUploadURL' => array('setting-url-abs'),
179 'resourceBase' => array('setting-url-rel', 'userFrameworkResourceURL'),
180 'userFrameworkResourceURL' => array('setting-url-abs'),
181
182 'geocodeMethod' => array('callback', 'CRM_Utils_Geocode', 'getProviderClass'),
183 'defaultCurrencySymbol' => array('callback', 'CRM_Core_BAO_Country', 'getDefaultCurrencySymbol'),
184 );
185 }
186
187 public function __get($k) {
188 if (!isset($this->map[$k])) {
189 throw new \CRM_Core_Exception("Cannot read unrecognized property CRM_Core_Config::\${$k}.");
190 }
191 if (isset($this->cache[$k])) {
192 return $this->cache[$k];
193 }
194
195 $type = $this->map[$k][0];
196 $name = isset($this->map[$k][1]) ? $this->map[$k][1] : $k;
197
198 switch ($type) {
199 case 'setting':
200 return $this->getSettings()->get($name);
201
202 case 'setting-path':
203 // Array(0 => $type, 1 => $setting, 2 => $actions).
204 $value = $this->getSettings()->get($name);
205 $value = Civi::paths()->getPath($value);
206 if ($value) {
207 $value = CRM_Utils_File::addTrailingSlash($value);
208 if (isset($this->map[$k][2]) && in_array('mkdir', $this->map[$k][2])) {
209 CRM_Utils_File::createDir($value);
210 }
211 if (isset($this->map[$k][2]) && in_array('restrict', $this->map[$k][2])) {
212 CRM_Utils_File::restrictAccess($value);
213 }
214 }
215 $this->cache[$k] = $value;
216 return $value;
217
218 case 'setting-url-abs':
219 $value = $this->getSettings()->get($name);
220 $this->cache[$k] = Civi::paths()->getUrl($value, 'absolute');
221 return $this->cache[$k];
222
223 case 'setting-url-rel':
224 $value = $this->getSettings()->get($name);
225 $this->cache[$k] = Civi::paths()->getUrl($value, 'relative');
226 return $this->cache[$k];
227
228 case 'runtime':
229 return $this->getRuntime()->{$name};
230
231 case 'local':
232 $this->initLocals();
233 return $this->locals[$name];
234
235 case 'service':
236 return \Civi::service($name);
237
238 case 'callback':
239 // Array(0 => $type, 1 => $obj, 2 => $getter, 3 => $setter, 4 => $unsetter).
240 if (!isset($this->map[$k][1], $this->map[$k][2])) {
241 throw new \CRM_Core_Exception("Cannot find getter for property CRM_Core_Config::\${$k}");
242 }
243 return \Civi\Core\Resolver::singleton()->call(array($this->map[$k][1], $this->map[$k][2]), array($k));
244
245 default:
246 throw new \CRM_Core_Exception("Cannot read property CRM_Core_Config::\${$k} ($type)");
247 }
248 }
249
250 public function __set($k, $v) {
251 if (!isset($this->map[$k])) {
252 throw new \CRM_Core_Exception("Cannot set unrecognized property CRM_Core_Config::\${$k}");
253 }
254 unset($this->cache[$k]);
255 $type = $this->map[$k][0];
256 $name = isset($this->map[$k][1]) ? $this->map[$k][1] : $k;
257
258 switch ($type) {
259 case 'setting':
260 case 'setting-path':
261 case 'setting-url-abs':
262 case 'setting-url-rel':
263 case 'runtime':
264 case 'callback':
265 // In the past, changes to $config were not persisted automatically.
266 $this->cache[$name] = $v;
267 return;
268
269 case 'local':
270 $this->initLocals();
271 $this->locals[$name] = $v;
272 return;
273
274 default:
275 throw new \CRM_Core_Exception("Cannot set property CRM_Core_Config::\${$k} ($type)");
276 }
277 }
278
279 public function __isset($k) {
280 return isset($this->map[$k]);
281 }
282
283 public function __unset($k) {
284 if (!isset($this->map[$k])) {
285 throw new \CRM_Core_Exception("Cannot unset unrecognized property CRM_Core_Config::\${$k}");
286 }
287 unset($this->cache[$k]);
288 $type = $this->map[$k][0];
289 $name = isset($this->map[$k][1]) ? $this->map[$k][1] : $k;
290
291 switch ($type) {
292 case 'setting':
293 case 'setting-path':
294 case 'setting-url-abs':
295 case 'setting-url-rel':
296 $this->getSettings()->revert($k);
297 return;
298
299 case 'local':
300 $this->initLocals();
301 $this->locals[$name] = NULL;
302 return;
303
304 case 'callback':
305 // Array(0 => $type, 1 => $obj, 2 => $getter, 3 => $setter, 4 => $unsetter).
306 if (!isset($this->map[$k][1], $this->map[$k][4])) {
307 throw new \CRM_Core_Exception("Cannot find unsetter for property CRM_Core_Config::\${$k}");
308 }
309 \Civi\Core\Resolver::singleton()->call(array($this->map[$k][1], $this->map[$k][4]), array($k));
310 return;
311
312 default:
313 throw new \CRM_Core_Exception("Cannot unset property CRM_Core_Config::\${$k} ($type)");
314 }
315 }
316
317 /**
318 * @return CRM_Core_Config_Runtime
319 */
320 protected function getRuntime() {
321 if ($this->runtime === NULL) {
322 $this->runtime = new CRM_Core_Config_Runtime();
323 }
324 return $this->runtime;
325 }
326
327 /**
328 * @return \Civi\Core\SettingsBag
329 */
330 protected function getSettings() {
331 if ($this->settings === NULL) {
332 $this->settings = Civi::settings();
333 }
334 return $this->settings;
335 }
336
337 private function initLocals() {
338 if ($this->locals === NULL) {
339 $this->locals = array(
340 'inCiviCRM' => FALSE,
341 'doNotResetCache' => 0,
342 'initialized' => FALSE,
343 'userFrameworkFrontend' => FALSE,
344 );
345 }
346 }
347
348 }