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.
This commit is contained in:
Alexis Degrugillier
2021-07-11 09:11:10 -04:00
committed by GitHub
parent 3ed9d00f0a
commit 714b40e2de
2 changed files with 84 additions and 27 deletions

View File

@@ -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.

View File

@@ -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()) {
return [];
}
if (!$this->isExtensionConfigured()) {
private function getConfiguration(string $type) {
if (!$this->isConfigurationEnabled($type)) {
return [];
}
return FreshRSS_Context::$user_conf->{$this->config_key}[$this->getName()];
if (!$this->isExtensionConfigured($type)) {
return [];
}
$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()) {
return;
}
if (!$this->isExtensionConfigured()) {
private function removeConfiguration(string $type) {
if (!$this->isConfigurationEnabled($type)) {
return;
}
$extensions = FreshRSS_Context::$user_conf->{$this->config_key};
if (!$this->isExtensionConfigured($type)) {
return;
}
$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;
}