Revert node.config.php into Config table

This commit is contained in:
Philipp 2023-02-05 00:15:01 +01:00
parent 6db89adc04
commit 513ef03421
No known key found for this signature in database
GPG key ID: 24A7501396EB5432
27 changed files with 425 additions and 829 deletions

View file

@ -1,139 +0,0 @@
<?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\Capability\ISetConfigValuesTransactionally;
use Friendica\Core\Config\Exception\ConfigFileException;
use Friendica\Core\Config\Exception\ConfigPersistenceException;
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;
}
/**
* Load all configuration values from a given cache and saves it back in the configuration node store
* @see ConfigFileManager::CONFIG_DATA_FILE
*
* All configuration values of the system are stored in the cache.
*
* @param Cache $cache a new cache
*
* @return void
*
* @throws ConfigPersistenceException In case the persistence layer throws errors
*/
public function setCacheAndSave(Cache $cache)
{
$this->configCache = $cache;
$this->save();
}
/**
* {@inheritDoc}
*/
public function getCache(): Cache
{
return $this->configCache;
}
/** {@inheritDoc} */
public function beginTransaction(): ISetConfigValuesTransactionally
{
return new ConfigTransaction($this);
}
/**
* Saves the current Configuration back into the data config.
* @see ConfigFileManager::CONFIG_DATA_FILE
*/
protected function save()
{
try {
$this->configFileManager->saveData($this->configCache);
// reload after the save to possible reload default values of lower source-priorities again
$this->reload();
} catch (ConfigFileException $e) {
throw new ConfigPersistenceException('Cannot save config', $e);
}
}
/** {@inheritDoc} */
public function reload()
{
$configCache = new Cache();
try {
$this->configFileManager->setupCache($configCache);
} catch (ConfigFileException $e) {
throw new ConfigPersistenceException('Cannot reload config', $e);
}
$this->configCache = $configCache;
}
/** {@inheritDoc} */
public function get(string $cat, string $key = null, $default_value = null)
{
return $this->configCache->get($cat, $key) ?? $default_value;
}
/** {@inheritDoc} */
public function set(string $cat, string $key, $value): bool
{
if ($this->configCache->set($cat, $key, $value, Cache::SOURCE_DATA)) {
$this->save();
return true;
} else {
return false;
}
}
/** {@inheritDoc} */
public function delete(string $cat, string $key): bool
{
if ($this->configCache->delete($cat, $key)) {
$this->save();
return true;
} else {
return false;
}
}
}

View file

@ -34,20 +34,23 @@ class ConfigTransaction implements ISetConfigValuesTransactionally
/** @var IManageConfigValues */
protected $config;
/** @var Cache */
protected $cache;
protected $setCache;
/** @var Cache */
protected $delCache;
/** @var bool field to check if something is to save */
protected $changedConfig = false;
public function __construct(IManageConfigValues $config)
public function __construct(DatabaseConfig $config)
{
$this->config = $config;
$this->cache = clone $config->getCache();
$this->config = $config;
$this->setCache = new Cache();
$this->delCache = new Cache();
}
/** {@inheritDoc} */
public function set(string $cat, string $key, $value): ISetConfigValuesTransactionally
{
$this->cache->set($cat, $key, $value, Cache::SOURCE_DATA);
$this->setCache->set($cat, $key, $value, Cache::SOURCE_DATA);
$this->changedConfig = true;
return $this;
@ -57,7 +60,7 @@ class ConfigTransaction implements ISetConfigValuesTransactionally
/** {@inheritDoc} */
public function delete(string $cat, string $key): ISetConfigValuesTransactionally
{
$this->cache->delete($cat, $key);
$this->delCache->delete($cat, $key);
$this->changedConfig = true;
return $this;
@ -72,8 +75,9 @@ class ConfigTransaction implements ISetConfigValuesTransactionally
}
try {
$this->config->setCacheAndSave($this->cache);
$this->cache = clone $this->config->getCache();
$this->config->setAndSave($this->setCache, $this->delCache);
$this->setCache = new Cache();
$this->delCache = new Cache();
} catch (\Exception $e) {
throw new ConfigPersistenceException('Cannot save config', $e);
}

View file

@ -0,0 +1,91 @@
<?php
namespace Friendica\Core\Config\Model;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\Config\Capability\ISetConfigValuesTransactionally;
use Friendica\Core\Config\Util\SerializeUtil;
use Friendica\Core\Config\ValueObject\Cache;
use Friendica\Database\Database;
/**
* Complete system configuration model, bound with the database
*/
class DatabaseConfig implements IManageConfigValues
{
/** @var Database */
protected $database;
/** @var Cache */
protected $cache;
public function __construct(Database $database, Cache $cache)
{
$this->database = $database;
$this->cache = $cache;
$this->reload();
}
/** {@inheritDoc} */
public function reload()
{
$config = $this->database->selectToArray('config');
foreach ($config as $entry) {
$this->cache->set($entry['cat'], $entry['k'], SerializeUtil::maybeUnserialize($entry['v']), Cache::SOURCE_DATA);
}
}
public function setAndSave(Cache $setCache, Cache $delCache): bool
{
$this->database->transaction();
foreach ($setCache->getAll() as $category => $data) {
foreach ($data as $key => $value) {
$this->cache->set($category, $key, $value, Cache::SOURCE_DATA);
$this->database->insert('config', ['cat' => $category, 'k' => $key, 'v' => serialize($value)], Database::INSERT_UPDATE);
}
}
foreach ($delCache->getAll() as $category => $keys) {
foreach ($keys as $key => $value) {
$this->cache->delete($category, $key);
$this->database->delete('config', ['cat' => $category, 'k' => $key]);
}
}
return $this->database->commit();
}
/** {@inheritDoc} */
public function get(string $cat, string $key = null, $default_value = null)
{
return $this->cache->get($cat, $key) ?? $default_value;
}
/** {@inheritDoc} */
public function set(string $cat, string $key, $value): bool
{
$this->cache->set($cat, $key, $value, Cache::SOURCE_DATA);
return $this->database->insert('config', ['cat' => $cat, 'k' => $key, 'v' => serialize($value)], Database::INSERT_UPDATE);
}
/** {@inheritDoc} */
public function beginTransaction(): ISetConfigValuesTransactionally
{
return new ConfigTransaction($this);
}
/** {@inheritDoc} */
public function delete(string $cat, string $key): bool
{
$this->cache->delete($cat, $key);
return $this->database->delete('config', ['cat' => $cat, 'k' => $key]);
}
/** {@inheritDoc} */
public function getCache(): Cache
{
return $this->cache;
}
}

View file

@ -0,0 +1,82 @@
<?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\Capability\ISetConfigValuesTransactionally;
use Friendica\Core\Config\Exception\ConfigPersistenceException;
use Friendica\Core\Config\ValueObject\Cache;
/**
* Creates a basic, readonly model for the file-based configuration
*/
class ReadOnlyFileConfig implements IManageConfigValues
{
/** @var Cache */
protected $configCache;
/**
* @param Cache $configCache The configuration cache (based on the config-files)
*/
public function __construct(Cache $configCache)
{
$this->configCache = $configCache;
}
/**
* {@inheritDoc}
*/
public function getCache(): Cache
{
return $this->configCache;
}
/** {@inheritDoc} */
public function beginTransaction(): ISetConfigValuesTransactionally
{
throw new ConfigPersistenceException('beginTransaction not allowed.');
}
/** {@inheritDoc} */
public function reload()
{
throw new ConfigPersistenceException('reload not allowed.');
}
/** {@inheritDoc} */
public function get(string $cat, string $key = null, $default_value = null)
{
return $this->configCache->get($cat, $key) ?? $default_value;
}
/** {@inheritDoc} */
public function set(string $cat, string $key, $value): bool
{
throw new ConfigPersistenceException('set not allowed.');
}
/** {@inheritDoc} */
public function delete(string $cat, string $key): bool
{
throw new ConfigPersistenceException('Save not allowed');
}
}