Commit | Line | Data |
---|---|---|
c0a1f187 TO |
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 | ||
23bb9c85 | 57 | private $runtime, $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( |
e3d28c74 | 85 | 'backtrace' => array('setting'), |
9c325795 | 86 | 'contact_default_language' => array('contact_default_language'), |
e3d28c74 TO |
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'), | |
c0a1f187 | 95 | 'debug' => array('setting', 'debug_enabled'), // renamed. |
e3d28c74 TO |
96 | 'defaultContactCountry' => array('setting'), |
97 | 'defaultContactStateProvince' => array('setting'), | |
98 | 'defaultCurrency' => array('setting'), | |
99 | 'defaultSearchProfileID' => array('setting'), | |
100 | 'doNotAttachPDFReceipt' => array('setting'), | |
101 | 'empoweredBy' => array('setting'), | |
c0a1f187 | 102 | 'enableComponents' => array('setting', 'enable_components'), // renamed. |
e3d28c74 TO |
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'), | |
c0a1f187 | 126 | 'maxAttachments' => array('setting', 'max_attachments'), // renamed. |
e3d28c74 TO |
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'), | |
606999ec | 135 | 'replyTo' => array('setting'), |
e3d28c74 TO |
136 | 'secondDegRelPermissions' => array('setting'), |
137 | 'smartGroupCacheTimeout' => array('setting'), | |
138 | 'timeInputFormat' => array('setting'), | |
139 | 'userFrameworkLogging' => array('setting'), | |
140 | 'userFrameworkUsersTableName' => array('setting'), | |
141 | 'verpSeparator' => array('setting'), | |
606999ec | 142 | 'versionCheck' => array('setting'), |
e3d28c74 TO |
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'), | |
e3d28c74 TO |
150 | |
151 | 'dsn' => array('runtime'), | |
606999ec | 152 | 'initialized' => array('runtime'), |
e3d28c74 TO |
153 | 'userFramework' => array('runtime'), |
154 | 'userFrameworkBaseURL' => array('runtime'), | |
155 | 'userFrameworkClass' => array('runtime'), | |
156 | 'userFrameworkDSN' => array('runtime'), | |
c0a1f187 | 157 | 'useFrameworkRelativeBase' => array('runtime', 'useFrameworkRelativeBase'), |
e3d28c74 | 158 | 'userFrameworkURLVar' => array('runtime'), |
606999ec | 159 | 'userFrameworkVersion' => array('runtime'), |
e3d28c74 TO |
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 | ||
ac47f7ca | 169 | 'customFileUploadDir' => array('setting-path', NULL, array('mkdir', 'restrict')), |
e3d28c74 TO |
170 | 'customPHPPathDir' => array('setting-path'), |
171 | 'customTemplateDir' => array('setting-path'), | |
172 | 'extensionsDir' => array('setting-path'), | |
ac47f7ca TO |
173 | 'imageUploadDir' => array('setting-path', NULL, array('mkdir')), |
174 | 'uploadDir' => array('setting-path', NULL, array('mkdir', 'restrict')), | |
e3d28c74 TO |
175 | |
176 | 'customCSSURL' => array('setting-url-abs'), | |
177 | 'extensionsURL' => array('setting-url-abs'), | |
ac47f7ca TO |
178 | 'imageUploadURL' => array('setting-url-abs'), |
179 | 'resourceBase' => array('setting-url-rel', 'userFrameworkResourceURL'), | |
180 | 'userFrameworkResourceURL' => array('setting-url-abs'), | |
c0a1f187 TO |
181 | |
182 | 'geocodeMethod' => array('callback', 'CRM_Utils_Geocode', 'getProviderClass'), | |
d7c217ae | 183 | 'defaultCurrencySymbol' => array('callback', 'CRM_Core_BAO_Country', 'getDefaultCurrencySymbol'), |
c0a1f187 TO |
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 | } | |
7dba62da TO |
191 | if (isset($this->cache[$k])) { |
192 | return $this->cache[$k]; | |
193 | } | |
e3d28c74 TO |
194 | |
195 | $type = $this->map[$k][0]; | |
196 | $name = isset($this->map[$k][1]) ? $this->map[$k][1] : $k; | |
c0a1f187 TO |
197 | |
198 | switch ($type) { | |
199 | case 'setting': | |
23bb9c85 | 200 | return $this->getSettings()->get($name); |
c0a1f187 TO |
201 | |
202 | case 'setting-path': | |
ac47f7ca | 203 | // Array(0 => $type, 1 => $setting, 2 => $actions). |
e3d28c74 | 204 | $value = $this->getSettings()->get($name); |
e3d28c74 TO |
205 | $value = Civi::paths()->getPath($value); |
206 | if ($value) { | |
207 | $value = CRM_Utils_File::addTrailingSlash($value); | |
ac47f7ca | 208 | if (isset($this->map[$k][2]) && in_array('mkdir', $this->map[$k][2])) { |
e3d28c74 TO |
209 | CRM_Utils_File::createDir($value); |
210 | } | |
ac47f7ca | 211 | if (isset($this->map[$k][2]) && in_array('restrict', $this->map[$k][2])) { |
e3d28c74 TO |
212 | CRM_Utils_File::restrictAccess($value); |
213 | } | |
214 | } | |
7dba62da | 215 | $this->cache[$k] = $value; |
e3d28c74 TO |
216 | return $value; |
217 | ||
218 | case 'setting-url-abs': | |
e3d28c74 | 219 | $value = $this->getSettings()->get($name); |
7dba62da TO |
220 | $this->cache[$k] = Civi::paths()->getUrl($value, 'absolute'); |
221 | return $this->cache[$k]; | |
c0a1f187 | 222 | |
e3d28c74 | 223 | case 'setting-url-rel': |
e3d28c74 | 224 | $value = $this->getSettings()->get($name); |
7dba62da TO |
225 | $this->cache[$k] = Civi::paths()->getUrl($value, 'relative'); |
226 | return $this->cache[$k]; | |
c0a1f187 TO |
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 | } | |
7dba62da | 254 | unset($this->cache[$k]); |
dc640254 TO |
255 | $type = $this->map[$k][0]; |
256 | $name = isset($this->map[$k][1]) ? $this->map[$k][1] : $k; | |
c0a1f187 TO |
257 | |
258 | switch ($type) { | |
259 | case 'setting': | |
dc640254 TO |
260 | case 'setting-path': |
261 | case 'setting-url-abs': | |
262 | case 'setting-url-rel': | |
c0a1f187 | 263 | case 'runtime': |
dc640254 TO |
264 | case 'callback': |
265 | // In the past, changes to $config were not persisted automatically. | |
266 | $this->cache[$name] = $v; | |
c0a1f187 TO |
267 | return; |
268 | ||
269 | case 'local': | |
270 | $this->initLocals(); | |
271 | $this->locals[$name] = $v; | |
272 | return; | |
273 | ||
c0a1f187 TO |
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 | } | |
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': | |
293 | case 'setting-path': | |
e3d28c74 TO |
294 | case 'setting-url-abs': |
295 | case 'setting-url-rel': | |
23bb9c85 | 296 | $this->getSettings()->revert($k); |
c0a1f187 TO |
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 | ||
23bb9c85 TO |
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 | ||
c0a1f187 TO |
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 | } |