فهرست منبع

Add system configuration for extension (#3626)

Before, only the user configuration was supported by extensions. But it was
limiting if one has to create a system extension with configuration.
Now, both user and system configuration are supported.
Alexis Degrugillier 4 سال پیش
والد
کامیت
714b40e2de
2فایلهای تغییر یافته به همراه80 افزوده شده و 23 حذف شده
  1. 3 0
      docs/en/developers/03_Backend/05_Extensions.md
  2. 77 23
      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 `getSystemConfiguration` method retrieves the extension configuration for the system.
+* the `setSystemConfiguration` method stores the extension configuration for the system.
+* the `removeSystemConfiguration` method removes the extension configuration for the system.
 * 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.

+ 77 - 23
lib/Minz/Extension.php

@@ -13,6 +13,7 @@ abstract class Minz_Extension {
 	private $type;
 	private $config_key = 'extensions';
 	private $user_configuration;
+	private $system_configuration;
 
 	public static $authorized_types = array(
 		'system',
@@ -203,40 +204,76 @@ abstract class Minz_Extension {
 	/**
 	 * @return bool
 	 */
-	private function isUserConfigurationEnabled() {
+	private function isConfigurationEnabled(string $type) {
 		if (!class_exists('FreshRSS_Context', false)) {
 			return false;
 		}
-		if (null === FreshRSS_Context::$user_conf) {
+
+		$conf = "{$type}_conf";
+		if (null === FreshRSS_Context::$$conf) {
 			return false;
 		}
+
 		return true;
 	}
 
 	/**
 	 * @return bool
 	 */
-	private function isExtensionConfigured() {
-		if (!FreshRSS_Context::$user_conf->hasParam($this->config_key)) {
+	private function isExtensionConfigured(string $type) {
+		$conf = "{$type}_conf";
+
+		if (!FreshRSS_Context::$$conf->hasParam($this->config_key)) {
 			return false;
 		}
 
-		$extensions = FreshRSS_Context::$user_conf->{$this->config_key};
+		$extensions = FreshRSS_Context::$$conf->{$this->config_key};
 		return array_key_exists($this->getName(), $extensions);
 	}
 
 	/**
 	 * @return array
 	 */
-	public function getUserConfiguration() {
-		if (!$this->isUserConfigurationEnabled()) {
+	private function getConfiguration(string $type) {
+		if (!$this->isConfigurationEnabled($type)) {
 			return [];
 		}
-		if (!$this->isExtensionConfigured()) {
+
+		if (!$this->isExtensionConfigured($type)) {
 			return [];
 		}
 
-		return FreshRSS_Context::$user_conf->{$this->config_key}[$this->getName()];
+		$conf = "{$type}_conf";
+		return FreshRSS_Context::$$conf->{$this->config_key}[$this->getName()];
+	}
+
+	/**
+	 * @return array
+	 */
+	public function getSystemConfiguration() {
+		return $this->getConfiguration('system');
+	}
+
+	/**
+	 * @return array
+	 */
+	public function getUserConfiguration() {
+		return $this->getConfiguration('user');
+	}
+
+	/**
+	 * @param mixed $default
+	 * @return mixed
+	 */
+	public function getSystemConfigurationValue(string $key, $default = null) {
+		if (!is_array($this->system_configuration)) {
+			$this->system_configuration = $this->getSystemConfiguration();
+		}
+
+		if (array_key_exists($key, $this->system_configuration)) {
+			return $this->system_configuration[$key];
+		}
+		return $default;
 	}
 
 	/**
@@ -254,40 +291,57 @@ abstract class Minz_Extension {
 		return $default;
 	}
 
-	public function setUserConfiguration(array $configuration) {
-		if (!$this->isUserConfigurationEnabled()) {
-			return;
-		}
-		if (FreshRSS_Context::$user_conf->hasParam($this->config_key)) {
-			$extensions = FreshRSS_Context::$user_conf->{$this->config_key};
+	private function setConfiguration(string $type, array $configuration) {
+		$conf = "{$type}_conf";
+
+		if (FreshRSS_Context::$$conf->hasParam($this->config_key)) {
+			$extensions = FreshRSS_Context::$$conf->{$this->config_key};
 		} else {
 			$extensions = [];
 		}
 		$extensions[$this->getName()] = $configuration;
 
-		FreshRSS_Context::$user_conf->{$this->config_key} = $extensions;
-		FreshRSS_Context::$user_conf->save();
+		FreshRSS_Context::$$conf->{$this->config_key} = $extensions;
+		FreshRSS_Context::$$conf->save();
+	}
+
+	public function setSystemConfiguration(array $configuration) {
+		$this->setConfiguration('system', $configuration);
+		$this->system_configuration = $configuration;
+	}
 
+	public function setUserConfiguration(array $configuration) {
+		$this->setConfiguration('user', $configuration);
 		$this->user_configuration = $configuration;
 	}
 
-	public function removeUserConfiguration() {
-		if (!$this->isUserConfigurationEnabled()) {
+	private function removeConfiguration(string $type) {
+		if (!$this->isConfigurationEnabled($type)) {
 			return;
 		}
-		if (!$this->isExtensionConfigured()) {
+
+		if (!$this->isExtensionConfigured($type)) {
 			return;
 		}
 
-		$extensions = FreshRSS_Context::$user_conf->{$this->config_key};
+		$conf = "{$type}_conf";
+		$extensions = FreshRSS_Context::$$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();
+		FreshRSS_Context::$$conf->{$this->config_key} = $extensions;
+		FreshRSS_Context::$$conf->save();
+	}
 
+	public function removeSystemConfiguration() {
+		$this->removeConfiguration('system');
+		$this->system_configuration = null;
+	}
+
+	public function removeUserConfiguration() {
+		$this->removeConfiguration('user');
 		$this->user_configuration = null;
 	}