1
1
Fork 0

wrong indent

This commit is contained in:
Philipp Holzer 2019-03-30 19:08:47 +01:00
parent 24f5a8c3a1
commit 0a447cb61b
No known key found for this signature in database
GPG key ID: 517BE60E2CE5C8A5
3 changed files with 118 additions and 118 deletions

View file

@ -189,31 +189,31 @@ class ConfigCache implements IConfigCache, IPConfigCache
return $this->config; return $this->config;
} }
/** /**
* Returns an array with missing categories/Keys * Returns an array with missing categories/Keys
* *
* @param array $config The array to check * @param array $config The array to check
* *
* @return array * @return array
*/ */
public function keyDiff(array $config) public function keyDiff(array $config)
{ {
$return = []; $return = [];
$categories = array_keys($config); $categories = array_keys($config);
foreach ($categories as $category) { foreach ($categories as $category) {
if (is_array($config[$category])) { if (is_array($config[$category])) {
$keys = array_keys($config[$category]); $keys = array_keys($config[$category]);
foreach ($keys as $key) { foreach ($keys as $key) {
if (!isset($this->config[$category][$key])) { if (!isset($this->config[$category][$key])) {
$return[$category][$key] = $config[$category][$key]; $return[$category][$key] = $config[$category][$key];
} }
} }
} }
} }
return $return; return $return;
} }
} }

View file

