Refactor IManageConfigValues interface
authorPhilipp <admin@philipp.info>
Wed, 28 Dec 2022 01:07:38 +0000 (02:07 +0100)
committerPhilipp <admin@philipp.info>
Tue, 3 Jan 2023 13:22:04 +0000 (14:22 +0100)
13 files changed:
bin/daemon.php
src/App.php
src/Console/Config.php
src/Core/Config/Capability/IManageConfigValues.php
src/Core/Config/Factory/Config.php
src/Core/Config/Model/Config.php
src/Core/Update.php
src/Core/Worker.php
src/Module/Friendica.php
src/Worker/DBUpdate.php
static/dependencies.config.php
update.php
view/theme/frio/style.php

index 385f725..f9ed693 100755 (executable)
@@ -71,7 +71,7 @@ if (DI::mode()->isInstall()) {
 
 DI::mode()->setExecutor(Mode::DAEMON);
 
-DI::config()->load();
+DI::config()->reload();
 
 if (empty(DI::config()->get('system', 'pidfile'))) {
        die(<<<TXT
index c7c6c35..18a6967 100644 (file)
@@ -296,8 +296,7 @@ class App
         */
        public function getBasePath(): string
        {
-               // Don't use the basepath of the config table for basepath (it should always be the config-file one)
-               return $this->config->getCache()->get('system', 'basepath');
+               return $this->config->get('system', 'basepath');
        }
 
        /**
index b57cdbe..8df7c28 100644 (file)
@@ -157,7 +157,7 @@ HELP;
 
                if (count($this->args) == 1) {
                        $cat = $this->getArgument(0);
-                       $this->config->load($cat);
+                       $this->config->reload();
                        $configCache = $this->config->getCache();
 
                        if ($configCache->get($cat) !== null) {
@@ -178,7 +178,7 @@ HELP;
                }
 
                if (count($this->args) == 0) {
-                       $this->config->load();
+                       $this->config->reload();
 
                        if ($this->config->get('system', 'config_adapter') == 'jit' && $this->appMode->has(App\Mode::DBCONFIGAVAILABLE)) {
                                $this->out('Warning: The JIT (Just In Time) Config adapter doesn\'t support loading the entire configuration, showing file config only');
index 2723882..88fa963 100644 (file)
@@ -30,36 +30,32 @@ use Friendica\Core\Config\ValueObject\Cache;
 interface IManageConfigValues
 {
        /**
-        * Loads all configuration values of family into a cached storage.
+        * Reloads all configuration values (from filesystem and environment variables)
         *
         * All configuration values of the system are stored in the cache.
         *
-        * @param string $cat The category of the configuration value
-        *
         * @return void
         *
         * @throws ConfigPersistenceException In case the persistence layer throws errors
         */
-       public function load(string $cat = 'config');
+       public function reload();
 
        /**
         * Get a particular user's config variable given the category name
         * ($cat) and a $key.
         *
         * Get a particular config value from the given category ($cat)
-        * and the $key from a cached storage either from the database or from the cache.
         *
         * @param string  $cat        The category of the configuration value
         * @param string  $key           The configuration key to query
         * @param mixed   $default_value Deprecated, use `Config->get($cat, $key, null, $refresh) ?? $default_value` instead
-        * @param boolean $refresh       optional, If true the config is loaded from the db and not from the cache (default: false)
         *
         * @return mixed Stored value or null if it does not exist
         *
         * @throws ConfigPersistenceException In case the persistence layer throws errors
         *
         */
-       public function get(string $cat, string $key, $default_value = null, bool $refresh = false);
+       public function get(string $cat, string $key, $default_value = null);
 
        /**
         * Sets a configuration value for system config
@@ -81,6 +77,8 @@ interface IManageConfigValues
 
        /**
         * Save back the overridden values of the config cache
+        *
+        * @throws ConfigPersistenceException In case the persistence layer throws errors
         */
        public function save();
 
index fac931f..e1094fd 100644 (file)
@@ -81,21 +81,4 @@ class Config
 
                return $configCache;
        }
-
-       /**
-        * @param Cache $configCache The config cache of this adapter
-        * @param Repository\Config $configRepo  The configuration repository
-        *
-        * @return Capability\IManageConfigValues
-        */
-       public function create(Util\ConfigFileManager $loader, Cache $configCache, Repository\Config $configRepo)
-       {
-               if ($configCache->get('system', 'config_adapter') === 'preload') {
-                       $configuration = new Type\PreloadConfig($loader, $configCache, $configRepo);
-               } else {
-                       $configuration = new Type\JitConfig($loader, $configCache, $configRepo);
-               }
-
-               return $configuration;
-       }
 }
index 3af2f7e..3de9715 100644 (file)
@@ -22,6 +22,8 @@
 namespace Friendica\Core\Config\Model;
 
 use Friendica\Core\Config\Capability\IManageConfigValues;
+use Friendica\Core\Config\Exception\ConfigFileException;
+use Friendica\Core\Config\Exception\ConfigPersistenceException;
 use Friendica\Core\Config\Util\ConfigFileManager;
 use Friendica\Core\Config\ValueObject\Cache;
 
@@ -38,14 +40,19 @@ class Config implements IManageConfigValues
        /** @var ConfigFileManager */
        protected $configFileManager;
 
+       /** @var array */
+       protected $server;
+
        /**
         * @param ConfigFileManager $configFileManager The configuration file manager to save back configs
-        * @param Cache  $configCache The configuration cache (based on the config-files)
+        * @param Cache             $configCache       The configuration cache (based on the config-files)
+        * @param array             $server            The $_SERVER variable
         */
-       public function __construct(ConfigFileManager $configFileManager, Cache $configCache)
+       public function __construct(ConfigFileManager $configFileManager, Cache $configCache, array $server = [])
        {
                $this->configFileManager = $configFileManager;
                $this->configCache       = $configCache;
+               $this->server            = $server;
        }
 
        /**
@@ -56,24 +63,36 @@ class Config implements IManageConfigValues
                return $this->configCache;
        }
 
+       /** {@inheritDoc} */
        public function save()
        {
-               $this->configFileManager->saveData($this->configCache);
+               try {
+                       $this->configFileManager->saveData($this->configCache);
+               } catch (ConfigFileException $e) {
+                       throw new ConfigPersistenceException('Cannot save config', $e);
+               }
        }
 
-       public function load(string $cat = 'config')
+       /** {@inheritDoc} */
+       public function reload()
        {
                $configCache = new Cache();
 
-               $this->configFileManager->setupCache($configCache, $_SERVER);
+               try {
+                       $this->configFileManager->setupCache($configCache, $this->server);
+               } catch (ConfigFileException $e) {
+                       throw new ConfigPersistenceException('Cannot reload config', $e);
+               }
                $this->configCache = $configCache;
        }
 
-       public function get(string $cat, string $key, $default_value = null, bool $refresh = false)
+       /** {@inheritDoc} */
+       public function get(string $cat, string $key, $default_value = null)
        {
                return $this->configCache->get($cat, $key) ?? $default_value;
        }
 
+       /** {@inheritDoc} */
        public function set(string $cat, string $key, $value, bool $autosave = true): bool
        {
                $stored = $this->configCache->set($cat, $key, $value, Cache::SOURCE_DATA);
@@ -85,6 +104,7 @@ class Config implements IManageConfigValues
                return $stored;
        }
 
+       /** {@inheritDoc} */
        public function delete(string $cat, string $key, bool $autosave = true): bool
        {
                $removed = $this->configCache->delete($cat, $key);
index 8da4738..9a2ebe1 100644 (file)
@@ -54,7 +54,7 @@ class Update
                }
 
                // Don't check the status if the last update was failed
-               if (DI::config()->get('system', 'update', Update::SUCCESS, true) == Update::FAILED) {
+               if (DI::config()->get('system', 'update', Update::SUCCESS) == Update::FAILED) {
                        return;
                }
 
@@ -119,7 +119,7 @@ class Update
                        DI::lock()->release('dbupdate', true);
                }
 
-               $build = DI::config()->get('system', 'build', null, true);
+               $build = DI::config()->get('system', 'build', null);
 
                if (empty($build) || ($build > DB_UPDATE_VERSION)) {
                        $build = DB_UPDATE_VERSION - 1;
@@ -132,7 +132,7 @@ class Update
                        $stored = intval($build);
                        $current = intval(DB_UPDATE_VERSION);
                        if ($stored < $current || $force) {
-                               DI::config()->load('database');
+                               DI::config()->reload();
 
                                // Compare the current structure with the defined structure
                                // If the Lock is acquired, never release it automatically to avoid double updates
@@ -141,7 +141,7 @@ class Update
                                        Logger::notice('Update starting.', ['from' => $stored, 'to' => $current]);
 
                                        // Checks if the build changed during Lock acquiring (so no double update occurs)
-                                       $retryBuild = DI::config()->get('system', 'build', null, true);
+                                       $retryBuild = DI::config()->get('system', 'build', null);
                                        if ($retryBuild !== $build) {
                                                Logger::notice('Update already done.', ['from' => $stored, 'to' => $current]);
                                                DI::lock()->release('dbupdate');
index 8fc74f1..2db9256 100644 (file)
@@ -331,7 +331,7 @@ class Worker
                $mypid = getmypid();
 
                // Quit when in maintenance
-               if (DI::config()->get('system', 'maintenance', false, true)) {
+               if (DI::config()->get('system', 'maintenance', false)) {
                        Logger::notice('Maintenance mode - quit process', ['pid' => $mypid]);
                        return false;
                }
index 72360dd..bbcccd7 100644 (file)
@@ -157,7 +157,7 @@ class Friendica extends BaseModule
 
                $visible_addons = Addon::getVisibleList();
 
-               $config->load('feature_lock');
+               $config->reload();
                $locked_features = [];
                $featureLocks = $config->get('config', 'feature_lock');
                if (isset($featureLocks)) {
index e11f7bf..7b7c3b8 100644 (file)
@@ -32,7 +32,7 @@ class DBUpdate
        public static function execute()
        {
                // Just in case the last update wasn't failed
-               if (DI::config()->get('system', 'update', Update::SUCCESS, true) != Update::FAILED) {
+               if (DI::config()->get('system', 'update', Update::SUCCESS) != Update::FAILED) {
                        Update::run(DI::app()->getBasePath());
                }
        }
index 90f01fe..1844001 100644 (file)
@@ -99,6 +99,9 @@ return [
        ],
        Config\Capability\IManageConfigValues::class => [
                'instanceOf' => Config\Model\Config::class,
+               'constructParams' => [
+                       $_SERVER,
+               ],
        ],
        PConfig\Capability\IManagePersonalConfigValues::class => [
                'instanceOf' => PConfig\Factory\PConfig::class,
index fda04b6..b9edbab 100644 (file)
@@ -1178,12 +1178,6 @@ function update_1505()
 
 function update_1508()
 {
-       $categories = DBA::toArray(DBA::p("SELECT DISTINCT `cat` AS 'cat' FROM `config`"));
-
-       foreach ($categories as $category) {
-               DI::config()->load($category['cat']);
-       }
-
        $config = DBA::selectToArray('config');
 
        foreach ($config as $entry) {
index 6e05c6b..8feefdc 100644 (file)
@@ -49,7 +49,7 @@ $login_bg_color   = '';
 $modified         = time();
 
 if (DI::mode()->has(\Friendica\App\Mode::MAINTENANCEDISABLED)) {
-       DI::config()->load('frio');
+       DI::config()->reload('frio');
 
        // Default to hard-coded values for empty settings
        $scheme           = DI::config()->get('frio', 'scheme', DI::config()->get('frio', 'schema'));