mirror of
https://github.com/friendica/friendica
synced 2025-04-26 10:30:11 +00:00
Config: Improve the node.config.php transformation
- Add more types - Improvement for assoziative arrays and key-value arrays - Add a lot more tests
This commit is contained in:
parent
eda65296f5
commit
4c28f9cf9c
9 changed files with 301 additions and 43 deletions
|
@ -26,67 +26,191 @@ namespace Friendica\Core\Config\Util;
|
|||
*/
|
||||
class ConfigFileTransformer
|
||||
{
|
||||
/**
|
||||
* The public method to start the encoding
|
||||
*
|
||||
* @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 [' . PHP_EOL;
|
||||
$dataString .= 'return ';
|
||||
|
||||
$categories = array_keys($data);
|
||||
$dataString .= static::extractArray($data);
|
||||
|
||||
foreach ($categories as $category) {
|
||||
if (is_null($data[$category])) {
|
||||
$dataString .= "\t'$category' => null," . PHP_EOL;
|
||||
continue;
|
||||
}
|
||||
|
||||
$dataString .= "\t'$category' => [" . PHP_EOL;
|
||||
|
||||
if (is_array($data[$category])) {
|
||||
$keys = array_keys($data[$category]);
|
||||
|
||||
foreach ($keys as $key) {
|
||||
$dataString .= static::mapConfigValue($key, $data[$category][$key]);
|
||||
}
|
||||
}
|
||||
$dataString .= "\t]," . PHP_EOL;
|
||||
}
|
||||
|
||||
$dataString .= "];" . PHP_EOL;
|
||||
// the last array line, close it with a semicolon
|
||||
$dataString .= ";" . PHP_EOL;
|
||||
|
||||
return $dataString;
|
||||
}
|
||||
|
||||
protected static function extractArray(array $config, int $level = 0): string
|
||||
/**
|
||||
* Extracts an inner config array.
|
||||
* Either as a Key => Value pair array or as an assoziative array
|
||||
*
|
||||
* @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 $inAssoziativeArray If true, the current array resides inside another assoziative array. Different rules may be applicable
|
||||
*
|
||||
* @return string The config string
|
||||
*/
|
||||
protected static function extractArray(array $config, int $level = 0, bool $inAssoziativeArray = false): string
|
||||
{
|
||||
if (array_values($config) === $config) {
|
||||
return self::extractAssoziativeArray($config, $level, $inAssoziativeArray);
|
||||
} else {
|
||||
return self::extractKeyValueArray($config, $level, $inAssoziativeArray);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts a key-value array and save it into a string
|
||||
* output:
|
||||
* [
|
||||
* 'key' => value,
|
||||
* 'key' => value,
|
||||
* ...
|
||||
* ]
|
||||
*
|
||||
* @param array $config The key-value array
|
||||
* @param int $level The current level of recursion (necessary for tab-indentation calculation)
|
||||
* @param bool $inAssoziativeArray If true, the current array resides inside another assoziative array. Different rules may be applicable
|
||||
*
|
||||
* @return string The config string
|
||||
*/
|
||||
protected static function extractKeyValueArray(array $config, int $level = 0, bool $inAssoziativeArray = false): string
|
||||
{
|
||||
$string = '';
|
||||
|
||||
foreach ($config as $configKey => $configValue) {
|
||||
$string .= static::mapConfigValue($configKey, $configValue, $level);
|
||||
// Because we're in an assoziative array, we have to add a line-break first
|
||||
if ($inAssoziativeArray) {
|
||||
$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;
|
||||
}
|
||||
|
||||
protected static function mapConfigValue(string $key, $value, $level = 0): string
|
||||
/**
|
||||
* Extracts an assoziative array 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 assoziative array
|
||||
* @param int $level The current level of recursion (necessary for tab-indentation calculation)
|
||||
* @param bool $inAssoziativeArray If true, the current array resides inside another assoziative array. Different rules may be applicable
|
||||
*
|
||||
* @return string The config string
|
||||
*/
|
||||
protected static function extractAssoziativeArray(array $config, int $level = 0, bool $inAssoziativeArray = false): string
|
||||
{
|
||||
$string = str_repeat("\t", $level + 2) . "'$key' => ";
|
||||
$string = '[';
|
||||
|
||||
if (is_null($value)) {
|
||||
$string .= "null,";
|
||||
} elseif (is_array($value)) {
|
||||
$string .= "[" . PHP_EOL;
|
||||
$string .= static::extractArray($value, ++$level);
|
||||
$string .= str_repeat("\t", $level + 1) . '],';
|
||||
} elseif (is_bool($value)) {
|
||||
$string .= ($value ? 'true' : 'false') . ",";
|
||||
} elseif (is_numeric($value)) {
|
||||
$string .= $value . ",";
|
||||
} else {
|
||||
$string .= sprintf('\'%s\',', addcslashes($value, '\'\\'));
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
||||
$string .= PHP_EOL;
|
||||
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, $inAssoziativeArray) . ',';
|
||||
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 $inAssoziativeArray If true, the current array resides inside another assoziative array. Different rules may be applicable
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function transformConfigValue($value, int $level = 0, bool $inAssoziativeArray = 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, $inAssoziativeArray);
|
||||
case "NULL":
|
||||
return "null";
|
||||
case "object":
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue