Refactoring ConfigCacheLoader methods

This commit is contained in:
Philipp Holzer 2019-02-05 22:27:57 +01:00
parent 104086215c
commit 6a9d73f7d9
No known key found for this signature in database
GPG Key ID: 517BE60E2CE5C8A5
9 changed files with 56 additions and 57 deletions

View File

@ -54,7 +54,7 @@ chdir($directory);
require dirname(__DIR__) . '/vendor/autoload.php'; require dirname(__DIR__) . '/vendor/autoload.php';
$basedir = BasePath::create(dirname(__DIR__)); $basedir = BasePath::create(dirname(__DIR__), $_SERVER);
$configLoader = new Config\ConfigCacheLoader($basedir); $configLoader = new Config\ConfigCacheLoader($basedir);
$config = Factory\ConfigFactory::createCache($configLoader); $config = Factory\ConfigFactory::createCache($configLoader);
$logger = Factory\LoggerFactory::create('auth_ejabberd', $config); $logger = Factory\LoggerFactory::create('auth_ejabberd', $config);

View File

@ -7,7 +7,7 @@ use Friendica\Core\Config;
use Friendica\Factory; use Friendica\Factory;
use Friendica\Util\BasePath; use Friendica\Util\BasePath;
$basedir = BasePath::create(dirname(__DIR__)); $basedir = BasePath::create(dirname(__DIR__), $_SERVER);
$configLoader = new Config\ConfigCacheLoader($basedir); $configLoader = new Config\ConfigCacheLoader($basedir);
$config = Factory\ConfigFactory::createCache($configLoader); $config = Factory\ConfigFactory::createCache($configLoader);
$logger = Factory\LoggerFactory::create('console', $config); $logger = Factory\LoggerFactory::create('console', $config);

View File

@ -33,7 +33,7 @@ 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__)); $basedir = BasePath::create(dirname(__DIR__), $_SERVER);
$configLoader = new Config\ConfigCacheLoader($basedir); $configLoader = new Config\ConfigCacheLoader($basedir);
$config = Factory\ConfigFactory::createCache($configLoader); $config = Factory\ConfigFactory::createCache($configLoader);
$logger = Factory\LoggerFactory::create('daemon', $config); $logger = Factory\LoggerFactory::create('daemon', $config);

View File

@ -31,7 +31,7 @@ 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__)); $basedir = BasePath::create(dirname(__DIR__), $_SERVER);
$configLoader = new Config\ConfigCacheLoader($basedir); $configLoader = new Config\ConfigCacheLoader($basedir);
$config = Factory\ConfigFactory::createCache($configLoader); $config = Factory\ConfigFactory::createCache($configLoader);
$logger = Factory\LoggerFactory::create('worker', $config); $logger = Factory\LoggerFactory::create('worker', $config);

View File

@ -15,7 +15,7 @@ if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
require __DIR__ . '/vendor/autoload.php'; require __DIR__ . '/vendor/autoload.php';
$basedir = BasePath::create(__DIR__); $basedir = BasePath::create(__DIR__, $_SERVER);
$configLoader = new Config\ConfigCacheLoader($basedir); $configLoader = new Config\ConfigCacheLoader($basedir);
$config = Factory\ConfigFactory::createCache($configLoader); $config = Factory\ConfigFactory::createCache($configLoader);
$logger = Factory\LoggerFactory::create('index', $config); $logger = Factory\LoggerFactory::create('index', $config);

View File

@ -387,7 +387,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->loadAddonConfig(), true); $this->config->loadConfigArray($loader->loadCoreConfig('addon'), true);
} }
$this->loadDefaultTimezone(); $this->loadDefaultTimezone();

View File

