From 8285f1df43ce19bbd533c70c3fd1487908883725 Mon Sep 17 00:00:00 2001 From: Alexis Degrugillier Date: Mon, 1 Feb 2021 18:03:09 -0500 Subject: Add comprehensive user configuration in extensions (#3397) Before, the extension configuration was handled by its author. There was discrepancies between extensions on how the configuration was stored. Now, we could rely on a single way of storing configuration. This won't invalidate how the extensions are storing their configuration but will allow authors to focus on what is important. --- lib/Minz/Configuration.php | 9 ++++++ lib/Minz/Extension.php | 73 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) (limited to 'lib') diff --git a/lib/Minz/Configuration.php b/lib/Minz/Configuration.php index 3cf356a2b..7c60c7567 100644 --- a/lib/Minz/Configuration.php +++ b/lib/Minz/Configuration.php @@ -140,6 +140,15 @@ class Minz_Configuration { return $this->configuration_setter; } + /** + * Check if a parameter is defined in the configuration + * + * @return bool + */ + public function hasParam(string $key) { + return isset($this->data[$key]); + } + /** * Return the value of the given param. * diff --git a/lib/Minz/Extension.php b/lib/Minz/Extension.php index 6b13f5a4e..e463c03c4 100644 --- a/lib/Minz/Extension.php +++ b/lib/Minz/Extension.php @@ -11,6 +11,7 @@ abstract class Minz_Extension { private $description; private $version; private $type; + private $config_key = 'extensions'; public static $authorized_types = array( 'system', @@ -195,4 +196,76 @@ abstract class Minz_Extension { public function registerHook($hook_name, $hook_function) { Minz_ExtensionManager::addHook($hook_name, $hook_function, $this); } + + /** + * @return bool + */ + private function isUserConfigurationEnabled() { + if (!class_exists('FreshRSS_Context', false)) { + return false; + } + if (null === FreshRSS_Context::$user_conf) { + return false; + } + return true; + } + + /** + * @return bool + */ + private function isExtensionConfigured() { + if (!FreshRSS_Context::$user_conf->hasParam($this->config_key)) { + return false; + } + + $extensions = FreshRSS_Context::$user_conf->{$this->config_key}; + return array_key_exists($this->getName(), $extensions); + } + + /** + * @return array + */ + public function getUserConfiguration() { + if (!$this->isUserConfigurationEnabled()) { + return []; + } + if (!$this->isExtensionConfigured()) { + return []; + } + + return FreshRSS_Context::$user_conf->{$this->config_key}[$this->getName()]; + } + + public function setUserConfiguration(array $configuration) { + if (!$this->isUserConfigurationEnabled()) { + return; + } + if ($this->isExtensionConfigured()) { + $extensions = FreshRSS_Context::$user_conf->{$this->config_key}; + } else { + $extensions = []; + } + $extensions[$this->getName()] = $configuration; + + FreshRSS_Context::$user_conf->{$this->config_key} = $extensions; + FreshRSS_Context::$user_conf->save(); + } + + public function removeUserConfiguration(){ + if (!$this->isUserConfigurationEnabled()) { + return; + } + if (!$this->isExtensionConfigured()) { + return; + } + + $extensions = FreshRSS_Context::$user_conf->{$this->config_key}; + unset($extensions[$this->getName()]); + if (empty($extensions)) { + $extensions = null; + } + + FreshRSS_Context::$user_conf->{$this->config_key} = $extensions; + FreshRSS_Context::$user_conf->save(); + } } -- cgit v1.2.3