2019-07-03 00:42:47 +02:00
|
|
|
<?php
|
2020-02-09 15:45:36 +01:00
|
|
|
/**
|
2022-01-02 08:27:47 +01:00
|
|
|
* @copyright Copyright (C) 2010-2022, 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
|
|
|
|
2021-10-23 11:29:16 +02:00
|
|
|
namespace Friendica\Core\Config\Type;
|
2019-07-03 00:42:47 +02:00
|
|
|
|
2021-10-26 21:44:29 +02:00
|
|
|
use Friendica\Core\Config\ValueObject\Cache;
|
|
|
|
use Friendica\Core\Config\Repository\Config;
|
2019-07-03 00:42:47 +02:00
|
|
|
|
|
|
|
/**
|
2019-07-14 22:41:09 +02:00
|
|
|
* This class implements the Just-In-Time configuration, which will cache
|
|
|
|
* config values in a cache, once they are retrieved.
|
|
|
|
*
|
|
|
|
* Default Configuration type.
|
|
|
|
* Provides the best performance for pages loading few configuration variables.
|
2019-07-03 00:42:47 +02:00
|
|
|
*/
|
2021-10-26 21:44:29 +02:00
|
|
|
class JitConfig extends AbstractConfig
|
2019-07-03 00:42:47 +02:00
|
|
|
{
|
2019-07-15 08:58:37 +02:00
|
|
|
/**
|
|
|
|
* @var array Array of already loaded db values (even if there was no value)
|
|
|
|
*/
|
|
|
|
private $db_loaded;
|
2019-07-03 00:42:47 +02:00
|
|
|
|
|
|
|
/**
|
2021-10-26 21:44:29 +02:00
|
|
|
* @param Cache $configCache The configuration cache (based on the config-files)
|
|
|
|
* @param Config $configRepo The configuration model
|
2019-07-03 00:42:47 +02:00
|
|
|
*/
|
2021-10-26 21:44:29 +02:00
|
|
|
public function __construct(Cache $configCache, Config $configRepo)
|
2019-07-03 00:42:47 +02:00
|
|
|
{
|
2021-10-26 21:44:29 +02:00
|
|
|
parent::__construct($configCache, $configRepo);
|
2019-07-15 08:58:37 +02:00
|
|
|
$this->db_loaded = [];
|
2019-07-03 00:42:47 +02:00
|
|
|
|
|
|
|
$this->load();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function load(string $cat = 'config')
|
|
|
|
{
|
|
|
|
// If not connected, do nothing
|
2021-10-26 21:44:29 +02:00
|
|
|
if (!$this->configRepo->isConnected()) {
|
2019-07-03 00:42:47 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-26 21:44:29 +02:00
|
|
|
$config = $this->configRepo->load($cat);
|
2019-07-03 00:42:47 +02:00
|
|
|
|
2019-07-04 21:39:49 +02:00
|
|
|
if (!empty($config[$cat])) {
|
|
|
|
foreach ($config[$cat] as $key => $value) {
|
2019-07-15 08:58:37 +02:00
|
|
|
$this->db_loaded[$cat][$key] = true;
|
2019-07-04 21:39:49 +02:00
|
|
|
}
|
2019-07-03 00:42:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 the value isn't loaded or refresh is needed, load it to the cache
|
2021-10-26 21:44:29 +02:00
|
|
|
if ($this->configRepo->isConnected() &&
|
|
|
|
(empty($this->db_loaded[$cat][$key]) ||
|
|
|
|
$refresh)) {
|
|
|
|
$dbValue = $this->configRepo->get($cat, $key);
|
|
|
|
|
|
|
|
if (isset($dbValue)) {
|
|
|
|
$this->configCache->set($cat, $key, $dbValue, Cache::SOURCE_DB);
|
|
|
|
unset($dbValue);
|
2019-07-03 00:42:47 +02:00
|
|
|
}
|
2019-07-15 08:58:37 +02:00
|
|
|
|
|
|
|
$this->db_loaded[$cat][$key] = true;
|
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}
|
|
|
|
*/
|
2021-10-26 21:44:29 +02:00
|
|
|
public function set(string $cat, string $key, $value): bool
|
2019-07-03 00:42:47 +02:00
|
|
|
{
|
|
|
|
// 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
|
2021-10-26 21:44:29 +02:00
|
|
|
if (!$this->configRepo->isConnected()) {
|
2019-07-03 00:42:47 +02:00
|
|
|
return $cached;
|
|
|
|
}
|
|
|
|
|
2021-10-26 21:44:29 +02:00
|
|
|
$stored = $this->configRepo->set($cat, $key, $value);
|
2019-07-03 00:42:47 +02:00
|
|
|
|
2019-07-15 08:58:37 +02:00
|
|
|
$this->db_loaded[$cat][$key] = $stored;
|
2019-07-03 00:42:47 +02:00
|
|
|
|
|
|
|
return $cached && $stored;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-07-14 22:41:09 +02:00
|
|
|
* {@inheritDoc}
|
2019-07-03 00:42:47 +02:00
|
|
|
*/
|
2021-10-26 21:44:29 +02:00
|
|
|
public function delete(string $cat, string $key): bool
|
2019-07-03 00:42:47 +02:00
|
|
|
{
|
|
|
|
$cacheRemoved = $this->configCache->delete($cat, $key);
|
|
|
|
|
2019-07-15 08:58:37 +02:00
|
|
|
if (isset($this->db_loaded[$cat][$key])) {
|
|
|
|
unset($this->db_loaded[$cat][$key]);
|
2019-07-03 00:42:47 +02:00
|
|
|
}
|
|
|
|
|
2021-10-26 21:44:29 +02:00
|
|
|
if (!$this->configRepo->isConnected()) {
|
2019-07-03 00:42:47 +02:00
|
|
|
return $cacheRemoved;
|
|
|
|
}
|
|
|
|
|
2021-10-26 21:44:29 +02:00
|
|
|
$storeRemoved = $this->configRepo->delete($cat, $key);
|
2019-07-03 00:42:47 +02:00
|
|
|
|
|
|
|
return $cacheRemoved || $storeRemoved;
|
|
|
|
}
|
|
|
|
}
|