@ -11,24 +11,14 @@ namespace Friendica\Core\Config;
*/ */
class ConfigCache implements IConfigCache, IPConfigCache class ConfigCache implements IConfigCache, IPConfigCache
{ {
/** private $config;
* NEVER, EVER use this public config array outside of the class
* It is only public due to backward compatibility to .htconfig.php
*
* @var array The cached config array
*/
public $config;
/** /**
* @param array $config A initial config array * @param array $config A initial config array
*/ */
public function __construct(array $config = []) public function __construct(array $config = [])
{ {
$this->config = []; $this->config = $config;
if (isset($config)) {
$this->loadConfigArray($config, true);
}
} }
/** /**

View File

@ -40,8 +40,8 @@ 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->loadConfigFile('defaults')); $config->loadConfigArray($this->loadCoreConfig('defaults'));
$config->loadConfigArray($this->loadConfigFile('settings')); $config->loadConfigArray($this->loadCoreConfig('settings'));
// Legacy .htconfig.php support // Legacy .htconfig.php support
if (file_exists($this->baseDir . '/.htpreconfig.php')) { if (file_exists($this->baseDir . '/.htpreconfig.php')) {
@ -82,16 +82,11 @@ class ConfigCacheLoader
} }
} }
if (file_exists($this->baseDir . '/config/local.config.php')) { $config->loadConfigArray($this->loadCoreConfig('local'), true);
$config->loadConfigArray($this->loadConfigFile('local'), true);
} elseif (file_exists($this->baseDir . '/config/local.ini.php')) {
$config->loadConfigArray($this->loadINIConfigFile('local'), true);
}
} }
/** /**
* Tries to load the specified legacy configuration file into the App->config array. * Tries to load the specified legacy configuration file into the ConfigCache (@see ConfigCache ).
* Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
* *
* @deprecated since version 2018.12 * @deprecated since version 2018.12
* @param string $filename * @param string $filename
@ -119,8 +114,7 @@ class ConfigCacheLoader
} }
/** /**
* Tries to load the specified configuration file into the App->config array. * Tries to load the specified configuration file and returns the config array.
* Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
* *
* The config format is PHP array and the template for configuration files is the following: * The config format is PHP array and the template for configuration files is the following:
* *
@ -130,19 +124,13 @@ class ConfigCacheLoader
* ], * ],
* ]; * ];
* *
* @param string $filename * @param string $filepath The filepath of the
* @param bool $addon True, if a config for an addon should be loaded * @return array The config array0
* @return array The configuration *
* @throws \Exception * @throws \Exception if the config cannot get loaded.
*/ */
public function loadConfigFile($filename, $addon = false) private function loadConfigFile($filepath)
{ {
if ($addon) {
$filepath = $this->baseDir . Addon::DIRECTORY . $filename . self::SUBDIRECTORY . $filename . ".config.php";
} else {
$filepath = $this->configDir . $filename . ".config.php";
}
if (!file_exists($filepath)) { if (!file_exists($filepath)) {
throw new \Exception('Error loading non-existent config file ' . $filepath); throw new \Exception('Error loading non-existent config file ' . $filepath);
} }
@ -157,22 +145,43 @@ class ConfigCacheLoader
} }
/** /**
* Loads addons configuration files * Tries to load the specified core-configuration and returns the config array.
*
* @param string $name The name of the configuration
*
* @return array The config array (empty if no config found)
*
* @throws \Exception if the configuration file isn't readable
*/
public function loadCoreConfig($name)
{
if (file_exists($this->configDir . $name . '.config.php')) {
return $this->loadConfigFile($this->configDir . $name . '.config.php');
} elseif (file_exists($this->configDir . $name . '.ini.php')) {
return $this->loadINIConfigFile($this->configDir . $name . '.ini.php');
} else {
return [];
}
}
/**
* Tries to load the specified addon-configuration and returns the config array.
* *
* First loads all activated addons default configuration through the load_config hook, then load the local.config.php * First loads all activated addons default configuration through the load_config hook, then load the local.config.php
* again to overwrite potential local addon configuration. * again to overwrite potential local addon configuration.
* *
* @return array The config array * @param string $name The name of the configuration
* *
* @throws \Exception * @return array The config array (empty if no config found)
*
* @throws \Exception if the configuration file isn't readable
*/ */
public function loadAddonConfig() public function loadAddonConfig($name)
{ {
// Load the local addon config file to overwritten default addon config values $filepath = $this->baseDir . Addon::DIRECTORY . $name . self::SUBDIRECTORY . $name . ".config.php";
if (file_exists($this->configDir . 'addon.config.php')) {
return $this->loadConfigFile('addon'); if (file_exists($filepath)) {
} elseif (file_exists($this->configDir . 'addon.ini.php')) { return $this->loadConfigFile($filepath);
return $this->loadINIConfigFile('addon');
} else { } else {
return []; return [];
} }

View File

@ -12,23 +12,24 @@ class BasePath
* It first checks for the internal variable, then for DOCUMENT_ROOT and * It first checks for the internal variable, then for DOCUMENT_ROOT and
* finally for PWD * finally for PWD
* *
* @param string|null $basepath * @param string|null $basePath The default base path
* @param array $server server arguments
* *
* @return string * @return string
* *
* @throws \Exception if directory isn't usable * @throws \Exception if directory isn't usable
*/ */
public static function create($basepath) public static function create($basePath, $server = [])
{ {
if (!$basepath && !empty($_SERVER['DOCUMENT_ROOT'])) { if (!$basePath && !empty($server['DOCUMENT_ROOT'])) {
$basepath = $_SERVER['DOCUMENT_ROOT']; $basePath = $server['DOCUMENT_ROOT'];
} }
if (!$basepath && !empty($_SERVER['PWD'])) { if (!$basePath && !empty($server['PWD'])) {
$basepath = $_SERVER['PWD']; $basePath = $server['PWD'];
} }
return self::getRealPath($basepath); return self::getRealPath($basePath);
} }
/** /**
@ -52,7 +53,6 @@ class BasePath
} }
} }
/** /**
* @brief Checks if a given directory is usable for the system * @brief Checks if a given directory is usable for the system
* *