From 8fa24f221cb84fc3f6707bd14c034cacd2892c28 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Wed, 28 Jun 2023 01:00:21 -0700 Subject: [PATCH] (REF) InstallSettingsFile - Extract 'createParams()` and `evaluate()` --- .../InstallSettingsFile.civi-setup.php | 63 +------------ setup/src/Setup/SettingsUtil.php | 88 +++++++++++++++++++ 2 files changed, 90 insertions(+), 61 deletions(-) create mode 100644 setup/src/Setup/SettingsUtil.php diff --git a/setup/plugins/installFiles/InstallSettingsFile.civi-setup.php b/setup/plugins/installFiles/InstallSettingsFile.civi-setup.php index 5902ade15c..735909d643 100644 --- a/setup/plugins/installFiles/InstallSettingsFile.civi-setup.php +++ b/setup/plugins/installFiles/InstallSettingsFile.civi-setup.php @@ -66,62 +66,7 @@ if (!defined('CIVI_SETUP')) { * @var \Civi\Setup\Model $m */ $m = $e->getModel(); - - // Map from the logical $model to civicrm.settings.php variables. - $params = array(); - $params['crmRoot'] = addslashes(rtrim($m->srcPath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR); - $params['templateCompileDir'] = addslashes($m->templateCompilePath); - // ??why is frontEnd=0?? - $params['frontEnd'] = 0; - $params['baseURL'] = addslashes(rtrim($m->cmsBaseUrl, '/')); - $params['dbUser'] = addslashes(urlencode($m->db['username'])); - $params['dbPass'] = addslashes(urlencode($m->db['password'])); - $params['dbHost'] = addslashes(implode(':', array_map('urlencode', explode(':', $m->db['server'])))); - $params['dbName'] = addslashes(urlencode($m->db['database'])); - // The '&' prefix is awkward, but we don't know what's already in the file. - // At the time of writing, it has ?new_link=true. If that is removed, - // then need to update this. - // The PHP_QUERY_RFC3986 is important because PEAR::DB will interpret plus - // signs as a reference to its old DSN format and mangle the DSN, so we - // need to use %20 for spaces. - $params['dbSSL'] = empty($m->db['ssl_params']) ? '' : addslashes('&' . http_build_query($m->db['ssl_params'], '', '&', PHP_QUERY_RFC3986)); - $params['cms'] = addslashes($m->cms); - $params['CMSdbUser'] = addslashes(urlencode($m->cmsDb['username'])); - $params['CMSdbPass'] = addslashes(urlencode($m->cmsDb['password'])); - $params['CMSdbHost'] = addslashes(implode(':', array_map('urlencode', explode(':', $m->cmsDb['server'])))); - $params['CMSdbName'] = addslashes(urlencode($m->cmsDb['database'])); - // The '&' prefix is awkward, but we don't know what's already in the file. - // At the time of writing, it has ?new_link=true. If that is removed, - // then need to update this. - // The PHP_QUERY_RFC3986 is important because PEAR::DB will interpret plus - // signs as a reference to its old DSN format and mangle the DSN, so we - // need to use %20 for spaces. - $params['CMSdbSSL'] = empty($m->cmsDb['ssl_params']) ? '' : addslashes('&' . http_build_query($m->cmsDb['ssl_params'], '', '&', PHP_QUERY_RFC3986)); - $params['siteKey'] = addslashes($m->siteKey); - $params['credKeys'] = addslashes(implode(' ', $m->credKeys)); - $params['signKeys'] = addslashes(implode(' ', $m->signKeys)); - $params['deployID'] = addslashes($m->deployID); - - $extraSettings = []; - - foreach ($m->paths as $key => $aspects) { - foreach ($aspects as $aspect => $value) { - $extraSettings[] = sprintf('$civicrm_paths[%s][%s] = %s;', var_export($key, 1), var_export($aspect, 1), var_export($value, 1)); - } - } - - foreach ($m->mandatorySettings as $key => $value) { - $extraSettings[] = sprintf('$civicrm_setting[%s][%s] = %s;', '\'domain\'', var_export($key, 1), var_export($value, 1)); - } - - // FIXME $m->defaultSettings, $m->components, $m->extensions, $m->callbacks - - if ($extraSettings) { - $params['extraSettings'] = "Additional settings generated by installer:\n" . implode("\n", $extraSettings); - } - else { - $params['extraSettings'] = ""; - } + $params = \Civi\Setup\SettingsUtil::createParams($m); $parent = dirname($m->settingsPath); if (!file_exists($parent)) { @@ -134,11 +79,7 @@ if (!defined('CIVI_SETUP')) { $tplPath = implode(DIRECTORY_SEPARATOR, [$m->srcPath, 'templates', 'CRM', 'common', 'civicrm.settings.php.template'] ); - $str = file_get_contents($tplPath); - foreach ($params as $key => $value) { - $str = str_replace('%%' . $key . '%%', $value, $str); - } - $str = trim($str) . "\n"; + $str = \Civi\Setup\SettingsUtil::evaluate($tplPath, $params); file_put_contents($m->settingsPath, $str); }, \Civi\Setup::PRIORITY_LATE); diff --git a/setup/src/Setup/SettingsUtil.php b/setup/src/Setup/SettingsUtil.php new file mode 100644 index 0000000000..2e217087b0 --- /dev/null +++ b/setup/src/Setup/SettingsUtil.php @@ -0,0 +1,88 @@ +srcPath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR); + $params['templateCompileDir'] = addslashes($m->templateCompilePath); + // ??why is frontEnd=0?? + $params['frontEnd'] = 0; + $params['baseURL'] = addslashes(rtrim($m->cmsBaseUrl, '/')); + $params['dbUser'] = addslashes(urlencode($m->db['username'])); + $params['dbPass'] = addslashes(urlencode($m->db['password'])); + $params['dbHost'] = addslashes(implode(':', array_map('urlencode', explode(':', $m->db['server'])))); + $params['dbName'] = addslashes(urlencode($m->db['database'])); + // The '&' prefix is awkward, but we don't know what's already in the file. + // At the time of writing, it has ?new_link=true. If that is removed, + // then need to update this. + // The PHP_QUERY_RFC3986 is important because PEAR::DB will interpret plus + // signs as a reference to its old DSN format and mangle the DSN, so we + // need to use %20 for spaces. + $params['dbSSL'] = empty($m->db['ssl_params']) ? '' : addslashes('&' . http_build_query($m->db['ssl_params'], '', '&', PHP_QUERY_RFC3986)); + $params['cms'] = addslashes($m->cms); + $params['CMSdbUser'] = addslashes(urlencode($m->cmsDb['username'])); + $params['CMSdbPass'] = addslashes(urlencode($m->cmsDb['password'])); + $params['CMSdbHost'] = addslashes(implode(':', array_map('urlencode', explode(':', $m->cmsDb['server'])))); + $params['CMSdbName'] = addslashes(urlencode($m->cmsDb['database'])); + // The '&' prefix is awkward, but we don't know what's already in the file. + // At the time of writing, it has ?new_link=true. If that is removed, + // then need to update this. + // The PHP_QUERY_RFC3986 is important because PEAR::DB will interpret plus + // signs as a reference to its old DSN format and mangle the DSN, so we + // need to use %20 for spaces. + $params['CMSdbSSL'] = empty($m->cmsDb['ssl_params']) ? '' : addslashes('&' . http_build_query($m->cmsDb['ssl_params'], '', '&', PHP_QUERY_RFC3986)); + $params['siteKey'] = addslashes($m->siteKey); + $params['credKeys'] = addslashes(implode(' ', $m->credKeys)); + $params['signKeys'] = addslashes(implode(' ', $m->signKeys)); + $params['deployID'] = addslashes($m->deployID); + + $extraSettings = []; + + foreach ($m->paths as $key => $aspects) { + foreach ($aspects as $aspect => $value) { + $extraSettings[] = sprintf('$civicrm_paths[%s][%s] = %s;', var_export($key, 1), var_export($aspect, 1), var_export($value, 1)); + } + } + + foreach ($m->mandatorySettings as $key => $value) { + $extraSettings[] = sprintf('$civicrm_setting[%s][%s] = %s;', '\'domain\'', var_export($key, 1), var_export($value, 1)); + } + + // FIXME $m->defaultSettings, $m->components, $m->extensions, $m->callbacks + + if ($extraSettings) { + $params['extraSettings'] = "Additional settings generated by installer:\n" . implode("\n", $extraSettings); + } + else { + $params['extraSettings'] = ""; + } + + return $params; + } + + /** + * Evaluate the settings template. + * + * @param string $tplPath + * Readable path of the 'civicrm.settings.php.template' file. + * @param array $params + * List of values to pass into the template. + * @return string + * Evaluated template. + */ + public static function evaluate(string $tplPath, array $params): string { + $str = file_get_contents($tplPath); + // FIXME: Use a single call to 'strtr($str, $vars)' + foreach ($params as $key => $value) { + $str = str_replace('%%' . $key . '%%', $value, $str); + } + return trim($str) . "\n"; + } + +} -- 2.25.1