@ -21,8 +21,8 @@ class Update
* *
* @param string $basePath The base path of this application * @param string $basePath The base path of this application
* @param boolean $via_worker Is the check run via the worker? * @param boolean $via_worker Is the check run via the worker?
* @param App\Mode $mode The current app mode * @param App\Mode $mode The current app mode
* *
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/ */
public static function check($basePath, $via_worker, App\Mode $mode) public static function check($basePath, $via_worker, App\Mode $mode)
@ -32,7 +32,7 @@ class Update
} }
// Check if the config files are set correctly // Check if the config files are set correctly
self::checkConfigFile($basePath, $mode); self::checkConfigFile($basePath, $mode);
// Don't check the status if the last update was failed // Don't check the status if the last update was failed
if (Config::get('system', 'update', Update::SUCCESS, true) == Update::FAILED) { if (Config::get('system', 'update', Update::SUCCESS, true) == Update::FAILED) {
@ -239,73 +239,73 @@ class Update
*/ */
public static function checkConfigFile($basePath, App\Mode $mode) public static function checkConfigFile($basePath, App\Mode $mode)
{ {
if (empty($basePath)) { if (empty($basePath)) {
$basePath = BasePath::create(dirname(__DIR__, 2)); $basePath = BasePath::create(dirname(__DIR__, 2));
} }
$config = [ $config = [
'config' => [ 'config' => [
'hostname' => [ 'hostname' => [
'allowEmpty' => false, 'allowEmpty' => false,
'default' => '', 'default' => '',
], ],
], ],
'system' => [ 'system' => [
'basepath' => [ 'basepath' => [
'allowEmpty' => false, 'allowEmpty' => false,
'default' => $basePath, 'default' => $basePath,
], ],
] ]
]; ];
$configFileLoader = new ConfigFileLoader($basePath, $mode); $configFileLoader = new ConfigFileLoader($basePath, $mode);
$configCache = new Config\Cache\ConfigCache(); $configCache = new Config\Cache\ConfigCache();
$configFileLoader->setupCache($configCache, true); $configFileLoader->setupCache($configCache, true);
// checks if something is to update, otherwise skip this function at all // checks if something is to update, otherwise skip this function at all
$missingConfig = $configCache->keyDiff($config); $missingConfig = $configCache->keyDiff($config);
if (empty($missingConfig)) { if (empty($missingConfig)) {
return true; return true;
} }
// We just want one update process // We just want one update process
if (Lock::acquire('config_update')) { if (Lock::acquire('config_update')) {
$configFileSaver = new ConfigFileSaver($basePath); $configFileSaver = new ConfigFileSaver($basePath);
$updated = false; $updated = false;
$toDelete = []; $toDelete = [];
foreach ($missingConfig as $category => $keys) { foreach ($missingConfig as $category => $keys) {
foreach ($keys as $key => $value) { foreach ($keys as $key => $value) {
if (self::updateConfigEntry($configCache, $configFileSaver, $category, $key, $value['allowEmpty'], $value['default'])) { if (self::updateConfigEntry($configCache, $configFileSaver, $category, $key, $value['allowEmpty'], $value['default'])) {
$toDelete[] = ['cat' => $category, 'key' => $key]; $toDelete[] = ['cat' => $category, 'key' => $key];
$updated = true; $updated = true;
}; };
} }
} }
// In case there is nothing to do, skip the update // In case there is nothing to do, skip the update
if (!$updated) { if (!$updated) {
Lock::release('config_update'); Lock::release('config_update');
return true; return true;
} }
if (!$configFileSaver->saveToConfigFile()) { if (!$configFileSaver->saveToConfigFile()) {
Logger::alert('Config entry update failed - maybe wrong permission?'); Logger::alert('Config entry update failed - maybe wrong permission?');
Lock::release('config_update'); Lock::release('config_update');
return false; return false;
} }
// After the successful save, remove the db values // After the successful save, remove the db values
foreach ($toDelete as $delete) { foreach ($toDelete as $delete) {
DBA::delete('config', ['cat' => $delete['cat'], 'k' => $delete['key']]); DBA::delete('config', ['cat' => $delete['cat'], 'k' => $delete['key']]);
} }
Lock::release('config_update'); Lock::release('config_update');
} }
return true; return true;
} }
/** /**
* Adds a value to the ConfigFileSave in case it isn't already updated * Adds a value to the ConfigFileSave in case it isn't already updated
@ -322,38 +322,38 @@ class Update
* @throws \Exception if DBA or Logger doesn't work * @throws \Exception if DBA or Logger doesn't work
*/ */
private static function updateConfigEntry( private static function updateConfigEntry(
IConfigCache $configCache, IConfigCache $configCache,
ConfigFileSaver $configFileSaver, ConfigFileSaver $configFileSaver,
$cat, $cat,
$key, $key,
$allowEmpty = false, $allowEmpty = false,
$default = '') $default = '')
{ {
// check if the config file differs from the whole configuration (= The db contains other values) // check if the config file differs from the whole configuration (= The db contains other values)
$fileValue = $configCache->get($cat, $key); $fileValue = $configCache->get($cat, $key);
$dbConfig = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $key]); $dbConfig = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $key]);
if (DBA::isResult($dbConfig)) { if (DBA::isResult($dbConfig)) {
$dbValue = $dbConfig['v']; $dbValue = $dbConfig['v'];
} else { } else {
$dbValue = null; $dbValue = null;
} }
// If the db contains a config value, check it // If the db contains a config value, check it
if (( if ((
($allowEmpty && isset($dbValue)) || ($allowEmpty && isset($dbValue)) ||
(!$allowEmpty && !empty($dbValue)) (!$allowEmpty && !empty($dbValue))
) && ) &&
$fileValue !== $dbValue) { $fileValue !== $dbValue) {
Logger::info('Difference in config found', ['cat' => $cat, 'key' => $key, 'file' => $fileValue, 'db' => $dbValue]); Logger::info('Difference in config found', ['cat' => $cat, 'key' => $key, 'file' => $fileValue, 'db' => $dbValue]);
$configFileSaver->addConfigValue($cat, $key, $dbValue); $configFileSaver->addConfigValue($cat, $key, $dbValue);
return true; return true;
// If both config values are not set, use the default value // If both config values are not set, use the default value
} elseif ( } elseif (
($allowEmpty && !isset($fileValue) && !isset($dbValue)) || ($allowEmpty && !isset($fileValue) && !isset($dbValue)) ||
(!$allowEmpty && empty($fileValue) && empty($dbValue) && !empty($default))) { (!$allowEmpty && empty($fileValue) && empty($dbValue) && !empty($default))) {
Logger::info('Using default for config', ['cat' => $cat, 'key' => $key, 'value' => $default]); Logger::info('Using default for config', ['cat' => $cat, 'key' => $key, 'value' => $default]);
$configFileSaver->addConfigValue($cat, $key, $default); $configFileSaver->addConfigValue($cat, $key, $default);

View file

@ -246,33 +246,33 @@ class ConfigCacheTest extends MockedTest
$this->assertEmpty($configCache->getAll()); $this->assertEmpty($configCache->getAll());
} }
/** /**
* Test the keyDiff() method with result * Test the keyDiff() method with result
* @dataProvider dataTests * @dataProvider dataTests
*/ */
public function testKeyDiffWithResult($data) public function testKeyDiffWithResult($data)
{ {
$configCache = new ConfigCache($data); $configCache = new ConfigCache($data);
$diffConfig = [ $diffConfig = [
'fakeCat' => [ 'fakeCat' => [
'fakeKey' => 'value', 'fakeKey' => 'value',
] ]
]; ];
$this->assertEquals($diffConfig, $configCache->keyDiff($diffConfig)); $this->assertEquals($diffConfig, $configCache->keyDiff($diffConfig));
} }
/** /**
* Test the keyDiff() method without result * Test the keyDiff() method without result
* @dataProvider dataTests * @dataProvider dataTests
*/ */
public function testKeyDiffWithoutResult($data) public function testKeyDiffWithoutResult($data)
{ {
$configCache = new ConfigCache($data); $configCache = new ConfigCache($data);
$diffConfig = $configCache->getAll(); $diffConfig = $configCache->getAll();
$this->assertEmpty($configCache->keyDiff($diffConfig)); $this->assertEmpty($configCache->keyDiff($diffConfig));
} }
} }