aboutsummaryrefslogtreecommitdiff
path: root/lib/Minz/Extension.php
diff options
context:
space:
mode:
authorGravatar Alexis Degrugillier <aledeg@users.noreply.github.com> 2021-07-11 09:11:10 -0400
committerGravatar GitHub <noreply@github.com> 2021-07-11 15:11:10 +0200
commit714b40e2dec44075ff58eba1c7c81577f377ac98 (patch)
treedc7ffda07fe96cd8df46808951de959bf4f8cf85 /lib/Minz/Extension.php
parent3ed9d00f0a782a948234e33a6f2060acf9362c14 (diff)
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.
Diffstat (limited to 'lib/Minz/Extension.php')
-rw-r--r--lib/Minz/Extension.php100
1 files changed, 77 insertions, 23 deletions
diff --git a/lib/Minz/Extension.php b/lib/Minz/Extension.php
index 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;
}