Merge pull request #6199 from MrPetovan/task/move-config-to-php-array

Move config to PHP array
This commit is contained in:
Michael Vogel 2018-11-26 23:57:56 +01:00 committed by GitHub
commit ea4e772b1e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
46 changed files with 1117 additions and 1009 deletions

View file

@ -214,7 +214,7 @@ class App
set_include_path(
get_include_path() . PATH_SEPARATOR
. $this->getBasePath() . DIRECTORY_SEPARATOR . 'include' . PATH_SEPARATOR
. $this->getBasePath(). DIRECTORY_SEPARATOR . 'library' . PATH_SEPARATOR
. $this->getBasePath() . DIRECTORY_SEPARATOR . 'library' . PATH_SEPARATOR
. $this->getBasePath());
if (!empty($_SERVER['QUERY_STRING']) && strpos($_SERVER['QUERY_STRING'], 'pagename=') === 0) {
@ -329,24 +329,24 @@ class App
* Load the configuration files
*
* First loads the default value for all the configuration keys, then the legacy configuration files, then the
* expected local.ini.php
* expected local.config.php
*/
private function loadConfigFiles()
{
$this->loadConfigFile($this->getBasePath() . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'config.ini.php');
$this->loadConfigFile($this->getBasePath() . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'settings.ini.php');
$this->loadConfigFile($this->getBasePath() . '/config/defaults.config.php');
$this->loadConfigFile($this->getBasePath() . '/config/settings.config.php');
// Legacy .htconfig.php support
if (file_exists($this->getBasePath() . DIRECTORY_SEPARATOR . '.htpreconfig.php')) {
if (file_exists($this->getBasePath() . '/.htpreconfig.php')) {
$a = $this;
include $this->getBasePath() . DIRECTORY_SEPARATOR . '.htpreconfig.php';
include $this->getBasePath() . '/.htpreconfig.php';
}
// Legacy .htconfig.php support
if (file_exists($this->getBasePath() . DIRECTORY_SEPARATOR . '.htconfig.php')) {
if (file_exists($this->getBasePath() . '/.htconfig.php')) {
$a = $this;
include $this->getBasePath() . DIRECTORY_SEPARATOR . '.htconfig.php';
include $this->getBasePath() . '/.htconfig.php';
$this->setConfigValue('database', 'hostname', $db_host);
$this->setConfigValue('database', 'username', $db_user);
@ -374,24 +374,50 @@ class App
}
}
if (file_exists($this->getBasePath() . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.ini.php')) {
$this->loadConfigFile($this->getBasePath() . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.ini.php', true);
if (file_exists($this->getBasePath() . '/config/local.config.php')) {
$this->loadConfigFile($this->getBasePath() . '/config/local.config.php', true);
} elseif (file_exists($this->getBasePath() . '/config/local.ini.php')) {
$this->loadINIConfigFile($this->getBasePath() . '/config/local.ini.php', true);
}
}
/**
* Tries to load the specified legacy configuration file into the App->config array.
* Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
*
* @deprecated since version 2018.12
* @param string $filepath
* @param bool $overwrite Force value overwrite if the config key already exists
* @throws Exception
*/
public function loadINIConfigFile($filepath, $overwrite = false)
{
if (!file_exists($filepath)) {
throw new Exception('Error parsing non-existent INI config file ' . $filepath);
}
$contents = include($filepath);
$config = parse_ini_string($contents, true, INI_SCANNER_TYPED);
if ($config === false) {
throw new Exception('Error parsing INI config file ' . $filepath);
}
$this->loadConfigArray($config, $overwrite);
}
/**
* Tries to load the specified configuration file into the App->config array.
* Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
*
* The config format is INI and the template for configuration files is the following:
* The config format is PHP array and the template for configuration files is the following:
*
* <?php return <<<INI
*
* [section]
* key = value
*
* INI;
* // Keep this line
* <?php return [
* 'section' => [
* 'key' => 'value',
* ],
* ];
*
* @param string $filepath
* @param bool $overwrite Force value overwrite if the config key already exists
@ -400,17 +426,46 @@ class App
public function loadConfigFile($filepath, $overwrite = false)
{
if (!file_exists($filepath)) {
throw new Exception('Error parsing non-existent config file ' . $filepath);
throw new Exception('Error loading non-existent config file ' . $filepath);
}
$contents = include($filepath);
$config = include($filepath);
$config = parse_ini_string($contents, true, INI_SCANNER_TYPED);
if ($config === false) {
throw new Exception('Error parsing config file ' . $filepath);
if (!is_array($config)) {
throw new Exception('Error loading config file ' . $filepath);
}
$this->loadConfigArray($config, $overwrite);
}
/**
* Loads addons configuration files
*
* First loads all activated addons default configuration through the load_config hook, then load the local.config.php
* again to overwrite potential local addon configuration.
*/
private function loadAddonConfig()
{
// Loads addons default config
Core\Hook::callAll('load_config');
// Load the local addon config file to overwritten default addon config values
if (file_exists($this->getBasePath() . '/config/addon.config.php')) {
$this->loadConfigFile($this->getBasePath() . '/config/addon.config.php', true);
} elseif (file_exists($this->getBasePath() . '/config/addon.ini.php')) {
$this->loadINIConfigFile($this->getBasePath() . '/config/addon.ini.php', true);
}
}
/**
* Tries to load the specified configuration array into the App->config array.
* Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
*
* @param array $config
* @param bool $overwrite Force value overwrite if the config key already exists
*/
private function loadConfigArray(array $config, $overwrite = false)
{
foreach ($config as $category => $values) {
foreach ($values as $key => $value) {
if ($overwrite) {
@ -422,23 +477,6 @@ class App
}
}
/**
* Loads addons configuration files
*
* First loads all activated addons default configuration throught the load_config hook, then load the local.ini.php
* again to overwrite potential local addon configuration.
*/
private function loadAddonConfig()
{
// Loads addons default config
Core\Addon::callHooks('load_config');
// Load the local addon config file to overwritten default addon config values
if (file_exists($this->getBasePath() . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'addon.ini.php')) {
$this->loadConfigFile($this->getBasePath() . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'addon.ini.php', true);
}
}
/**
* Loads the default timezone
*
@ -661,8 +699,8 @@ class App
$this->urlPath = trim($parsed['path'], '\\/');
}
if (file_exists($this->getBasePath() . DIRECTORY_SEPARATOR . '.htpreconfig.php')) {
include $this->getBasePath() . DIRECTORY_SEPARATOR . '.htpreconfig.php';
if (file_exists($this->getBasePath() . '/.htpreconfig.php')) {
include $this->getBasePath() . '/.htpreconfig.php';
}
if (Core\Config::get('config', 'hostname') != '') {

View file

@ -52,8 +52,9 @@ class Mode
$this->mode = 0;
if (!file_exists($this->basepath . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.ini.php')
&& !file_exists($this->basepath . DIRECTORY_SEPARATOR . '.htconfig.php')) {
if (!file_exists($this->basepath . '/config/local.config.php')
&& !file_exists($this->basepath . '/config/local.ini.php')
&& !file_exists($this->basepath . '/.htconfig.php')) {
return;
}

View file

@ -65,13 +65,13 @@ class JITConfigAdapter extends BaseObject implements IConfigAdapter
$this->in_db[$cat][$k] = true;
return $value;
} elseif (isset($a->config[$cat][$k])) {
// Assign the value (mostly) from config/local.ini.php file to the cache
// Assign the value (mostly) from config/local.config.php file to the cache
$this->cache[$cat][$k] = $a->config[$cat][$k];
$this->in_db[$cat][$k] = false;
return $a->config[$cat][$k];
} elseif (isset($a->config[$k])) {
// Assign the value (mostly) from config/local.ini.php file to the cache
// Assign the value (mostly) from config/local.config.php file to the cache
$this->cache[$k] = $a->config[$k];
$this->in_db[$k] = false;

View file

@ -23,7 +23,7 @@ Synopsis
bin/console autoinstall [-h|--help|-?] [-v] [-a] [-f]
Description
Installs Friendica with data based on the local.ini.php file or environment variables
Installs Friendica with data based on the local.config.php file or environment variables
Notes
Not checking .htaccess/URL-Rewrite during CLI installation.
@ -32,7 +32,7 @@ Options
-h|--help|-? Show help information
-v Show more debug information.
-a All setup checks are required (except .htaccess)
-f|--file <config> prepared config file (e.g. "config/local.ini.php" itself) which will override every other config option - except the environment variables)
-f|--file <config> prepared config file (e.g. "config/local.config.php" itself) which will override every other config option - except the environment variables)
-s|--savedb Save the DB credentials to the file (if environment variables is used)
-H|--dbhost <host> The host of the mysql/mariadb database (env MYSQL_HOST)
-p|--dbport <port> The port of the mysql/mariadb database (env MYSQL_PORT)
@ -58,11 +58,11 @@ Environment variables
FRIENDICA_LANG The langauge of Friendica
Examples
bin/console autoinstall -f 'input.ini.php
Installs Friendica with the prepared 'input.ini.php' file
bin/console autoinstall -f 'input.config.php
Installs Friendica with the prepared 'input.config.php' file
bin/console autoinstall --savedb
Installs Friendica with environment variables and saves them to the 'config/local.ini.php' file
Installs Friendica with environment variables and saves them to the 'config/local.config.php' file
bin/console autoinstall -h localhost -p 3365 -U user -P passwort1234 -d friendica
Installs Friendica with a local mysql database with credentials
@ -96,11 +96,11 @@ HELP;
$config_file = $this->getOption(['f', 'file']);
if (!empty($config_file)) {
if ($config_file != 'config' . DIRECTORY_SEPARATOR . 'local.ini.php') {
if ($config_file != 'config' . DIRECTORY_SEPARATOR . 'local.config.php') {
// Copy config file
$this->out("Copying config file...\n");
if (!copy($a->getBasePath() . DIRECTORY_SEPARATOR . $config_file, $a->getBasePath() . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.ini.php')) {
throw new RuntimeException("ERROR: Saving config file failed. Please copy '$config_file' to '" . $a->getBasePath() . "'" . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "local.ini.php' manually.\n");
if (!copy($a->getBasePath() . DIRECTORY_SEPARATOR . $config_file, $a->getBasePath() . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.config.php')) {
throw new RuntimeException("ERROR: Saving config file failed. Please copy '$config_file' to '" . $a->getBasePath() . "'" . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "local.config.php' manually.\n");
}
}
@ -175,7 +175,7 @@ HELP;
Theme::install(Config::get('system', 'theme'));
$this->out(" Complete\n\n");
} else {
$this->out(" Theme setting is empty. Please check the file 'config/local.ini.php'\n\n");
$this->out(" Theme setting is empty. Please check the file 'config/local.config.php'\n\n");
}
$this->out("\nInstallation is finished\n");

View file

@ -59,7 +59,7 @@ Description
Sets the value of the provided key in the category
Notes:
Setting config entries which are manually set in config/local.ini.php may result in
Setting config entries which are manually set in config/local.config.php may result in
conflict between database settings and the manual startup settings.
Options

View file

@ -123,7 +123,7 @@ class Installer
/**
* Executes the installation of Friendica in the given environment.
* - Creates `config/local.ini.php`
* - Creates `config/local.config.php`
* - Installs Database Structure
*
* @param string $phppath Path to the PHP-Binary (optional, if not set e.g. 'php' or '/usr/bin/php')
@ -141,7 +141,7 @@ class Installer
*/
public function createConfig($phppath, $urlpath, $dbhost, $dbuser, $dbpass, $dbdata, $timezone, $language, $adminmail, $basepath)
{
$tpl = Renderer::getMarkupTemplate('local.ini.tpl');
$tpl = Renderer::getMarkupTemplate('local.config.tpl');
$txt = Renderer::replaceMacros($tpl, [
'$phpath' => $phppath,
'$dbhost' => $dbhost,
@ -154,10 +154,10 @@ class Installer
'$adminmail' => $adminmail,
]);
$result = file_put_contents($basepath . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.ini.php', $txt);
$result = file_put_contents($basepath . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.config.php', $txt);
if (!$result) {
$this->addCheck(L10n::t('The database configuration file "config/local.ini.php" could not be written. Please use the enclosed text to create a configuration file in your web server root.'), false, false, htmlentities($txt, ENT_COMPAT, 'UTF-8'));
$this->addCheck(L10n::t('The database configuration file "config/local.config.php" could not be written. Please use the enclosed text to create a configuration file in your web server root.'), false, false, htmlentities($txt, ENT_COMPAT, 'UTF-8'));
}
return $result;
@ -444,9 +444,9 @@ class Installer
}
/**
* "config/local.ini.php" - Check
* "config/local.config.php" - Check
*
* Checks if it's possible to create the "config/local.ini.php"
* Checks if it's possible to create the "config/local.config.php"
*
* @return bool false if something required failed
*/
@ -454,17 +454,17 @@ class Installer
{
$status = true;
$help = "";
if ((file_exists('config/local.ini.php') && !is_writable('config/local.ini.php')) ||
(!file_exists('config/local.ini.php') && !is_writable('.'))) {
if ((file_exists('config/local.config.php') && !is_writable('config/local.config.php')) ||
(!file_exists('config/local.config.php') && !is_writable('.'))) {
$status = false;
$help = L10n::t('The web installer needs to be able to create a file called "local.ini.php" in the "config" folder of your web server and it is unable to do so.') . EOL;
$help = L10n::t('The web installer needs to be able to create a file called "local.config.php" in the "config" folder of your web server and it is unable to do so.') . EOL;
$help .= L10n::t('This is most often a permission setting, as the web server may not be able to write files in your folder - even if you can.') . EOL;
$help .= L10n::t('At the end of this procedure, we will give you a text to save in a file named local.ini.php in your Friendica "config" folder.') . EOL;
$help .= L10n::t('At the end of this procedure, we will give you a text to save in a file named local.config.php in your Friendica "config" folder.') . EOL;
$help .= L10n::t('You can alternatively skip this procedure and perform a manual installation. Please see the file "INSTALL.txt" for instructions.') . EOL;
}
$this->addCheck(L10n::t('config/local.ini.php is writable'), $status, false, $help);
$this->addCheck(L10n::t('config/local.config.php is writable'), $status, false, $help);
// Local INI File is not required
return true;

View file

@ -28,7 +28,7 @@ class DBStructure
const UPDATE_FAILED = 2; // Database check failed
/**
* Database structure definition loaded from config/dbstructure.php
* Database structure definition loaded from config/dbstructure.config.php
*
* @var array
*/
@ -783,10 +783,10 @@ class DBStructure
}
/**
* Loads the database structure definition from the config/dbstructure.php file.
* Loads the database structure definition from the config/dbstructure.config.php file.
* On first pass, defines DB_UPDATE_VERSION constant.
*
* @see config/dbstructure.php
* @see config/dbstructure.config.php
* @param boolean $with_addons_structure Whether to tack on addons additional tables
* @return array
* @throws Exception
@ -796,16 +796,16 @@ class DBStructure
if (!self::$definition) {
$a = \Friendica\BaseObject::getApp();
$filename = $a->getBasePath() . '/config/dbstructure.php';
$filename = $a->getBasePath() . '/config/dbstructure.config.php';
if (!is_readable($filename)) {
throw new Exception('Missing database structure config file config/dbstructure.php');
throw new Exception('Missing database structure config file config/dbstructure.config.php');
}
$definition = require $filename;
if (!$definition) {
throw new Exception('Corrupted database structure config file config/dbstructure.php');
throw new Exception('Corrupted database structure config file config/dbstructure.config.php');
}
self::$definition = $definition;