aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorGravatar Alexis Degrugillier <aledeg@users.noreply.github.com> 2021-02-01 18:03:09 -0500
committerGravatar GitHub <noreply@github.com> 2021-02-02 00:03:09 +0100
commit8285f1df43ce19bbd533c70c3fd1487908883725 (patch)
tree38ef8d767f334f79ffcbb7bd2972ddd500f8b487 /lib
parentbed710b260e0032b65eda92d3c395c4f1c71308a (diff)
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.
Diffstat (limited to 'lib')
-rw-r--r--lib/Minz/Configuration.php9
-rw-r--r--lib/Minz/Extension.php73
2 files changed, 82 insertions, 0 deletions
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
@@ -141,6 +141,15 @@ class Minz_Configuration {
}
/**
+ * 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.
*
* @param $key the name of the 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();
+ }
}