[core] Add configuration for bridges, allowing private bridges (#1343)

This commit is contained in:
Lyra 2020-12-12 17:05:22 +01:00 committed by GitHub
parent 56b2c516e4
commit 810a2503c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 6 deletions

View file

@ -131,6 +131,7 @@ class DisplayAction extends ActionAbstract {
try { try {
$bridge->setDatas($bridge_params); $bridge->setDatas($bridge_params);
$bridge->loadConfiguration();
$bridge->collectData(); $bridge->collectData();
$items = $bridge->getItems(); $items = $bridge->getItems();

View file

@ -61,6 +61,13 @@ abstract class BridgeAbstract implements BridgeInterface {
*/ */
const CACHE_TIMEOUT = 3600; const CACHE_TIMEOUT = 3600;
/**
* Configuration for the bridge
*
* Use {@see BridgeAbstract::getConfiguration()} to read this parameter
*/
const CONFIGURATION = array();
/** /**
* Parameters for the bridge * 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 * Returns the value for the provided input
* *
@ -251,6 +288,19 @@ abstract class BridgeAbstract implements BridgeInterface {
return $this->inputs[$this->queriedContext][$input]['value']; 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} */ /** {@inheritdoc} */
public function getDescription(){ public function getDescription(){
return static::DESCRIPTION; return static::DESCRIPTION;
@ -271,6 +321,11 @@ abstract class BridgeAbstract implements BridgeInterface {
return static::URI . '/favicon.ico'; return static::URI . '/favicon.ico';
} }
/** {@inheritdoc} */
public function getConfiguration(){
return static::CONFIGURATION;
}
/** {@inheritdoc} */ /** {@inheritdoc} */
public function getParameters(){ public function getParameters(){
return static::PARAMETERS; return static::PARAMETERS;

View file

@ -58,6 +58,19 @@ interface BridgeInterface {
*/ */
public function collectData(); 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 * Returns the description
* *

View file

@ -145,13 +145,10 @@ final class Configuration {
// Replace default configuration with custom settings // Replace default configuration with custom settings
foreach(parse_ini_file(FILE_CONFIG, true, INI_SCANNER_TYPED) as $header => $section) { foreach(parse_ini_file(FILE_CONFIG, true, INI_SCANNER_TYPED) as $header => $section) {
foreach($section as $key => $value) { 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;
} }
} }
} }
}
if(!is_string(self::getConfig('system', 'timezone')) if(!is_string(self::getConfig('system', 'timezone'))
|| !in_array(self::getConfig('system', 'timezone'), timezone_identifiers_list(DateTimeZone::ALL_WITH_BC))) || !in_array(self::getConfig('system', 'timezone'), timezone_identifiers_list(DateTimeZone::ALL_WITH_BC)))
@ -218,13 +215,11 @@ final class Configuration {
* @return mixed|null The parameter value. * @return mixed|null The parameter value.
*/ */
public static function getConfig($section, $key) { public static function getConfig($section, $key) {
if(array_key_exists($section, self::$config) && array_key_exists($key, self::$config[$section])) { if(array_key_exists($section, self::$config) && array_key_exists($key, self::$config[$section])) {
return self::$config[$section][$key]; return self::$config[$section][$key];
} }
return null; return null;
} }
/** /**