[CONFIG] Various fixes to use new configuration format

This commit is contained in:
Hugo Sales 2020-09-30 22:57:25 +00:00 committed by Hugo Sales
parent c4c693b283
commit e949dd654a
No known key found for this signature in database
GPG Key ID: 7D0C7EAFC9D835A0
7 changed files with 75 additions and 16 deletions

View File

@ -33,7 +33,6 @@
namespace App\Controller; namespace App\Controller;
use App\Core\Controller; use App\Core\Controller;
use App\Core\DB\DefaultSettings;
use App\Core\Form; use App\Core\Form;
use function App\Core\I18n\_m; use function App\Core\I18n\_m;
use App\Util\Common; use App\Util\Common;
@ -48,7 +47,7 @@ class AdminPanel extends Controller
{ {
public function site(Request $request) public function site(Request $request)
{ {
$defaults = DefaultSettings::$defaults; $defaults = Common::getConfigDefaults();
$options = []; $options = [];
foreach ($defaults as $key => $inner) { foreach ($defaults as $key => $inner) {
$options[$key] = []; $options[$key] = [];
@ -71,6 +70,8 @@ class AdminPanel extends Controller
$value = $data['value']; $value = $data['value'];
if (preg_match('/^[0-9]+$/', $value)) { if (preg_match('/^[0-9]+$/', $value)) {
$value = (int) $value; $value = (int) $value;
} elseif (strstr($value, ',') === false) {
// empty, string
} elseif (Formatting::toArray($value, $value)) { } elseif (Formatting::toArray($value, $value)) {
// empty // empty
} elseif (preg_match('/true|false/i', $value)) { } elseif (preg_match('/true|false/i', $value)) {

View File

@ -126,6 +126,7 @@ abstract class DB
public static function __callStatic(string $name, array $args) public static function __callStatic(string $name, array $args)
{ {
foreach (['find', 'getReference', 'getPartialReference', 'getRepository'] as $m) { foreach (['find', 'getReference', 'getPartialReference', 'getRepository'] as $m) {
// TODO Plugins
$pref = '\App\Entity\\'; $pref = '\App\Entity\\';
if ($name == $m && Formatting::startsWith($name, $pref) === false) { if ($name == $m && Formatting::startsWith($name, $pref) === false) {
$args[0] = $pref . ucfirst(Formatting::snakeCaseToCamelCase($args[0])); $args[0] = $pref . ucfirst(Formatting::snakeCaseToCamelCase($args[0]));

View File

@ -130,7 +130,7 @@ class GNUsocial implements EventSubscriberInterface
public function initialize(): void public function initialize(): void
{ {
if (!$this->initialized) { if (!$this->initialized) {
Common::setConfigBag($this->config); Common::setupConfig($this->config);
Log::setLogger($this->logger); Log::setLogger($this->logger);
Event::setDispatcher($this->event_dispatcher); Event::setDispatcher($this->event_dispatcher);
I18n::setTranslator($this->translator); I18n::setTranslator($this->translator);

View File

@ -89,7 +89,6 @@ class ModuleManager
$module_manager = new self(); $module_manager = new self();
$entity_paths = []; $entity_paths = [];
foreach ($module_paths as $path) { foreach ($module_paths as $path) {
// 'modules' and 'plugins' have the same length
$type = ucfirst(preg_replace('%' . INSTALLDIR . '/(component|plugin)s/.*%', '\1', $path)); $type = ucfirst(preg_replace('%' . INSTALLDIR . '/(component|plugin)s/.*%', '\1', $path));
$dir = dirname($path); $dir = dirname($path);
$module = basename($dir); $module = basename($dir);

View File

@ -50,6 +50,7 @@ class Extension extends AbstractExtension
new TwigFunction('active', [Runtime::class, 'isCurrentRouteActive']), new TwigFunction('active', [Runtime::class, 'isCurrentRouteActive']),
new TwigFunction('is_route', [Runtime::class, 'isCurrentRoute']), new TwigFunction('is_route', [Runtime::class, 'isCurrentRoute']),
new TwigFunction('get_note_actions', [Runtime::class, 'getNoteActions']), new TwigFunction('get_note_actions', [Runtime::class, 'getNoteActions']),
new TwigFunction('config', [Runtime::class, 'getConfig']),
]; ];
} }
} }

View File

@ -32,6 +32,7 @@ namespace App\Twig;
use App\Core\Event; use App\Core\Event;
use App\Entity\Note; use App\Entity\Note;
use App\Util\Common;
use App\Util\Formatting; use App\Util\Formatting;
use Functional as F; use Functional as F;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
@ -43,8 +44,9 @@ use Twig\Extension\RuntimeExtensionInterface;
class Runtime implements RuntimeExtensionInterface, EventSubscriberInterface class Runtime implements RuntimeExtensionInterface, EventSubscriberInterface
{ {
private Request $request; private Request $request;
public function __constructor() public function __constructor(Request $req)
{ {
$this->request = $req;
} }
public function isCurrentRouteActive(string ...$routes): string public function isCurrentRouteActive(string ...$routes): string
@ -65,6 +67,11 @@ class Runtime implements RuntimeExtensionInterface, EventSubscriberInterface
return $actions; return $actions;
} }
public function getConfig(...$args)
{
return Common::config(...$args);
}
// ---------------------------------------------------------- // ----------------------------------------------------------
// Request is not a service, can't find a better way to get it // Request is not a service, can't find a better way to get it

View File

@ -32,7 +32,6 @@
namespace App\Util; namespace App\Util;
use App\Core\DB\DB;
use App\Core\Router\Router; use App\Core\Router\Router;
use App\Core\Security; use App\Core\Security;
use App\Entity\GSActor; use App\Entity\GSActor;
@ -41,13 +40,16 @@ use App\Util\Exception\NoLoggedInUser;
use Exception; use Exception;
use Functional as F; use Functional as F;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface; use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
use Symfony\Component\Yaml;
abstract class Common abstract class Common
{ {
private static array $defaults;
private static ?array $config = null; private static ?array $config = null;
public static function setConfigBag(ContainerBagInterface $config) public static function setupConfig(ContainerBagInterface $config)
{ {
self::$config = $config->get('gnusocial'); self::$config = $config->get('gnusocial');
self::$defaults = $config->get('gnusocial_defaults');
} }
/** /**
@ -55,7 +57,7 @@ abstract class Common
*/ */
public static function config(string $section, string $setting) public static function config(string $section, string $setting)
{ {
return $config[$section][$setting]; return self::$config[$section][$setting];
} }
/** /**
@ -65,14 +67,16 @@ abstract class Common
*/ */
public static function setConfig(string $section, string $setting, $value): void public static function setConfig(string $section, string $setting, $value): void
{ {
throw new Exception('Implement this, ya dingus'); self::$config[$section][$setting] = $value;
$c = DB::getPartialReference('config', ['section' => $section, 'setting' => $setting]); $diff = self::array_diff_recursive(self::$config, self::$defaults);
if ($c === null) { $yaml = (new Yaml\Dumper(2))->dump(['parameters' => ['gnusocial' => $diff]], Yaml\Yaml::DUMP_OBJECT_AS_MAP);
throw new \Exception("The field section = {$section} and setting = {$setting} doesn't exist"); rename(INSTALLDIR . '/social.local.yaml', INSTALLDIR . '/social.local.yaml.back');
} file_put_contents(INSTALLDIR . '/social.local.yaml', $yaml);
}
$c->setValue(serialize($value)); public static function getConfigDefaults()
DB::flush(); {
return self::$defaults;
} }
public static function user(): ?LocalUser public static function user(): ?LocalUser
@ -110,6 +114,52 @@ abstract class Common
} }
} }
// function array_diff_recursive($arr1, $arr2)
// {
// $outputDiff = [];
// foreach ($arr1 as $key => $value) {
// // if the key exists in the second array, recursively call this function
// // if it is an array, otherwise check if the value is in arr2
// if (array_key_exists($key, $arr2)) {
// if (is_array($value)) {
// $recursiveDiff = self::array_diff_recursive($value, $arr2[$key]);
// if (count($recursiveDiff)) {
// $outputDiff[$key] = $recursiveDiff;
// }
// } else if (!in_array($value, $arr2)) {
// $outputDiff[$key] = $value;
// }
// } else if (!in_array($value, $arr2)) {
// // if the key is not in the second array, check if the value is in
// // the second array (this is a quirk of how array_diff works)
// $outputDiff[$key] = $value;
// }
// }
// return $outputDiff;
// }
public function array_diff_recursive(array $array1, array $array2)
{
$difference = [];
foreach ($array1 as $key => $value) {
if (is_array($value)) {
if (!isset($array2[$key]) || !is_array($array2[$key])) {
$difference[$key] = $value;
} else {
$new_diff = self::array_diff_recursive($value, $array2[$key]);
if (!empty($new_diff)) {
$difference[$key] = $new_diff;
}
}
} elseif ((!isset($array2[$key]) || $array2[$key] != $value) && !($array2[$key] === null && $value === null)) {
$difference[$key] = $value;
}
}
return $difference ?? false;
}
/** /**
* Remove keys from the _values_ of $keys from the array $from * Remove keys from the _values_ of $keys from the array $from
*/ */