mirror of
https://github.com/friendica/friendica
synced 2024-11-09 16:22:56 +00:00
Introduce ConfigFileTransformer for Config files
This commit is contained in:
parent
6318406951
commit
fea4b202c1
4 changed files with 199 additions and 0 deletions
85
src/Core/Config/Util/ConfigFileTransformer.php
Normal file
85
src/Core/Config/Util/ConfigFileTransformer.php
Normal file
|
@ -0,0 +1,85 @@
|
|||
<?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
|
||||
{
|
||||
public static function encode(array $data): string
|
||||
{
|
||||
$dataString = '<?php' . PHP_EOL . PHP_EOL;
|
||||
$dataString .= 'return [' . PHP_EOL;
|
||||
|
||||
$categories = array_keys($data);
|
||||
|
||||
foreach ($categories as $category) {
|
||||
$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;
|
||||
|
||||
return $dataString;
|
||||
}
|
||||
|
||||
protected static function extractArray(array $config, int $level = 0): string
|
||||
{
|
||||
$string = '';
|
||||
|
||||
foreach ($config as $configKey => $configValue) {
|
||||
$string .= static::mapConfigValue($configKey, $configValue, $level);
|
||||
}
|
||||
|
||||
return $string;
|
||||
}
|
||||
|
||||
protected static function mapConfigValue(string $key, $value, $level = 0): string
|
||||
{
|
||||
$string = str_repeat("\t", $level + 2) . "'$key' => ";
|
||||
|
||||
if (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\',', $value);
|
||||
}
|
||||
|
||||
$string .= PHP_EOL;
|
||||
|
||||
return $string;
|
||||
}
|
||||
}
|
22
tests/datasets/config/A.node.config.php
Normal file
22
tests/datasets/config/A.node.config.php
Normal file
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
return [
|
||||
'database' => [
|
||||
'hostname' => 'testhost',
|
||||
'username' => 'testuser',
|
||||
'password' => 'testpw',
|
||||
'database' => 'testdb',
|
||||
'charset' => 'utf8mb4',
|
||||
],
|
||||
'config' => [
|
||||
'admin_email' => 'admin@test.it',
|
||||
'sitename' => 'Friendica Social Network',
|
||||
'register_policy' => 2,
|
||||
'register_text' => '',
|
||||
],
|
||||
'system' => [
|
||||
'default_timezone' => 'UTC',
|
||||
'language' => 'en',
|
||||
'theme' => 'frio',
|
||||
],
|
||||
];
|
38
tests/datasets/config/B.node.config.php
Normal file
38
tests/datasets/config/B.node.config.php
Normal file
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
return [
|
||||
'database' => [
|
||||
'hostname' => 'testhost',
|
||||
'username' => 'testuser',
|
||||
'password' => 'testpw',
|
||||
'database' => 'testdb',
|
||||
'charset' => 'utf8mb4',
|
||||
],
|
||||
'config' => [
|
||||
'admin_email' => 'admin@test.it',
|
||||
'sitename' => 'Friendica Social Network',
|
||||
'register_policy' => 2,
|
||||
'register_text' => '',
|
||||
'test' => [
|
||||
'a' => [
|
||||
'next' => 'value',
|
||||
'bool' => false,
|
||||
'innerArray' => [
|
||||
'a' => 4.55,
|
||||
'b' => false,
|
||||
'string2' => 'false',
|
||||
],
|
||||
],
|
||||
'v' => true,
|
||||
'v3' => 1,
|
||||
'v4' => 5.6443,
|
||||
],
|
||||
],
|
||||
'system' => [
|
||||
'default_timezone' => 'UTC',
|
||||
'language' => 'en',
|
||||
'theme' => 'frio',
|
||||
'int' => 23,
|
||||
'float' => 2.5,
|
||||
],
|
||||
];
|
54
tests/src/Core/Config/Util/ConfigFileTransformerTest.php
Normal file
54
tests/src/Core/Config/Util/ConfigFileTransformerTest.php
Normal file
|
@ -0,0 +1,54 @@
|
|||
<?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;
|
||||
|
||||
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'),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
$dataArray = include $configFile;
|
||||
|
||||
$newConfig = ConfigFileTransformer::encode($dataArray);
|
||||
|
||||
self::assertEquals(file_get_contents($configFile), $newConfig);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue