2019-07-03 00:42:47 +02:00
|
|
|
<?php
|
2020-02-09 15:45:36 +01:00
|
|
|
/**
|
2021-03-29 08:40:20 +02:00
|
|
|
* @copyright Copyright (C) 2010-2021, the Friendica project
|
2020-02-09 15:45:36 +01:00
|
|
|
*
|
|
|
|
* @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/>.
|
|
|
|
*
|
|
|
|
*/
|
2019-07-03 00:42:47 +02:00
|
|
|
|
|
|
|
namespace Friendica\Core\Config;
|
|
|
|
|
2020-01-19 21:29:36 +01:00
|
|
|
use Friendica\Core\BaseConfig;
|
2019-07-03 00:42:47 +02:00
|
|
|
use Friendica\Model;
|
|
|
|
|
|
|
|
/**
|
2019-07-15 20:54:33 +02:00
|
|
|
* This class implements the preload configuration, which will cache
|
2019-07-14 22:41:09 +02:00
|
|
|
* all config values per call in a cache.
|
|
|
|
*
|
|
|
|
* Minimizes the number of database queries to retrieve configuration values at the cost of memory.
|
2019-07-03 00:42:47 +02:00
|
|
|
*/
|
2020-01-19 21:29:36 +01:00
|
|
|
class PreloadConfig extends BaseConfig
|
2019-07-03 00:42:47 +02:00
|
|
|
{
|
|
|
|
/** @var bool */
|
|
|
|
private $config_loaded;
|
|
|
|
|
|
|
|
/**
|
2020-01-19 22:23:44 +01:00
|
|
|
* @param Cache $configCache The configuration cache (based on the config-files)
|
2019-07-03 00:42:47 +02:00
|
|
|
* @param Model\Config\Config $configModel The configuration model
|
|
|
|
*/
|
2020-01-19 22:23:44 +01:00
|
|
|
public function __construct(Cache $configCache, Model\Config\Config $configModel)
|
2019-07-03 00:42:47 +02:00
|
|
|
{
|
|
|
|
parent::__construct($configCache, $configModel);
|
|
|
|
$this->config_loaded = false;
|
|
|
|
|
|
|
|
$this->load();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*
|
|
|
|
* This loads all config values everytime load is called
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function load(string $cat = 'config')
|
|
|
|
{
|
|
|
|
// Don't load the whole configuration twice
|
|
|
|
if ($this->config_loaded) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not connected, do nothing
|
|
|
|
if (!$this->configModel->isConnected()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$config = $this->configModel->load();
|
|
|
|
$this->config_loaded = true;
|
|
|
|
|
|
|
|
// load the whole category out of the DB into the cache
|
2020-10-04 20:37:35 +02:00
|
|
|
$this->configCache->load($config, Cache::SOURCE_DB);
|
2019-07-03 00:42:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function get(string $cat, string $key, $default_value = null, bool $refresh = false)
|
|
|
|
{
|
|
|
|
if ($refresh) {
|
|
|
|
if ($this->configModel->isConnected()) {
|
|
|
|
$config = $this->configModel->get($cat, $key);
|
|
|
|
if (isset($config)) {
|
2021-03-28 23:25:16 +02:00
|
|
|
$this->configCache->set($cat, $key, $config, Cache::SOURCE_DB);
|
2019-07-03 00:42:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// use the config cache for return
|
|
|
|
$result = $this->configCache->get($cat, $key);
|
|
|
|
|
|
|
|
return (isset($result)) ? $result : $default_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function set(string $cat, string $key, $value)
|
|
|
|
{
|
|
|
|
if (!$this->config_loaded) {
|
|
|
|
$this->load();
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the cache first
|
2021-03-28 23:25:16 +02:00
|
|
|
$cached = $this->configCache->set($cat, $key, $value, Cache::SOURCE_DB);
|
2019-07-03 00:42:47 +02:00
|
|
|
|
|
|
|
// If there is no connected adapter, we're finished
|
|
|
|
if (!$this->configModel->isConnected()) {
|
|
|
|
return $cached;
|
|
|
|
}
|
|
|
|
|
|
|
|
$stored = $this->configModel->set($cat, $key, $value);
|
|
|
|
|
|
|
|
return $cached && $stored;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function delete(string $cat, string $key)
|
|
|
|
{
|
|
|
|
if ($this->config_loaded) {
|
|
|
|
$this->load();
|
|
|
|
}
|
|
|
|
|
|
|
|
$cacheRemoved = $this->configCache->delete($cat, $key);
|
|
|
|
|
|
|
|
if (!$this->configModel->isConnected()) {
|
|
|
|
return $cacheRemoved;
|
|
|
|
}
|
|
|
|
|
|
|
|
$storeRemoved = $this->configModel->delete($cat, $key);
|
|
|
|
|
|
|
|
return $cacheRemoved || $storeRemoved;
|
|
|
|
}
|
2021-03-28 23:25:16 +02:00
|
|
|
|
|
|
|
public function testSetDouble()
|
|
|
|
{
|
|
|
|
$this->configModel->shouldReceive('isConnected')
|
|
|
|
->andReturn(true);
|
|
|
|
|
|
|
|
// constructor loading
|
|
|
|
$this->configModel->shouldReceive('load')
|
|
|
|
->with('config')
|
|
|
|
->andReturn(['config' => ['test' => 'it']])
|
|
|
|
->once();
|
|
|
|
|
|
|
|
parent::testSetDouble();
|
|
|
|
}
|
2019-07-03 00:42:47 +02:00
|
|
|
}
|