From 810a2503c94d6030cd905dc9e6b5855257fb5641 Mon Sep 17 00:00:00 2001 From: Lyra Date: Sat, 12 Dec 2020 17:05:22 +0100 Subject: [PATCH] [core] Add configuration for bridges, allowing private bridges (#1343) --- actions/DisplayAction.php | 1 + lib/BridgeAbstract.php | 55 +++++++++++++++++++++++++++++++++++++++ lib/BridgeInterface.php | 13 +++++++++ lib/Configuration.php | 7 +---- 4 files changed, 70 insertions(+), 6 deletions(-) diff --git a/actions/DisplayAction.php b/actions/DisplayAction.php index 579630a1..16a67d54 100644 --- a/actions/DisplayAction.php +++ b/actions/DisplayAction.php @@ -131,6 +131,7 @@ class DisplayAction extends ActionAbstract { try { $bridge->setDatas($bridge_params); + $bridge->loadConfiguration(); $bridge->collectData(); $items = $bridge->getItems(); diff --git a/lib/BridgeAbstract.php b/lib/BridgeAbstract.php index b77af16b..ee885bf9 100644 --- a/lib/BridgeAbstract.php +++ b/lib/BridgeAbstract.php @@ -61,6 +61,13 @@ abstract class BridgeAbstract implements BridgeInterface { */ const CACHE_TIMEOUT = 3600; + /** + * Configuration for the bridge + * + * Use {@see BridgeAbstract::getConfiguration()} to read this parameter + */ + const CONFIGURATION = array(); + /** * Parameters for the bridge * @@ -238,6 +245,36 @@ abstract class BridgeAbstract implements BridgeInterface { } + /** + * Loads configuration for the bridge + * + * Returns errors and aborts execution if the provided configuration is + * invalid. + * + * @return void + */ + public function loadConfiguration() { + foreach(static::CONFIGURATION as $optionName => $optionValue) { + + $configurationOption = Configuration::getConfig(get_class($this), $optionName); + + if($configurationOption !== null) { + $this->configuration[$optionName] = $configurationOption; + continue; + } + + if(isset($optionValue['required']) && $optionValue['required'] === true) { + returnServerError( + 'Missing configuration option: ' + . $optionName + ); + } elseif(isset($optionValue['defaultValue'])) { + $this->configuration[$optionName] = $optionValue['defaultValue']; + } + + } + } + /** * Returns the value for the provided input * @@ -251,6 +288,19 @@ abstract class BridgeAbstract implements BridgeInterface { return $this->inputs[$this->queriedContext][$input]['value']; } + /** + * Returns the value for the selected configuration + * + * @param string $input The option name + * @return mixed|null The option value or null if the input is not defined + */ + public function getOption($name){ + if(!isset($this->configuration[$name])) { + return null; + } + return $this->configuration[$name]; + } + /** {@inheritdoc} */ public function getDescription(){ return static::DESCRIPTION; @@ -271,6 +321,11 @@ abstract class BridgeAbstract implements BridgeInterface { return static::URI . '/favicon.ico'; } + /** {@inheritdoc} */ + public function getConfiguration(){ + return static::CONFIGURATION; + } + /** {@inheritdoc} */ public function getParameters(){ return static::PARAMETERS; diff --git a/lib/BridgeInterface.php b/lib/BridgeInterface.php index d0069180..e9309dbf 100644 --- a/lib/BridgeInterface.php +++ b/lib/BridgeInterface.php @@ -58,6 +58,19 @@ interface BridgeInterface { */ public function collectData(); + /** + * Get the user's supplied configuration for the bridge + */ + public function getConfiguration(); + + /** + * Returns the value for the selected configuration + * + * @param string $input The option name + * @return mixed|null The option value or null if the input is not defined + */ + public function getOption($name); + /** * Returns the description * diff --git a/lib/Configuration.php b/lib/Configuration.php index c7be6aee..16849e17 100644 --- a/lib/Configuration.php +++ b/lib/Configuration.php @@ -145,10 +145,7 @@ final class Configuration { // Replace default configuration with custom settings foreach(parse_ini_file(FILE_CONFIG, true, INI_SCANNER_TYPED) as $header => $section) { foreach($section as $key => $value) { - // Skip unknown sections and keys - if(array_key_exists($header, Configuration::$config) && array_key_exists($key, Configuration::$config[$header])) { - Configuration::$config[$header][$key] = $value; - } + Configuration::$config[$header][$key] = $value; } } } @@ -218,13 +215,11 @@ final class Configuration { * @return mixed|null The parameter value. */ public static function getConfig($section, $key) { - if(array_key_exists($section, self::$config) && array_key_exists($key, self::$config[$section])) { return self::$config[$section][$key]; } return null; - } /**