]> git.mxchange.org Git - friendica.git/commitdiff
Introduce lightweight Config model
authorPhilipp <admin@philipp.info>
Tue, 27 Dec 2022 23:18:29 +0000 (00:18 +0100)
committerPhilipp <admin@philipp.info>
Tue, 3 Jan 2023 13:22:02 +0000 (14:22 +0100)
src/Core/Config/Model/Config.php [new file with mode: 0644]
static/dependencies.config.php

diff --git a/src/Core/Config/Model/Config.php b/src/Core/Config/Model/Config.php
new file mode 100644 (file)
index 0000000..3af2f7e
--- /dev/null
@@ -0,0 +1,98 @@
+<?php
+/**
+ * @copyright Copyright (C) 2010-2023, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Core\Config\Model;
+
+use Friendica\Core\Config\Capability\IManageConfigValues;
+use Friendica\Core\Config\Util\ConfigFileManager;
+use Friendica\Core\Config\ValueObject\Cache;
+
+/**
+ * Configuration model, which manages the whole system configuration
+ */
+class Config implements IManageConfigValues
+{
+       /**
+        * @var Cache
+        */
+       protected $configCache;
+
+       /** @var ConfigFileManager */
+       protected $configFileManager;
+
+       /**
+        * @param ConfigFileManager $configFileManager The configuration file manager to save back configs
+        * @param Cache  $configCache The configuration cache (based on the config-files)
+        */
+       public function __construct(ConfigFileManager $configFileManager, Cache $configCache)
+       {
+               $this->configFileManager = $configFileManager;
+               $this->configCache       = $configCache;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public function getCache(): Cache
+       {
+               return $this->configCache;
+       }
+
+       public function save()
+       {
+               $this->configFileManager->saveData($this->configCache);
+       }
+
+       public function load(string $cat = 'config')
+       {
+               $configCache = new Cache();
+
+               $this->configFileManager->setupCache($configCache, $_SERVER);
+               $this->configCache = $configCache;
+       }
+
+       public function get(string $cat, string $key, $default_value = null, bool $refresh = false)
+       {
+               return $this->configCache->get($cat, $key) ?? $default_value;
+       }
+
+       public function set(string $cat, string $key, $value, bool $autosave = true): bool
+       {
+               $stored = $this->configCache->set($cat, $key, $value, Cache::SOURCE_DATA);
+
+               if ($stored && $autosave) {
+                       $this->save();
+               }
+
+               return $stored;
+       }
+
+       public function delete(string $cat, string $key, bool $autosave = true): bool
+       {
+               $removed = $this->configCache->delete($cat, $key);
+
+               if ($removed && $autosave) {
+                       $this->save();
+               }
+
+               return $removed;
+       }
+}
index 08867d86d947ba55f6ed6d271724b3f7b7253602..90f01feede02ecfdc68cc29b85a3c47a69299863 100644 (file)
@@ -98,10 +98,7 @@ return [
                ],
        ],
        Config\Capability\IManageConfigValues::class => [
-               'instanceOf' => Config\Factory\Config::class,
-               'call'       => [
-                       ['create', [], Dice::CHAIN_CALL],
-               ],
+               'instanceOf' => Config\Model\Config::class,
        ],
        PConfig\Capability\IManagePersonalConfigValues::class => [
                'instanceOf' => PConfig\Factory\PConfig::class,