mirror of https://github.com/friendica/friendica
Revert node.config.php into Config table
parent
6db89adc04
commit
513ef03421
@ -1,139 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2023, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Config\Model;
|
||||
|
||||
use Friendica\Core\Config\Capability\IManageConfigValues;
|
||||
use Friendica\Core\Config\Capability\ISetConfigValuesTransactionally;
|
||||
use Friendica\Core\Config\Exception\ConfigFileException;
|
||||
use Friendica\Core\Config\Exception\ConfigPersistenceException;
|
||||
use Friendica\Core\Config\Util\ConfigFileManager;
|
||||
use Friendica\Core\Config\ValueObject\Cache;
|
||||
|
||||
/**
|
||||
* Configuration model, which manages the whole system configuration
|
||||
*/
|
||||
class Config implements IManageConfigValues
|
||||
{
|
||||
/** @var Cache */
|
||||
protected $configCache;
|
||||
|
||||
/** @var ConfigFileManager */
|
||||
protected $configFileManager;
|
||||
|
||||
/**
|
||||
* @param ConfigFileManager $configFileManager The configuration file manager to save back configs
|
||||
* @param Cache $configCache The configuration cache (based on the config-files)
|
||||
*/
|
||||
public function __construct(ConfigFileManager $configFileManager, Cache $configCache)
|
||||
{
|
||||
$this->configFileManager = $configFileManager;
|
||||
$this->configCache = $configCache;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load all configuration values from a given cache and saves it back in the configuration node store
|
||||
* @see ConfigFileManager::CONFIG_DATA_FILE
|
||||
*
|
||||
* All configuration values of the system are stored in the cache.
|
||||
*
|
||||
* @param Cache $cache a new cache
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws ConfigPersistenceException In case the persistence layer throws errors
|
||||
*/
|
||||
public function setCacheAndSave(Cache $cache)
|
||||
{
|
||||
$this->configCache = $cache;
|
||||
$this->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getCache(): Cache
|
||||
{
|
||||
return $this->configCache;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function beginTransaction(): ISetConfigValuesTransactionally
|
||||
{
|
||||
return new ConfigTransaction($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the current Configuration back into the data config.
|
||||
* @see ConfigFileManager::CONFIG_DATA_FILE
|
||||
*/
|
||||
protected function save()
|
||||
{
|
||||
try {
|
||||
$this->configFileManager->saveData($this->configCache);
|
||||
// reload after the save to possible reload default values of lower source-priorities again
|
||||
$this->reload();
|
||||
} catch (ConfigFileException $e) {
|
||||
throw new ConfigPersistenceException('Cannot save config', $e);
|
||||
}
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function reload()
|
||||
{
|
||||
$configCache = new Cache();
|
||||
|
||||
try {
|
||||
$this->configFileManager->setupCache($configCache);
|
||||
} catch (ConfigFileException $e) {
|
||||
throw new ConfigPersistenceException('Cannot reload config', $e);
|
||||
}
|
||||
$this->configCache = $configCache;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function get(string $cat, string $key = null, $default_value = null)
|
||||
{
|
||||
return $this->configCache->get($cat, $key) ?? $default_value;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function set(string $cat, string $key, $value): bool
|
||||
{
|
||||
if ($this->configCache->set($cat, $key, $value, Cache::SOURCE_DATA)) {
|
||||
$this->save();
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function delete(string $cat, string $key): bool
|
||||
{
|
||||
if ($this->configCache->delete($cat, $key)) {
|
||||
$this->save();
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace Friendica\Core\Config\Model;
|
||||
|
||||
use Friendica\Core\Config\Capability\IManageConfigValues;
|
||||
use Friendica\Core\Config\Capability\ISetConfigValuesTransactionally;
|
||||
use Friendica\Core\Config\Util\SerializeUtil;
|
||||
use Friendica\Core\Config\ValueObject\Cache;
|
||||
use Friendica\Database\Database;
|
||||
|
||||
/**
|
||||
* Complete system configuration model, bound with the database
|
||||
*/
|
||||
class DatabaseConfig implements IManageConfigValues
|
||||
{
|
||||
/** @var Database */
|
||||
protected $database;
|
||||
/** @var Cache */
|
||||
protected $cache;
|
||||
|
||||
public function __construct(Database $database, Cache $cache)
|
||||
{
|
||||
$this->database = $database;
|
||||
$this->cache = $cache;
|
||||
|
||||
$this->reload();
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function reload()
|
||||
{
|
||||
$config = $this->database->selectToArray('config');
|
||||
|
||||
foreach ($config as $entry) {
|
||||
$this->cache->set($entry['cat'], $entry['k'], SerializeUtil::maybeUnserialize($entry['v']), Cache::SOURCE_DATA);
|
||||
}
|
||||
}
|
||||
|
||||
public function setAndSave(Cache $setCache, Cache $delCache): bool
|
||||
{
|
||||
$this->database->transaction();
|
||||
|
||||
foreach ($setCache->getAll() as $category => $data) {
|
||||
foreach ($data as $key => $value) {
|
||||
$this->cache->set($category, $key, $value, Cache::SOURCE_DATA);
|
||||
$this->database->insert('config', ['cat' => $category, 'k' => $key, 'v' => serialize($value)], Database::INSERT_UPDATE);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($delCache->getAll() as $category => $keys) {
|
||||
foreach ($keys as $key => $value) {
|
||||
$this->cache->delete($category, $key);
|
||||
$this->database->delete('config', ['cat' => $category, 'k' => $key]);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->database->commit();
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function get(string $cat, string $key = null, $default_value = null)
|
||||
{
|
||||
return $this->cache->get($cat, $key) ?? $default_value;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function set(string $cat, string $key, $value): bool
|
||||
{
|
||||
$this->cache->set($cat, $key, $value, Cache::SOURCE_DATA);
|
||||
return $this->database->insert('config', ['cat' => $cat, 'k' => $key, 'v' => serialize($value)], Database::INSERT_UPDATE);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function beginTransaction(): ISetConfigValuesTransactionally
|
||||
{
|
||||
return new ConfigTransaction($this);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function delete(string $cat, string $key): bool
|
||||
{
|
||||
$this->cache->delete($cat, $key);
|
||||
return $this->database->delete('config', ['cat' => $cat, 'k' => $key]);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function getCache(): Cache
|
||||
{
|
||||
return $this->cache;
|
||||
}
|
||||
}
|
@ -0,0 +1,82 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2023, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Config\Model;
|
||||
|
||||
use Friendica\Core\Config\Capability\IManageConfigValues;
|
||||
use Friendica\Core\Config\Capability\ISetConfigValuesTransactionally;
|
||||
use Friendica\Core\Config\Exception\ConfigPersistenceException;
|
||||
use Friendica\Core\Config\ValueObject\Cache;
|
||||
|
||||
/**
|
||||
* Creates a basic, readonly model for the file-based configuration
|
||||
*/
|
||||
class ReadOnlyFileConfig implements IManageConfigValues
|
||||
{
|
||||
/** @var Cache */
|
||||
protected $configCache;
|
||||
|
||||
/**
|
||||
* @param Cache $configCache The configuration cache (based on the config-files)
|
||||
*/
|
||||
public function __construct(Cache $configCache)
|
||||
{
|
||||
$this->configCache = $configCache;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getCache(): Cache
|
||||
{
|
||||
return $this->configCache;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function beginTransaction(): ISetConfigValuesTransactionally
|
||||
{
|
||||
throw new ConfigPersistenceException('beginTransaction not allowed.');
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function reload()
|
||||
{
|
||||
throw new ConfigPersistenceException('reload not allowed.');
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function get(string $cat, string $key = null, $default_value = null)
|
||||
{
|
||||
return $this->configCache->get($cat, $key) ?? $default_value;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function set(string $cat, string $key, $value): bool
|
||||
{
|
||||
throw new ConfigPersistenceException('set not allowed.');
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function delete(string $cat, string $key): bool
|
||||
{
|
||||
throw new ConfigPersistenceException('Save not allowed');
|
||||
}
|
||||
}
|
@ -1,237 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2023, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Config\Util;
|
||||
|
||||
/**
|
||||
* Util to transform back the config array into a string
|
||||
*/
|
||||
class ConfigFileTransformer
|
||||
{
|
||||
/**
|
||||
* This method takes an array of config values and applies some standard rules for formatting on it
|
||||
*
|
||||
* Beware that the applied rules follow some basic formatting principles for node.config.php
|
||||
* and doesn't support any custom formatting rules.
|
||||
*
|
||||
* f.e. associative array and list formatting are very complex with newlines and indentations, thus there are
|
||||
* three hardcoded types of formatting for them.
|
||||
*
|
||||
* a negative example, what's NOT working:
|
||||
* key => [ value1, [inner_value1, inner_value2], value2]
|
||||
* Since this isn't necessary for config values, there's no further logic handling such complex-list-in-list scenarios
|
||||
*
|
||||
* @param array $data A full config array
|
||||
*
|
||||
* @return string The config stream, which can be saved
|
||||
*/
|
||||
public static function encode(array $data): string
|
||||
{
|
||||
// Add the typical header values
|
||||
$dataString = '<?php' . PHP_EOL . PHP_EOL;
|
||||
$dataString .= 'return ';
|
||||
|
||||
$dataString .= static::extractArray($data);
|
||||
|
||||
// the last array line, close it with a semicolon
|
||||
$dataString .= ";" . PHP_EOL;
|
||||
|
||||
return $dataString;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts an inner config array.
|
||||
* Either as an associative array or as a list
|
||||
*
|
||||
* @param array $config The config array which should get extracted
|
||||
* @param int $level The current level of recursion (necessary for tab-indentation calculation)
|
||||
* @param bool $inList If true, the current array resides inside another list. Different rules may be applicable
|
||||
*
|
||||
* @return string The config string
|
||||
*/
|
||||
protected static function extractArray(array $config, int $level = 0, bool $inList = false): string
|
||||
{
|
||||
if (array_values($config) === $config) {
|
||||
return self::extractList($config, $level, $inList);
|
||||
} else {
|
||||
return self::extractAssociativeArray($config, $level, $inList);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts a key-value array and save it into a string
|
||||
* output:
|
||||
* [
|
||||
* 'key' => value,
|
||||
* 'key' => value,
|
||||
* ...
|
||||
* ]
|
||||
*
|
||||
* @param array $config The associative/key-value array
|
||||
* @param int $level The current level of recursion (necessary for tab-indentation calculation)
|
||||
* @param bool $inList If true, the current array resides inside another list. Different rules may be applicable
|
||||
*
|
||||
* @return string The config string
|
||||
*/
|
||||
protected static function extractAssociativeArray(array $config, int $level = 0, bool $inList = false): string
|
||||
{
|
||||
$string = '';
|
||||
|
||||
// Because we're in a list, we have to add a line-break first
|
||||
if ($inList) {
|
||||
$string .= PHP_EOL . str_repeat("\t", $level);
|
||||
}
|
||||
|
||||
// Add a typical Line break for a taxative list of key-value pairs
|
||||
$string .= '[' . PHP_EOL;
|
||||
|
||||
foreach ($config as $configKey => $configValue) {
|
||||
$string .= str_repeat("\t", $level + 1) .
|
||||
"'$configKey' => " .
|
||||
static::transformConfigValue($configValue, $level) .
|
||||
',' . PHP_EOL;
|
||||
}
|
||||
|
||||
$string .= str_repeat("\t", $level) . ']';
|
||||
|
||||
return $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts a list and save it into a string
|
||||
* output1 - simple:
|
||||
* [ value, value, value ]
|
||||
*
|
||||
* output2 - complex:
|
||||
* [
|
||||
* [ value, value, value ],
|
||||
* value,
|
||||
* [
|
||||
* key => value,
|
||||
* key => value,
|
||||
* ],
|
||||
* ]
|
||||
*
|
||||
* @param array $config The list
|
||||
* @param int $level The current level of recursion (necessary for tab-indentation calculation)
|
||||
* @param bool $inList If true, the current array resides inside another list. Different rules may be applicable
|
||||
*
|
||||
* @return string The config string
|
||||
*/
|
||||
protected static function extractList(array $config, int $level = 0, bool $inList = false): string
|
||||
{
|
||||
$string = '[';
|
||||
|
||||
$countConfigValues = count($config);
|
||||
// multiline defines, if each entry uses a new line
|
||||
$multiline = false;
|
||||
|
||||
// Search if any value is an array, because then other formatting rules are applicable
|
||||
foreach ($config as $item) {
|
||||
if (is_array($item)) {
|
||||
$multiline = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for ($i = 0; $i < $countConfigValues; $i++) {
|
||||
$isArray = is_array($config[$i]);
|
||||
|
||||
/**
|
||||
* In case this is an array in an array, directly extract this array again and continue
|
||||
* Skip any other logic since this isn't applicable for an array in an array
|
||||
*/
|
||||
if ($isArray) {
|
||||
$string .= PHP_EOL . str_repeat("\t", $level + 1);
|
||||
$string .= static::extractArray($config[$i], $level + 1, $inList) . ',';
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($multiline) {
|
||||
$string .= PHP_EOL . str_repeat("\t", $level + 1);
|
||||
}
|
||||
|
||||
$string .= static::transformConfigValue($config[$i], $level, true);
|
||||
|
||||
// add trailing commas or whitespaces for certain config entries
|
||||
if (($i < ($countConfigValues - 1))) {
|
||||
$string .= ',';
|
||||
if (!$multiline) {
|
||||
$string .= ' ';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add a new line for the last bracket as well
|
||||
if ($multiline) {
|
||||
$string .= PHP_EOL . str_repeat("\t", $level);
|
||||
}
|
||||
|
||||
$string .= ']';
|
||||
|
||||
return $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms one config value and returns the corresponding text-representation
|
||||
*
|
||||
* @param mixed $value Any value to transform
|
||||
* @param int $level The current level of recursion (necessary for tab-indentation calculation)
|
||||
* @param bool $inList If true, the current array resides inside another list. Different rules may be applicable
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function transformConfigValue($value, int $level = 0, bool $inList = false): string
|
||||
{
|
||||
switch (gettype($value)) {
|
||||
case "boolean":
|
||||
return ($value ? 'true' : 'false');
|
||||
case "integer":
|
||||
case "double":
|
||||
return $value;
|
||||
case "string":
|
||||
return sprintf('\'%s\'', addcslashes($value, '\'\\'));
|
||||
case "array":
|
||||
return static::extractArray($value, ++$level, $inList);
|
||||
case "NULL":
|
||||
return "null";
|
||||
case "object":
|
||||
if (method_exists($value, '__toString')) {
|
||||
return sprintf('\'%s\'', $value);
|
||||
} elseif ($value instanceof \Serializable) {
|
||||
try {
|
||||
return $value->serialize();
|
||||
} catch (\Exception $e) {
|
||||
throw new \InvalidArgumentException(sprintf('Cannot serialize %s.', gettype($value)), $e);
|
||||
}
|
||||
} else {
|
||||
throw new \InvalidArgumentException(sprintf('%s is an object without stringify.', gettype($value)));
|
||||
}
|
||||
case "resource":
|
||||
case "resource (closed)":
|
||||
throw new \InvalidArgumentException(sprintf('%s in configs are not supported yet.', gettype($value)));
|
||||
case "unknown type":
|
||||
throw new \InvalidArgumentException(sprintf('%s is an unknown value', $value));
|
||||
default:
|
||||
throw new \InvalidArgumentException(sprintf('%s is currently unsupported', $value));
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
namespace Friendica\Core\Config\Util;
|
||||
|
||||
/**
|
||||
* Serialize utils
|
||||
*
|
||||
* Retrieved from https://github.com/WordPress/wordpress-develop/blob/6.1/src/wp-includes/functions.php
|
||||
*/
|
||||
class SerializeUtil
|
||||
{
|
||||
public static function maybeSerialize($value): string
|
||||
{
|
||||
if (is_array($value) || is_object($value)) {
|
||||
return serialize($value);
|
||||
}
|
||||
|
||||
if (static::isSerialized($value, false)) {
|
||||
return serialize($value);
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
public static function maybeUnserialize($value)
|
||||
{
|
||||
if (static::isSerialized($value)) {
|
||||
return @unserialize(trim($value));
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks value to find if it was serialized.
|
||||
*
|
||||
* If $data is not a string, then returned value will always be false.
|
||||
* Serialized data is always a string.
|
||||
*
|
||||
* @param mixed $data Value to check to see if was serialized.
|
||||
* @param bool $strict Optional. Whether to be strict about the end of the string. Default true.
|
||||
*
|
||||
* @return bool False if not serialized and true if it was.
|
||||
* @since 6.1.0 Added Enum support.
|
||||
*
|
||||
* @since 2.0.5
|
||||
*/
|
||||
public static function isSerialized($data, bool $strict = true): bool
|
||||
{
|
||||
// If it isn't a string, it isn't serialized.
|
||||
if (!is_string($data)) {
|
||||
return false;
|
||||
}
|
||||
$data = trim($data);
|
||||
if ('N;' === $data) {
|
||||
return true;
|
||||
}
|
||||
if (strlen($data) < 4) {
|
||||
return false;
|
||||
}
|
||||
if (':' !== $data[1]) {
|
||||
return false;
|
||||
}
|
||||
if ($strict) {
|
||||
$lastc = substr($data, -1);
|
||||
if (';' !== $lastc && '}' !== $lastc) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
$semicolon = strpos($data, ';');
|
||||
$brace = strpos($data, '}');
|
||||
// Either ; or } must exist.
|
||||
if (false === $semicolon && false === $brace) {
|
||||
return false;
|
||||
}
|
||||
// But neither must be in the first X characters.
|
||||
if (false !== $semicolon && $semicolon < 3) {
|
||||
return false;
|
||||
}
|
||||
if (false !== $brace && $brace < 4) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
$token = $data[0];
|
||||
switch ($token) {
|
||||
case 's':
|
||||
if ($strict) {
|
||||
if ('"' !== substr($data, -2, 1)) {
|
||||
return false;
|
||||
}
|
||||
} else if (false === strpos($data, '"')) {
|
||||
return false;
|
||||
}
|
||||
// Or else fall through.
|
||||
case 'a':
|
||||
case 'O':
|
||||
case 'E':
|
||||
return (bool)preg_match("/^{$token}:[0-9]+:/s", $data);
|
||||
case 'b':
|
||||
case 'i':
|
||||
case 'd':
|
||||
$end = $strict ? '$' : '';
|
||||
return (bool)preg_match("/^{$token}:[0-9.E+-]+;$end/", $data);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
@ -1,110 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2023, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Test\src\Core\Config\Util;
|
||||
|
||||
use Friendica\Core\Config\Util\ConfigFileTransformer;
|
||||
use Friendica\Test\MockedTest;
|
||||
use Friendica\Test\Util\SerializableObjectDouble;
|
||||
use ParagonIE\HiddenString\HiddenString;
|
||||
use function PHPUnit\Framework\assertEquals;
|
||||
|
||||
class ConfigFileTransformerTest extends MockedTest
|
||||
{
|
||||
public function dataTests()
|
||||
{
|
||||
return [
|
||||
'default' => [
|
||||
'configFile' => (dirname(__DIR__, 4) . '/datasets/config/A.node.config.php'),
|
||||
],
|
||||
'extended' => [
|
||||
'configFile' => (dirname(__DIR__, 4) . '/datasets/config/B.node.config.php'),
|
||||
],
|
||||
'friendica.local' => [
|
||||
'configFile' => (dirname(__DIR__, 4) . '/datasets/config/transformer/C.node.config.php'),
|
||||
],
|
||||
'friendica.local.2' => [
|
||||
'configFile' => (dirname(__DIR__, 4) . '/datasets/config/transformer/D.node.config.php'),
|
||||
],
|
||||
'object_invalid' => [
|
||||
'configFile' => (dirname(__DIR__, 4) . '/datasets/config/transformer/object.node.config.php'),
|
||||
'assertException' => true,
|
||||
],
|
||||
'resource' => [
|
||||
'configFile' => (dirname(__DIR__, 4) . '/datasets/config/transformer/ressource.node.config.php'),
|
||||
'assertException' => false,
|
||||
'assertion' => <<<EOF
|
||||
<?php
|
||||
|
||||
return [
|
||||
'ressource' => [
|
||||
'ressources_not_allowed' => '',
|
||||
],
|
||||
];
|
||||
|
||||
EOF,
|
||||
],
|
||||
'object_valid' => [
|
||||
'configFile' => (dirname(__DIR__, 4) . '/datasets/config/transformer/object_valid.node.config.php'),
|
||||
'assertException' => false,
|
||||
'assertion' => <<<EOF
|
||||
<?php
|
||||
|
||||
return [
|
||||
'object' => [
|
||||
'toString' => 'test',
|
||||
'serializable' => 'serialized',
|
||||
],
|
||||
];
|
||||
|
||||
EOF,
|
||||
],
|
||||
'test_types' => [
|
||||
'configFile' => (dirname(__DIR__, 4) . '/datasets/config/transformer/types.node.config.php'),
|
||||
],
|
||||
'small_types' => [
|
||||
'configFile' => (dirname(__DIR__, 4) . '/datasets/config/transformer/small_types.node.config.php'),
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if the given config will be decoded into an array and encoded into the same string again
|
||||
*
|
||||
* @dataProvider dataTests
|
||||
*/
|
||||
public function testConfigFile(string $configFile, bool $assertException = false, $assertion = null)
|
||||
{
|
||||
$dataArray = include $configFile;
|
||||
|
||||
if ($assertException) {
|
||||
self::expectException(\InvalidArgumentException::class);
|
||||
}
|
||||
|
||||
$newConfig = ConfigFileTransformer::encode($dataArray);
|
||||
|
||||
if (empty($assertion)) {
|
||||
self::assertEquals(file_get_contents($configFile), $newConfig);
|
||||
} else {
|
||||
self::assertEquals($assertion, $newConfig);
|
||||
}
|
||||
}
|
||||
}
|