]> git.mxchange.org Git - friendica.git/commitdiff
Introduce ConfigFileTransformer for Config files
authorPhilipp <admin@philipp.info>
Sun, 1 Jan 2023 17:50:02 +0000 (18:50 +0100)
committerPhilipp <admin@philipp.info>
Tue, 3 Jan 2023 13:22:02 +0000 (14:22 +0100)
src/Core/Config/Util/ConfigFileTransformer.php [new file with mode: 0644]
tests/datasets/config/A.node.config.php [new file with mode: 0644]
tests/datasets/config/B.node.config.php [new file with mode: 0644]
tests/src/Core/Config/Util/ConfigFileTransformerTest.php [new file with mode: 0644]

diff --git a/src/Core/Config/Util/ConfigFileTransformer.php b/src/Core/Config/Util/ConfigFileTransformer.php
new file mode 100644 (file)
index 0000000..9b80991
--- /dev/null
@@ -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;
+       }
+}
diff --git a/tests/datasets/config/A.node.config.php b/tests/datasets/config/A.node.config.php
new file mode 100644 (file)
index 0000000..b81e017
--- /dev/null
@@ -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',
+       ],
+];
diff --git a/tests/datasets/config/B.node.config.php b/tests/datasets/config/B.node.config.php
new file mode 100644 (file)
index 0000000..94b2e3f
--- /dev/null
@@ -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,
+       ],
+];
diff --git a/tests/src/Core/Config/Util/ConfigFileTransformerTest.php b/tests/src/Core/Config/Util/ConfigFileTransformerTest.php
new file mode 100644 (file)
index 0000000..6cd5bc7
--- /dev/null
@@ -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);
+       }
+}