3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Class CRM_Core_Config_MagicMerge
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
40 * The config object is now deprecated. Settings and service objects
41 * should generally be accessed via Civi::settings() and Civi::service().
43 * MagicMerge provides backward compatibility. You may still access
44 * old properties via $config, but they will be loaded from their
47 class CRM_Core_Config_MagicMerge
{
50 * Map old config properties to their contemporary counterparts.
53 * Array(string $configAlias => Array(string $realType, string $realName)).
57 private $runtime, $locals, $settings;
59 private $cache = array();
61 public function __construct() {
62 $this->map
= self
::getPropertyMap();
65 public function __wakeup() {
66 $this->map
= self
::getPropertyMap();
70 * Get a list of $config properties and the entities to which they map.
72 * This is used for two purposes:
74 * 1. Runtime: Provide backward-compatible interface for reading these
76 * 2. Upgrade: Migrate old properties of config_backend into settings.
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.
85 'backtrace' => array('setting'),
86 'countryLimit' => array('setting'),
87 'dashboardCacheTimeout' => array('setting'),
88 'dateInputFormat' => array('setting'),
89 'dateformatDatetime' => array('setting'),
90 'dateformatFull' => array('setting'),
91 'dateformatPartial' => array('setting'),
92 'dateformatTime' => array('setting'),
93 'dateformatYear' => array('setting'),
94 'debug' => array('setting', 'debug_enabled'), // renamed.
95 'defaultContactCountry' => array('setting'),
96 'defaultContactStateProvince' => array('setting'),
97 'defaultCurrency' => array('setting'),
98 'defaultSearchProfileID' => array('setting'),
99 'doNotAttachPDFReceipt' => array('setting'),
100 'empoweredBy' => array('setting'),
101 'enableComponents' => array('setting', 'enable_components'), // renamed.
102 'enableSSL' => array('setting'),
103 'fatalErrorHandler' => array('setting'),
104 'fieldSeparator' => array('setting'),
105 'fiscalYearStart' => array('setting'),
106 'geoAPIKey' => array('setting'),
107 'geoProvider' => array('setting'),
108 'includeAlphabeticalPager' => array('setting'),
109 'includeEmailInName' => array('setting'),
110 'includeNickNameInName' => array('setting'),
111 'includeOrderByClause' => array('setting'),
112 'includeWildCardInName' => array('setting'),
113 'inheritLocale' => array('setting'),
114 'languageLimit' => array('setting'),
115 'lcMessages' => array('setting'),
116 'legacyEncoding' => array('setting'),
117 'logging' => array('setting'),
118 'mailThrottleTime' => array('setting'),
119 'mailerBatchLimit' => array('setting'),
120 'mailerJobSize' => array('setting'),
121 'mailerJobsMax' => array('setting'),
122 'mapAPIKey' => array('setting'),
123 'mapProvider' => array('setting'),
124 'maxFileSize' => array('setting'),
125 'maxAttachments' => array('setting', 'max_attachments'), // renamed.
126 'monetaryDecimalPoint' => array('setting'),
127 'monetaryThousandSeparator' => array('setting'),
128 'moneyformat' => array('setting'),
129 'moneyvalueformat' => array('setting'),
130 'provinceLimit' => array('setting'),
131 'recaptchaOptions' => array('setting'),
132 'recaptchaPublicKey' => array('setting'),
133 'recaptchaPrivateKey' => array('setting'),
134 'replyTo' => array('setting'),
135 'secondDegRelPermissions' => array('setting'),
136 'smartGroupCacheTimeout' => array('setting'),
137 'timeInputFormat' => array('setting'),
138 'userFrameworkLogging' => array('setting'),
139 'userFrameworkUsersTableName' => array('setting'),
140 'verpSeparator' => array('setting'),
141 'versionCheck' => array('setting'),
142 'wkhtmltopdfPath' => array('setting'),
143 'wpBasePage' => array('setting'),
144 'wpLoadPhp' => array('setting'),
146 'doNotResetCache' => array('local'),
147 'inCiviCRM' => array('local'),
148 'userFrameworkFrontend' => array('local'),
150 'dsn' => array('runtime'),
151 'initialized' => array('runtime'),
152 'userFramework' => array('runtime'),
153 'userFrameworkBaseURL' => array('runtime'),
154 'userFrameworkClass' => array('runtime'),
155 'userFrameworkDSN' => array('runtime'),
156 'useFrameworkRelativeBase' => array('runtime', 'useFrameworkRelativeBase'),
157 'userFrameworkURLVar' => array('runtime'),
158 'userFrameworkVersion' => array('runtime'),
159 'userPermissionClass' => array('runtime'),
160 'userPermissionTemp' => array('runtime'),
161 'userSystem' => array('runtime'),
162 'userHookClass' => array('runtime'),
163 'cleanURL' => array('runtime'),
164 'configAndLogDir' => array('runtime'),
165 'templateCompileDir' => array('runtime'),
166 'templateDir' => array('runtime'),
168 'customFileUploadDir' => array('setting-path', NULL, '[civicrm.files]/custom/', array('mkdir', 'restrict')),
169 'customPHPPathDir' => array('setting-path'),
170 'customTemplateDir' => array('setting-path'),
171 'extensionsDir' => array('setting-path'),
172 'imageUploadDir' => array('setting-path', NULL, '[civicrm.files]/persist/contribute/', array('mkdir')),
173 'uploadDir' => array('setting-path', NULL, '[civicrm.files]/upload/', array('mkdir', 'restrict')),
175 'customCSSURL' => array('setting-url-abs'),
176 'extensionsURL' => array('setting-url-abs'),
177 'imageUploadURL' => array('setting-url-abs', NULL, '[civicrm.files]/persist/contribute/'),
178 'resourceBase' => array('setting-url-rel', 'userFrameworkResourceURL', '[civicrm]/.'),
179 'userFrameworkResourceURL' => array('setting-url-abs', NULL, '[civicrm]/.'),
181 'geocodeMethod' => array('callback', 'CRM_Utils_Geocode', 'getProviderClass'),
182 'defaultCurrencySymbol' => array('callback', 'CRM_Core_BAO_Country', 'getDefaultCurrencySymbol'),
186 public function __get($k) {
187 if (!isset($this->map
[$k])) {
188 throw new \
CRM_Core_Exception("Cannot read unrecognized property CRM_Core_Config::\${$k}.");
190 if (isset($this->cache
[$k])) {
191 return $this->cache
[$k];
194 $type = $this->map
[$k][0];
195 $name = isset($this->map
[$k][1]) ?
$this->map
[$k][1] : $k;
199 return $this->getSettings()->get($name);
202 // Array(0 => $type, 1 => $setting, 2 => $default, 3 => $actions).
203 $value = $this->getSettings()->get($name);
204 if (empty($value) && isset($this->map
[$k][2])) {
205 $value = $this->map
[$k][2];
207 $value = Civi
::paths()->getPath($value);
209 $value = CRM_Utils_File
::addTrailingSlash($value);
210 if (isset($this->map
[$k][3]) && in_array('mkdir', $this->map
[$k][3])) {
211 CRM_Utils_File
::createDir($value);
213 if (isset($this->map
[$k][3]) && in_array('restrict', $this->map
[$k][3])) {
214 CRM_Utils_File
::restrictAccess($value);
217 $this->cache
[$k] = $value;
220 case 'setting-url-abs':
221 // Array(0 => $type, 1 => $setting, 2 => $default).
222 $value = $this->getSettings()->get($name);
223 if (empty($value) && isset($this->map
[$k][2])) {
224 $value = $this->map
[$k][2];
226 $this->cache
[$k] = Civi
::paths()->getUrl($value, 'absolute');
227 return $this->cache
[$k];
229 case 'setting-url-rel':
230 // Array(0 => $type, 1 => $setting, 2 => $default).
231 $value = $this->getSettings()->get($name);
232 if (empty($value) && isset($this->map
[$k][2])) {
233 $value = $this->map
[$k][2];
235 $this->cache
[$k] = Civi
::paths()->getUrl($value, 'relative');
236 return $this->cache
[$k];
239 return $this->getRuntime()->{$name};
243 return $this->locals
[$name];
246 return \Civi
::service($name);
249 // Array(0 => $type, 1 => $obj, 2 => $getter, 3 => $setter, 4 => $unsetter).
250 if (!isset($this->map
[$k][1], $this->map
[$k][2])) {
251 throw new \
CRM_Core_Exception("Cannot find getter for property CRM_Core_Config::\${$k}");
253 return \Civi\Core\Resolver
::singleton()->call(array($this->map
[$k][1], $this->map
[$k][2]), array($k));
256 throw new \
CRM_Core_Exception("Cannot read property CRM_Core_Config::\${$k} ($type)");
260 public function __set($k, $v) {
261 if (!isset($this->map
[$k])) {
262 throw new \
CRM_Core_Exception("Cannot set unrecognized property CRM_Core_Config::\${$k}");
264 unset($this->cache
[$k]);
265 $type = $this->map
[$k][0];
266 $name = isset($this->map
[$k][1]) ?
$this->map
[$k][1] : $k;
271 case 'setting-url-abs':
272 case 'setting-url-rel':
275 // In the past, changes to $config were not persisted automatically.
276 $this->cache
[$name] = $v;
281 $this->locals
[$name] = $v;
285 throw new \
CRM_Core_Exception("Cannot set property CRM_Core_Config::\${$k} ($type)");
289 public function __isset($k) {
290 return isset($this->map
[$k]);
293 public function __unset($k) {
294 if (!isset($this->map
[$k])) {
295 throw new \
CRM_Core_Exception("Cannot unset unrecognized property CRM_Core_Config::\${$k}");
297 unset($this->cache
[$k]);
298 $type = $this->map
[$k][0];
299 $name = isset($this->map
[$k][1]) ?
$this->map
[$k][1] : $k;
304 case 'setting-url-abs':
305 case 'setting-url-rel':
306 $this->getSettings()->revert($k);
311 $this->locals
[$name] = NULL;
315 // Array(0 => $type, 1 => $obj, 2 => $getter, 3 => $setter, 4 => $unsetter).
316 if (!isset($this->map
[$k][1], $this->map
[$k][4])) {
317 throw new \
CRM_Core_Exception("Cannot find unsetter for property CRM_Core_Config::\${$k}");
319 \Civi\Core\Resolver
::singleton()->call(array($this->map
[$k][1], $this->map
[$k][4]), array($k));
323 throw new \
CRM_Core_Exception("Cannot unset property CRM_Core_Config::\${$k} ($type)");
328 * @return CRM_Core_Config_Runtime
330 protected function getRuntime() {
331 if ($this->runtime
=== NULL) {
332 $this->runtime
= new CRM_Core_Config_Runtime();
334 return $this->runtime
;
338 * @return \Civi\Core\SettingsBag
340 protected function getSettings() {
341 if ($this->settings
=== NULL) {
342 $this->settings
= Civi
::settings();
344 return $this->settings
;
347 private function initLocals() {
348 if ($this->locals
=== NULL) {
349 $this->locals
= array(
350 'inCiviCRM' => FALSE,
351 'doNotResetCache' => 0,
352 'initialized' => FALSE,
353 'userFrameworkFrontend' => FALSE,