From 92fb0a82ca9b62065647a8c34134030415a67c55 Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Sun, 23 Jun 2019 19:56:21 +0200 Subject: [PATCH] Dynamic config loading - Move settings, defaults and dbstructure to directory 'static' - Dynamic loading of config files (after the static loading) - Filter out '-sample.config.php' and '-sample.ini.php' files - Remove unnecessary ConfigFileManager - Move ConfigFileLoader to Utils - Add tests for multi-loading for INI, config and sample-filtering --- config/local-sample.config.php | 2 +- doc/Config.md | 17 +- src/App.php | 5 +- src/Console/AutomaticInstallation.php | 2 +- src/Core/Config/Configuration.php | 4 +- src/Database/DBStructure.php | 8 +- src/Factory/ConfigFactory.php | 2 +- src/Factory/DependencyFactory.php | 4 +- src/Module/Admin/Summary.php | 2 +- src/Util/Config/ConfigFileManager.php | 90 ---------- src/Util/{Config => }/ConfigFileLoader.php | 170 ++++++++++++++---- {config => static}/dbstructure.config.php | 0 {config => static}/defaults.config.php | 0 {config => static}/settings.config.php | 0 tests/DatabaseTest.php | 3 +- tests/Util/VFSTrait.php | 22 +-- .../config/{local.config.php => A.config.php} | 0 .../config/{local.ini.php => A.ini.php} | 0 tests/datasets/config/B.config.php | 29 +++ tests/datasets/config/B.ini.php | 20 +++ tests/include/ApiTest.php | 1 + .../AutomaticInstallationConsoleTest.php | 2 +- .../src/Util/Config/ConfigFileLoaderTest.php | 95 +++++++++- update.php | 6 +- view/templates/local.config.tpl | 2 +- 25 files changed, 325 insertions(+), 161 deletions(-) delete mode 100644 src/Util/Config/ConfigFileManager.php rename src/Util/{Config => }/ConfigFileLoader.php (52%) rename {config => static}/dbstructure.config.php (100%) rename {config => static}/defaults.config.php (100%) rename {config => static}/settings.config.php (100%) rename tests/datasets/config/{local.config.php => A.config.php} (100%) rename tests/datasets/config/{local.ini.php => A.ini.php} (100%) create mode 100644 tests/datasets/config/B.config.php create mode 100644 tests/datasets/config/B.ini.php diff --git a/config/local-sample.config.php b/config/local-sample.config.php index 9b977ff53f..9bf073df1d 100644 --- a/config/local-sample.config.php +++ b/config/local-sample.config.php @@ -12,7 +12,7 @@ * * Then set the following for your MySQL installation * - * If you're unsure about what any of the config keys below do, please check the config/defaults.config.php file for + * If you're unsure about what any of the config keys below do, please check the static/defaults.config.php file for * detailed documentation of their data type and behavior. */ diff --git a/doc/Config.md b/doc/Config.md index 7eae371c28..f2224f5de7 100644 --- a/doc/Config.md +++ b/doc/Config.md @@ -33,15 +33,24 @@ return [ ### Configuration location -The `config` directory holds key configuration files: +The `config` directory holds key configuration files and can have different config files. +All of them have to end with `.config.php` and must not include `-sample` in their name. -- `defaults.config.php` holds the default values for all the configuration keys that can only be set in `local.config.php`. -- `settings.config.php` holds the default values for some configuration keys that are set through the admin settings page. +Some examples of common known configuration files: - `local.config.php` holds the current node custom configuration. - `addon.config.php` is optional and holds the custom configuration for specific addons. Addons can define their own default configuration values in `addon/[addon]/config/[addon].config.php` which is loaded when the addon is activated. +### Static Configuration location + +The `static` directory holds the codebase default configurations files. +They must not be changed by users, because they can get changed from release to release. + +Currently, the following configurations are included: +- `defaults.config.php` holds the default values for all the configuration keys that can only be set in `local.config.php`. +- `settings.config.php` holds the default values for some configuration keys that are set through the admin settings page. + #### Migrating from .htconfig.php to config/local.config.php The legacy `.htconfig.php` configuration file is still supported, but is deprecated and will be removed in a subsequent Friendica release. @@ -292,7 +301,7 @@ Or it is for testing purposes only. **Attention:** Please be warned that you shouldn't use one of these values without the knowledge what it could trigger. Especially don't do that with undocumented values. -These configurations keys and their default value are listed in `config/defaults.config.php` and should be overwritten in `config/local.config.php`. +These configurations keys and their default value are listed in `static/defaults.config.php` and should be overwritten in `config/local.config.php`. ## Administrator Options diff --git a/src/App.php b/src/App.php index 8b2d50512b..21df2505d0 100644 --- a/src/App.php +++ b/src/App.php @@ -17,7 +17,7 @@ use Friendica\Database\DBA; use Friendica\Model\Profile; use Friendica\Network\HTTPException; use Friendica\Util\BaseURL; -use Friendica\Util\Config\ConfigFileLoader; +use Friendica\Util\ConfigFileLoader; use Friendica\Util\HTTPSignature; use Friendica\Util\Profiler; use Friendica\Util\Strings; @@ -360,9 +360,6 @@ class App $this->getMode()->determine($this->getBasePath()); if ($this->getMode()->has(App\Mode::DBAVAILABLE)) { - $loader = new ConfigFileLoader($this->getBasePath(), $this->getMode()); - $this->config->getCache()->load($loader->loadCoreConfig('addon'), true); - $this->profiler->update( $this->config->get('system', 'profiler', false), $this->config->get('rendertime', 'callstack', false)); diff --git a/src/Console/AutomaticInstallation.php b/src/Console/AutomaticInstallation.php index 2978ed1ecf..401d9c292c 100644 --- a/src/Console/AutomaticInstallation.php +++ b/src/Console/AutomaticInstallation.php @@ -9,7 +9,7 @@ use Friendica\Core\Installer; use Friendica\Core\Theme; use Friendica\Util\BasePath; use Friendica\Util\BaseURL; -use Friendica\Util\Config\ConfigFileLoader; +use Friendica\Util\ConfigFileLoader; use RuntimeException; class AutomaticInstallation extends Console diff --git a/src/Core/Config/Configuration.php b/src/Core/Config/Configuration.php index 37b947d11b..f904f369bb 100644 --- a/src/Core/Config/Configuration.php +++ b/src/Core/Config/Configuration.php @@ -45,7 +45,7 @@ abstract class Configuration /** * @brief Loads all configuration values of family into a cached storage. * - * All configuration values of the system are stored in the cache ( @see IConfigCache ) + * All configuration values of the system are stored in the cache ( @see ConfigCache ) * * @param string $cat The category of the configuration value * @@ -59,7 +59,7 @@ abstract class Configuration * * Get a particular config value from the given category ($cat) * and the $key from a cached storage either from the $this->configAdapter - * (@see IConfigAdapter ) or from the $this->configCache (@see IConfigCache ). + * (@see IConfigAdapter ) or from the $this->configCache (@see ConfigCache ). * * @param string $cat The category of the configuration value * @param string $key The configuration key to query diff --git a/src/Database/DBStructure.php b/src/Database/DBStructure.php index abbac4e781..3726183493 100644 --- a/src/Database/DBStructure.php +++ b/src/Database/DBStructure.php @@ -96,7 +96,7 @@ class DBStructure * Loads the database structure definition from the config/dbstructure.config.php file. * On first pass, defines DB_UPDATE_VERSION constant. * - * @see config/dbstructure.config.php + * @see static/dbstructure.config.php * @param boolean $with_addons_structure Whether to tack on addons additional tables * @param string $basePath The base path of this application * @return array @@ -106,16 +106,16 @@ class DBStructure { if (!self::$definition) { - $filename = $basePath . '/config/dbstructure.config.php'; + $filename = $basePath . '/static/dbstructure.config.php'; if (!is_readable($filename)) { - throw new Exception('Missing database structure config file config/dbstructure.config.php'); + throw new Exception('Missing database structure config file static/dbstructure.config.php'); } $definition = require $filename; if (!$definition) { - throw new Exception('Corrupted database structure config file config/dbstructure.config.php'); + throw new Exception('Corrupted database structure config file static/dbstructure.config.php'); } self::$definition = $definition; diff --git a/src/Factory/ConfigFactory.php b/src/Factory/ConfigFactory.php index 559411d623..1aba8fc824 100644 --- a/src/Factory/ConfigFactory.php +++ b/src/Factory/ConfigFactory.php @@ -5,9 +5,9 @@ namespace Friendica\Factory; use Friendica\Core; use Friendica\Core\Config; use Friendica\Core\Config\Cache; +use Friendica\Util\ConfigFileLoader; use Friendica\Model\Config\Config as ConfigModel; use Friendica\Model\Config\PConfig as PConfigModel; -use Friendica\Util\Config\ConfigFileLoader; class ConfigFactory { diff --git a/src/Factory/DependencyFactory.php b/src/Factory/DependencyFactory.php index d444f5d2f3..c0a256a942 100644 --- a/src/Factory/DependencyFactory.php +++ b/src/Factory/DependencyFactory.php @@ -7,7 +7,7 @@ use Friendica\Core\Config\Cache\PConfigCache; use Friendica\Factory; use Friendica\Util\BasePath; use Friendica\Util\BaseURL; -use Friendica\Util\Config; +use Friendica\Util\ConfigFileLoader; class DependencyFactory { @@ -27,7 +27,7 @@ class DependencyFactory $basePath = BasePath::create($directory, $_SERVER); $mode = new App\Mode($basePath); $router = new App\Router(); - $configLoader = new Config\ConfigFileLoader($basePath, $mode); + $configLoader = new ConfigFileLoader($basePath, $mode); $configCache = Factory\ConfigFactory::createCache($configLoader); $profiler = Factory\ProfilerFactory::create($configCache); $database = Factory\DBFactory::init($configCache, $profiler, $_SERVER); diff --git a/src/Module/Admin/Summary.php b/src/Module/Admin/Summary.php index ca042c6fa7..beb439de2a 100644 --- a/src/Module/Admin/Summary.php +++ b/src/Module/Admin/Summary.php @@ -12,7 +12,7 @@ use Friendica\Database\DBA; use Friendica\Database\DBStructure; use Friendica\Model\Register; use Friendica\Module\BaseAdminModule; -use Friendica\Util\Config\ConfigFileLoader; +use Friendica\Util\ConfigFileLoader; use Friendica\Util\DateTimeFormat; use Friendica\Util\Network; diff --git a/src/Util/Config/ConfigFileManager.php b/src/Util/Config/ConfigFileManager.php deleted file mode 100644 index 729e59d746..0000000000 --- a/src/Util/Config/ConfigFileManager.php +++ /dev/null @@ -1,90 +0,0 @@ -baseDir = $baseDir; - $this->configDir = $baseDir . DIRECTORY_SEPARATOR . self::SUBDIRECTORY; - } - - /** - * Gets the full name (including the path) for a *.config.php (default is local.config.php) - * - * @param string $name The config name (default is empty, which means local.config.php) - * - * @return string The full name or empty if not found - */ - protected function getConfigFullName($name = '') - { - $name = !empty($name) ? $name : self::CONFIG_LOCAL; - - $fullName = $this->configDir . DIRECTORY_SEPARATOR . $name . '.config.php'; - return file_exists($fullName) ? $fullName : ''; - } - - /** - * Gets the full name (including the path) for a *.ini.php (default is local.ini.php) - * - * @param string $name The config name (default is empty, which means local.ini.php) - * - * @return string The full name or empty if not found - */ - protected function getIniFullName($name = '') - { - $name = !empty($name) ? $name : self::CONFIG_INI; - - $fullName = $this->configDir . DIRECTORY_SEPARATOR . $name . '.ini.php'; - return file_exists($fullName) ? $fullName : ''; - } - - /** - * Gets the full name (including the path) for a .*.php (default is .htconfig.php) - * - * @param string $name The config name (default is empty, which means .htconfig.php) - * - * @return string The full name or empty if not found - */ - protected function getHtConfigFullName($name = '') - { - $name = !empty($name) ? $name : self::CONFIG_HTCONFIG; - - $fullName = $this->baseDir . DIRECTORY_SEPARATOR . '.' . $name . '.php'; - return file_exists($fullName) ? $fullName : ''; - } -} diff --git a/src/Util/Config/ConfigFileLoader.php b/src/Util/ConfigFileLoader.php similarity index 52% rename from src/Util/Config/ConfigFileLoader.php rename to src/Util/ConfigFileLoader.php index cc6f89ce9c..7c41097c2d 100644 --- a/src/Util/Config/ConfigFileLoader.php +++ b/src/Util/ConfigFileLoader.php @@ -1,7 +1,8 @@ appMode = $mode; + $this->baseDir = $baseDir; + $this->configDir = $baseDir . DIRECTORY_SEPARATOR . self::CONFIG_DIR; + $this->staticDir = $baseDir . DIRECTORY_SEPARATOR . self::STATIC_DIR; + $this->appMode = $mode; } /** @@ -36,17 +86,20 @@ class ConfigFileLoader extends ConfigFileManager * @param ConfigCache $config The config cache to load to * @param bool $raw Setup the raw config format * - * @throws \Exception + * @throws Exception */ public function setupCache(ConfigCache $config, $raw = false) { - $config->load($this->loadCoreConfig('defaults')); - $config->load($this->loadCoreConfig('settings')); + // Load static config files first, the order is important + $config->load($this->loadStaticConfig('defaults')); + $config->load($this->loadStaticConfig('settings')); + // try to load the legacy config first $config->load($this->loadLegacyConfig('htpreconfig'), true); $config->load($this->loadLegacyConfig('htconfig'), true); - $config->load($this->loadCoreConfig('local'), true); + // Now load every other config you find inside the 'config/' directory + $this->loadCoreConfig($config); // In case of install mode, add the found basepath (because there isn't a basepath set yet if (!$raw && ($this->appMode->isInstall() || empty($config->get('system', 'basepath')))) { @@ -56,25 +109,52 @@ class ConfigFileLoader extends ConfigFileManager } /** - * Tries to load the specified core-configuration and returns the config array. + * Tries to load the static core-configuration and returns the config array. * - * @param string $name The name of the configuration (default is empty, which means 'local') + * @param string $name The name of the configuration * * @return array The config array (empty if no config found) * - * @throws \Exception if the configuration file isn't readable + * @throws Exception if the configuration file isn't readable */ - public function loadCoreConfig($name = '') + private function loadStaticConfig($name) { - if (!empty($this->getConfigFullName($name))) { - return $this->loadConfigFile($this->getConfigFullName($name)); - } elseif (!empty($this->getIniFullName($name))) { - return $this->loadINIConfigFile($this->getIniFullName($name)); + $configName = $this->staticDir . DIRECTORY_SEPARATOR . $name . '.config.php'; + $iniName = $this->staticDir . DIRECTORY_SEPARATOR . $name . '.ini.php'; + + if (file_exists($configName)) { + return $this->loadConfigFile($configName); + } elseif (file_exists($iniName)) { + return $this->loadINIConfigFile($iniName); } else { return []; } } + /** + * Tries to load the specified core-configuration into the config cache. + * + * @param ConfigCache $config The Config cache + * + * @return array The config array (empty if no config found) + * + * @throws Exception if the configuration file isn't readable + */ + private function loadCoreConfig(ConfigCache $config) + { + // try to load legacy ini-files first + foreach ($this->getConfigFiles(true) as $configFile) { + $config->load($this->loadINIConfigFile($configFile), true); + } + + // try to load supported config at last to overwrite it + foreach ($this->getConfigFiles() as $configFile) { + $config->load($this->loadConfigFile($configFile), true); + } + + return []; + } + /** * Tries to load the specified addon-configuration and returns the config array. * @@ -82,15 +162,15 @@ class ConfigFileLoader extends ConfigFileManager * * @return array The config array (empty if no config found) * - * @throws \Exception if the configuration file isn't readable + * @throws Exception if the configuration file isn't readable */ public function loadAddonConfig($name) { $filepath = $this->baseDir . DIRECTORY_SEPARATOR . // /var/www/html/ - Addon::DIRECTORY . DIRECTORY_SEPARATOR . // addon/ - $name . DIRECTORY_SEPARATOR . // openstreetmap/ - self::SUBDIRECTORY . DIRECTORY_SEPARATOR . // config/ - $name . ".config.php"; // openstreetmap.config.php + Addon::DIRECTORY . DIRECTORY_SEPARATOR . // addon/ + $name . DIRECTORY_SEPARATOR . // openstreetmap/ + self::CONFIG_DIR . DIRECTORY_SEPARATOR . // config/ + $name . ".config.php"; // openstreetmap.config.php if (file_exists($filepath)) { return $this->loadConfigFile($filepath); @@ -99,6 +179,32 @@ class ConfigFileLoader extends ConfigFileManager } } + /** + * Get the config files of the config-directory + * + * @param bool $ini True, if scan for ini-files instead of config files + * + * @return array + */ + private function getConfigFiles(bool $ini = false) + { + $files = scandir($this->configDir); + $found = array(); + + $filePattern = ($ini ? '*.ini.php' : '*.config.php'); + + // Don't load sample files + $sampleEnd = self::SAMPLE_END . ($ini ? '.ini.php' : '.config.php'); + + foreach ($files as $filename) { + if (fnmatch($filePattern, $filename) && substr_compare($filename, $sampleEnd, -strlen($sampleEnd))) { + $found[] = $this->configDir . '/' . $filename; + } + } + + return $found; + } + /** * Tries to load the legacy config files (.htconfig.php, .htpreconfig.php) and returns the config array. * @@ -110,11 +216,14 @@ class ConfigFileLoader extends ConfigFileManager */ private function loadLegacyConfig($name = '') { + $name = !empty($name) ? $name : self::CONFIG_HTCONFIG; + $fullName = $this->baseDir . DIRECTORY_SEPARATOR . '.' . $name . '.php'; + $config = []; - if (!empty($this->getHtConfigFullName($name))) { - $a = new \stdClass(); + if (file_exists($fullName)) { + $a = new \stdClass(); $a->config = []; - include $this->getHtConfigFullName($name); + include $fullName; $htConfigCategories = array_keys($a->config); @@ -172,11 +281,11 @@ class ConfigFileLoader extends ConfigFileManager /** * Tries to load the specified legacy configuration file and returns the config array. * - * @deprecated since version 2018.12 * @param string $filepath * * @return array The configuration array - * @throws \Exception + * @throws Exception + * @deprecated since version 2018.12 */ private function loadINIConfigFile($filepath) { @@ -185,7 +294,7 @@ class ConfigFileLoader extends ConfigFileManager $config = parse_ini_string($contents, true, INI_SCANNER_TYPED); if ($config === false) { - throw new \Exception('Error parsing INI config file ' . $filepath); + throw new Exception('Error parsing INI config file ' . $filepath); } return $config; @@ -202,17 +311,18 @@ class ConfigFileLoader extends ConfigFileManager * ], * ]; * - * @param string $filepath The filepath of the + * @param string $filepath The filepath of the + * * @return array The config array0 * - * @throws \Exception if the config cannot get loaded. + * @throws Exception if the config cannot get loaded. */ private function loadConfigFile($filepath) { $config = include($filepath); if (!is_array($config)) { - throw new \Exception('Error loading config file ' . $filepath); + throw new Exception('Error loading config file ' . $filepath); } return $config; diff --git a/config/dbstructure.config.php b/static/dbstructure.config.php similarity index 100% rename from config/dbstructure.config.php rename to static/dbstructure.config.php diff --git a/config/defaults.config.php b/static/defaults.config.php similarity index 100% rename from config/defaults.config.php rename to static/defaults.config.php diff --git a/config/settings.config.php b/static/settings.config.php similarity index 100% rename from config/settings.config.php rename to static/settings.config.php diff --git a/tests/DatabaseTest.php b/tests/DatabaseTest.php index 999da871db..950da5af6f 100644 --- a/tests/DatabaseTest.php +++ b/tests/DatabaseTest.php @@ -6,14 +6,13 @@ namespace Friendica\Test; use Friendica\App\Mode; -use Friendica\App\Router; use Friendica\Core\Config\Cache\ConfigCache; use Friendica\Database\Database; use Friendica\Factory\ConfigFactory; use Friendica\Factory\DBFactory; use Friendica\Factory\ProfilerFactory; use Friendica\Util\BasePath; -use Friendica\Util\Config\ConfigFileLoader; +use Friendica\Util\ConfigFileLoader; use Friendica\Util\Profiler; use PHPUnit\DbUnit\DataSet\YamlDataSet; use PHPUnit\DbUnit\TestCaseTrait; diff --git a/tests/Util/VFSTrait.php b/tests/Util/VFSTrait.php index e9b7dfc509..110f24a61a 100644 --- a/tests/Util/VFSTrait.php +++ b/tests/Util/VFSTrait.php @@ -21,33 +21,34 @@ trait VFSTrait $structure = [ 'config' => [], 'bin' => [], - 'test' => [] + 'static' => [], + 'test' => [], ]; // create a virtual directory and copy all needed files and folders to it $this->root = vfsStream::setup('friendica', 0777, $structure); - $this->setConfigFile('defaults.config.php'); - $this->setConfigFile('settings.config.php'); + $this->setConfigFile('defaults.config.php', true); + $this->setConfigFile('settings.config.php', true); $this->setConfigFile('local.config.php'); - $this->setConfigFile('dbstructure.config.php'); } /** * Copying a config file from the file system to the Virtual File System * * @param string $filename The filename of the config file + * @param bool $static True, if the folder `static` instead of `config` should be used */ - protected function setConfigFile($filename) + protected function setConfigFile($filename, bool $static = false) { $file = dirname(__DIR__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . - 'config' . DIRECTORY_SEPARATOR . + ($static ? 'static' : 'config') . DIRECTORY_SEPARATOR . $filename; if (file_exists($file)) { vfsStream::newFile($filename) - ->at($this->root->getChild('config')) + ->at($this->root->getChild(($static ? 'static' : 'config'))) ->setContent(file_get_contents($file)); } } @@ -56,11 +57,12 @@ trait VFSTrait * Delets a config file from the Virtual File System * * @param string $filename The filename of the config file + * @param bool $static True, if the folder `static` instead of `config` should be used */ - protected function delConfigFile($filename) + protected function delConfigFile($filename, bool $static = false) { - if ($this->root->hasChild('config/' . $filename)) { - $this->root->getChild('config')->removeChild($filename); + if ($this->root->hasChild(($static ? 'static' : 'config') . '/' . $filename)) { + $this->root->getChild(($static ? 'static' : 'config'))->removeChild($filename); } } } diff --git a/tests/datasets/config/local.config.php b/tests/datasets/config/A.config.php similarity index 100% rename from tests/datasets/config/local.config.php rename to tests/datasets/config/A.config.php diff --git a/tests/datasets/config/local.ini.php b/tests/datasets/config/A.ini.php similarity index 100% rename from tests/datasets/config/local.ini.php rename to tests/datasets/config/A.ini.php diff --git a/tests/datasets/config/B.config.php b/tests/datasets/config/B.config.php new file mode 100644 index 0000000000..59fadcf55e --- /dev/null +++ b/tests/datasets/config/B.config.php @@ -0,0 +1,29 @@ + [ + 'hostname' => 'testhost', + 'username' => 'testuser', + 'password' => 'testpw', + 'database' => 'testdb', + 'charset' => 'utf8mb4', + ], + + 'config' => [ + 'admin_email' => 'admin@overwritten.local', + 'sitename' => 'Friendica Social Network', + 'register_policy' => \Friendica\Module\Register::OPEN, + 'register_text' => '', + ], + 'system' => [ + 'default_timezone' => 'UTC', + 'language' => 'en', + 'theme' => 'frio', + 'newKey' => 'newValue', + ], +]; diff --git a/tests/datasets/config/B.ini.php b/tests/datasets/config/B.ini.php new file mode 100644 index 0000000000..b9aa311a7e --- /dev/null +++ b/tests/datasets/config/B.ini.php @@ -0,0 +1,20 @@ +at($this->root->getChild('config')) @@ -105,7 +105,7 @@ class ConfigFileLoaderTest extends MockedTest '..' . DIRECTORY_SEPARATOR . 'datasets' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . - 'local.ini.php'; + 'A.ini.php'; vfsStream::newFile('local.ini.php') ->at($this->root->getChild('config')) @@ -185,7 +185,7 @@ class ConfigFileLoaderTest extends MockedTest '..' . DIRECTORY_SEPARATOR . 'datasets' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . - 'local.config.php'; + 'A.config.php'; vfsStream::newFile('test.config.php') ->at($this->root->getChild('addon')->getChild('test')->getChild('config')) @@ -202,4 +202,91 @@ class ConfigFileLoaderTest extends MockedTest $this->assertEquals('admin@test.it', $conf['config']['admin_email']); } + + /** + * test loading multiple config files - the last config should work + */ + public function testLoadMultipleConfigs() + { + $this->delConfigFile('local.config.php'); + + $fileDir = dirname(__DIR__) . DIRECTORY_SEPARATOR . + '..' . DIRECTORY_SEPARATOR . + '..' . DIRECTORY_SEPARATOR . + 'datasets' . DIRECTORY_SEPARATOR . + 'config' . DIRECTORY_SEPARATOR; + + vfsStream::newFile('A.config.php') + ->at($this->root->getChild('config')) + ->setContent(file_get_contents($fileDir . 'A.config.php')); + vfsStream::newFile('B.config.php') + ->at($this->root->getChild('config')) + ->setContent(file_get_contents($fileDir . 'B.config.php')); + + $configFileLoader = new ConfigFileLoader($this->root->url(), $this->mode); + $configCache = new ConfigCache(); + + $configFileLoader->setupCache($configCache); + + $this->assertEquals('admin@overwritten.local', $configCache->get('config', 'admin_email')); + $this->assertEquals('newValue', $configCache->get('system', 'newKey')); + } + + /** + * test loading multiple config files - the last config should work (INI-version) + */ + public function testLoadMultipleInis() + { + $this->delConfigFile('local.config.php'); + + $fileDir = dirname(__DIR__) . DIRECTORY_SEPARATOR . + '..' . DIRECTORY_SEPARATOR . + '..' . DIRECTORY_SEPARATOR . + 'datasets' . DIRECTORY_SEPARATOR . + 'config' . DIRECTORY_SEPARATOR; + + vfsStream::newFile('A.ini.php') + ->at($this->root->getChild('config')) + ->setContent(file_get_contents($fileDir . 'A.ini.php')); + vfsStream::newFile('B.ini.php') + ->at($this->root->getChild('config')) + ->setContent(file_get_contents($fileDir . 'B.ini.php')); + + $configFileLoader = new ConfigFileLoader($this->root->url(), $this->mode); + $configCache = new ConfigCache(); + + $configFileLoader->setupCache($configCache); + + $this->assertEquals('admin@overwritten.local', $configCache->get('config', 'admin_email')); + $this->assertEquals('newValue', $configCache->get('system', 'newKey')); + } + + /** + * Test that sample-files (e.g. local-sample.config.php) is never loaded + */ + public function testNotLoadingSamples() + { + $this->delConfigFile('local.config.php'); + + $fileDir = dirname(__DIR__) . DIRECTORY_SEPARATOR . + '..' . DIRECTORY_SEPARATOR . + '..' . DIRECTORY_SEPARATOR . + 'datasets' . DIRECTORY_SEPARATOR . + 'config' . DIRECTORY_SEPARATOR; + + vfsStream::newFile('A.ini.php') + ->at($this->root->getChild('config')) + ->setContent(file_get_contents($fileDir . 'A.ini.php')); + vfsStream::newFile('B-sample.ini.php') + ->at($this->root->getChild('config')) + ->setContent(file_get_contents($fileDir . 'B.ini.php')); + + $configFileLoader = new ConfigFileLoader($this->root->url(), $this->mode); + $configCache = new ConfigCache(); + + $configFileLoader->setupCache($configCache); + + $this->assertEquals('admin@test.it', $configCache->get('config', 'admin_email')); + $this->assertEmpty($configCache->get('system', 'NewKey')); + } } diff --git a/update.php b/update.php index 1bdccddeaa..18442f7958 100644 --- a/update.php +++ b/update.php @@ -22,7 +22,7 @@ use Friendica\Worker\Delivery; * This function is responsible for doing post update changes to the data * (not the structure) in the database. * - * Database structure changes are done in config/dbstructure.config.php + * Database structure changes are done in static/dbstructure.config.php * * If there is a need for a post process to a structure change, update this file * by adding a new function at the end with the number of the new DB_UPDATE_VERSION. @@ -33,8 +33,8 @@ use Friendica\Worker\Delivery; * You are currently on version 4711 and you are preparing changes that demand an update script. * * 1. Create a function "update_4712()" here in the update.php - * 2. Apply the needed structural changes in config/dbStructure.php - * 3. Set DB_UPDATE_VERSION in config/dbstructure.config.php to 4712. + * 2. Apply the needed structural changes in static/dbStructure.php + * 3. Set DB_UPDATE_VERSION in static/dbstructure.config.php to 4712. * * If you need to run a script before the database update, name the function "pre_update_4712()" */ diff --git a/view/templates/local.config.tpl b/view/templates/local.config.tpl index 05c87056c6..ad809c9d85 100644 --- a/view/templates/local.config.tpl +++ b/view/templates/local.config.tpl @@ -2,7 +2,7 @@ // Local configuration -// If you're unsure about what any of the config keys below do, please check the config/defaults.config.php for detailed +// If you're unsure about what any of the config keys below do, please check the static/defaults.config.php for detailed // documentation of their data type and behavior. return [ -- 2.39.5