A free, self-hostable news aggregator…
Add system configuration for extension (#3626)
Alexis Degrugillier714b40e2dec4
diff --git a/docs/en/developers/03_Backend/05_Extensions.md b/docs/en/developers/03_Backend/05_Extensions.mdindex 0c87793c9..e5e2e51e6 100644--- a/docs/en/developers/03_Backend/05_Extensions.md+++ b/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.diff --git a/lib/Minz/Extension.php b/lib/Minz/Extension.phpindex 3bbb3925a..6637ab895 100644--- a/lib/Minz/Extension.php+++ b/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;}