From c7d90a9f50980bcf3d57177803fc7eb45e3e0af1 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 11 Sep 2015 23:16:24 -0700 Subject: [PATCH] Move BAO_Setting::getSettingSpecification => SettingsMetdata::getMetadata When this function is in the BAO, it requires that the DAO exist. In a new installation running GenCode, this can create dependency loop (where generating code requires ts(), and using ts() requires settings, and using settings requires that you already have the generated code). --- CRM/Core/BAO/Setting.php | 96 +------------------ Civi/Core/SettingsManager.php | 2 +- Civi/Core/SettingsMetadata.php | 163 +++++++++++++++++++++++++++++++++ 3 files changed, 165 insertions(+), 96 deletions(-) create mode 100644 Civi/Core/SettingsMetadata.php diff --git a/CRM/Core/BAO/Setting.php b/CRM/Core/BAO/Setting.php index e249aeca52..85a45d71e6 100644 --- a/CRM/Core/BAO/Setting.php +++ b/CRM/Core/BAO/Setting.php @@ -44,7 +44,6 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting { * Various predefined settings that have been migrated to the setting table. */ const - ALL = 'all', ADDRESS_STANDARDIZATION_PREFERENCES_NAME = 'Address Standardization Preferences', CAMPAIGN_PREFERENCES_NAME = 'Campaign Preferences', DEVELOPER_PREFERENCES_NAME = 'Developer Preferences', @@ -634,100 +633,7 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting { $domainID = NULL, $profile = NULL ) { - $cache = Civi::cache('settings'); - - $cacheString = 'settingsMetadata_' . $domainID . '_' . $profile . '_' . $componentID; - foreach ($filters as $filterField => $filterString) { - $cacheString .= "_{$filterField}_{$filterString}"; - } - $cached = 1; - // the caching into 'All' seems to be a duplicate of caching to - // settingsMetadata__ - I think the reason was to cache all settings as defined & then those altered by a hook - $settingsMetadata = $cache->get($cacheString); - - if ($settingsMetadata === NULL) { - $settingsMetadata = $cache->get(self::ALL); - if (empty($settingsMetadata)) { - global $civicrm_root; - $metaDataFolders = array($civicrm_root . '/settings'); - CRM_Utils_Hook::alterSettingsFolders($metaDataFolders); - $settingsMetadata = self::loadSettingsMetaDataFolders($metaDataFolders); - $cache->set(self::ALL, $settingsMetadata); - } - $cached = 0; - } - - CRM_Utils_Hook::alterSettingsMetaData($settingsMetadata, $domainID, $profile); - self::_filterSettingsSpecification($filters, $settingsMetadata); - - if (!$cached) { - // this is a bit 'heavy' if you are using hooks but this function - // is expected to only be called during setting administration - // it should not be called by 'getvalue' or 'getitem - $cache->set($cacheString, $settingsMetadata); - } - return $settingsMetadata; - - } - - /** - * Load the settings files defined in a series of folders. - * @param array $metaDataFolders - * List of folder paths. - * @return array - */ - public static function loadSettingsMetaDataFolders($metaDataFolders) { - $settingsMetadata = array(); - $loadedFolders = array(); - foreach ($metaDataFolders as $metaDataFolder) { - $realFolder = realpath($metaDataFolder); - if (is_dir($realFolder) && !isset($loadedFolders[$realFolder])) { - $loadedFolders[$realFolder] = TRUE; - $settingsMetadata = $settingsMetadata + self::loadSettingsMetaData($metaDataFolder); - } - } - return $settingsMetadata; - } - - /** - * Load up settings metadata from files. - */ - public static function loadSettingsMetadata($metaDataFolder) { - $settingMetaData = array(); - $settingsFiles = CRM_Utils_File::findFiles($metaDataFolder, '*.setting.php'); - foreach ($settingsFiles as $file) { - $settings = include $file; - $settingMetaData = array_merge($settingMetaData, $settings); - } - Civi::cache('settings')->set(self::ALL, $settingMetaData); - return $settingMetaData; - } - - /** - * Filter the settings metadata according to filters passed in. This is a convenience filter - * and allows selective reverting / filling of settings - * - * @param array $filters - * Filters to match against data. - * @param array $settingSpec - * Metadata to filter. - */ - public static function _filterSettingsSpecification($filters, &$settingSpec) { - if (empty($filters)) { - return; - } - elseif (array_keys($filters) == array('name')) { - $settingSpec = array($filters['name'] => CRM_Utils_Array::value($filters['name'], $settingSpec, '')); - return; - } - else { - foreach ($settingSpec as $field => $fieldValues) { - if (array_intersect_assoc($fieldValues, $filters) != $filters) { - unset($settingSpec[$field]); - } - } - return; - } + return \Civi\Core\SettingsMetadata::getMetadata($filters, $domainID, $profile); } /** diff --git a/Civi/Core/SettingsManager.php b/Civi/Core/SettingsManager.php index 1c89e50abf..c150d6aaa1 100644 --- a/Civi/Core/SettingsManager.php +++ b/Civi/Core/SettingsManager.php @@ -177,7 +177,7 @@ class SettingsManager { $cacheKey = 'defaults:' . $entity; $defaults = $this->cache->get($cacheKey); if (!is_array($defaults)) { - $specs = \CRM_Core_BAO_Setting::getSettingSpecification(NULL, array( + $specs = SettingsMetadata::getMetadata(array( 'is_contact' => ($entity === 'contact' ? 1 : 0), )); $defaults = array(); diff --git a/Civi/Core/SettingsMetadata.php b/Civi/Core/SettingsMetadata.php new file mode 100644 index 0000000000..46a9ad3028 --- /dev/null +++ b/Civi/Core/SettingsMetadata.php @@ -0,0 +1,163 @@ +get($cacheString); + $cached = ($settingsMetadata === NULL); + + if ($settingsMetadata === NULL) { + $settingsMetadata = $cache->get(self::ALL); + if (empty($settingsMetadata)) { + global $civicrm_root; + $metaDataFolders = array($civicrm_root . '/settings'); + \CRM_Utils_Hook::alterSettingsFolders($metaDataFolders); + $settingsMetadata = self::loadSettingsMetaDataFolders($metaDataFolders); + $cache->set(self::ALL, $settingsMetadata); + } + } + + \CRM_Utils_Hook::alterSettingsMetaData($settingsMetadata, $domainID, $profile); + + if (!$cached) { + $cache->set($cacheString, $settingsMetadata); + } + + self::_filterSettingsSpecification($filters, $settingsMetadata); + + return $settingsMetadata; + } + + /** + * Load the settings files defined in a series of folders. + * @param array $metaDataFolders + * List of folder paths. + * @return array + */ + protected static function loadSettingsMetaDataFolders($metaDataFolders) { + $settingsMetadata = array(); + $loadedFolders = array(); + foreach ($metaDataFolders as $metaDataFolder) { + $realFolder = realpath($metaDataFolder); + if (is_dir($realFolder) && !isset($loadedFolders[$realFolder])) { + $loadedFolders[$realFolder] = TRUE; + $settingsMetadata = $settingsMetadata + self::loadSettingsMetaData($metaDataFolder); + } + } + return $settingsMetadata; + } + + /** + * Load up settings metadata from files. + */ + protected static function loadSettingsMetadata($metaDataFolder) { + $settingMetaData = array(); + $settingsFiles = \CRM_Utils_File::findFiles($metaDataFolder, '*.setting.php'); + foreach ($settingsFiles as $file) { + $settings = include $file; + $settingMetaData = array_merge($settingMetaData, $settings); + } + \Civi::cache('settings')->set(self::ALL, $settingMetaData); + return $settingMetaData; + } + + /** + * Filter the settings metadata according to filters passed in. This is a convenience filter + * and allows selective reverting / filling of settings + * + * @param array $filters + * Filters to match against data. + * @param array $settingSpec + * Metadata to filter. + */ + protected static function _filterSettingsSpecification($filters, &$settingSpec) { + if (empty($filters)) { + return; + } + elseif (array_keys($filters) == array('name')) { + $settingSpec = array($filters['name'] => \CRM_Utils_Array::value($filters['name'], $settingSpec, '')); + return; + } + else { + foreach ($settingSpec as $field => $fieldValues) { + if (array_intersect_assoc($fieldValues, $filters) != $filters) { + unset($settingSpec[$field]); + } + } + return; + } + } + +} -- 2.25.1