Config FollowUp
- New Configuration (Config is now only holding the instance) - New PConfiguration (PConfig is now only holding the instance) - Config & PConfig-Adapter don't need "ConfigCache" anymore - DB-Connection is now outside App->reload() for better dependency-chaining
This commit is contained in:
parent
c36a0eabdb
commit
eafcf3592d
59 changed files with 1754 additions and 1038 deletions
|
@ -33,7 +33,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use Friendica\App;
|
use Friendica\App;
|
||||||
use Friendica\Core\Config;
|
use Friendica\Core\Config\Cache;
|
||||||
use Friendica\Factory;
|
use Friendica\Factory;
|
||||||
use Friendica\Util\BasePath;
|
use Friendica\Util\BasePath;
|
||||||
use Friendica\Util\ExAuth;
|
use Friendica\Util\ExAuth;
|
||||||
|
@ -55,8 +55,11 @@ chdir($directory);
|
||||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||||
|
|
||||||
$basedir = BasePath::create(dirname(__DIR__), $_SERVER);
|
$basedir = BasePath::create(dirname(__DIR__), $_SERVER);
|
||||||
$configLoader = new Config\ConfigCacheLoader($basedir);
|
$configLoader = new Cache\ConfigCacheLoader($basedir);
|
||||||
$config = Factory\ConfigFactory::createCache($configLoader);
|
$configCache = Factory\ConfigFactory::createCache($configLoader);
|
||||||
|
Factory\DBFactory::init($configCache, $_SERVER);
|
||||||
|
$config = Factory\ConfigFactory::createConfig($configCache);
|
||||||
|
$pconfig = Factory\ConfigFactory::createPConfig($configCache);
|
||||||
$logger = Factory\LoggerFactory::create('auth_ejabberd', $config);
|
$logger = Factory\LoggerFactory::create('auth_ejabberd', $config);
|
||||||
$profiler = Factory\ProfilerFactory::create($logger, $config);
|
$profiler = Factory\ProfilerFactory::create($logger, $config);
|
||||||
|
|
||||||
|
|
|
@ -3,13 +3,16 @@
|
||||||
|
|
||||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||||
|
|
||||||
use Friendica\Core\Config;
|
use Friendica\Core\Config\Cache;
|
||||||
use Friendica\Factory;
|
use Friendica\Factory;
|
||||||
use Friendica\Util\BasePath;
|
use Friendica\Util\BasePath;
|
||||||
|
|
||||||
$basedir = BasePath::create(dirname(__DIR__), $_SERVER);
|
$basedir = BasePath::create(dirname(__DIR__), $_SERVER);
|
||||||
$configLoader = new Config\ConfigCacheLoader($basedir);
|
$configLoader = new Cache\ConfigCacheLoader($basedir);
|
||||||
$config = Factory\ConfigFactory::createCache($configLoader);
|
$configCache = Factory\ConfigFactory::createCache($configLoader);
|
||||||
|
Factory\DBFactory::init($configCache, $_SERVER);
|
||||||
|
$config = Factory\ConfigFactory::createConfig($configCache);
|
||||||
|
$pconfig = Factory\ConfigFactory::createPConfig($configCache);
|
||||||
$logger = Factory\LoggerFactory::create('console', $config);
|
$logger = Factory\LoggerFactory::create('console', $config);
|
||||||
$profiler = Factory\ProfilerFactory::create($logger, $config);
|
$profiler = Factory\ProfilerFactory::create($logger, $config);
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
|
|
||||||
use Friendica\App;
|
use Friendica\App;
|
||||||
use Friendica\Core\Config;
|
use Friendica\Core\Config;
|
||||||
|
use Friendica\Core\Config\Cache;
|
||||||
use Friendica\Core\Worker;
|
use Friendica\Core\Worker;
|
||||||
use Friendica\Database\DBA;
|
use Friendica\Database\DBA;
|
||||||
use Friendica\Factory;
|
use Friendica\Factory;
|
||||||
|
@ -34,8 +35,11 @@ if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) {
|
||||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||||
|
|
||||||
$basedir = BasePath::create(dirname(__DIR__), $_SERVER);
|
$basedir = BasePath::create(dirname(__DIR__), $_SERVER);
|
||||||
$configLoader = new Config\ConfigCacheLoader($basedir);
|
$configLoader = new Cache\ConfigCacheLoader($basedir);
|
||||||
$config = Factory\ConfigFactory::createCache($configLoader);
|
$configCache = Factory\ConfigFactory::createCache($configLoader);
|
||||||
|
Factory\DBFactory::init($configCache, $_SERVER);
|
||||||
|
$config = Factory\ConfigFactory::createConfig($configCache);
|
||||||
|
$pconfig = Factory\ConfigFactory::createPConfig($configCache);
|
||||||
$logger = Factory\LoggerFactory::create('daemon', $config);
|
$logger = Factory\LoggerFactory::create('daemon', $config);
|
||||||
$profiler = Factory\ProfilerFactory::create($logger, $config);
|
$profiler = Factory\ProfilerFactory::create($logger, $config);
|
||||||
|
|
||||||
|
@ -151,7 +155,7 @@ if (!$foreground) {
|
||||||
file_put_contents($pidfile, $pid);
|
file_put_contents($pidfile, $pid);
|
||||||
|
|
||||||
// We lose the database connection upon forking
|
// We lose the database connection upon forking
|
||||||
$a->loadDatabase();
|
Factory\DBFactory::init($configCache, $_SERVER);
|
||||||
}
|
}
|
||||||
|
|
||||||
Config::set('system', 'worker_daemon_mode', true);
|
Config::set('system', 'worker_daemon_mode', true);
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
|
|
||||||
use Friendica\App;
|
use Friendica\App;
|
||||||
use Friendica\Core\Config;
|
use Friendica\Core\Config;
|
||||||
|
use Friendica\Core\Config\Cache;
|
||||||
use Friendica\Core\Update;
|
use Friendica\Core\Update;
|
||||||
use Friendica\Core\Worker;
|
use Friendica\Core\Worker;
|
||||||
use Friendica\Factory;
|
use Friendica\Factory;
|
||||||
|
@ -32,8 +33,11 @@ if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) {
|
||||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||||
|
|
||||||
$basedir = BasePath::create(dirname(__DIR__), $_SERVER);
|
$basedir = BasePath::create(dirname(__DIR__), $_SERVER);
|
||||||
$configLoader = new Config\ConfigCacheLoader($basedir);
|
$configLoader = new Cache\ConfigCacheLoader($basedir);
|
||||||
$config = Factory\ConfigFactory::createCache($configLoader);
|
$configCache = Factory\ConfigFactory::createCache($configLoader);
|
||||||
|
Factory\DBFactory::init($configCache, $_SERVER);
|
||||||
|
$config = Factory\ConfigFactory::createConfig($configCache);
|
||||||
|
$pconfig = Factory\ConfigFactory::createPConfig($configCache);
|
||||||
$logger = Factory\LoggerFactory::create('worker', $config);
|
$logger = Factory\LoggerFactory::create('worker', $config);
|
||||||
$profiler = Factory\ProfilerFactory::create($logger, $config);
|
$profiler = Factory\ProfilerFactory::create($logger, $config);
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use Friendica\App;
|
use Friendica\App;
|
||||||
use Friendica\Core\Config;
|
use Friendica\Core\Config\Cache;
|
||||||
use Friendica\Factory;
|
use Friendica\Factory;
|
||||||
use Friendica\Util\BasePath;
|
use Friendica\Util\BasePath;
|
||||||
|
|
||||||
|
@ -16,8 +16,11 @@ if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
|
||||||
require __DIR__ . '/vendor/autoload.php';
|
require __DIR__ . '/vendor/autoload.php';
|
||||||
|
|
||||||
$basedir = BasePath::create(__DIR__, $_SERVER);
|
$basedir = BasePath::create(__DIR__, $_SERVER);
|
||||||
$configLoader = new Config\ConfigCacheLoader($basedir);
|
$configLoader = new Cache\ConfigCacheLoader($basedir);
|
||||||
$config = Factory\ConfigFactory::createCache($configLoader);
|
$configCache = Factory\ConfigFactory::createCache($configLoader);
|
||||||
|
Factory\DBFactory::init($configCache, $_SERVER);
|
||||||
|
$config = Factory\ConfigFactory::createConfig($configCache);
|
||||||
|
$pconfig = Factory\ConfigFactory::createPConfig($configCache);
|
||||||
$logger = Factory\LoggerFactory::create('index', $config);
|
$logger = Factory\LoggerFactory::create('index', $config);
|
||||||
$profiler = Factory\ProfilerFactory::create($logger, $config);
|
$profiler = Factory\ProfilerFactory::create($logger, $config);
|
||||||
|
|
||||||
|
|
72
src/App.php
72
src/App.php
|
@ -8,8 +8,9 @@ use Detection\MobileDetect;
|
||||||
use DOMDocument;
|
use DOMDocument;
|
||||||
use DOMXPath;
|
use DOMXPath;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Friendica\Core\Config\ConfigCache;
|
use Friendica\Core\Config\Cache\ConfigCacheLoader;
|
||||||
use Friendica\Core\Config\ConfigCacheLoader;
|
use Friendica\Core\Config\Cache\IConfigCache;
|
||||||
|
use Friendica\Core\Config\Configuration;
|
||||||
use Friendica\Database\DBA;
|
use Friendica\Database\DBA;
|
||||||
use Friendica\Factory\ConfigFactory;
|
use Friendica\Factory\ConfigFactory;
|
||||||
use Friendica\Network\HTTPException\InternalServerErrorException;
|
use Friendica\Network\HTTPException\InternalServerErrorException;
|
||||||
|
@ -114,7 +115,7 @@ class App
|
||||||
private $logger;
|
private $logger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var ConfigCache The cached config
|
* @var Configuration The config
|
||||||
*/
|
*/
|
||||||
private $config;
|
private $config;
|
||||||
|
|
||||||
|
@ -126,11 +127,11 @@ class App
|
||||||
/**
|
/**
|
||||||
* Returns the current config cache of this node
|
* Returns the current config cache of this node
|
||||||
*
|
*
|
||||||
* @return ConfigCache
|
* @return IConfigCache
|
||||||
*/
|
*/
|
||||||
public function getConfig()
|
public function getConfigCache()
|
||||||
{
|
{
|
||||||
return $this->config;
|
return $this->config->getCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -195,14 +196,14 @@ class App
|
||||||
/**
|
/**
|
||||||
* @brief App constructor.
|
* @brief App constructor.
|
||||||
*
|
*
|
||||||
* @param ConfigCache $config The Cached Config
|
* @param Configuration $config The Configuration
|
||||||
* @param LoggerInterface $logger Logger of this application
|
* @param LoggerInterface $logger Logger of this application
|
||||||
* @param Profiler $profiler The profiler of this application
|
* @param Profiler $profiler The profiler of this application
|
||||||
* @param bool $isBackend Whether it is used for backend or frontend (Default true=backend)
|
* @param bool $isBackend Whether it is used for backend or frontend (Default true=backend)
|
||||||
*
|
*
|
||||||
* @throws Exception if the Basepath is not usable
|
* @throws Exception if the Basepath is not usable
|
||||||
*/
|
*/
|
||||||
public function __construct(ConfigCache $config, LoggerInterface $logger, Profiler $profiler, $isBackend = true)
|
public function __construct(Configuration $config, LoggerInterface $logger, Profiler $profiler, $isBackend = true)
|
||||||
{
|
{
|
||||||
$this->config = $config;
|
$this->config = $config;
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
|
@ -358,21 +359,11 @@ class App
|
||||||
*/
|
*/
|
||||||
public function reload()
|
public function reload()
|
||||||
{
|
{
|
||||||
Core\Config::init($this->config);
|
|
||||||
Core\PConfig::init($this->config);
|
|
||||||
|
|
||||||
$this->loadDatabase();
|
|
||||||
|
|
||||||
$this->getMode()->determine($this->basePath);
|
$this->getMode()->determine($this->basePath);
|
||||||
|
|
||||||
$this->determineURLPath();
|
$this->determineURLPath();
|
||||||
|
|
||||||
if ($this->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
|
if ($this->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
|
||||||
$adapterType = $this->config->get('system', 'config_adapter');
|
|
||||||
$adapter = ConfigFactory::createConfig($adapterType, $this->config);
|
|
||||||
Core\Config::setAdapter($adapter);
|
|
||||||
$adapterP = ConfigFactory::createPConfig($adapterType, $this->config);
|
|
||||||
Core\PConfig::setAdapter($adapterP);
|
|
||||||
Core\Config::load();
|
Core\Config::load();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -383,7 +374,7 @@ class App
|
||||||
Core\Hook::loadHooks();
|
Core\Hook::loadHooks();
|
||||||
$loader = new ConfigCacheLoader($this->basePath);
|
$loader = new ConfigCacheLoader($this->basePath);
|
||||||
Core\Hook::callAll('load_config', $loader);
|
Core\Hook::callAll('load_config', $loader);
|
||||||
$this->config->loadConfigArray($loader->loadCoreConfig('addon'), true);
|
$this->config->getCache()->load($loader->loadCoreConfig('addon'), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->loadDefaultTimezone();
|
$this->loadDefaultTimezone();
|
||||||
|
@ -453,49 +444,6 @@ class App
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function loadDatabase()
|
|
||||||
{
|
|
||||||
if (DBA::connected()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$db_host = $this->config->get('database', 'hostname');
|
|
||||||
$db_user = $this->config->get('database', 'username');
|
|
||||||
$db_pass = $this->config->get('database', 'password');
|
|
||||||
$db_data = $this->config->get('database', 'database');
|
|
||||||
$charset = $this->config->get('database', 'charset');
|
|
||||||
|
|
||||||
// Use environment variables for mysql if they are set beforehand
|
|
||||||
if (!empty(getenv('MYSQL_HOST'))
|
|
||||||
&& !empty(getenv('MYSQL_USERNAME') || !empty(getenv('MYSQL_USER')))
|
|
||||||
&& getenv('MYSQL_PASSWORD') !== false
|
|
||||||
&& !empty(getenv('MYSQL_DATABASE')))
|
|
||||||
{
|
|
||||||
$db_host = getenv('MYSQL_HOST');
|
|
||||||
if (!empty(getenv('MYSQL_PORT'))) {
|
|
||||||
$db_host .= ':' . getenv('MYSQL_PORT');
|
|
||||||
}
|
|
||||||
if (!empty(getenv('MYSQL_USERNAME'))) {
|
|
||||||
$db_user = getenv('MYSQL_USERNAME');
|
|
||||||
} else {
|
|
||||||
$db_user = getenv('MYSQL_USER');
|
|
||||||
}
|
|
||||||
$db_pass = (string) getenv('MYSQL_PASSWORD');
|
|
||||||
$db_data = getenv('MYSQL_DATABASE');
|
|
||||||
}
|
|
||||||
|
|
||||||
$stamp1 = microtime(true);
|
|
||||||
|
|
||||||
if (DBA::connect($this->config, $this->profiler, $db_host, $db_user, $db_pass, $db_data, $charset)) {
|
|
||||||
// Loads DB_UPDATE_VERSION constant
|
|
||||||
Database\DBStructure::definition($this->basePath, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
unset($db_host, $db_user, $db_pass, $db_data, $charset);
|
|
||||||
|
|
||||||
$this->profiler->saveTimestamp($stamp1, 'network', Core\System::callstack());
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getScheme()
|
public function getScheme()
|
||||||
{
|
{
|
||||||
return $this->scheme;
|
return $this->scheme;
|
||||||
|
|
|
@ -22,116 +22,76 @@ use Friendica\Core\Config\IConfigCache;
|
||||||
class Config
|
class Config
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var Config\IConfigAdapter|null
|
* @var Config\Configuration
|
||||||
*/
|
*/
|
||||||
private static $adapter;
|
private static $config;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Config\IConfigCache
|
* Initialize the config
|
||||||
*/
|
|
||||||
private static $cache;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initialize the config with only the cache
|
|
||||||
*
|
*
|
||||||
* @param Config\IConfigCache $cache The configuration cache
|
* @param Config\Configuration $config
|
||||||
*/
|
*/
|
||||||
public static function init(Config\IConfigCache $cache)
|
public static function init(Config\Configuration $config)
|
||||||
{
|
{
|
||||||
self::$cache = $cache;
|
self::$config = $config;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add the adapter for DB-backend
|
|
||||||
*
|
|
||||||
* @param Config\IConfigAdapter $adapter
|
|
||||||
*/
|
|
||||||
public static function setAdapter(Config\IConfigAdapter $adapter)
|
|
||||||
{
|
|
||||||
self::$adapter = $adapter;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Loads all configuration values of family into a cached storage.
|
* @brief Loads all configuration values of family into a cached storage.
|
||||||
*
|
*
|
||||||
* All configuration values of the system are stored in the cache ( @see IConfigCache )
|
* @param string $cat The category of the configuration value
|
||||||
*
|
|
||||||
* @param string $family The category of the configuration value
|
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public static function load($family = "config")
|
public static function load($cat = "config")
|
||||||
{
|
{
|
||||||
if (!isset(self::$adapter) || !self::$adapter->isConnected()) {
|
self::$config->load($cat);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
self::$adapter->load($family);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get a particular user's config variable given the category name
|
* @brief Get a particular user's config variable given the category name
|
||||||
* ($family) and a key.
|
* ($family) and a key.
|
||||||
*
|
*
|
||||||
* Get a particular config value from the given category ($family)
|
* @param string $cat The category of the configuration value
|
||||||
* and the $key from a cached storage either from the self::$adapter
|
|
||||||
* (@see IConfigAdapter ) or from the static::$cache (@see IConfigCache ).
|
|
||||||
*
|
|
||||||
* @param string $family The category of the configuration value
|
|
||||||
* @param string $key The configuration key to query
|
* @param string $key The configuration key to query
|
||||||
* @param mixed $default_value optional, The value to return if key is not set (default: null)
|
* @param mixed $default_value optional, The value to return if key is not set (default: null)
|
||||||
* @param boolean $refresh optional, If true the config is loaded from the db and not from the cache (default: false)
|
* @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
|
* @return mixed Stored value or null if it does not exist
|
||||||
*/
|
*/
|
||||||
public static function get($family, $key, $default_value = null, $refresh = false)
|
public static function get($cat, $key, $default_value = null, $refresh = false)
|
||||||
{
|
{
|
||||||
if (!isset(self::$adapter) || !self::$adapter->isConnected()) {
|
return self::$config->get($cat, $key, $default_value, $refresh);
|
||||||
return self::$cache->get($family, $key, $default_value);
|
|
||||||
}
|
|
||||||
|
|
||||||
return self::$adapter->get($family, $key, $default_value, $refresh);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Sets a configuration value for system config
|
* @brief Sets a configuration value for system config
|
||||||
*
|
*
|
||||||
* Stores a config value ($value) in the category ($family) under the key ($key)
|
* Stores a config value ($value) in the category ($cat) under the key ($key)
|
||||||
*
|
*
|
||||||
* Note: Please do not store booleans - convert to 0/1 integer values!
|
* Note: Please do not store booleans - convert to 0/1 integer values!
|
||||||
*
|
*
|
||||||
* @param string $family The category of the configuration value
|
* @param string $cat The category of the configuration value
|
||||||
* @param string $key The configuration key to set
|
* @param string $key The configuration key to set
|
||||||
* @param mixed $value The value to store
|
* @param mixed $value The value to store
|
||||||
*
|
*
|
||||||
* @return bool Operation success
|
* @return bool Operation success
|
||||||
*/
|
*/
|
||||||
public static function set($family, $key, $value)
|
public static function set($cat, $key, $value)
|
||||||
{
|
{
|
||||||
if (!isset(self::$adapter) || !self::$adapter->isConnected()) {
|
return self::$config->set($cat, $key, $value);
|
||||||
return self::$cache->set($family, $key, $value);
|
|
||||||
}
|
|
||||||
|
|
||||||
return self::$adapter->set($family, $key, $value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Deletes the given key from the system configuration.
|
* @brief Deletes the given key from the system configuration.
|
||||||
*
|
*
|
||||||
* Removes the configured value from the stored cache in self::$config
|
* @param string $cat The category of the configuration value
|
||||||
* (@see ConfigCache ) and removes it from the database (@see IConfigAdapter ).
|
|
||||||
*
|
|
||||||
* @param string $family The category of the configuration value
|
|
||||||
* @param string $key The configuration key to delete
|
* @param string $key The configuration key to delete
|
||||||
*
|
*
|
||||||
* @return mixed
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public static function delete($family, $key)
|
public static function delete($cat, $key)
|
||||||
{
|
{
|
||||||
if (!isset(self::$adapter) || !self::$adapter->isConnected()) {
|
return self::$config->delete($cat, $key);
|
||||||
self::$cache->delete($family, $key);
|
|
||||||
}
|
|
||||||
|
|
||||||
return self::$adapter->delete($family, $key);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,19 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Friendica\Core\Config;
|
namespace Friendica\Core\Config\Adapter;
|
||||||
|
|
||||||
|
use Friendica\Database\DBA;
|
||||||
|
|
||||||
abstract class AbstractDbaConfigAdapter
|
abstract class AbstractDbaConfigAdapter
|
||||||
{
|
{
|
||||||
/** @var bool */
|
/** @var bool */
|
||||||
protected $connected = true;
|
protected $connected = true;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->connected = DBA::connected();
|
||||||
|
}
|
||||||
|
|
||||||
public function isConnected()
|
public function isConnected()
|
||||||
{
|
{
|
||||||
return $this->connected;
|
return $this->connected;
|
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Friendica\Core\Config;
|
namespace Friendica\Core\Config\Adapter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
@ -13,7 +13,7 @@ interface IConfigAdapter
|
||||||
*
|
*
|
||||||
* @param string $cat The category of the configuration values to load
|
* @param string $cat The category of the configuration values to load
|
||||||
*
|
*
|
||||||
* @return void
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function load($cat = "config");
|
public function load($cat = "config");
|
||||||
|
|
||||||
|
@ -21,14 +21,12 @@ interface IConfigAdapter
|
||||||
* Get a particular user's config variable given the category name
|
* Get a particular user's config variable given the category name
|
||||||
* ($family) and a key.
|
* ($family) and a key.
|
||||||
*
|
*
|
||||||
* @param string $cat The category of the configuration value
|
* @param string $cat The category of the configuration value
|
||||||
* @param string $k The configuration key to query
|
* @param string $key The configuration key to query
|
||||||
* @param mixed $default_value optional, The value to return if key is not set (default: null)
|
|
||||||
* @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
|
* @return mixed Stored value or "!<unset>!" if it does not exist
|
||||||
*/
|
*/
|
||||||
public function get($cat, $k, $default_value = null, $refresh = false);
|
public function get($cat, $key);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stores a config value ($value) in the category ($family) under the key ($key)
|
* Stores a config value ($value) in the category ($family) under the key ($key)
|
||||||
|
@ -37,23 +35,23 @@ interface IConfigAdapter
|
||||||
* Note: Please do not store booleans - convert to 0/1 integer values!
|
* Note: Please do not store booleans - convert to 0/1 integer values!
|
||||||
*
|
*
|
||||||
* @param string $cat The category of the configuration value
|
* @param string $cat The category of the configuration value
|
||||||
* @param string $k The configuration key to set
|
* @param string $key The configuration key to set
|
||||||
* @param mixed $value The value to store
|
* @param mixed $value The value to store
|
||||||
*
|
*
|
||||||
* @return bool Operation success
|
* @return bool Operation success
|
||||||
*/
|
*/
|
||||||
public function set($cat, $k, $value);
|
public function set($cat, $key, $value);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes the configured value from the stored cache
|
* Removes the configured value from the stored cache
|
||||||
* and removes it from the database.
|
* and removes it from the database.
|
||||||
*
|
*
|
||||||
* @param string $cat The category of the configuration value
|
* @param string $cat The category of the configuration value
|
||||||
* @param string $k The configuration key to delete
|
* @param string $key The configuration key to delete
|
||||||
*
|
*
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function delete($cat, $k);
|
public function delete($cat, $key);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks, if the current adapter is connected to the backend
|
* Checks, if the current adapter is connected to the backend
|
|
@ -6,7 +6,7 @@
|
||||||
* and open the template in the editor.
|
* and open the template in the editor.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace Friendica\Core\Config;
|
namespace Friendica\Core\Config\Adapter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
@ -20,7 +20,7 @@ interface IPConfigAdapter
|
||||||
* @param string $uid The user_id
|
* @param string $uid The user_id
|
||||||
* @param string $cat The category of the configuration value
|
* @param string $cat The category of the configuration value
|
||||||
*
|
*
|
||||||
* @return void
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function load($uid, $cat);
|
public function load($uid, $cat);
|
||||||
|
|
||||||
|
@ -30,13 +30,11 @@ interface IPConfigAdapter
|
||||||
*
|
*
|
||||||
* @param string $uid The user_id
|
* @param string $uid The user_id
|
||||||
* @param string $cat The category of the configuration value
|
* @param string $cat The category of the configuration value
|
||||||
* @param string $k The configuration key to query
|
* @param string $key The configuration key to query
|
||||||
* @param mixed $default_value optional, The value to return if key is not set (default: null)
|
|
||||||
* @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
|
* @return mixed Stored value or "!<unset>!" if it does not exist
|
||||||
*/
|
*/
|
||||||
public function get($uid, $cat, $k, $default_value = null, $refresh = false);
|
public function get($uid, $cat, $key);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stores a config value ($value) in the category ($family) under the key ($key)
|
* Stores a config value ($value) in the category ($family) under the key ($key)
|
||||||
|
@ -46,12 +44,12 @@ interface IPConfigAdapter
|
||||||
*
|
*
|
||||||
* @param string $uid The user_id
|
* @param string $uid The user_id
|
||||||
* @param string $cat The category of the configuration value
|
* @param string $cat The category of the configuration value
|
||||||
* @param string $k The configuration key to set
|
* @param string $key The configuration key to set
|
||||||
* @param string $value The value to store
|
* @param string $value The value to store
|
||||||
*
|
*
|
||||||
* @return bool Operation success
|
* @return bool Operation success
|
||||||
*/
|
*/
|
||||||
public function set($uid, $cat, $k, $value);
|
public function set($uid, $cat, $key, $value);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes the configured value from the stored cache
|
* Removes the configured value from the stored cache
|
||||||
|
@ -59,9 +57,16 @@ interface IPConfigAdapter
|
||||||
*
|
*
|
||||||
* @param string $uid The user_id
|
* @param string $uid The user_id
|
||||||
* @param string $cat The category of the configuration value
|
* @param string $cat The category of the configuration value
|
||||||
* @param string $k The configuration key to delete
|
* @param string $key The configuration key to delete
|
||||||
*
|
*
|
||||||
* @return mixed
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function delete($uid, $cat, $k);
|
public function delete($uid, $cat, $key);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks, if the current adapter is connected to the backend
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isConnected();
|
||||||
}
|
}
|
123
src/Core/Config/Adapter/JITConfigAdapter.php
Normal file
123
src/Core/Config/Adapter/JITConfigAdapter.php
Normal file
|
@ -0,0 +1,123 @@
|
||||||
|
<?php
|
||||||
|
namespace Friendica\Core\Config\Adapter;
|
||||||
|
|
||||||
|
use Friendica\Database\DBA;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JustInTime Configuration Adapter
|
||||||
|
*
|
||||||
|
* Default Config Adapter. Provides the best performance for pages loading few configuration variables.
|
||||||
|
*
|
||||||
|
* @author Hypolite Petovan <hypolite@mrpetovan.com>
|
||||||
|
*/
|
||||||
|
class JITConfigAdapter extends AbstractDbaConfigAdapter implements IConfigAdapter
|
||||||
|
{
|
||||||
|
private $in_db;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function load($cat = "config")
|
||||||
|
{
|
||||||
|
$return = [];
|
||||||
|
|
||||||
|
if (!$this->isConnected()) {
|
||||||
|
return $return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We don't preload "system" anymore.
|
||||||
|
// This reduces the number of database reads a lot.
|
||||||
|
if ($cat === 'system') {
|
||||||
|
return $return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$configs = DBA::select('config', ['v', 'k'], ['cat' => $cat]);
|
||||||
|
while ($config = DBA::fetch($configs)) {
|
||||||
|
$key = $config['k'];
|
||||||
|
|
||||||
|
$return[$key] = $config['v'];
|
||||||
|
$this->in_db[$cat][$key] = true;
|
||||||
|
}
|
||||||
|
DBA::close($configs);
|
||||||
|
|
||||||
|
return [$cat => $config];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function get($cat, $key)
|
||||||
|
{
|
||||||
|
if (!$this->isConnected()) {
|
||||||
|
return '!<unset>!';
|
||||||
|
}
|
||||||
|
|
||||||
|
$config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $key]);
|
||||||
|
if (DBA::isResult($config)) {
|
||||||
|
// manage array value
|
||||||
|
$value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
|
||||||
|
|
||||||
|
$this->in_db[$cat][$key] = true;
|
||||||
|
return $value;
|
||||||
|
} else {
|
||||||
|
|
||||||
|
$this->in_db[$cat][$key] = false;
|
||||||
|
return '!<unset>!';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function set($cat, $key, $value)
|
||||||
|
{
|
||||||
|
if (!$this->isConnected()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We store our setting values in a string variable.
|
||||||
|
// So we have to do the conversion here so that the compare below works.
|
||||||
|
// The exception are array values.
|
||||||
|
$dbvalue = (!is_array($value) ? (string)$value : $value);
|
||||||
|
|
||||||
|
$stored = $this->get($cat, $key);
|
||||||
|
|
||||||
|
if (!isset($this->in_db[$cat])) {
|
||||||
|
$this->in_db[$cat] = [];
|
||||||
|
}
|
||||||
|
if (!isset($this->in_db[$cat][$key])) {
|
||||||
|
$this->in_db[$cat][$key] = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (($stored === $dbvalue) && $this->in_db[$cat][$key]) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// manage array value
|
||||||
|
$dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
|
||||||
|
|
||||||
|
$result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $key], true);
|
||||||
|
|
||||||
|
$this->in_db[$cat][$key] = $result;
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function delete($cat, $key)
|
||||||
|
{
|
||||||
|
if (!$this->isConnected()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($this->cache[$cat][$key])) {
|
||||||
|
unset($this->in_db[$cat][$key]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = DBA::delete('config', ['cat' => $cat, 'k' => $key]);
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
}
|
126
src/Core/Config/Adapter/JITPConfigAdapter.php
Normal file
126
src/Core/Config/Adapter/JITPConfigAdapter.php
Normal file
|
@ -0,0 +1,126 @@
|
||||||
|
<?php
|
||||||
|
namespace Friendica\Core\Config\Adapter;
|
||||||
|
|
||||||
|
use Friendica\Database\DBA;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JustInTime User Configuration Adapter
|
||||||
|
*
|
||||||
|
* Default PConfig Adapter. Provides the best performance for pages loading few configuration variables.
|
||||||
|
*
|
||||||
|
* @author Hypolite Petovan <hypolite@mrpetovan.com>
|
||||||
|
*/
|
||||||
|
class JITPConfigAdapter extends AbstractDbaConfigAdapter implements IPConfigAdapter
|
||||||
|
{
|
||||||
|
private $in_db;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function load($uid, $cat)
|
||||||
|
{
|
||||||
|
$return = [];
|
||||||
|
|
||||||
|
if (!$this->isConnected()) {
|
||||||
|
return $return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$pconfigs = DBA::select('pconfig', ['v', 'k'], ['cat' => $cat, 'uid' => $uid]);
|
||||||
|
if (DBA::isResult($pconfigs)) {
|
||||||
|
while ($pconfig = DBA::fetch($pconfigs)) {
|
||||||
|
$key = $pconfig['k'];
|
||||||
|
|
||||||
|
$return[$key] = $pconfig['v'];
|
||||||
|
|
||||||
|
$this->in_db[$uid][$cat][$key] = true;
|
||||||
|
}
|
||||||
|
} else if ($cat != 'config') {
|
||||||
|
// Negative caching
|
||||||
|
$return[null] = "!<unset>!";
|
||||||
|
}
|
||||||
|
DBA::close($pconfigs);
|
||||||
|
|
||||||
|
return [$cat => $return];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function get($uid, $cat, $key)
|
||||||
|
{
|
||||||
|
if (!$this->isConnected()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$pconfig = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
|
||||||
|
if (DBA::isResult($pconfig)) {
|
||||||
|
// manage array value
|
||||||
|
$value = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']);
|
||||||
|
|
||||||
|
$this->in_db[$uid][$cat][$key] = true;
|
||||||
|
return $value;
|
||||||
|
} else {
|
||||||
|
|
||||||
|
$this->in_db[$uid][$cat][$key] = false;
|
||||||
|
return '!<unset>!';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function set($uid, $cat, $key, $value)
|
||||||
|
{
|
||||||
|
if (!$this->isConnected()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We store our setting values in a string variable.
|
||||||
|
// So we have to do the conversion here so that the compare below works.
|
||||||
|
// The exception are array values.
|
||||||
|
$dbvalue = (!is_array($value) ? (string)$value : $value);
|
||||||
|
|
||||||
|
$stored = $this->get($uid, $cat, $key);
|
||||||
|
|
||||||
|
if (!isset($this->in_db[$uid])) {
|
||||||
|
$this->in_db[$uid] = [];
|
||||||
|
}
|
||||||
|
if (!isset($this->in_db[$uid][$cat])) {
|
||||||
|
$this->in_db[$uid][$cat] = [];
|
||||||
|
}
|
||||||
|
if (!isset($this->in_db[$uid][$cat][$key])) {
|
||||||
|
$this->in_db[$uid][$cat][$key] = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (($stored === $dbvalue) && $this->in_db[$uid][$cat][$key]) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// manage array value
|
||||||
|
$dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
|
||||||
|
|
||||||
|
$result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
|
||||||
|
|
||||||
|
$this->in_db[$uid][$cat][$key] = $result;
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function delete($uid, $cat, $key)
|
||||||
|
{
|
||||||
|
if (!$this->isConnected()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($this->in_db[$uid][$cat][$key])) {
|
||||||
|
unset($this->in_db[$uid][$cat][$key]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
}
|
104
src/Core/Config/Adapter/PreloadConfigAdapter.php
Normal file
104
src/Core/Config/Adapter/PreloadConfigAdapter.php
Normal file
|
@ -0,0 +1,104 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Friendica\Core\Config\Adapter;
|
||||||
|
|
||||||
|
use Friendica\Database\DBA;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Preload Configuration Adapter
|
||||||
|
*
|
||||||
|
* Minimizes the number of database queries to retrieve configuration values at the cost of memory.
|
||||||
|
*
|
||||||
|
* @author Hypolite Petovan <hypolite@mrpetovan.com>
|
||||||
|
*/
|
||||||
|
class PreloadConfigAdapter extends AbstractDbaConfigAdapter implements IConfigAdapter
|
||||||
|
{
|
||||||
|
private $config_loaded = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function load($cat = 'config')
|
||||||
|
{
|
||||||
|
$return = [];
|
||||||
|
|
||||||
|
if (!$this->isConnected()) {
|
||||||
|
return $return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->config_loaded) {
|
||||||
|
return $return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$configs = DBA::select('config', ['cat', 'v', 'k']);
|
||||||
|
while ($config = DBA::fetch($configs)) {
|
||||||
|
$return[$config['k']] = $config['v'];
|
||||||
|
}
|
||||||
|
DBA::close($configs);
|
||||||
|
|
||||||
|
$this->config_loaded = true;
|
||||||
|
|
||||||
|
return [$cat => $return];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function get($cat, $key)
|
||||||
|
{
|
||||||
|
if (!$this->isConnected()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $key]);
|
||||||
|
if (DBA::isResult($config)) {
|
||||||
|
// manage array value
|
||||||
|
$value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
|
||||||
|
|
||||||
|
return $value;
|
||||||
|
} else {
|
||||||
|
|
||||||
|
return '!<unset>!';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function set($cat, $key, $value)
|
||||||
|
{
|
||||||
|
if (!$this->isConnected()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We store our setting values as strings.
|
||||||
|
// So we have to do the conversion here so that the compare below works.
|
||||||
|
// The exception are array values.
|
||||||
|
$compare_value = !is_array($value) ? (string)$value : $value;
|
||||||
|
|
||||||
|
if ($this->get($cat, $key) === $compare_value) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// manage array value
|
||||||
|
$dbvalue = is_array($value) ? serialize($value) : $value;
|
||||||
|
|
||||||
|
$result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $key], true);
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function delete($cat, $key)
|
||||||
|
{
|
||||||
|
if (!$this->isConnected()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = DBA::delete('config', ['cat' => $cat, 'k' => $key]);
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
}
|
126
src/Core/Config/Adapter/PreloadPConfigAdapter.php
Normal file
126
src/Core/Config/Adapter/PreloadPConfigAdapter.php
Normal file
|
@ -0,0 +1,126 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Friendica\Core\Config\Adapter;
|
||||||
|
|
||||||
|
use Friendica\Database\DBA;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Preload User Configuration Adapter
|
||||||
|
*
|
||||||
|
* Minimizes the number of database queries to retrieve configuration values at the cost of memory.
|
||||||
|
*
|
||||||
|
* @author Hypolite Petovan <hypolite@mrpetovan.com>
|
||||||
|
*/
|
||||||
|
class PreloadPConfigAdapter extends AbstractDbaConfigAdapter implements IPConfigAdapter
|
||||||
|
{
|
||||||
|
private $config_loaded = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $uid The UID of the current user
|
||||||
|
*/
|
||||||
|
public function __construct($uid = null)
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
|
||||||
|
if (isset($uid)) {
|
||||||
|
$this->load($uid, 'config');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function load($uid, $cat)
|
||||||
|
{
|
||||||
|
$return = [];
|
||||||
|
|
||||||
|
if ($this->config_loaded) {
|
||||||
|
return $return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($uid)) {
|
||||||
|
return $return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$pconfigs = DBA::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
|
||||||
|
while ($pconfig = DBA::fetch($pconfigs)) {
|
||||||
|
$return[$pconfig['k']] = $pconfig['v'];
|
||||||
|
}
|
||||||
|
DBA::close($pconfigs);
|
||||||
|
|
||||||
|
$this->config_loaded = true;
|
||||||
|
|
||||||
|
return [$cat => $return];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function get($uid, $cat, $key)
|
||||||
|
{
|
||||||
|
if (!$this->isConnected()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$this->config_loaded) {
|
||||||
|
$this->load($uid, $cat);
|
||||||
|
}
|
||||||
|
|
||||||
|
$config = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
|
||||||
|
if (DBA::isResult($config)) {
|
||||||
|
// manage array value
|
||||||
|
$value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
|
||||||
|
|
||||||
|
return $value;
|
||||||
|
} else {
|
||||||
|
return '!<unset>!';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function set($uid, $cat, $key, $value)
|
||||||
|
{
|
||||||
|
if (!$this->isConnected()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$this->config_loaded) {
|
||||||
|
$this->load($uid, $cat);
|
||||||
|
}
|
||||||
|
// We store our setting values as strings.
|
||||||
|
// So we have to do the conversion here so that the compare below works.
|
||||||
|
// The exception are array values.
|
||||||
|
$compare_value = !is_array($value) ? (string)$value : $value;
|
||||||
|
|
||||||
|
if ($this->get($uid, $cat, $key) === $compare_value) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// manage array value
|
||||||
|
$dbvalue = is_array($value) ? serialize($value) : $value;
|
||||||
|
|
||||||
|
$result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function delete($uid, $cat, $key)
|
||||||
|
{
|
||||||
|
if (!$this->isConnected()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$this->config_loaded) {
|
||||||
|
$this->load($uid, $cat);
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Friendica\Core\Config;
|
namespace Friendica\Core\Config\Cache;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The Friendica config cache for the application
|
* The Friendica config cache for the application
|
||||||
|
@ -11,6 +11,9 @@ namespace Friendica\Core\Config;
|
||||||
*/
|
*/
|
||||||
class ConfigCache implements IConfigCache, IPConfigCache
|
class ConfigCache implements IConfigCache, IPConfigCache
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
private $config;
|
private $config;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -18,24 +21,28 @@ class ConfigCache implements IConfigCache, IPConfigCache
|
||||||
*/
|
*/
|
||||||
public function __construct(array $config = [])
|
public function __construct(array $config = [])
|
||||||
{
|
{
|
||||||
$this->loadConfigArray($config);
|
$this->load($config);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tries to load the specified configuration array into the App->config array.
|
* {@inheritdoc}
|
||||||
* Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
|
|
||||||
*
|
|
||||||
* @param array $config
|
|
||||||
* @param bool $overwrite Force value overwrite if the config key already exists
|
|
||||||
*/
|
*/
|
||||||
public function loadConfigArray(array $config, $overwrite = false)
|
public function load(array $config, $overwrite = false)
|
||||||
{
|
{
|
||||||
foreach ($config as $category => $values) {
|
$categories = array_keys($config);
|
||||||
foreach ($values as $key => $value) {
|
|
||||||
if ($overwrite) {
|
foreach ($categories as $category) {
|
||||||
$this->set($category, $key, $value);
|
if (isset($config[$category]) && is_array($config[$category])) {
|
||||||
} else {
|
$keys = array_keys($config[$category]);
|
||||||
$this->setDefault($category, $key, $value);
|
|
||||||
|
foreach ($keys as $key) {
|
||||||
|
if (isset($config[$category][$key])) {
|
||||||
|
if ($overwrite) {
|
||||||
|
$this->set($category, $key, $config[$category][$key]);
|
||||||
|
} else {
|
||||||
|
$this->setDefault($category, $key, $config[$category][$key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -44,23 +51,22 @@ class ConfigCache implements IConfigCache, IPConfigCache
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function get($cat, $key = null, $default = null)
|
public function get($cat, $key = null)
|
||||||
{
|
{
|
||||||
$return = $default;
|
if (isset($this->config[$cat][$key])) {
|
||||||
|
return $this->config[$cat][$key];
|
||||||
if ($cat === 'config') {
|
|
||||||
if (isset($this->config[$key])) {
|
|
||||||
$return = $this->config[$key];
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if (isset($this->config[$cat][$key])) {
|
return '!<unset>!';
|
||||||
$return = $this->config[$cat][$key];
|
|
||||||
} elseif ($key == null && isset($this->config[$cat])) {
|
|
||||||
$return = $this->config[$cat];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $return;
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function has($cat, $key = null)
|
||||||
|
{
|
||||||
|
return isset($this->config[$cat][$key])
|
||||||
|
&& $this->config[$cat][$key] !== '!<unset>!';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -85,34 +91,48 @@ class ConfigCache implements IConfigCache, IPConfigCache
|
||||||
// Only arrays are serialized in database, so we have to unserialize sparingly
|
// Only arrays are serialized in database, so we have to unserialize sparingly
|
||||||
$value = is_string($value) && preg_match("|^a:[0-9]+:{.*}$|s", $value) ? unserialize($value) : $value;
|
$value = is_string($value) && preg_match("|^a:[0-9]+:{.*}$|s", $value) ? unserialize($value) : $value;
|
||||||
|
|
||||||
if ($cat === 'config') {
|
if (!isset($this->config[$cat])) {
|
||||||
$this->config[$key] = $value;
|
$this->config[$cat] = [];
|
||||||
} else {
|
|
||||||
if (!isset($this->config[$cat])) {
|
|
||||||
$this->config[$cat] = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->config[$cat][$key] = $value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->config[$cat][$key] = $value;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function hasP($uid, $cat, $key = null)
|
||||||
|
{
|
||||||
|
return isset($this->config[$uid][$cat][$key])
|
||||||
|
&& $this->config[$uid][$cat][$key] !== '!<unset>!';
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function delete($cat, $key)
|
public function delete($cat, $key)
|
||||||
{
|
{
|
||||||
if ($cat === 'config') {
|
if (isset($this->config[$cat][$key])) {
|
||||||
if (isset($this->config[$key])) {
|
unset($this->config[$cat][$key]);
|
||||||
unset($this->config[$key]);
|
if (count($this->config[$cat]) == 0) {
|
||||||
|
unset($this->config[$cat]);
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
} else {
|
} else {
|
||||||
if (isset($this->config[$cat][$key])) {
|
return false;
|
||||||
unset($this->config[$cat][$key]);
|
}
|
||||||
if (count($this->config[$cat]) == 0) {
|
}
|
||||||
unset($this->config[$cat]);
|
|
||||||
}
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function loadP($uid, array $config)
|
||||||
|
{
|
||||||
|
foreach ($config as $category => $values) {
|
||||||
|
foreach ($values as $key => $value) {
|
||||||
|
$this->setP($uid, $category, $key, $value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -120,17 +140,13 @@ class ConfigCache implements IConfigCache, IPConfigCache
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function getP($uid, $cat, $key = null, $default = null)
|
public function getP($uid, $cat, $key = null)
|
||||||
{
|
{
|
||||||
$return = $default;
|
|
||||||
|
|
||||||
if (isset($this->config[$uid][$cat][$key])) {
|
if (isset($this->config[$uid][$cat][$key])) {
|
||||||
$return = $this->config[$uid][$cat][$key];
|
return $this->config[$uid][$cat][$key];
|
||||||
} elseif ($key === null && isset($this->config[$uid][$cat])) {
|
} else {
|
||||||
$return = $this->config[$uid][$cat];
|
return '!<unset>!';
|
||||||
}
|
}
|
||||||
|
|
||||||
return $return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -145,15 +161,13 @@ class ConfigCache implements IConfigCache, IPConfigCache
|
||||||
$this->config[$uid] = [];
|
$this->config[$uid] = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isset($this->config[$uid][$cat]) || !is_array($this->config[$uid][$cat])) {
|
if (!isset($this->config[$uid][$cat])) {
|
||||||
$this->config[$uid][$cat] = [];
|
$this->config[$uid][$cat] = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($key === null) {
|
$this->config[$uid][$cat][$key] = $value;
|
||||||
$this->config[$uid][$cat] = $value;
|
|
||||||
} else {
|
return true;
|
||||||
$this->config[$uid][$cat][$key] = $value;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -169,6 +183,10 @@ class ConfigCache implements IConfigCache, IPConfigCache
|
||||||
unset($this->config[$uid]);
|
unset($this->config[$uid]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Friendica\Core\Config;
|
namespace Friendica\Core\Config\Cache;
|
||||||
|
|
||||||
use Friendica\Core\Addon;
|
use Friendica\Core\Addon;
|
||||||
|
|
||||||
|
@ -40,13 +40,13 @@ class ConfigCacheLoader
|
||||||
// Setting at least the basepath we know
|
// Setting at least the basepath we know
|
||||||
$config->set('system', 'basepath', $this->baseDir);
|
$config->set('system', 'basepath', $this->baseDir);
|
||||||
|
|
||||||
$config->loadConfigArray($this->loadCoreConfig('defaults'));
|
$config->load($this->loadCoreConfig('defaults'));
|
||||||
$config->loadConfigArray($this->loadCoreConfig('settings'));
|
$config->load($this->loadCoreConfig('settings'));
|
||||||
|
|
||||||
$config->loadConfigArray($this->loadLegacyConfig('htpreconfig'), true);
|
$config->load($this->loadLegacyConfig('htpreconfig'), true);
|
||||||
$config->loadConfigArray($this->loadLegacyConfig('htconfig'), true);
|
$config->load($this->loadLegacyConfig('htconfig'), true);
|
||||||
|
|
||||||
$config->loadConfigArray($this->loadCoreConfig('local'), true);
|
$config->load($this->loadCoreConfig('local'), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
65
src/Core/Config/Cache/IConfigCache.php
Normal file
65
src/Core/Config/Cache/IConfigCache.php
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Friendica\Core\Config\Cache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The interface for a system-wide ConfigCache
|
||||||
|
*/
|
||||||
|
interface IConfigCache
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Tries to load the specified configuration array into the config array.
|
||||||
|
* Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
|
||||||
|
*
|
||||||
|
* @param array $config
|
||||||
|
* @param bool $overwrite Force value overwrite if the config key already exists
|
||||||
|
*/
|
||||||
|
function load(array $config, $overwrite = false);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a value from the config cache.
|
||||||
|
*
|
||||||
|
* @param string $cat Config category
|
||||||
|
* @param string $key Config key
|
||||||
|
*
|
||||||
|
* @return mixed Returns the value of the Config entry or '!<unset>!' if not set
|
||||||
|
*/
|
||||||
|
function get($cat, $key = null);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a value in the config cache. Accepts raw output from the config table
|
||||||
|
*
|
||||||
|
* @param string $cat Config category
|
||||||
|
* @param string $key Config key
|
||||||
|
* @param mixed $value Value to set
|
||||||
|
*
|
||||||
|
* @return bool True, if the value is set
|
||||||
|
*/
|
||||||
|
function set($cat, $key, $value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes a value from the config cache.
|
||||||
|
*
|
||||||
|
* @param string $cat Config category
|
||||||
|
* @param string $key Config key
|
||||||
|
*
|
||||||
|
* @return bool true, if deleted
|
||||||
|
*/
|
||||||
|
function delete($cat, $key);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if a value is set in the config cache.
|
||||||
|
*
|
||||||
|
* @param string $cat Config category
|
||||||
|
* @param string $key Config key
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
function has($cat, $key = null);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the whole configuration cache
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
function getAll();
|
||||||
|
}
|
70
src/Core/Config/Cache/IPConfigCache.php
Normal file
70
src/Core/Config/Cache/IPConfigCache.php
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Friendica\Core\Config\Cache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The interface for a user-specific config cache
|
||||||
|
*/
|
||||||
|
interface IPConfigCache
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Tries to load the specified configuration array into the user specific config array.
|
||||||
|
* Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
|
||||||
|
*
|
||||||
|
* @param int $uid
|
||||||
|
* @param array $config
|
||||||
|
*/
|
||||||
|
function loadP($uid, array $config);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a value from the user config cache
|
||||||
|
*
|
||||||
|
* @param int $uid User Id
|
||||||
|
* @param string $cat Config category
|
||||||
|
* @param string $key Config key
|
||||||
|
*
|
||||||
|
* @return string The value of the config entry or '!<unset>!' if not set
|
||||||
|
*/
|
||||||
|
function getP($uid, $cat, $key = null);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a value in the user config cache
|
||||||
|
*
|
||||||
|
* Accepts raw output from the pconfig table
|
||||||
|
*
|
||||||
|
* @param int $uid User Id
|
||||||
|
* @param string $cat Config category
|
||||||
|
* @param string $key Config key
|
||||||
|
* @param mixed $value Value to set
|
||||||
|
*/
|
||||||
|
function setP($uid, $cat, $key, $value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes a value from the user config cache
|
||||||
|
*
|
||||||
|
* @param int $uid User Id
|
||||||
|
* @param string $cat Config category
|
||||||
|
* @param string $key Config key
|
||||||
|
*
|
||||||
|
* @return bool true, if deleted
|
||||||
|
*/
|
||||||
|
function deleteP($uid, $cat, $key);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if a value is set in the user config cache.
|
||||||
|
*
|
||||||
|
* @param int $uid User Id
|
||||||
|
* @param string $cat Config category
|
||||||
|
* @param string $key Config key
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
function hasP($uid, $cat, $key = null);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the whole configuration cache
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
function getAll();
|
||||||
|
}
|
154
src/Core/Config/Configuration.php
Normal file
154
src/Core/Config/Configuration.php
Normal file
|
@ -0,0 +1,154 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Friendica\Core\Config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class is responsible for all system-wide configuration values in Friendica
|
||||||
|
* There are two types of storage
|
||||||
|
* - The Config-Files (loaded into the FileCache @see Cache\IConfigCache )
|
||||||
|
* - The Config-DB-Table (per Config-DB-adapter @see Adapter\IConfigAdapter )
|
||||||
|
*/
|
||||||
|
class Configuration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var Cache\IConfigCache
|
||||||
|
*/
|
||||||
|
private $configCache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Adapter\IConfigAdapter
|
||||||
|
*/
|
||||||
|
private $configAdapter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Cache\IConfigCache $configCache The configuration cache (based on the config-files)
|
||||||
|
* @param Adapter\IConfigAdapter $configAdapter The configuration DB-backend
|
||||||
|
*/
|
||||||
|
public function __construct(Cache\IConfigCache $configCache, Adapter\IConfigAdapter $configAdapter)
|
||||||
|
{
|
||||||
|
$this->configCache = $configCache;
|
||||||
|
$this->configAdapter = $configAdapter;
|
||||||
|
|
||||||
|
$this->load();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the Config Cache
|
||||||
|
*
|
||||||
|
* @return Cache\IConfigCache
|
||||||
|
*/
|
||||||
|
public function getCache()
|
||||||
|
{
|
||||||
|
return $this->configCache;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Loads all configuration values of family into a cached storage.
|
||||||
|
*
|
||||||
|
* All configuration values of the system are stored in the cache ( @see IConfigCache )
|
||||||
|
*
|
||||||
|
* @param string $cat The category of the configuration value
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function load($cat = 'config')
|
||||||
|
{
|
||||||
|
// If not connected, do nothing
|
||||||
|
if (!$this->configAdapter->isConnected()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// load the whole category out of the DB into the cache
|
||||||
|
$this->configCache->load($this->configAdapter->load($cat), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 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 $this->configAdapter
|
||||||
|
* (@see IConfigAdapter ) or from the $this->configCache (@see IConfigCache ).
|
||||||
|
*
|
||||||
|
* @param string $cat The category of the configuration value
|
||||||
|
* @param string $key The configuration key to query
|
||||||
|
* @param mixed $default_value optional, The value to return if key is not set (default: null)
|
||||||
|
* @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
|
||||||
|
*/
|
||||||
|
public function get($cat, $key, $default_value = null, $refresh = false)
|
||||||
|
{
|
||||||
|
// Return the value of the cache if found and no refresh is forced
|
||||||
|
if (!$refresh && $this->configCache->has($cat, $key)) {
|
||||||
|
return $this->configCache->get($cat, $key);
|
||||||
|
}
|
||||||
|
|
||||||
|
// if we don't find the value in the cache and the adapter isn't ready, return the default value
|
||||||
|
if (!$this->configAdapter->isConnected()) {
|
||||||
|
return $default_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// load DB value to cache
|
||||||
|
$dbvalue = $this->configAdapter->get($cat, $key);
|
||||||
|
|
||||||
|
if ($dbvalue !== '!<unset>!') {
|
||||||
|
$this->configCache->set($cat, $key, $dbvalue);
|
||||||
|
return $dbvalue;
|
||||||
|
} else {
|
||||||
|
return $default_value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Sets a configuration value for system config
|
||||||
|
*
|
||||||
|
* Stores a config value ($value) in the category ($cat) under the key ($key)
|
||||||
|
*
|
||||||
|
* Note: Please do not store booleans - convert to 0/1 integer values!
|
||||||
|
*
|
||||||
|
* @param string $cat The category of the configuration value
|
||||||
|
* @param string $key The configuration key to set
|
||||||
|
* @param mixed $value The value to store
|
||||||
|
*
|
||||||
|
* @return bool Operation success
|
||||||
|
*/
|
||||||
|
public function set($cat, $key, $value)
|
||||||
|
{
|
||||||
|
// set the cache first
|
||||||
|
$cached = $this->configCache->set($cat, $key, $value);
|
||||||
|
|
||||||
|
// If there is no connected adapter, we're finished
|
||||||
|
if (!$this->configAdapter->isConnected()) {
|
||||||
|
return $cached;
|
||||||
|
}
|
||||||
|
|
||||||
|
$stored = $this->configAdapter->set($cat, $key, $value);
|
||||||
|
|
||||||
|
return $cached && $stored;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Deletes the given key from the system configuration.
|
||||||
|
*
|
||||||
|
* Removes the configured value from the stored cache in $this->configCache
|
||||||
|
* (@see ConfigCache ) and removes it from the database (@see IConfigAdapter ).
|
||||||
|
*
|
||||||
|
* @param string $cat The category of the configuration value
|
||||||
|
* @param string $key The configuration key to delete
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function delete($cat, $key)
|
||||||
|
{
|
||||||
|
$cacheRemoved = $this->configCache->delete($cat, $key);
|
||||||
|
|
||||||
|
if (!$this->configAdapter->isConnected()) {
|
||||||
|
return $cacheRemoved;
|
||||||
|
}
|
||||||
|
|
||||||
|
$storeRemoved = $this->configAdapter->delete($cat, $key);
|
||||||
|
|
||||||
|
return $cacheRemoved || $storeRemoved;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,39 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Friendica\Core\Config;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The interface for a system-wide ConfigCache
|
|
||||||
*/
|
|
||||||
interface IConfigCache
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @param string $cat Config category
|
|
||||||
* @param string $key Config key
|
|
||||||
* @param mixed $default Default value if it isn't set
|
|
||||||
*
|
|
||||||
* @return mixed Returns the value of the Config entry
|
|
||||||
*/
|
|
||||||
function get($cat, $key = null, $default = null);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets a value in the config cache. Accepts raw output from the config table
|
|
||||||
*
|
|
||||||
* @param string $cat Config category
|
|
||||||
* @param string $key Config key
|
|
||||||
* @param mixed $value Value to set
|
|
||||||
*
|
|
||||||
* @return bool True, if the value is set
|
|
||||||
*/
|
|
||||||
function set($cat, $key, $value);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Deletes a value from the config cache
|
|
||||||
*
|
|
||||||
* @param string $cat Config category
|
|
||||||
* @param string $key Config key
|
|
||||||
*/
|
|
||||||
function delete($cat, $key);
|
|
||||||
|
|
||||||
function getAll();
|
|
||||||
}
|
|
|
@ -1,44 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Friendica\Core\Config;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The interface for a user-specific config cache
|
|
||||||
*/
|
|
||||||
interface IPConfigCache
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Retrieves a value from the user config cache
|
|
||||||
*
|
|
||||||
* @param int $uid User Id
|
|
||||||
* @param string $cat Config category
|
|
||||||
* @param string $key Config key
|
|
||||||
* @param mixed $default Default value if key isn't set
|
|
||||||
*
|
|
||||||
* @return string The value of the config entry
|
|
||||||
*/
|
|
||||||
function getP($uid, $cat, $key = null, $default = null);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets a value in the user config cache
|
|
||||||
*
|
|
||||||
* Accepts raw output from the pconfig table
|
|
||||||
*
|
|
||||||
* @param int $uid User Id
|
|
||||||
* @param string $cat Config category
|
|
||||||
* @param string $key Config key
|
|
||||||
* @param mixed $value Value to set
|
|
||||||
*/
|
|
||||||
function setP($uid, $cat, $key, $value);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Deletes a value from the user config cache
|
|
||||||
*
|
|
||||||
* @param int $uid User Id
|
|
||||||
* @param string $cat Config category
|
|
||||||
* @param string $key Config key
|
|
||||||
*/
|
|
||||||
function deleteP($uid, $cat, $key);
|
|
||||||
|
|
||||||
function getAll();
|
|
||||||
}
|
|
|
@ -1,172 +0,0 @@
|
||||||
<?php
|
|
||||||
namespace Friendica\Core\Config;
|
|
||||||
|
|
||||||
use Friendica\Database\DBA;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* JustInTime Configuration Adapter
|
|
||||||
*
|
|
||||||
* Default Config Adapter. Provides the best performance for pages loading few configuration variables.
|
|
||||||
*
|
|
||||||
* @author Hypolite Petovan <hypolite@mrpetovan.com>
|
|
||||||
*/
|
|
||||||
class JITConfigAdapter extends AbstractDbaConfigAdapter implements IConfigAdapter
|
|
||||||
{
|
|
||||||
private $cache;
|
|
||||||
private $in_db;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var IConfigCache The config cache of this driver
|
|
||||||
*/
|
|
||||||
private $configCache;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param IConfigCache $configCache The config cache of this driver
|
|
||||||
*/
|
|
||||||
public function __construct(IConfigCache $configCache)
|
|
||||||
{
|
|
||||||
$this->configCache = $configCache;
|
|
||||||
$this->connected = DBA::connected();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function load($cat = "config")
|
|
||||||
{
|
|
||||||
if (!$this->isConnected()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// We don't preload "system" anymore.
|
|
||||||
// This reduces the number of database reads a lot.
|
|
||||||
if ($cat === 'system') {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$configs = DBA::select('config', ['v', 'k'], ['cat' => $cat]);
|
|
||||||
while ($config = DBA::fetch($configs)) {
|
|
||||||
$k = $config['k'];
|
|
||||||
|
|
||||||
$this->configCache->set($cat, $k, $config['v']);
|
|
||||||
|
|
||||||
if ($cat !== 'config') {
|
|
||||||
$this->cache[$cat][$k] = $config['v'];
|
|
||||||
$this->in_db[$cat][$k] = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
DBA::close($configs);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function get($cat, $k, $default_value = null, $refresh = false)
|
|
||||||
{
|
|
||||||
if (!$this->isConnected()) {
|
|
||||||
return $default_value;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$refresh) {
|
|
||||||
// Do we have the cached value? Then return it
|
|
||||||
if (isset($this->cache[$cat][$k])) {
|
|
||||||
if ($this->cache[$cat][$k] === '!<unset>!') {
|
|
||||||
return $default_value;
|
|
||||||
} else {
|
|
||||||
return $this->cache[$cat][$k];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]);
|
|
||||||
if (DBA::isResult($config)) {
|
|
||||||
// manage array value
|
|
||||||
$value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
|
|
||||||
|
|
||||||
// Assign the value from the database to the cache
|
|
||||||
$this->cache[$cat][$k] = $value;
|
|
||||||
$this->in_db[$cat][$k] = true;
|
|
||||||
return $value;
|
|
||||||
} elseif ($this->configCache->get($cat, $k) !== null) {
|
|
||||||
// Assign the value (mostly) from config/local.config.php file to the cache
|
|
||||||
$this->cache[$cat][$k] = $this->configCache->get($cat, $k);
|
|
||||||
$this->in_db[$cat][$k] = false;
|
|
||||||
|
|
||||||
return $this->configCache->get($cat, $k);
|
|
||||||
} elseif ($this->configCache->get('config', $k) !== null) {
|
|
||||||
// Assign the value (mostly) from config/local.config.php file to the cache
|
|
||||||
$this->cache[$k] = $this->configCache->get('config', $k);
|
|
||||||
$this->in_db[$k] = false;
|
|
||||||
|
|
||||||
return $this->configCache->get('config', $k);
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->cache[$cat][$k] = '!<unset>!';
|
|
||||||
$this->in_db[$cat][$k] = false;
|
|
||||||
|
|
||||||
return $default_value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function set($cat, $k, $value)
|
|
||||||
{
|
|
||||||
if (!$this->isConnected()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// We store our setting values in a string variable.
|
|
||||||
// So we have to do the conversion here so that the compare below works.
|
|
||||||
// The exception are array values.
|
|
||||||
$dbvalue = (!is_array($value) ? (string)$value : $value);
|
|
||||||
|
|
||||||
$stored = $this->get($cat, $k, null, true);
|
|
||||||
|
|
||||||
if (!isset($this->in_db[$cat])) {
|
|
||||||
$this->in_db[$cat] = [];
|
|
||||||
}
|
|
||||||
if (!isset($this->in_db[$cat][$k])) {
|
|
||||||
$this->in_db[$cat] = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (($stored === $dbvalue) && $this->in_db[$cat][$k]) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->configCache->set($cat, $k, $value);
|
|
||||||
|
|
||||||
// Assign the just added value to the cache
|
|
||||||
$this->cache[$cat][$k] = $dbvalue;
|
|
||||||
|
|
||||||
// manage array value
|
|
||||||
$dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
|
|
||||||
|
|
||||||
$result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $k], true);
|
|
||||||
|
|
||||||
if ($result) {
|
|
||||||
$this->in_db[$cat][$k] = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function delete($cat, $k)
|
|
||||||
{
|
|
||||||
if (!$this->isConnected()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isset($this->cache[$cat][$k])) {
|
|
||||||
unset($this->cache[$cat][$k]);
|
|
||||||
unset($this->in_db[$cat][$k]);
|
|
||||||
}
|
|
||||||
|
|
||||||
$result = DBA::delete('config', ['cat' => $cat, 'k' => $k]);
|
|
||||||
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,136 +0,0 @@
|
||||||
<?php
|
|
||||||
namespace Friendica\Core\Config;
|
|
||||||
|
|
||||||
use Friendica\Database\DBA;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* JustInTime User Configuration Adapter
|
|
||||||
*
|
|
||||||
* Default PConfig Adapter. Provides the best performance for pages loading few configuration variables.
|
|
||||||
*
|
|
||||||
* @author Hypolite Petovan <hypolite@mrpetovan.com>
|
|
||||||
*/
|
|
||||||
class JITPConfigAdapter implements IPConfigAdapter
|
|
||||||
{
|
|
||||||
private $in_db;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The config cache of this adapter
|
|
||||||
* @var IPConfigCache
|
|
||||||
*/
|
|
||||||
private $configCache;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param IPConfigCache $configCache The config cache of this adapter
|
|
||||||
*/
|
|
||||||
public function __construct(IPConfigCache $configCache)
|
|
||||||
{
|
|
||||||
$this->configCache = $configCache;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function load($uid, $cat)
|
|
||||||
{
|
|
||||||
$pconfigs = DBA::select('pconfig', ['v', 'k'], ['cat' => $cat, 'uid' => $uid]);
|
|
||||||
if (DBA::isResult($pconfigs)) {
|
|
||||||
while ($pconfig = DBA::fetch($pconfigs)) {
|
|
||||||
$k = $pconfig['k'];
|
|
||||||
|
|
||||||
$this->configCache->setP($uid, $cat, $k, $pconfig['v']);
|
|
||||||
|
|
||||||
$this->in_db[$uid][$cat][$k] = true;
|
|
||||||
}
|
|
||||||
} else if ($cat != 'config') {
|
|
||||||
// Negative caching
|
|
||||||
$this->configCache->setP($uid, $cat, null, "!<unset>!");
|
|
||||||
}
|
|
||||||
DBA::close($pconfigs);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function get($uid, $cat, $k, $default_value = null, $refresh = false)
|
|
||||||
{
|
|
||||||
if (!$refresh) {
|
|
||||||
// Looking if the whole family isn't set
|
|
||||||
if ($this->configCache->getP($uid, $cat) !== null) {
|
|
||||||
if ($this->configCache->getP($uid, $cat) === '!<unset>!') {
|
|
||||||
return $default_value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->configCache->getP($uid, $cat, $k) !== null) {
|
|
||||||
if ($this->configCache->getP($uid, $cat, $k) === '!<unset>!') {
|
|
||||||
return $default_value;
|
|
||||||
}
|
|
||||||
return $this->configCache->getP($uid, $cat, $k);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$pconfig = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
|
|
||||||
if (DBA::isResult($pconfig)) {
|
|
||||||
$val = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']);
|
|
||||||
|
|
||||||
$this->configCache->setP($uid, $cat, $k, $val);
|
|
||||||
|
|
||||||
$this->in_db[$uid][$cat][$k] = true;
|
|
||||||
|
|
||||||
return $val;
|
|
||||||
} else {
|
|
||||||
$this->configCache->setP($uid, $cat, $k, '!<unset>!');
|
|
||||||
|
|
||||||
$this->in_db[$uid][$cat][$k] = false;
|
|
||||||
|
|
||||||
return $default_value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function set($uid, $cat, $k, $value)
|
|
||||||
{
|
|
||||||
// We store our setting values in a string variable.
|
|
||||||
// So we have to do the conversion here so that the compare below works.
|
|
||||||
// The exception are array values.
|
|
||||||
$dbvalue = (!is_array($value) ? (string)$value : $value);
|
|
||||||
|
|
||||||
$stored = $this->get($uid, $cat, $k, null, true);
|
|
||||||
|
|
||||||
if (($stored === $dbvalue) && $this->in_db[$uid][$cat][$k]) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->configCache->setP($uid, $cat, $k, $value);
|
|
||||||
|
|
||||||
// manage array value
|
|
||||||
$dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
|
|
||||||
|
|
||||||
$result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
|
|
||||||
|
|
||||||
if ($result) {
|
|
||||||
$this->in_db[$uid][$cat][$k] = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function delete($uid, $cat, $k)
|
|
||||||
{
|
|
||||||
$this->configCache->deleteP($uid, $cat, $k);
|
|
||||||
|
|
||||||
if (!empty($this->in_db[$uid][$cat][$k])) {
|
|
||||||
unset($this->in_db[$uid][$cat][$k]);
|
|
||||||
}
|
|
||||||
|
|
||||||
$result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
|
|
||||||
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
}
|
|
150
src/Core/Config/PConfiguration.php
Normal file
150
src/Core/Config/PConfiguration.php
Normal file
|
@ -0,0 +1,150 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Friendica\Core\Config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class is responsible for the user-specific configuration values in Friendica
|
||||||
|
* The values are set through the Config-DB-Table (per Config-DB-adapter @see Adapter\IPConfigAdapter )
|
||||||
|
*
|
||||||
|
* The configuration cache (@see Cache\IPConfigCache ) is used for temporary caching of database calls. This will
|
||||||
|
* increase the performance.
|
||||||
|
*/
|
||||||
|
class PConfiguration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var Cache\IPConfigCache
|
||||||
|
*/
|
||||||
|
private $configCache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Adapter\IPConfigAdapter
|
||||||
|
*/
|
||||||
|
private $configAdapter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Cache\IPConfigCache $configCache The configuration cache
|
||||||
|
* @param Adapter\IPConfigAdapter $configAdapter The configuration DB-backend
|
||||||
|
*/
|
||||||
|
public function __construct(Cache\IPConfigCache $configCache, Adapter\IPConfigAdapter $configAdapter)
|
||||||
|
{
|
||||||
|
$this->configCache = $configCache;
|
||||||
|
$this->configAdapter = $configAdapter;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Loads all configuration values of a user's config family into a cached storage.
|
||||||
|
*
|
||||||
|
* All configuration values of the given user are stored with the $uid in
|
||||||
|
* the cache ( @see IPConfigCache )
|
||||||
|
*
|
||||||
|
* @param string $uid The user_id
|
||||||
|
* @param string $cat The category of the configuration value
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function load($uid, $cat = 'config')
|
||||||
|
{
|
||||||
|
// If not connected, do nothing
|
||||||
|
if (!$this->configAdapter->isConnected()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// load the whole category out of the DB into the cache
|
||||||
|
$this->configCache->loadP($uid, $this->configAdapter->load($uid, $cat));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Get a particular user's config variable given the category name
|
||||||
|
* ($cat) and a key.
|
||||||
|
*
|
||||||
|
* Get a particular user's config value from the given category ($cat)
|
||||||
|
* and the $key with the $uid from a cached storage either from the $this->configAdapter
|
||||||
|
* (@see IConfigAdapter ) or from the $this->configCache (@see IConfigCache ).
|
||||||
|
*
|
||||||
|
* @param string $uid The user_id
|
||||||
|
* @param string $cat The category of the configuration value
|
||||||
|
* @param string $key The configuration key to query
|
||||||
|
* @param mixed $default_value optional, The value to return if key is not set (default: null)
|
||||||
|
* @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
|
||||||
|
*/
|
||||||
|
public function get($uid, $cat, $key, $default_value = null, $refresh = false)
|
||||||
|
{
|
||||||
|
// Return the value of the cache if found and no refresh is forced
|
||||||
|
if (!$refresh && $this->configCache->hasP($uid, $cat, $key)) {
|
||||||
|
return $this->configCache->getP($uid, $cat, $key);
|
||||||
|
}
|
||||||
|
|
||||||
|
// if we don't find the value in the cache and the adapter isn't ready, return the default value
|
||||||
|
if (!$this->configAdapter->isConnected()) {
|
||||||
|
return $default_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// load DB value to cache
|
||||||
|
$dbvalue = $this->configAdapter->get($uid, $cat, $key);
|
||||||
|
|
||||||
|
if ($dbvalue !== '!<unset>!') {
|
||||||
|
$this->configCache->setP($uid, $cat, $key, $dbvalue);
|
||||||
|
return $dbvalue;
|
||||||
|
} else {
|
||||||
|
return $default_value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Sets a configuration value for a user
|
||||||
|
*
|
||||||
|
* Stores a config value ($value) in the category ($family) under the key ($key)
|
||||||
|
* for the user_id $uid.
|
||||||
|
*
|
||||||
|
* @note Please do not store booleans - convert to 0/1 integer values!
|
||||||
|
*
|
||||||
|
* @param string $uid The user_id
|
||||||
|
* @param string $cat The category of the configuration value
|
||||||
|
* @param string $key The configuration key to set
|
||||||
|
* @param mixed $value The value to store
|
||||||
|
*
|
||||||
|
* @return bool Operation success
|
||||||
|
*/
|
||||||
|
public function set($uid, $cat, $key, $value)
|
||||||
|
{
|
||||||
|
// set the cache first
|
||||||
|
$cached = $this->configCache->setP($uid, $cat, $key, $value);
|
||||||
|
|
||||||
|
// If there is no connected adapter, we're finished
|
||||||
|
if (!$this->configAdapter->isConnected()) {
|
||||||
|
return $cached;
|
||||||
|
}
|
||||||
|
|
||||||
|
$stored = $this->configAdapter->set($uid, $cat, $key, $value);
|
||||||
|
|
||||||
|
return $cached && $stored;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Deletes the given key from the users's configuration.
|
||||||
|
*
|
||||||
|
* Removes the configured value from the stored cache in $this->configCache
|
||||||
|
* (@see ConfigCache ) and removes it from the database (@see IConfigAdapter )
|
||||||
|
* with the given $uid.
|
||||||
|
*
|
||||||
|
* @param string $uid The user_id
|
||||||
|
* @param string $cat The category of the configuration value
|
||||||
|
* @param string $key The configuration key to delete
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function delete($uid, $cat, $key)
|
||||||
|
{
|
||||||
|
$cacheRemoved = $this->configCache->deleteP($uid, $cat, $key);
|
||||||
|
|
||||||
|
if (!$this->configAdapter->isConnected()) {
|
||||||
|
return $cacheRemoved;
|
||||||
|
}
|
||||||
|
|
||||||
|
$storeRemoved = $this->configAdapter->delete($uid, $cat, $key);
|
||||||
|
|
||||||
|
return $cacheRemoved || $storeRemoved;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,123 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Friendica\Core\Config;
|
|
||||||
|
|
||||||
use Exception;
|
|
||||||
use Friendica\Database\DBA;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Preload Configuration Adapter
|
|
||||||
*
|
|
||||||
* Minimizes the number of database queries to retrieve configuration values at the cost of memory.
|
|
||||||
*
|
|
||||||
* @author Hypolite Petovan <hypolite@mrpetovan.com>
|
|
||||||
*/
|
|
||||||
class PreloadConfigAdapter extends AbstractDbaConfigAdapter implements IConfigAdapter
|
|
||||||
{
|
|
||||||
private $config_loaded = false;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var IConfigCache The config cache of this driver
|
|
||||||
*/
|
|
||||||
private $configCache;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param IConfigCache $configCache The config cache of this driver
|
|
||||||
*/
|
|
||||||
public function __construct(IConfigCache $configCache)
|
|
||||||
{
|
|
||||||
$this->configCache = $configCache;
|
|
||||||
$this->connected = DBA::connected();
|
|
||||||
$this->load();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function load($family = 'config')
|
|
||||||
{
|
|
||||||
if (!$this->isConnected()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->config_loaded) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$configs = DBA::select('config', ['cat', 'v', 'k']);
|
|
||||||
while ($config = DBA::fetch($configs)) {
|
|
||||||
$this->configCache->set($config['cat'], $config['k'], $config['v']);
|
|
||||||
}
|
|
||||||
DBA::close($configs);
|
|
||||||
|
|
||||||
$this->config_loaded = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function get($cat, $k, $default_value = null, $refresh = false)
|
|
||||||
{
|
|
||||||
if (!$this->isConnected()) {
|
|
||||||
return $default_value;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($refresh) {
|
|
||||||
$config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]);
|
|
||||||
if (DBA::isResult($config)) {
|
|
||||||
$this->configCache->set($cat, $k, $config['v']);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$return = $this->configCache->get($cat, $k, $default_value);
|
|
||||||
|
|
||||||
return $return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function set($cat, $k, $value)
|
|
||||||
{
|
|
||||||
if (!$this->isConnected()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// We store our setting values as strings.
|
|
||||||
// So we have to do the conversion here so that the compare below works.
|
|
||||||
// The exception are array values.
|
|
||||||
$compare_value = !is_array($value) ? (string)$value : $value;
|
|
||||||
|
|
||||||
if ($this->configCache->get($cat, $k) === $compare_value) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->configCache->set($cat, $k, $value);
|
|
||||||
|
|
||||||
// manage array value
|
|
||||||
$dbvalue = is_array($value) ? serialize($value) : $value;
|
|
||||||
|
|
||||||
$result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $k], true);
|
|
||||||
if (!$result) {
|
|
||||||
throw new Exception('Unable to store config value in [' . $cat . '][' . $k . ']');
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function delete($cat, $k)
|
|
||||||
{
|
|
||||||
if (!$this->isConnected()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->configCache->delete($cat, $k);
|
|
||||||
|
|
||||||
$result = DBA::delete('config', ['cat' => $cat, 'k' => $k]);
|
|
||||||
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,125 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Friendica\Core\Config;
|
|
||||||
|
|
||||||
use Exception;
|
|
||||||
use Friendica\Database\DBA;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Preload User Configuration Adapter
|
|
||||||
*
|
|
||||||
* Minimizes the number of database queries to retrieve configuration values at the cost of memory.
|
|
||||||
*
|
|
||||||
* @author Hypolite Petovan <hypolite@mrpetovan.com>
|
|
||||||
*/
|
|
||||||
class PreloadPConfigAdapter implements IPConfigAdapter
|
|
||||||
{
|
|
||||||
private $config_loaded = false;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The config cache of this adapter
|
|
||||||
* @var IPConfigCache
|
|
||||||
*/
|
|
||||||
private $configCache;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param IPConfigCache $configCache The config cache of this adapter
|
|
||||||
* @param int $uid The UID of the current user
|
|
||||||
*/
|
|
||||||
public function __construct(IPConfigCache $configCache, $uid = null)
|
|
||||||
{
|
|
||||||
$this->configCache = $configCache;
|
|
||||||
if (isset($uid)) {
|
|
||||||
$this->load($uid, 'config');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function load($uid, $family)
|
|
||||||
{
|
|
||||||
if ($this->config_loaded) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (empty($uid)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$pconfigs = DBA::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
|
|
||||||
while ($pconfig = DBA::fetch($pconfigs)) {
|
|
||||||
$this->configCache->setP($uid, $pconfig['cat'], $pconfig['k'], $pconfig['v']);
|
|
||||||
}
|
|
||||||
DBA::close($pconfigs);
|
|
||||||
|
|
||||||
$this->config_loaded = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function get($uid, $cat, $k, $default_value = null, $refresh = false)
|
|
||||||
{
|
|
||||||
if (!$this->config_loaded) {
|
|
||||||
$this->load($uid, $cat);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($refresh) {
|
|
||||||
$config = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
|
|
||||||
if (DBA::isResult($config)) {
|
|
||||||
$this->configCache->setP($uid, $cat, $k, $config['v']);
|
|
||||||
} else {
|
|
||||||
$this->configCache->deleteP($uid, $cat, $k);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->configCache->getP($uid, $cat, $k, $default_value);;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function set($uid, $cat, $k, $value)
|
|
||||||
{
|
|
||||||
if (!$this->config_loaded) {
|
|
||||||
$this->load($uid, $cat);
|
|
||||||
}
|
|
||||||
// We store our setting values as strings.
|
|
||||||
// So we have to do the conversion here so that the compare below works.
|
|
||||||
// The exception are array values.
|
|
||||||
$compare_value = !is_array($value) ? (string)$value : $value;
|
|
||||||
|
|
||||||
if ($this->configCache->getP($uid, $cat, $k) === $compare_value) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->configCache->setP($uid, $cat, $k, $value);
|
|
||||||
|
|
||||||
// manage array value
|
|
||||||
$dbvalue = is_array($value) ? serialize($value) : $value;
|
|
||||||
|
|
||||||
$result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
|
|
||||||
if (!$result) {
|
|
||||||
throw new Exception('Unable to store config value in [' . $uid . '][' . $cat . '][' . $k . ']');
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function delete($uid, $cat, $k)
|
|
||||||
{
|
|
||||||
if (!$this->config_loaded) {
|
|
||||||
$this->load($uid, $cat);
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->configCache->deleteP($uid, $cat, $k);
|
|
||||||
|
|
||||||
$result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
|
|
||||||
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -100,10 +100,10 @@ HELP;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$db_host = $a->getConfig()->get('database', 'hostname');
|
$db_host = $a->getConfigCache()->get('database', 'hostname');
|
||||||
$db_user = $a->getConfig()->get('database', 'username');
|
$db_user = $a->getConfigCache()->get('database', 'username');
|
||||||
$db_pass = $a->getConfig()->get('database', 'password');
|
$db_pass = $a->getConfigCache()->get('database', 'password');
|
||||||
$db_data = $a->getConfig()->get('database', 'database');
|
$db_data = $a->getConfigCache()->get('database', 'database');
|
||||||
} else {
|
} else {
|
||||||
// Creating config file
|
// Creating config file
|
||||||
$this->out("Creating config file...\n");
|
$this->out("Creating config file...\n");
|
||||||
|
@ -146,7 +146,7 @@ HELP;
|
||||||
|
|
||||||
$installer->resetChecks();
|
$installer->resetChecks();
|
||||||
|
|
||||||
if (!$installer->checkDB($a->getConfig(), $a->getProfiler(), $db_host, $db_user, $db_pass, $db_data)) {
|
if (!$installer->checkDB($a->getConfigCache(), $a->getProfiler(), $db_host, $db_user, $db_pass, $db_data)) {
|
||||||
$errorMessage = $this->extractErrors($installer->getChecks());
|
$errorMessage = $this->extractErrors($installer->getChecks());
|
||||||
throw new RuntimeException($errorMessage);
|
throw new RuntimeException($errorMessage);
|
||||||
}
|
}
|
||||||
|
|
|
@ -124,9 +124,9 @@ HELP;
|
||||||
$cat = $this->getArgument(0);
|
$cat = $this->getArgument(0);
|
||||||
Core\Config::load($cat);
|
Core\Config::load($cat);
|
||||||
|
|
||||||
if ($a->getConfig()->get($cat) !== null) {
|
if ($a->getConfigCache()->get($cat) !== null) {
|
||||||
$this->out("[{$cat}]");
|
$this->out("[{$cat}]");
|
||||||
$catVal = $a->getConfig()->get($cat);
|
$catVal = $a->getConfigCache()->get($cat);
|
||||||
foreach ($catVal as $key => $value) {
|
foreach ($catVal as $key => $value) {
|
||||||
if (is_array($value)) {
|
if (is_array($value)) {
|
||||||
foreach ($value as $k => $v) {
|
foreach ($value as $k => $v) {
|
||||||
|
@ -148,7 +148,7 @@ HELP;
|
||||||
$this->out('Warning: The JIT (Just In Time) Config adapter doesn\'t support loading the entire configuration, showing file config only');
|
$this->out('Warning: The JIT (Just In Time) Config adapter doesn\'t support loading the entire configuration, showing file config only');
|
||||||
}
|
}
|
||||||
|
|
||||||
$config = $a->getConfig()->getAll();
|
$config = $a->getConfigCache()->getAll();
|
||||||
foreach ($config as $cat => $section) {
|
foreach ($config as $cat => $section) {
|
||||||
if (is_array($section)) {
|
if (is_array($section)) {
|
||||||
foreach ($section as $key => $value) {
|
foreach ($section as $key => $value) {
|
||||||
|
|
|
@ -43,7 +43,7 @@ HELP;
|
||||||
throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
|
throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
|
||||||
}
|
}
|
||||||
|
|
||||||
$php_path = BaseObject::getApp()->getConfig()->get('config', 'php_path', 'php');
|
$php_path = BaseObject::getApp()->getConfigCache()->get('config', 'php_path', 'php');
|
||||||
|
|
||||||
if ($this->getOption('v')) {
|
if ($this->getOption('v')) {
|
||||||
$this->out('Directory: src');
|
$this->out('Directory: src');
|
||||||
|
|
|
@ -6,7 +6,7 @@ namespace Friendica\Core;
|
||||||
|
|
||||||
use DOMDocument;
|
use DOMDocument;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Friendica\Core\Config\ConfigCache;
|
use Friendica\Core\Config\Cache\IConfigCache;
|
||||||
use Friendica\Database\DBA;
|
use Friendica\Database\DBA;
|
||||||
use Friendica\Database\DBStructure;
|
use Friendica\Database\DBStructure;
|
||||||
use Friendica\Object\Image;
|
use Friendica\Object\Image;
|
||||||
|
@ -583,7 +583,7 @@ class Installer
|
||||||
/**
|
/**
|
||||||
* Checking the Database connection and if it is available for the current installation
|
* Checking the Database connection and if it is available for the current installation
|
||||||
*
|
*
|
||||||
* @param ConfigCache $configCache The configuration cache
|
* @param IConfigCache $configCache The configuration cache
|
||||||
* @param Profiler $profiler The profiler of this app
|
* @param Profiler $profiler The profiler of this app
|
||||||
* @param string $dbhost Hostname/IP of the Friendica Database
|
* @param string $dbhost Hostname/IP of the Friendica Database
|
||||||
* @param string $dbuser Username of the Database connection credentials
|
* @param string $dbuser Username of the Database connection credentials
|
||||||
|
@ -593,7 +593,7 @@ class Installer
|
||||||
* @return bool true if the check was successful, otherwise false
|
* @return bool true if the check was successful, otherwise false
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function checkDB(ConfigCache $configCache, Profiler $profiler, $dbhost, $dbuser, $dbpass, $dbdata)
|
public function checkDB(IConfigCache $configCache, Profiler $profiler, $dbhost, $dbuser, $dbpass, $dbdata)
|
||||||
{
|
{
|
||||||
if (!DBA::connect($configCache, $profiler, $dbhost, $dbuser, $dbpass, $dbdata)) {
|
if (!DBA::connect($configCache, $profiler, $dbhost, $dbuser, $dbpass, $dbdata)) {
|
||||||
$this->addCheck(L10n::t('Could not connect to database.'), false, true, '');
|
$this->addCheck(L10n::t('Could not connect to database.'), false, true, '');
|
||||||
|
|
|
@ -18,123 +18,78 @@ namespace Friendica\Core;
|
||||||
class PConfig
|
class PConfig
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var Config\IPConfigAdapter
|
* @var Config\PConfiguration
|
||||||
*/
|
*/
|
||||||
private static $adapter;
|
private static $config;
|
||||||
|
|
||||||
/**
|
|
||||||
* @var Config\IPConfigCache
|
|
||||||
*/
|
|
||||||
private static $cache;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize the config with only the cache
|
* Initialize the config with only the cache
|
||||||
*
|
*
|
||||||
* @param Config\IPConfigCache $cache The configuration cache
|
* @param Config\PConfiguration $config The configuration cache
|
||||||
*/
|
*/
|
||||||
public static function init(Config\IPConfigCache $cache)
|
public static function init(Config\PConfiguration $config)
|
||||||
{
|
{
|
||||||
self::$cache = $cache;
|
self::$config = $config;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add the adapter for DB-backend
|
|
||||||
*
|
|
||||||
* @param Config\IPConfigAdapter $adapter
|
|
||||||
*/
|
|
||||||
public static function setAdapter(Config\IPConfigAdapter $adapter)
|
|
||||||
{
|
|
||||||
self::$adapter = $adapter;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Loads all configuration values of a user's config family into a cached storage.
|
* @brief Loads all configuration values of a user's config family into a cached storage.
|
||||||
*
|
*
|
||||||
* All configuration values of the given user are stored with the $uid in
|
* @param string $uid The user_id
|
||||||
* the cache ( @see IPConfigCache )
|
* @param string $cat The category of the configuration value
|
||||||
*
|
|
||||||
* @param string $uid The user_id
|
|
||||||
* @param string $family The category of the configuration value
|
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public static function load($uid, $family)
|
public static function load($uid, $cat)
|
||||||
{
|
{
|
||||||
if (!isset(self::$adapter)) {
|
self::$config->load($uid, $cat);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
self::$adapter->load($uid, $family);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get a particular user's config variable given the category name
|
* @brief Get a particular user's config variable given the category name
|
||||||
* ($family) and a key.
|
* ($cat) and a key.
|
||||||
*
|
|
||||||
* Get a particular user's config value from the given category ($family)
|
|
||||||
* and the $key with the $uid from a cached storage either from the self::$adapter
|
|
||||||
* (@see IConfigAdapter ) or from the static::$cache (@see IConfigCache ).
|
|
||||||
*
|
*
|
||||||
* @param string $uid The user_id
|
* @param string $uid The user_id
|
||||||
* @param string $family The category of the configuration value
|
* @param string $cat The category of the configuration value
|
||||||
* @param string $key The configuration key to query
|
* @param string $key The configuration key to query
|
||||||
* @param mixed $default_value optional, The value to return if key is not set (default: null)
|
* @param mixed $default_value optional, The value to return if key is not set (default: null)
|
||||||
* @param boolean $refresh optional, If true the config is loaded from the db and not from the cache (default: false)
|
* @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
|
* @return mixed Stored value or null if it does not exist
|
||||||
*/
|
*/
|
||||||
public static function get($uid, $family, $key, $default_value = null, $refresh = false)
|
public static function get($uid, $cat, $key, $default_value = null, $refresh = false)
|
||||||
{
|
{
|
||||||
if (!isset(self::$adapter)) {
|
return self::$config->get($uid, $cat, $key, $default_value, $refresh);
|
||||||
return self::$cache->getP($uid, $family, $key, $default_value);
|
|
||||||
}
|
|
||||||
|
|
||||||
return self::$adapter->get($uid, $family, $key, $default_value, $refresh);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Sets a configuration value for a user
|
* @brief Sets a configuration value for a user
|
||||||
*
|
*
|
||||||
* Stores a config value ($value) in the category ($family) under the key ($key)
|
|
||||||
* for the user_id $uid.
|
|
||||||
*
|
|
||||||
* @note Please do not store booleans - convert to 0/1 integer values!
|
* @note Please do not store booleans - convert to 0/1 integer values!
|
||||||
*
|
*
|
||||||
* @param string $uid The user_id
|
* @param string $uid The user_id
|
||||||
* @param string $family The category of the configuration value
|
* @param string $cat The category of the configuration value
|
||||||
* @param string $key The configuration key to set
|
* @param string $key The configuration key to set
|
||||||
* @param mixed $value The value to store
|
* @param mixed $value The value to store
|
||||||
*
|
*
|
||||||
* @return bool Operation success
|
* @return bool Operation success
|
||||||
*/
|
*/
|
||||||
public static function set($uid, $family, $key, $value)
|
public static function set($uid, $cat, $key, $value)
|
||||||
{
|
{
|
||||||
if (!isset(self::$adapter)) {
|
return self::$config->set($uid, $cat, $key, $value);
|
||||||
return self::$cache->setP($uid, $family, $key, $value);
|
|
||||||
}
|
|
||||||
|
|
||||||
return self::$adapter->set($uid, $family, $key, $value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Deletes the given key from the users's configuration.
|
* @brief Deletes the given key from the users's configuration.
|
||||||
*
|
*
|
||||||
* Removes the configured value from the stored cache in self::$config
|
* @param string $uid The user_id
|
||||||
* (@see ConfigCache ) and removes it from the database (@see IConfigAdapter )
|
* @param string $cat The category of the configuration value
|
||||||
* with the given $uid.
|
* @param string $key The configuration key to delete
|
||||||
*
|
*
|
||||||
* @param string $uid The user_id
|
* @return bool
|
||||||
* @param string $family The category of the configuration value
|
|
||||||
* @param string $key The configuration key to delete
|
|
||||||
*
|
|
||||||
* @return mixed
|
|
||||||
*/
|
*/
|
||||||
public static function delete($uid, $family, $key)
|
public static function delete($uid, $cat, $key)
|
||||||
{
|
{
|
||||||
if (!isset(self::$adapter)) {
|
return self::$config->delete($uid, $cat, $key);
|
||||||
return self::$cache->deleteP($uid, $family, $key);
|
|
||||||
}
|
|
||||||
|
|
||||||
return self::$adapter->delete($uid, $family, $key);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
namespace Friendica\Database;
|
namespace Friendica\Database;
|
||||||
|
|
||||||
use Friendica\Core\Config\IConfigCache;
|
use Friendica\Core\Config\Cache\IConfigCache;
|
||||||
use Friendica\Core\Logger;
|
use Friendica\Core\Logger;
|
||||||
use Friendica\Core\System;
|
use Friendica\Core\System;
|
||||||
use Friendica\Util\DateTimeFormat;
|
use Friendica\Util\DateTimeFormat;
|
||||||
|
|
|
@ -2,51 +2,66 @@
|
||||||
|
|
||||||
namespace Friendica\Factory;
|
namespace Friendica\Factory;
|
||||||
|
|
||||||
|
use Friendica\Core;
|
||||||
use Friendica\Core\Config;
|
use Friendica\Core\Config;
|
||||||
|
use Friendica\Core\Config\Adapter;
|
||||||
|
use Friendica\Core\Config\Cache;
|
||||||
|
|
||||||
class ConfigFactory
|
class ConfigFactory
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @param Config\ConfigCacheLoader $loader The Config Cache loader (INI/config/.htconfig)
|
* @param Cache\ConfigCacheLoader $loader The Config Cache loader (INI/config/.htconfig)
|
||||||
*
|
*
|
||||||
* @return Config\ConfigCache
|
* @return Cache\ConfigCache
|
||||||
*/
|
*/
|
||||||
public static function createCache(Config\ConfigCacheLoader $loader)
|
public static function createCache(Cache\ConfigCacheLoader $loader)
|
||||||
{
|
{
|
||||||
$configCache = new Config\ConfigCache();
|
$configCache = new Cache\ConfigCache();
|
||||||
$loader->loadConfigFiles($configCache);
|
$loader->loadConfigFiles($configCache);
|
||||||
|
|
||||||
return $configCache;
|
return $configCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $type The adapter type
|
* @param Cache\ConfigCache $configCache The config cache of this adapter
|
||||||
* @param Config\IConfigCache $config The config cache of this adapter
|
|
||||||
*
|
*
|
||||||
* @return Config\IConfigAdapter
|
* @return Config\Configuration
|
||||||
*/
|
*/
|
||||||
public static function createConfig($type, Config\IConfigCache $config)
|
public static function createConfig(Cache\ConfigCache $configCache)
|
||||||
{
|
{
|
||||||
if ($type == 'preload') {
|
if ($configCache->get('system', 'config_adapter') === 'preload') {
|
||||||
return new Config\PreloadConfigAdapter($config);
|
$configAdapter = new Adapter\PreloadConfigAdapter();
|
||||||
} else {
|
} else {
|
||||||
return new Config\JITConfigAdapter($config);
|
$configAdapter = new Adapter\JITConfigAdapter();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$configuration = new Config\Configuration($configCache, $configAdapter);
|
||||||
|
|
||||||
|
// Set the config in the static container for legacy usage
|
||||||
|
Core\Config::init($configuration);
|
||||||
|
|
||||||
|
return $configuration;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $type The adapter type
|
* @param Cache\ConfigCache $configCache The config cache of this adapter
|
||||||
* @param Config\IPConfigCache $config The config cache of this adapter
|
* @param int $uid The UID of the current user
|
||||||
* @param int $uid The UID of the current user
|
|
||||||
*
|
*
|
||||||
* @return Config\IPConfigAdapter
|
* @return Config\PConfiguration
|
||||||
*/
|
*/
|
||||||
public static function createPConfig($type, Config\IPConfigCache $config, $uid = null)
|
public static function createPConfig(Cache\ConfigCache $configCache, $uid = null)
|
||||||
{
|
{
|
||||||
if ($type == 'preload') {
|
if ($configCache->get('system', 'config_adapter') === 'preload') {
|
||||||
return new Config\PreloadPConfigAdapter($config, $uid);
|
$configAdapter = new Adapter\PreloadPConfigAdapter($uid);
|
||||||
} else {
|
} else {
|
||||||
return new Config\JITPConfigAdapter($config);
|
$configAdapter = new Adapter\JITPConfigAdapter();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$configuration = new Config\PConfiguration($configCache, $configAdapter);
|
||||||
|
|
||||||
|
// Set the config in the static container for legacy usage
|
||||||
|
Core\PConfig::init($configuration);
|
||||||
|
|
||||||
|
return $configuration;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
48
src/Factory/DBFactory.php
Normal file
48
src/Factory/DBFactory.php
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Friendica\Factory;
|
||||||
|
|
||||||
|
use Friendica\Core\Config\Cache;
|
||||||
|
use Friendica\Database;
|
||||||
|
|
||||||
|
class DBFactory
|
||||||
|
{
|
||||||
|
public static function init(Cache\ConfigCache $configCache, array $server)
|
||||||
|
{
|
||||||
|
if (Database\DBA::connected()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$db_host = $configCache->get('database', 'hostname');
|
||||||
|
$db_user = $configCache->get('database', 'username');
|
||||||
|
$db_pass = $configCache->get('database', 'password');
|
||||||
|
$db_data = $configCache->get('database', 'database');
|
||||||
|
$charset = $configCache->get('database', 'charset');
|
||||||
|
|
||||||
|
// Use environment variables for mysql if they are set beforehand
|
||||||
|
if (!empty($server['MYSQL_HOST'])
|
||||||
|
&& !empty($server['MYSQL_USERNAME'] || !empty($server['MYSQL_USER']))
|
||||||
|
&& $server['MYSQL_PASSWORD'] !== false
|
||||||
|
&& !empty($server['MYSQL_DATABASE']))
|
||||||
|
{
|
||||||
|
$db_host = $server['MYSQL_HOST'];
|
||||||
|
if (!empty($server['MYSQL_PORT'])) {
|
||||||
|
$db_host .= ':' . $server['MYSQL_PORT'];
|
||||||
|
}
|
||||||
|
if (!empty($server['MYSQL_USERNAME'])) {
|
||||||
|
$db_user = $server['MYSQL_USERNAME'];
|
||||||
|
} else {
|
||||||
|
$db_user = $server['MYSQL_USER'];
|
||||||
|
}
|
||||||
|
$db_pass = (string) $server['MYSQL_PASSWORD'];
|
||||||
|
$db_data = $server['MYSQL_DATABASE'];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Database\DBA::connect($configCache, $db_host, $db_user, $db_pass, $db_data, $charset)) {
|
||||||
|
// Loads DB_UPDATE_VERSION constant
|
||||||
|
Database\DBStructure::definition($configCache->get('system', 'basepath'), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
unset($db_host, $db_user, $db_pass, $db_data, $charset);
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
namespace Friendica\Factory;
|
namespace Friendica\Factory;
|
||||||
|
|
||||||
use Friendica\Core\Config\ConfigCache;
|
use Friendica\Core\Config\Configuration;
|
||||||
use Friendica\Core\Logger;
|
use Friendica\Core\Logger;
|
||||||
use Friendica\Network\HTTPException\InternalServerErrorException;
|
use Friendica\Network\HTTPException\InternalServerErrorException;
|
||||||
use Friendica\Util\Logger\FriendicaDevelopHandler;
|
use Friendica\Util\Logger\FriendicaDevelopHandler;
|
||||||
|
@ -22,12 +22,12 @@ class LoggerFactory
|
||||||
/**
|
/**
|
||||||
* Creates a new PSR-3 compliant logger instances
|
* Creates a new PSR-3 compliant logger instances
|
||||||
*
|
*
|
||||||
* @param string $channel The channel of the logger instance
|
* @param string $channel The channel of the logger instance
|
||||||
* @param ConfigCache $config The config
|
* @param Configuration $config The config
|
||||||
*
|
*
|
||||||
* @return LoggerInterface The PSR-3 compliant logger instance
|
* @return LoggerInterface The PSR-3 compliant logger instance
|
||||||
*/
|
*/
|
||||||
public static function create($channel, ConfigCache $config = null)
|
public static function create($channel, Configuration $config)
|
||||||
{
|
{
|
||||||
$logger = new Monolog\Logger($channel);
|
$logger = new Monolog\Logger($channel);
|
||||||
$logger->pushProcessor(new Monolog\Processor\PsrLogMessageProcessor());
|
$logger->pushProcessor(new Monolog\Processor\PsrLogMessageProcessor());
|
||||||
|
|
|
@ -75,7 +75,7 @@ class Install extends BaseModule
|
||||||
$dbdata = Strings::escapeTags(trim(defaults($_POST, 'dbdata', '')));
|
$dbdata = Strings::escapeTags(trim(defaults($_POST, 'dbdata', '')));
|
||||||
|
|
||||||
// If we cannot connect to the database, return to the previous step
|
// If we cannot connect to the database, return to the previous step
|
||||||
if (!self::$installer->checkDB($a->getConfig(), $a->getProfiler(), $dbhost, $dbuser, $dbpass, $dbdata)) {
|
if (!self::$installer->checkDB($a->getConfigCache(), $a->getProfiler(), $dbhost, $dbuser, $dbpass, $dbdata)) {
|
||||||
self::$currentWizardStep = self::DATABASE_CONFIG;
|
self::$currentWizardStep = self::DATABASE_CONFIG;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,7 +92,7 @@ class Install extends BaseModule
|
||||||
$adminmail = Strings::escapeTags(trim(defaults($_POST, 'adminmail', '')));
|
$adminmail = Strings::escapeTags(trim(defaults($_POST, 'adminmail', '')));
|
||||||
|
|
||||||
// If we cannot connect to the database, return to the Database config wizard
|
// If we cannot connect to the database, return to the Database config wizard
|
||||||
if (!self::$installer->checkDB($a->getConfig(), $a->getProfiler(), $dbhost, $dbuser, $dbpass, $dbdata)) {
|
if (!self::$installer->checkDB($a->getConfigCache(), $a->getProfiler(), $dbhost, $dbuser, $dbpass, $dbdata)) {
|
||||||
self::$currentWizardStep = self::DATABASE_CONFIG;
|
self::$currentWizardStep = self::DATABASE_CONFIG;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
namespace Friendica\Test;
|
namespace Friendica\Test;
|
||||||
|
|
||||||
use Friendica\Core\Config;
|
use Friendica\Core\Config\Cache;
|
||||||
use Friendica\Database\DBA;
|
use Friendica\Database\DBA;
|
||||||
use Friendica\Factory;
|
use Friendica\Factory;
|
||||||
use Friendica\Util\BasePath;
|
use Friendica\Util\BasePath;
|
||||||
|
@ -41,7 +41,7 @@ abstract class DatabaseTest extends MockedTest
|
||||||
}
|
}
|
||||||
|
|
||||||
$basedir = BasePath::create(dirname(__DIR__));
|
$basedir = BasePath::create(dirname(__DIR__));
|
||||||
$configLoader = new Config\ConfigCacheLoader($basedir);
|
$configLoader = new Cache\ConfigCacheLoader($basedir);
|
||||||
$config = Factory\ConfigFactory::createCache($configLoader);
|
$config = Factory\ConfigFactory::createCache($configLoader);
|
||||||
|
|
||||||
$profiler = \Mockery::mock(Profiler::class);
|
$profiler = \Mockery::mock(Profiler::class);
|
||||||
|
|
|
@ -5,7 +5,6 @@ namespace Friendica\Test\Util;
|
||||||
use Friendica\App;
|
use Friendica\App;
|
||||||
use Friendica\BaseObject;
|
use Friendica\BaseObject;
|
||||||
use Friendica\Core\Config;
|
use Friendica\Core\Config;
|
||||||
use Friendica\Core\Config\ConfigCache;
|
|
||||||
use Friendica\Render\FriendicaSmartyEngine;
|
use Friendica\Render\FriendicaSmartyEngine;
|
||||||
use Mockery\MockInterface;
|
use Mockery\MockInterface;
|
||||||
use org\bovigo\vfs\vfsStreamDirectory;
|
use org\bovigo\vfs\vfsStreamDirectory;
|
||||||
|
@ -21,7 +20,7 @@ trait AppMockTrait
|
||||||
protected $app;
|
protected $app;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var MockInterface|ConfigCache The mocked Config Cache
|
* @var MockInterface|Config\Configuration The mocked Config Cache
|
||||||
*/
|
*/
|
||||||
protected $configCache;
|
protected $configCache;
|
||||||
|
|
||||||
|
@ -29,9 +28,9 @@ trait AppMockTrait
|
||||||
* Mock the App
|
* Mock the App
|
||||||
*
|
*
|
||||||
* @param vfsStreamDirectory $root The root directory
|
* @param vfsStreamDirectory $root The root directory
|
||||||
* @param MockInterface|ConfigCache $config The config cache
|
* @param MockInterface|Config\Configuration $config The config cache
|
||||||
*/
|
*/
|
||||||
public function mockApp($root, $config)
|
public function mockApp($root, Config\Configuration $config)
|
||||||
{
|
{
|
||||||
$this->configCache = $config;
|
$this->configCache = $config;
|
||||||
// Mocking App and most used functions
|
// Mocking App and most used functions
|
||||||
|
@ -62,11 +61,14 @@ trait AppMockTrait
|
||||||
->andReturn('localhost');
|
->andReturn('localhost');
|
||||||
$config
|
$config
|
||||||
->shouldReceive('get')
|
->shouldReceive('get')
|
||||||
->with('system', 'theme', NULL)
|
->with('system', 'theme', NULL, false)
|
||||||
->andReturn('system_theme');
|
->andReturn('system_theme');
|
||||||
|
$config
|
||||||
|
->shouldReceive('getConfig')
|
||||||
|
->andReturn($config);
|
||||||
|
|
||||||
$this->app
|
$this->app
|
||||||
->shouldReceive('getConfig')
|
->shouldReceive('getConfigCache')
|
||||||
->andReturn($config);
|
->andReturn($config);
|
||||||
|
|
||||||
$this->app
|
$this->app
|
||||||
|
@ -84,11 +86,6 @@ trait AppMockTrait
|
||||||
|
|
||||||
// Initialize empty Config
|
// Initialize empty Config
|
||||||
Config::init($config);
|
Config::init($config);
|
||||||
$configAdapter = \Mockery::mock('Friendica\Core\Config\IConfigAdapter');
|
|
||||||
$configAdapter
|
|
||||||
->shouldReceive('isConnected')
|
|
||||||
->andReturn(false);
|
|
||||||
Config::setAdapter($configAdapter);
|
|
||||||
|
|
||||||
BaseObject::setApp($this->app);
|
BaseObject::setApp($this->app);
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ namespace Friendica\Test;
|
||||||
|
|
||||||
use Friendica\App;
|
use Friendica\App;
|
||||||
use Friendica\Core\Config;
|
use Friendica\Core\Config;
|
||||||
|
use Friendica\Core\Config\Cache;
|
||||||
use Friendica\Core\PConfig;
|
use Friendica\Core\PConfig;
|
||||||
use Friendica\Core\Protocol;
|
use Friendica\Core\Protocol;
|
||||||
use Friendica\Core\System;
|
use Friendica\Core\System;
|
||||||
|
@ -36,8 +37,11 @@ class ApiTest extends DatabaseTest
|
||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
$basedir = BasePath::create(dirname(__DIR__) . '/../');
|
$basedir = BasePath::create(dirname(__DIR__) . '/../');
|
||||||
$configLoader = new Config\ConfigCacheLoader($basedir);
|
$configLoader = new Cache\ConfigCacheLoader($basedir);
|
||||||
$config = Factory\ConfigFactory::createCache($configLoader);
|
$configCache = Factory\ConfigFactory::createCache($configLoader);
|
||||||
|
Factory\DBFactory::init($configCache, $_SERVER);
|
||||||
|
$config = Factory\ConfigFactory::createConfig($configCache);
|
||||||
|
$pconfig = Factory\ConfigFactory::createPConfig($configCache);
|
||||||
$logger = Factory\LoggerFactory::create('test', $config);
|
$logger = Factory\LoggerFactory::create('test', $config);
|
||||||
$profiler = Factory\ProfilerFactory::create($logger, $config);
|
$profiler = Factory\ProfilerFactory::create($logger, $config);
|
||||||
$this->app = new App($config, $logger, $profiler, false);
|
$this->app = new App($config, $logger, $profiler, false);
|
||||||
|
|
|
@ -90,19 +90,14 @@ class ModeTest extends MockedTest
|
||||||
$this->mockConnected(true, 1);
|
$this->mockConnected(true, 1);
|
||||||
$this->mockFetchFirst('SHOW TABLES LIKE \'config\'', true, 1);
|
$this->mockFetchFirst('SHOW TABLES LIKE \'config\'', true, 1);
|
||||||
|
|
||||||
$config = \Mockery::mock('Friendica\Core\Config\ConfigCache');
|
$config = \Mockery::mock('Friendica\Core\Config\Configuration');
|
||||||
$config
|
$config
|
||||||
->shouldReceive('get')
|
->shouldReceive('get')
|
||||||
->with('system', 'maintenance', null)
|
->with('system', 'maintenance', null, false)
|
||||||
->andReturn(true)
|
->andReturn(true)
|
||||||
->once();
|
->once();
|
||||||
// Initialize empty Config
|
// Initialize empty Config
|
||||||
Config::init($config);
|
Config::init($config);
|
||||||
$configAdapter = \Mockery::mock('Friendica\Core\Config\IConfigAdapter');
|
|
||||||
$configAdapter
|
|
||||||
->shouldReceive('isConnected')
|
|
||||||
->andReturn(false);
|
|
||||||
Config::setAdapter($configAdapter);
|
|
||||||
|
|
||||||
$mode = new Mode($this->root->url());
|
$mode = new Mode($this->root->url());
|
||||||
$mode->determine();
|
$mode->determine();
|
||||||
|
@ -123,19 +118,14 @@ class ModeTest extends MockedTest
|
||||||
$this->mockConnected(true, 1);
|
$this->mockConnected(true, 1);
|
||||||
$this->mockFetchFirst('SHOW TABLES LIKE \'config\'', true, 1);
|
$this->mockFetchFirst('SHOW TABLES LIKE \'config\'', true, 1);
|
||||||
|
|
||||||
$config = \Mockery::mock('Friendica\Core\Config\ConfigCache');
|
$config = \Mockery::mock('Friendica\Core\Config\Configuration');
|
||||||
$config
|
$config
|
||||||
->shouldReceive('get')
|
->shouldReceive('get')
|
||||||
->with('system', 'maintenance', null)
|
->with('system', 'maintenance', null, false)
|
||||||
->andReturn(false)
|
->andReturn(false)
|
||||||
->once();
|
->once();
|
||||||
// Initialize empty Config
|
// Initialize empty Config
|
||||||
Config::init($config);
|
Config::init($config);
|
||||||
$configAdapter = \Mockery::mock('Friendica\Core\Config\IConfigAdapter');
|
|
||||||
$configAdapter
|
|
||||||
->shouldReceive('isConnected')
|
|
||||||
->andReturn(false);
|
|
||||||
Config::setAdapter($configAdapter);
|
|
||||||
|
|
||||||
$mode = new Mode($this->root->url());
|
$mode = new Mode($this->root->url());
|
||||||
$mode->determine();
|
$mode->determine();
|
||||||
|
|
|
@ -31,7 +31,7 @@ class BaseObjectTest extends TestCase
|
||||||
{
|
{
|
||||||
$baseObject = new BaseObject();
|
$baseObject = new BaseObject();
|
||||||
$this->setUpVfsDir();
|
$this->setUpVfsDir();
|
||||||
$configMock = \Mockery::mock('Friendica\Core\Config\ConfigCache');
|
$configMock = \Mockery::mock('Friendica\Core\Config\Configuration');
|
||||||
$this->mockApp($this->root, $configMock);
|
$this->mockApp($this->root, $configMock);
|
||||||
|
|
||||||
$this->assertNull($baseObject->setApp($this->app));
|
$this->assertNull($baseObject->setApp($this->app));
|
||||||
|
|
|
@ -67,7 +67,7 @@ abstract class CacheTest extends MockedTest
|
||||||
protected function setUp()
|
protected function setUp()
|
||||||
{
|
{
|
||||||
$this->setUpVfsDir();
|
$this->setUpVfsDir();
|
||||||
$configMock = \Mockery::mock('Friendica\Core\Config\ConfigCache');
|
$configMock = \Mockery::mock('Friendica\Core\Config\Configuration');
|
||||||
$this->mockApp($this->root, $configMock);
|
$this->mockApp($this->root, $configMock);
|
||||||
$this->app
|
$this->app
|
||||||
->shouldReceive('getHostname')
|
->shouldReceive('getHostname')
|
||||||
|
|
|
@ -14,12 +14,12 @@ class MemcacheCacheDriverTest extends MemoryCacheTest
|
||||||
{
|
{
|
||||||
$this->configCache
|
$this->configCache
|
||||||
->shouldReceive('get')
|
->shouldReceive('get')
|
||||||
->with('system', 'memcache_host', NULL)
|
->with('system', 'memcache_host', NULL, false)
|
||||||
->andReturn('localhost');
|
->andReturn('localhost');
|
||||||
|
|
||||||
$this->configCache
|
$this->configCache
|
||||||
->shouldReceive('get')
|
->shouldReceive('get')
|
||||||
->with('system', 'memcache_port', NULL)
|
->with('system', 'memcache_port', NULL, false)
|
||||||
->andReturn(11211);
|
->andReturn(11211);
|
||||||
|
|
||||||
$this->cache = CacheDriverFactory::create('memcache');
|
$this->cache = CacheDriverFactory::create('memcache');
|
||||||
|
|
|
@ -14,7 +14,7 @@ class MemcachedCacheDriverTest extends MemoryCacheTest
|
||||||
{
|
{
|
||||||
$this->configCache
|
$this->configCache
|
||||||
->shouldReceive('get')
|
->shouldReceive('get')
|
||||||
->with('system', 'memcached_hosts', NULL)
|
->with('system', 'memcached_hosts', NULL, false)
|
||||||
->andReturn([0 => 'localhost, 11211']);
|
->andReturn([0 => 'localhost, 11211']);
|
||||||
|
|
||||||
$this->cache = CacheDriverFactory::create('memcached');
|
$this->cache = CacheDriverFactory::create('memcached');
|
||||||
|
|
|
@ -14,12 +14,12 @@ class RedisCacheDriverTest extends MemoryCacheTest
|
||||||
{
|
{
|
||||||
$this->configCache
|
$this->configCache
|
||||||
->shouldReceive('get')
|
->shouldReceive('get')
|
||||||
->with('system', 'redis_host', NULL)
|
->with('system', 'redis_host', NULL, false)
|
||||||
->andReturn('localhost');
|
->andReturn('localhost');
|
||||||
|
|
||||||
$this->configCache
|
$this->configCache
|
||||||
->shouldReceive('get')
|
->shouldReceive('get')
|
||||||
->with('system', 'redis_port', NULL)
|
->with('system', 'redis_port', NULL, false)
|
||||||
->andReturn(null);
|
->andReturn(null);
|
||||||
|
|
||||||
$this->cache = CacheDriverFactory::create('redis');
|
$this->cache = CacheDriverFactory::create('redis');
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Friendica\Test\Core\Config;
|
namespace Friendica\Test\Core\Config\Cache;
|
||||||
|
|
||||||
use Friendica\Core\Config\ConfigCache;
|
use Friendica\Core\Config\Cache\ConfigCache;
|
||||||
use Friendica\Core\Config\ConfigCacheLoader;
|
use Friendica\Core\Config\Cache\ConfigCacheLoader;
|
||||||
use Friendica\Test\MockedTest;
|
use Friendica\Test\MockedTest;
|
||||||
use Friendica\Test\Util\VFSTrait;
|
use Friendica\Test\Util\VFSTrait;
|
||||||
use org\bovigo\vfs\vfsStream;
|
use org\bovigo\vfs\vfsStream;
|
||||||
|
@ -59,6 +59,7 @@ class ConfigCacheLoaderTest extends MockedTest
|
||||||
$this->delConfigFile('local.config.php');
|
$this->delConfigFile('local.config.php');
|
||||||
|
|
||||||
$file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
|
$file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
|
||||||
|
'..' . DIRECTORY_SEPARATOR .
|
||||||
'..' . DIRECTORY_SEPARATOR .
|
'..' . DIRECTORY_SEPARATOR .
|
||||||
'..' . DIRECTORY_SEPARATOR .
|
'..' . DIRECTORY_SEPARATOR .
|
||||||
'datasets' . DIRECTORY_SEPARATOR .
|
'datasets' . DIRECTORY_SEPARATOR .
|
||||||
|
@ -91,6 +92,7 @@ class ConfigCacheLoaderTest extends MockedTest
|
||||||
$this->delConfigFile('local.config.php');
|
$this->delConfigFile('local.config.php');
|
||||||
|
|
||||||
$file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
|
$file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
|
||||||
|
'..' . DIRECTORY_SEPARATOR .
|
||||||
'..' . DIRECTORY_SEPARATOR .
|
'..' . DIRECTORY_SEPARATOR .
|
||||||
'..' . DIRECTORY_SEPARATOR .
|
'..' . DIRECTORY_SEPARATOR .
|
||||||
'datasets' . DIRECTORY_SEPARATOR .
|
'datasets' . DIRECTORY_SEPARATOR .
|
||||||
|
@ -122,6 +124,7 @@ class ConfigCacheLoaderTest extends MockedTest
|
||||||
$this->delConfigFile('local.config.php');
|
$this->delConfigFile('local.config.php');
|
||||||
|
|
||||||
$file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
|
$file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
|
||||||
|
'..' . DIRECTORY_SEPARATOR .
|
||||||
'..' . DIRECTORY_SEPARATOR .
|
'..' . DIRECTORY_SEPARATOR .
|
||||||
'..' . DIRECTORY_SEPARATOR .
|
'..' . DIRECTORY_SEPARATOR .
|
||||||
'datasets' . DIRECTORY_SEPARATOR .
|
'datasets' . DIRECTORY_SEPARATOR .
|
||||||
|
@ -160,6 +163,7 @@ class ConfigCacheLoaderTest extends MockedTest
|
||||||
vfsStream::create($structure, $this->root);
|
vfsStream::create($structure, $this->root);
|
||||||
|
|
||||||
$file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
|
$file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
|
||||||
|
'..' . DIRECTORY_SEPARATOR .
|
||||||
'..' . DIRECTORY_SEPARATOR .
|
'..' . DIRECTORY_SEPARATOR .
|
||||||
'..' . DIRECTORY_SEPARATOR .
|
'..' . DIRECTORY_SEPARATOR .
|
||||||
'datasets' . DIRECTORY_SEPARATOR .
|
'datasets' . DIRECTORY_SEPARATOR .
|
|
@ -1,8 +1,8 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Friendica\Test\Core\Config;
|
namespace Friendica\Test\Core\Config\Cache;
|
||||||
|
|
||||||
use Friendica\Core\Config\ConfigCache;
|
use Friendica\Core\Config\Cache\ConfigCache;
|
||||||
use Friendica\Test\MockedTest;
|
use Friendica\Test\MockedTest;
|
||||||
|
|
||||||
class ConfigCacheTest extends MockedTest
|
class ConfigCacheTest extends MockedTest
|
||||||
|
@ -48,7 +48,7 @@ class ConfigCacheTest extends MockedTest
|
||||||
public function testLoadConfigArray($data)
|
public function testLoadConfigArray($data)
|
||||||
{
|
{
|
||||||
$configCache = new ConfigCache();
|
$configCache = new ConfigCache();
|
||||||
$configCache->loadConfigArray($data);
|
$configCache->load($data);
|
||||||
|
|
||||||
$this->assertConfigValues($data, $configCache);
|
$this->assertConfigValues($data, $configCache);
|
||||||
}
|
}
|
||||||
|
@ -67,18 +67,38 @@ class ConfigCacheTest extends MockedTest
|
||||||
];
|
];
|
||||||
|
|
||||||
$configCache = new ConfigCache();
|
$configCache = new ConfigCache();
|
||||||
$configCache->loadConfigArray($data);
|
$configCache->load($data);
|
||||||
$configCache->loadConfigArray($override);
|
$configCache->load($override);
|
||||||
|
|
||||||
$this->assertConfigValues($data, $configCache);
|
$this->assertConfigValues($data, $configCache);
|
||||||
|
|
||||||
// override the value
|
// override the value
|
||||||
$configCache->loadConfigArray($override, true);
|
$configCache->load($override, true);
|
||||||
|
|
||||||
$this->assertEquals($override['system']['test'], $configCache->get('system', 'test'));
|
$this->assertEquals($override['system']['test'], $configCache->get('system', 'test'));
|
||||||
$this->assertEquals($override['system']['boolTrue'], $configCache->get('system', 'boolTrue'));
|
$this->assertEquals($override['system']['boolTrue'], $configCache->get('system', 'boolTrue'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test the loadConfigArray() method with wrong/empty datasets
|
||||||
|
*/
|
||||||
|
public function testLoadConfigArrayWrong()
|
||||||
|
{
|
||||||
|
$configCache = new ConfigCache();
|
||||||
|
|
||||||
|
// empty dataset
|
||||||
|
$configCache->load([]);
|
||||||
|
$this->assertEmpty($configCache->getAll());
|
||||||
|
|
||||||
|
// wrong dataset
|
||||||
|
$configCache->load(['system' => 'not_array']);
|
||||||
|
$this->assertEmpty($configCache->getAll());
|
||||||
|
|
||||||
|
// incomplete dataset (key is integer ID of the array)
|
||||||
|
$configCache->load(['system' => ['value']]);
|
||||||
|
$this->assertEquals('value', $configCache->get('system', 0));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test the getAll() method
|
* Test the getAll() method
|
||||||
* @dataProvider dataTests
|
* @dataProvider dataTests
|
||||||
|
@ -86,14 +106,12 @@ class ConfigCacheTest extends MockedTest
|
||||||
public function testGetAll($data)
|
public function testGetAll($data)
|
||||||
{
|
{
|
||||||
$configCache = new ConfigCache();
|
$configCache = new ConfigCache();
|
||||||
$configCache->loadConfigArray($data);
|
$configCache->load($data);
|
||||||
|
|
||||||
$all = $configCache->getAll();
|
$all = $configCache->getAll();
|
||||||
|
|
||||||
$this->assertContains($data['system'], $all);
|
$this->assertContains($data['system'], $all);
|
||||||
|
$this->assertContains($data['config'], $all);
|
||||||
// config values are stored directly in the array base
|
|
||||||
$this->assertEquals($data['config']['a'], $all['a']);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -113,6 +131,33 @@ class ConfigCacheTest extends MockedTest
|
||||||
$this->assertConfigValues($data, $configCache);
|
$this->assertConfigValues($data, $configCache);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test the get() method without a value
|
||||||
|
*/
|
||||||
|
public function testGetEmpty()
|
||||||
|
{
|
||||||
|
$configCache = new ConfigCache();
|
||||||
|
|
||||||
|
$this->assertEquals('!<unset>!', $configCache->get('something', 'value'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test the has() method
|
||||||
|
*/
|
||||||
|
public function testHas()
|
||||||
|
{
|
||||||
|
$configCache = new ConfigCache();
|
||||||
|
|
||||||
|
$this->assertFalse($configCache->has('system', 'test'));
|
||||||
|
|
||||||
|
$configCache->set('system', 'test', 'it');
|
||||||
|
$this->assertTrue($configCache->has('system', 'test'));
|
||||||
|
|
||||||
|
$this->assertFalse($configCache->has('system', null));
|
||||||
|
$configCache->set('system', null, 'it');
|
||||||
|
$this->assertTrue($configCache->has('system', null));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test the delete() method
|
* Test the delete() method
|
||||||
* @dataProvider dataTests
|
* @dataProvider dataTests
|
||||||
|
@ -172,4 +217,22 @@ class ConfigCacheTest extends MockedTest
|
||||||
|
|
||||||
$this->assertEmpty($configCache->getAll());
|
$this->assertEmpty($configCache->getAll());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test the hasP() method
|
||||||
|
*/
|
||||||
|
public function testHasP()
|
||||||
|
{
|
||||||
|
$configCache = new ConfigCache();
|
||||||
|
$uid = 345;
|
||||||
|
|
||||||
|
$this->assertFalse($configCache->hasP($uid, 'system', 'test'));
|
||||||
|
|
||||||
|
$configCache->setP($uid, 'system', 'test', 'it');
|
||||||
|
$this->assertTrue($configCache->hasP($uid, 'system', 'test'));
|
||||||
|
|
||||||
|
$this->assertFalse($configCache->hasP($uid, 'system', null));
|
||||||
|
$configCache->setP($uid, 'system', null, 'it');
|
||||||
|
$this->assertTrue($configCache->hasP($uid, 'system', null));
|
||||||
|
}
|
||||||
}
|
}
|
215
tests/src/Core/Config/ConfigurationTest.php
Normal file
215
tests/src/Core/Config/ConfigurationTest.php
Normal file
|
@ -0,0 +1,215 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Friendica\Test\Core\Config;
|
||||||
|
|
||||||
|
use Friendica\Core\Config\Cache\ConfigCache;
|
||||||
|
use Friendica\Core\Config\Cache\IConfigCache;
|
||||||
|
use Friendica\Core\Config\Configuration;
|
||||||
|
use Friendica\Test\MockedTest;
|
||||||
|
|
||||||
|
class ConfigurationTest extends MockedTest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Test the configuration initialization
|
||||||
|
*/
|
||||||
|
public function testSetUp()
|
||||||
|
{
|
||||||
|
$configCache = new ConfigCache();
|
||||||
|
$configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
|
||||||
|
$configAdapter->shouldReceive('isConnected')->andReturn(false)->once();
|
||||||
|
|
||||||
|
$configuration = new Configuration($configCache, $configAdapter);
|
||||||
|
|
||||||
|
$this->assertInstanceOf(IConfigCache::class, $configuration->getCache());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test if the configuration load() method
|
||||||
|
*/
|
||||||
|
public function testCacheLoad()
|
||||||
|
{
|
||||||
|
$configCache = new ConfigCache();
|
||||||
|
$configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
|
||||||
|
$configAdapter->shouldReceive('isConnected')->andReturn(true)->twice();
|
||||||
|
// constructor loading
|
||||||
|
$configAdapter->shouldReceive('load')->andReturn([])->once();
|
||||||
|
// expected loading
|
||||||
|
$configAdapter->shouldReceive('load')->andReturn(['testing' => ['test' => 'it']])->once();
|
||||||
|
|
||||||
|
$configuration = new Configuration($configCache, $configAdapter);
|
||||||
|
$configuration->load('testing');
|
||||||
|
|
||||||
|
$this->assertEquals('it', $configuration->get('testing', 'test'));
|
||||||
|
$this->assertEquals('it', $configuration->getCache()->get('testing', 'test'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test if the configuration load() method with overwrite
|
||||||
|
*/
|
||||||
|
public function testCacheLoadDouble()
|
||||||
|
{
|
||||||
|
$configCache = new ConfigCache();
|
||||||
|
$configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
|
||||||
|
$configAdapter->shouldReceive('isConnected')->andReturn(true)->times(3);
|
||||||
|
// constructor loading
|
||||||
|
$configAdapter->shouldReceive('load')->andReturn([])->once();
|
||||||
|
// expected loading
|
||||||
|
$configAdapter->shouldReceive('load')->andReturn(['testing' => ['test' => 'it']])->once();
|
||||||
|
// expected next loading
|
||||||
|
$configAdapter->shouldReceive('load')->andReturn(['testing' => ['test' => 'again']])->once();
|
||||||
|
|
||||||
|
$configuration = new Configuration($configCache, $configAdapter);
|
||||||
|
$configuration->load('testing');
|
||||||
|
|
||||||
|
$this->assertEquals('it', $configuration->get('testing', 'test'));
|
||||||
|
$this->assertEquals('it', $configuration->getCache()->get('testing', 'test'));
|
||||||
|
|
||||||
|
$configuration->load('testing');
|
||||||
|
|
||||||
|
$this->assertEquals('again', $configuration->get('testing', 'test'));
|
||||||
|
$this->assertEquals('again', $configuration->getCache()->get('testing', 'test'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test if the configuration get() and set() methods without adapter
|
||||||
|
*/
|
||||||
|
public function testSetGetWithoutDB()
|
||||||
|
{
|
||||||
|
$configCache = new ConfigCache();
|
||||||
|
$configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
|
||||||
|
$configAdapter->shouldReceive('isConnected')->andReturn(false)->twice();
|
||||||
|
|
||||||
|
$configuration = new Configuration($configCache, $configAdapter);
|
||||||
|
|
||||||
|
$this->assertTrue($configuration->set('test', 'it', 'now'));
|
||||||
|
|
||||||
|
$this->assertEquals('now', $configuration->get('test', 'it'));
|
||||||
|
$this->assertEquals('now', $configuration->getCache()->get('test', 'it'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test if the configuration get() and set() methods with adapter
|
||||||
|
*/
|
||||||
|
public function testSetGetWithDB()
|
||||||
|
{
|
||||||
|
$configCache = new ConfigCache();
|
||||||
|
$configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
|
||||||
|
$configAdapter->shouldReceive('isConnected')->andReturn(true)->twice();
|
||||||
|
// constructor loading
|
||||||
|
$configAdapter->shouldReceive('load')->andReturn([])->once();
|
||||||
|
$configAdapter->shouldReceive('set')->with('test', 'it', 'now')->andReturn(true)->once();
|
||||||
|
|
||||||
|
$configuration = new Configuration($configCache, $configAdapter);
|
||||||
|
|
||||||
|
$this->assertTrue($configuration->set('test', 'it', 'now'));
|
||||||
|
|
||||||
|
$this->assertEquals('now', $configuration->get('test', 'it'));
|
||||||
|
$this->assertEquals('now', $configuration->getCache()->get('test', 'it'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test the configuration get() method with wrong value and no db
|
||||||
|
*/
|
||||||
|
public function testGetWrongWithoutDB()
|
||||||
|
{
|
||||||
|
$configCache = new ConfigCache();
|
||||||
|
$configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
|
||||||
|
$configAdapter->shouldReceive('isConnected')->andReturn(false)->times(4);
|
||||||
|
|
||||||
|
$configuration = new Configuration($configCache, $configAdapter);
|
||||||
|
|
||||||
|
// without refresh
|
||||||
|
$this->assertNull($configuration->get('test', 'it'));
|
||||||
|
|
||||||
|
/// beware that the cache returns '!<unset>!' and not null for a non existing value
|
||||||
|
$this->assertEquals('!<unset>!', $configuration->getCache()->get('test', 'it'));
|
||||||
|
|
||||||
|
// with default value
|
||||||
|
$this->assertEquals('default', $configuration->get('test', 'it', 'default'));
|
||||||
|
|
||||||
|
// with default value and refresh
|
||||||
|
$this->assertEquals('default', $configuration->get('test', 'it', 'default', true));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test the configuration get() method with refresh
|
||||||
|
*/
|
||||||
|
public function testGetWithRefresh()
|
||||||
|
{
|
||||||
|
$configCache = new ConfigCache(['test' => ['it' => 'now']]);
|
||||||
|
$configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
|
||||||
|
$configAdapter->shouldReceive('isConnected')->andReturn(true)->times(3);
|
||||||
|
// constructor loading
|
||||||
|
$configAdapter->shouldReceive('load')->andReturn([])->once();
|
||||||
|
$configAdapter->shouldReceive('get')->with('test', 'it')->andReturn('again')->once();
|
||||||
|
$configAdapter->shouldReceive('get')->with('test', 'not')->andReturn('!<unset>!')->once();
|
||||||
|
|
||||||
|
$configuration = new Configuration($configCache, $configAdapter);
|
||||||
|
|
||||||
|
// without refresh
|
||||||
|
$this->assertEquals('now', $configuration->get('test', 'it'));
|
||||||
|
$this->assertEquals('now', $configuration->getCache()->get('test', 'it'));
|
||||||
|
|
||||||
|
// with refresh
|
||||||
|
$this->assertEquals('again', $configuration->get('test', 'it', null, true));
|
||||||
|
$this->assertEquals('again', $configuration->getCache()->get('test', 'it'));
|
||||||
|
|
||||||
|
// without refresh and wrong value and default
|
||||||
|
$this->assertEquals('default', $configuration->get('test', 'not', 'default'));
|
||||||
|
$this->assertEquals('!<unset>!', $configuration->getCache()->get('test', 'not'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test the configuration delete() method without adapter
|
||||||
|
*/
|
||||||
|
public function testDeleteWithoutDB()
|
||||||
|
{
|
||||||
|
$configCache = new ConfigCache(['test' => ['it' => 'now']]);
|
||||||
|
$configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
|
||||||
|
$configAdapter->shouldReceive('isConnected')->andReturn(false)->times(3);
|
||||||
|
|
||||||
|
$configuration = new Configuration($configCache, $configAdapter);
|
||||||
|
|
||||||
|
$this->assertEquals('now', $configuration->get('test', 'it'));
|
||||||
|
$this->assertEquals('now', $configuration->getCache()->get('test', 'it'));
|
||||||
|
|
||||||
|
$this->assertTrue($configuration->delete('test', 'it'));
|
||||||
|
$this->assertNull($configuration->get('test', 'it'));
|
||||||
|
$this->assertEquals('!<unset>!', $configuration->getCache()->get('test', 'it'));
|
||||||
|
|
||||||
|
$this->assertEmpty($configuration->getCache()->getAll());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test the configuration delete() method with adapter
|
||||||
|
*/
|
||||||
|
public function testDeleteWithDB()
|
||||||
|
{
|
||||||
|
$configCache = new ConfigCache(['test' => ['it' => 'now', 'quarter' => 'true']]);
|
||||||
|
$configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IConfigAdapter');
|
||||||
|
$configAdapter->shouldReceive('isConnected')->andReturn(true)->times(5);
|
||||||
|
// constructor loading
|
||||||
|
$configAdapter->shouldReceive('load')->andReturn([])->once();
|
||||||
|
$configAdapter->shouldReceive('delete')->with('test', 'it')->andReturn(false)->once();
|
||||||
|
|
||||||
|
$configAdapter->shouldReceive('delete')->with('test', 'second')->andReturn(true)->once();
|
||||||
|
$configAdapter->shouldReceive('delete')->with('test', 'third')->andReturn(false)->once();
|
||||||
|
$configAdapter->shouldReceive('delete')->with('test', 'quarter')->andReturn(true)->once();
|
||||||
|
|
||||||
|
$configuration = new Configuration($configCache, $configAdapter);
|
||||||
|
|
||||||
|
$this->assertEquals('now', $configuration->get('test', 'it'));
|
||||||
|
$this->assertEquals('now', $configuration->getCache()->get('test', 'it'));
|
||||||
|
|
||||||
|
// delete from cache only
|
||||||
|
$this->assertTrue($configuration->delete('test', 'it'));
|
||||||
|
// delete from db only
|
||||||
|
$this->assertTrue($configuration->delete('test', 'second'));
|
||||||
|
// no delete
|
||||||
|
$this->assertFalse($configuration->delete('test', 'third'));
|
||||||
|
// delete both
|
||||||
|
$this->assertTrue($configuration->delete('test', 'quarter'));
|
||||||
|
|
||||||
|
$this->assertEmpty($configuration->getCache()->getAll());
|
||||||
|
}
|
||||||
|
}
|
186
tests/src/Core/Config/PConfigurationTest.php
Normal file
186
tests/src/Core/Config/PConfigurationTest.php
Normal file
|
@ -0,0 +1,186 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Friendica\Test\Core\Config;
|
||||||
|
|
||||||
|
use Friendica\Core\Config\Cache\ConfigCache;
|
||||||
|
use Friendica\Core\Config\PConfiguration;
|
||||||
|
use Friendica\Test\MockedTest;
|
||||||
|
|
||||||
|
class PConfigurationTest extends MockedTest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Test if the configuration load() method
|
||||||
|
*/
|
||||||
|
public function testCacheLoad()
|
||||||
|
{
|
||||||
|
$uid = 234;
|
||||||
|
$configCache = new ConfigCache();
|
||||||
|
$configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
|
||||||
|
$configAdapter->shouldReceive('isConnected')->andReturn(true)->once();
|
||||||
|
// expected loading
|
||||||
|
$configAdapter->shouldReceive('load')->andReturn(['testing' => ['test' => 'it']])->once();
|
||||||
|
|
||||||
|
$configuration = new PConfiguration($configCache, $configAdapter);
|
||||||
|
$configuration->load($uid, 'testing');
|
||||||
|
|
||||||
|
$this->assertEquals('it', $configuration->get($uid, 'testing', 'test'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test if the configuration load() method with overwrite
|
||||||
|
*/
|
||||||
|
public function testCacheLoadDouble()
|
||||||
|
{
|
||||||
|
$uid = 234;
|
||||||
|
$configCache = new ConfigCache();
|
||||||
|
$configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
|
||||||
|
$configAdapter->shouldReceive('isConnected')->andReturn(true)->twice();
|
||||||
|
// expected loading
|
||||||
|
$configAdapter->shouldReceive('load')->andReturn(['testing' => ['test' => 'it']])->once();
|
||||||
|
// expected next loading
|
||||||
|
$configAdapter->shouldReceive('load')->andReturn(['testing' => ['test' => 'again']])->once();
|
||||||
|
|
||||||
|
$configuration = new PConfiguration($configCache, $configAdapter);
|
||||||
|
$configuration->load($uid, 'testing');
|
||||||
|
|
||||||
|
$this->assertEquals('it', $configuration->get($uid, 'testing', 'test'));
|
||||||
|
|
||||||
|
$configuration->load($uid, 'testing');
|
||||||
|
|
||||||
|
$this->assertEquals('again', $configuration->get($uid, 'testing', 'test'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test if the configuration get() and set() methods without adapter
|
||||||
|
*/
|
||||||
|
public function testSetGetWithoutDB()
|
||||||
|
{
|
||||||
|
$uid = 234;
|
||||||
|
$configCache = new ConfigCache();
|
||||||
|
$configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
|
||||||
|
$configAdapter->shouldReceive('isConnected')->andReturn(false)->once();
|
||||||
|
|
||||||
|
$configuration = new PConfiguration($configCache, $configAdapter);
|
||||||
|
|
||||||
|
$this->assertTrue($configuration->set($uid, 'test', 'it', 'now'));
|
||||||
|
|
||||||
|
$this->assertEquals('now', $configuration->get($uid, 'test', 'it'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test if the configuration get() and set() methods with adapter
|
||||||
|
*/
|
||||||
|
public function testSetGetWithDB()
|
||||||
|
{
|
||||||
|
$uid = 234;
|
||||||
|
$configCache = new ConfigCache();
|
||||||
|
$configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
|
||||||
|
$configAdapter->shouldReceive('isConnected')->andReturn(true)->once();
|
||||||
|
$configAdapter->shouldReceive('set')->with($uid, 'test', 'it', 'now')->andReturn(true)->once();
|
||||||
|
|
||||||
|
$configuration = new PConfiguration($configCache, $configAdapter);
|
||||||
|
|
||||||
|
$this->assertTrue($configuration->set($uid, 'test', 'it', 'now'));
|
||||||
|
|
||||||
|
$this->assertEquals('now', $configuration->get($uid, 'test', 'it'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test the configuration get() method with wrong value and no db
|
||||||
|
*/
|
||||||
|
public function testGetWrongWithoutDB()
|
||||||
|
{
|
||||||
|
$uid = 234;
|
||||||
|
$configCache = new ConfigCache();
|
||||||
|
$configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
|
||||||
|
$configAdapter->shouldReceive('isConnected')->andReturn(false)->times(3);
|
||||||
|
|
||||||
|
$configuration = new PConfiguration($configCache, $configAdapter);
|
||||||
|
|
||||||
|
// without refresh
|
||||||
|
$this->assertNull($configuration->get($uid, 'test', 'it'));
|
||||||
|
|
||||||
|
// with default value
|
||||||
|
$this->assertEquals('default', $configuration->get($uid, 'test', 'it', 'default'));
|
||||||
|
|
||||||
|
// with default value and refresh
|
||||||
|
$this->assertEquals('default', $configuration->get($uid, 'test', 'it', 'default', true));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test the configuration get() method with refresh
|
||||||
|
*/
|
||||||
|
public function testGetWithRefresh()
|
||||||
|
{
|
||||||
|
$uid = 234;
|
||||||
|
$configCache = new ConfigCache();
|
||||||
|
$configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
|
||||||
|
$configAdapter->shouldReceive('isConnected')->andReturn(true)->times(3);
|
||||||
|
$configAdapter->shouldReceive('get')->with($uid, 'test', 'it')->andReturn('now')->once();
|
||||||
|
$configAdapter->shouldReceive('get')->with($uid, 'test', 'it')->andReturn('again')->once();
|
||||||
|
$configAdapter->shouldReceive('get')->with($uid, 'test', 'not')->andReturn('!<unset>!')->once();
|
||||||
|
|
||||||
|
$configuration = new PConfiguration($configCache, $configAdapter);
|
||||||
|
|
||||||
|
// without refresh
|
||||||
|
$this->assertEquals('now', $configuration->get($uid, 'test', 'it'));
|
||||||
|
// use the cache again
|
||||||
|
$this->assertEquals('now', $configuration->get($uid, 'test', 'it'));
|
||||||
|
|
||||||
|
// with refresh (and load the second value out of the db)
|
||||||
|
$this->assertEquals('again', $configuration->get($uid, 'test', 'it', null, true));
|
||||||
|
|
||||||
|
// without refresh and wrong value and default
|
||||||
|
$this->assertEquals('default', $configuration->get($uid, 'test', 'not', 'default'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test the configuration delete() method without adapter
|
||||||
|
*/
|
||||||
|
public function testDeleteWithoutDB()
|
||||||
|
{
|
||||||
|
$uid = 234;
|
||||||
|
$configCache = new ConfigCache();
|
||||||
|
$configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
|
||||||
|
$configAdapter->shouldReceive('isConnected')->andReturn(false)->times(3);
|
||||||
|
|
||||||
|
$configuration = new PConfiguration($configCache, $configAdapter);
|
||||||
|
|
||||||
|
$this->assertTrue($configuration->set($uid, 'test', 'it', 'now'));
|
||||||
|
$this->assertEquals('now', $configuration->get($uid, 'test', 'it'));
|
||||||
|
|
||||||
|
$this->assertTrue($configuration->delete($uid, 'test', 'it'));
|
||||||
|
$this->assertNull($configuration->get($uid, 'test', 'it'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test the configuration delete() method with adapter
|
||||||
|
*/
|
||||||
|
public function testDeleteWithDB()
|
||||||
|
{
|
||||||
|
$uid = 234;
|
||||||
|
$configCache = new ConfigCache();
|
||||||
|
$configAdapter = \Mockery::mock('Friendica\Core\Config\Adapter\IPConfigAdapter');
|
||||||
|
$configAdapter->shouldReceive('isConnected')->andReturn(true)->times(5);
|
||||||
|
$configAdapter->shouldReceive('set')->with($uid, 'test', 'it', 'now')->andReturn(false)->once();
|
||||||
|
$configAdapter->shouldReceive('delete')->with($uid, 'test', 'it')->andReturn(false)->once();
|
||||||
|
|
||||||
|
$configAdapter->shouldReceive('delete')->with($uid, 'test', 'second')->andReturn(true)->once();
|
||||||
|
$configAdapter->shouldReceive('delete')->with($uid, 'test', 'third')->andReturn(false)->once();
|
||||||
|
$configAdapter->shouldReceive('delete')->with($uid, 'test', 'quarter')->andReturn(true)->once();
|
||||||
|
|
||||||
|
$configuration = new PConfiguration($configCache, $configAdapter);
|
||||||
|
|
||||||
|
$this->assertFalse($configuration->set($uid, 'test', 'it', 'now'));
|
||||||
|
$this->assertEquals('now', $configuration->get($uid, 'test', 'it'));
|
||||||
|
|
||||||
|
// delete from set
|
||||||
|
$this->assertTrue($configuration->delete($uid, 'test', 'it'));
|
||||||
|
// delete from db only
|
||||||
|
$this->assertTrue($configuration->delete($uid, 'test', 'second'));
|
||||||
|
// no delete
|
||||||
|
$this->assertFalse($configuration->delete($uid, 'test', 'third'));
|
||||||
|
// delete both
|
||||||
|
$this->assertTrue($configuration->delete($uid, 'test', 'quarter'));
|
||||||
|
}
|
||||||
|
}
|
|
@ -54,7 +54,7 @@ class AutomaticInstallationConsoleTest extends ConsoleTest
|
||||||
|
|
||||||
$this->configCache
|
$this->configCache
|
||||||
->shouldReceive('get')
|
->shouldReceive('get')
|
||||||
->with('config', 'php_path', NULL)
|
->with('config', 'php_path', NULL, false)
|
||||||
->andReturn(false);
|
->andReturn(false);
|
||||||
|
|
||||||
$this->mockL10nT();
|
$this->mockL10nT();
|
||||||
|
|
|
@ -39,7 +39,7 @@ class ConfigConsoleTest extends ConsoleTest
|
||||||
->once();
|
->once();
|
||||||
$this->configCache
|
$this->configCache
|
||||||
->shouldReceive('get')
|
->shouldReceive('get')
|
||||||
->with('config', 'test', NULL)
|
->with('config', 'test', NULL, false)
|
||||||
->andReturn('now')
|
->andReturn('now')
|
||||||
->twice();
|
->twice();
|
||||||
|
|
||||||
|
@ -52,7 +52,7 @@ class ConfigConsoleTest extends ConsoleTest
|
||||||
|
|
||||||
$this->configCache
|
$this->configCache
|
||||||
->shouldReceive('get')
|
->shouldReceive('get')
|
||||||
->with('config', 'test', null)
|
->with('config', 'test', null, false)
|
||||||
->andReturn('now')
|
->andReturn('now')
|
||||||
->once();
|
->once();
|
||||||
|
|
||||||
|
@ -64,7 +64,7 @@ class ConfigConsoleTest extends ConsoleTest
|
||||||
|
|
||||||
$this->configCache
|
$this->configCache
|
||||||
->shouldReceive('get')
|
->shouldReceive('get')
|
||||||
->with('config', 'test', null)
|
->with('config', 'test', null, false)
|
||||||
->andReturn(null)
|
->andReturn(null)
|
||||||
->once();
|
->once();
|
||||||
|
|
||||||
|
@ -79,7 +79,7 @@ class ConfigConsoleTest extends ConsoleTest
|
||||||
$testArray = [1, 2, 3];
|
$testArray = [1, 2, 3];
|
||||||
$this->configCache
|
$this->configCache
|
||||||
->shouldReceive('get')
|
->shouldReceive('get')
|
||||||
->with('config', 'test', null)
|
->with('config', 'test', null, false)
|
||||||
->andReturn($testArray)
|
->andReturn($testArray)
|
||||||
->once();
|
->once();
|
||||||
|
|
||||||
|
@ -107,7 +107,7 @@ class ConfigConsoleTest extends ConsoleTest
|
||||||
function testVerbose() {
|
function testVerbose() {
|
||||||
$this->configCache
|
$this->configCache
|
||||||
->shouldReceive('get')
|
->shouldReceive('get')
|
||||||
->with('test', 'it', null)
|
->with('test', 'it', null, false)
|
||||||
->andReturn('now')
|
->andReturn('now')
|
||||||
->once();
|
->once();
|
||||||
$console = new Config($this->consoleArgv);
|
$console = new Config($this->consoleArgv);
|
||||||
|
@ -140,7 +140,7 @@ CONF;
|
||||||
->once();
|
->once();
|
||||||
$this->configCache
|
$this->configCache
|
||||||
->shouldReceive('get')
|
->shouldReceive('get')
|
||||||
->with('test', 'it', NULL)
|
->with('test', 'it', NULL, false)
|
||||||
->andReturn(NULL)
|
->andReturn(NULL)
|
||||||
->once();
|
->once();
|
||||||
$console = new Config();
|
$console = new Config();
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
namespace Friendica\Test\src\Core\Console;
|
namespace Friendica\Test\src\Core\Console;
|
||||||
|
|
||||||
use Asika\SimpleConsole\Console;
|
use Asika\SimpleConsole\Console;
|
||||||
use Friendica\Core\Config\ConfigCache;
|
use Friendica\Core\Config\Configuration;
|
||||||
use Friendica\Test\MockedTest;
|
use Friendica\Test\MockedTest;
|
||||||
use Friendica\Test\Util\AppMockTrait;
|
use Friendica\Test\Util\AppMockTrait;
|
||||||
use Friendica\Test\Util\Intercept;
|
use Friendica\Test\Util\Intercept;
|
||||||
|
@ -31,7 +31,7 @@ abstract class ConsoleTest extends MockedTest
|
||||||
Intercept::setUp();
|
Intercept::setUp();
|
||||||
|
|
||||||
$this->setUpVfsDir();
|
$this->setUpVfsDir();
|
||||||
$configMock = \Mockery::mock(ConfigCache::class);
|
$configMock = \Mockery::mock(Configuration::class);
|
||||||
$this->mockApp($this->root, $configMock);
|
$this->mockApp($this->root, $configMock);
|
||||||
$profileMock = \Mockery::mock(Profiler::class);
|
$profileMock = \Mockery::mock(Profiler::class);
|
||||||
$this->app->shouldReceive('getProfiler')->andReturn($profileMock);
|
$this->app->shouldReceive('getProfiler')->andReturn($profileMock);
|
||||||
|
|
|
@ -27,7 +27,7 @@ abstract class LockTest extends MockedTest
|
||||||
{
|
{
|
||||||
// Reusable App object
|
// Reusable App object
|
||||||
$this->setUpVfsDir();
|
$this->setUpVfsDir();
|
||||||
$configMock = \Mockery::mock('Friendica\Core\Config\ConfigCache');
|
$configMock = \Mockery::mock('Friendica\Core\Config\Configuration');
|
||||||
$this->mockApp($this->root, $configMock);
|
$this->mockApp($this->root, $configMock);
|
||||||
$this->app
|
$this->app
|
||||||
->shouldReceive('getHostname')
|
->shouldReceive('getHostname')
|
||||||
|
|
|
@ -15,12 +15,12 @@ class MemcacheCacheLockDriverTest extends LockTest
|
||||||
{
|
{
|
||||||
$this->configCache
|
$this->configCache
|
||||||
->shouldReceive('get')
|
->shouldReceive('get')
|
||||||
->with('system', 'memcache_host', NULL)
|
->with('system', 'memcache_host', NULL, false)
|
||||||
->andReturn('localhost');
|
->andReturn('localhost');
|
||||||
|
|
||||||
$this->configCache
|
$this->configCache
|
||||||
->shouldReceive('get')
|
->shouldReceive('get')
|
||||||
->with('system', 'memcache_port', NULL)
|
->with('system', 'memcache_port', NULL, false)
|
||||||
->andReturn(11211);
|
->andReturn(11211);
|
||||||
|
|
||||||
return new CacheLockDriver(CacheDriverFactory::create('memcache'));
|
return new CacheLockDriver(CacheDriverFactory::create('memcache'));
|
||||||
|
|
|
@ -15,7 +15,7 @@ class MemcachedCacheLockDriverTest extends LockTest
|
||||||
{
|
{
|
||||||
$this->configCache
|
$this->configCache
|
||||||
->shouldReceive('get')
|
->shouldReceive('get')
|
||||||
->with('system', 'memcached_hosts', NULL)
|
->with('system', 'memcached_hosts', NULL, false)
|
||||||
->andReturn([0 => 'localhost, 11211']);
|
->andReturn([0 => 'localhost, 11211']);
|
||||||
|
|
||||||
return new CacheLockDriver(CacheDriverFactory::create('memcached'));
|
return new CacheLockDriver(CacheDriverFactory::create('memcached'));
|
||||||
|
|
|
@ -15,12 +15,12 @@ class RedisCacheLockDriverTest extends LockTest
|
||||||
{
|
{
|
||||||
$this->configCache
|
$this->configCache
|
||||||
->shouldReceive('get')
|
->shouldReceive('get')
|
||||||
->with('system', 'redis_host', NULL)
|
->with('system', 'redis_host', NULL, false)
|
||||||
->andReturn('localhost');
|
->andReturn('localhost');
|
||||||
|
|
||||||
$this->configCache
|
$this->configCache
|
||||||
->shouldReceive('get')
|
->shouldReceive('get')
|
||||||
->with('system', 'redis_port', NULL)
|
->with('system', 'redis_port', NULL, false)
|
||||||
->andReturn(null);
|
->andReturn(null);
|
||||||
|
|
||||||
return new CacheLockDriver(CacheDriverFactory::create('redis'));
|
return new CacheLockDriver(CacheDriverFactory::create('redis'));
|
||||||
|
|
|
@ -14,7 +14,7 @@ class SemaphoreLockDriverTest extends LockTest
|
||||||
|
|
||||||
$this->configCache
|
$this->configCache
|
||||||
->shouldReceive('get')
|
->shouldReceive('get')
|
||||||
->with('system', 'temppath', NULL)
|
->with('system', 'temppath', NULL, false)
|
||||||
->andReturn('/tmp/');
|
->andReturn('/tmp/');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ namespace Friendica\Test\Database;
|
||||||
|
|
||||||
use Friendica\App;
|
use Friendica\App;
|
||||||
use Friendica\Core\Config;
|
use Friendica\Core\Config;
|
||||||
|
use Friendica\Core\Config\Cache;
|
||||||
use Friendica\Database\DBA;
|
use Friendica\Database\DBA;
|
||||||
use Friendica\Factory;
|
use Friendica\Factory;
|
||||||
use Friendica\Test\DatabaseTest;
|
use Friendica\Test\DatabaseTest;
|
||||||
|
@ -13,8 +14,11 @@ class DBATest extends DatabaseTest
|
||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
$basedir = BasePath::create(dirname(__DIR__) . '/../../');
|
$basedir = BasePath::create(dirname(__DIR__) . '/../../');
|
||||||
$configLoader = new Config\ConfigCacheLoader($basedir);
|
$configLoader = new Cache\ConfigCacheLoader($basedir);
|
||||||
$config = Factory\ConfigFactory::createCache($configLoader);
|
$configCache = Factory\ConfigFactory::createCache($configLoader);
|
||||||
|
Factory\DBFactory::init($configCache, $_SERVER);
|
||||||
|
$config = Factory\ConfigFactory::createConfig($configCache);
|
||||||
|
$pconfig = Factory\ConfigFactory::createPConfig($configCache);
|
||||||
$logger = Factory\LoggerFactory::create('test', $config);
|
$logger = Factory\LoggerFactory::create('test', $config);
|
||||||
$profiler = Factory\ProfilerFactory::create($logger, $config);
|
$profiler = Factory\ProfilerFactory::create($logger, $config);
|
||||||
$this->app = new App($config, $logger, $profiler, false);
|
$this->app = new App($config, $logger, $profiler, false);
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
namespace Friendica\Test\Database;
|
namespace Friendica\Test\Database;
|
||||||
|
|
||||||
use Friendica\App;
|
use Friendica\App;
|
||||||
use Friendica\Core\Config;
|
use Friendica\Core\Config\Cache;
|
||||||
use Friendica\Database\DBStructure;
|
use Friendica\Database\DBStructure;
|
||||||
use Friendica\Factory;
|
use Friendica\Factory;
|
||||||
use Friendica\Test\DatabaseTest;
|
use Friendica\Test\DatabaseTest;
|
||||||
|
@ -14,8 +14,11 @@ class DBStructureTest extends DatabaseTest
|
||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
$basedir = BasePath::create(dirname(__DIR__) . '/../../');
|
$basedir = BasePath::create(dirname(__DIR__) . '/../../');
|
||||||
$configLoader = new Config\ConfigCacheLoader($basedir);
|
$configLoader = new Cache\ConfigCacheLoader($basedir);
|
||||||
$config = Factory\ConfigFactory::createCache($configLoader);
|
$configCache = Factory\ConfigFactory::createCache($configLoader);
|
||||||
|
Factory\DBFactory::init($configCache, $_SERVER);
|
||||||
|
$config = Factory\ConfigFactory::createConfig($configCache);
|
||||||
|
$pconfig = Factory\ConfigFactory::createPConfig($configCache);
|
||||||
$logger = Factory\LoggerFactory::create('test', $config);
|
$logger = Factory\LoggerFactory::create('test', $config);
|
||||||
$profiler = Factory\ProfilerFactory::create($logger, $config);
|
$profiler = Factory\ProfilerFactory::create($logger, $config);
|
||||||
$this->app = new App($config, $logger, $profiler, false);
|
$this->app = new App($config, $logger, $profiler, false);
|
||||||
|
|
Loading…
Reference in a new issue