Skip to content

Commit

Permalink
[core] Add configuration for bridges, allowing private bridges (#1343)
Browse files Browse the repository at this point in the history
  • Loading branch information
teromene authored Dec 12, 2020
1 parent 56b2c51 commit 810a250
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 6 deletions.
1 change: 1 addition & 0 deletions actions/DisplayAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public function execute() {

try {
$bridge->setDatas($bridge_params);
$bridge->loadConfiguration();
$bridge->collectData();

$items = $bridge->getItems();
Expand Down
55 changes: 55 additions & 0 deletions lib/BridgeAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -238,6 +245,36 @@ function($i){ return $i['name']; }, // Just display parameter names

}

/**
* 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
*
Expand All @@ -251,6 +288,19 @@ protected function getInput($input){
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;
Expand All @@ -271,6 +321,11 @@ public function getIcon(){
return static::URI . '/favicon.ico';
}

/** {@inheritdoc} */
public function getConfiguration(){
return static::CONFIGURATION;
}

/** {@inheritdoc} */
public function getParameters(){
return static::PARAMETERS;
Expand Down
13 changes: 13 additions & 0 deletions lib/BridgeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
7 changes: 1 addition & 6 deletions lib/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,7 @@ public static function loadConfiguration() {
// 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;
}
}
}
Expand Down Expand Up @@ -218,13 +215,11 @@ public static function loadConfiguration() {
* @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;

}

/**
Expand Down

0 comments on commit 810a250

Please sign in to comment.