Преглед изворни кода

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.
Alexis Degrugillier пре 5 година
родитељ
комит
8285f1df43
3 измењених фајлова са 85 додато и 0 уклоњено
  1. 3 0
      docs/en/developers/03_Backend/05_Extensions.md
  2. 9 0
      lib/Minz/Configuration.php
  3. 73 0
      lib/Minz/Extension.php

+ 3 - 0
docs/en/developers/03_Backend/05_Extensions.md

@@ -300,6 +300,9 @@ The `Minz_Extension` abstract class defines another set of methods that should n
 * the `registerViews` method registers the extension views in FreshRSS.
 * the `registerTranslates` method registers the extension translation files in FreshRSS.
 * the `registerHook` method registers hook actions in different part of the application.
+* the `getUserConfiguration` method retrieves the extension configuration for the current user.
+* the `setUserConfiguration` method stores the extension configuration for the current user.
+* the `removeUserConfiguration` method removes the extension configuration for the current user.
 
 > Note that if you modify the later set of methods, you might break the extension system. Thus making FreshRSS unusable. So it's highly recommended to let those unmodified.
 

+ 9 - 0
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.
 	 *

+ 73 - 0
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();
+	}
 }