The goal is to keep the actual default values for settings in the same place as the setting is declared, and applied using the regular means for loading the settings -- not in a separate piece of code that needs to be loaded through some entirely different mechanism. SetupDynamicConfig.php now contains a few categories of things: * Post-processing of configuration settings, where already-set settings are altered. This could be moved to MainConfigSchema too as a separate set of methods. * Processing of old aliases of settings (blacklist, slave) that are not registered as settings anymore and therefore are not available to MainConfigSchema. This could perhaps be moved to LocalSettings processing somehow? * Setting $wgUseEnotif, which is also not registered as a setting. Easiest would be just to declare it as a setting and have it set unconditionally. * Setting the actual timezone to $wgLocaltimezone. This is not related to configuration and should just be in Setup.php. Bug: T305093 Change-Id: Ia5c23b52dbbfcb3d07ffcf5d3b7f2d7befba2a26
79 lines
2.1 KiB
PHP
79 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace MediaWiki\Settings\Config;
|
|
|
|
use Config;
|
|
use HashConfig;
|
|
use MediaWiki\Config\IterableConfig;
|
|
use function array_key_exists;
|
|
|
|
class ArrayConfigBuilder extends ConfigBuilderBase {
|
|
|
|
/** @var array */
|
|
protected $config = [];
|
|
|
|
protected function has( string $key ): bool {
|
|
return array_key_exists( $key, $this->config );
|
|
}
|
|
|
|
public function get( string $key ) {
|
|
return $this->config[$key] ?? null;
|
|
}
|
|
|
|
protected function update( string $key, $value ) {
|
|
$this->config[$key] = $value;
|
|
}
|
|
|
|
public function setMulti( array $values, array $mergeStrategies = [] ): ConfigBuilder {
|
|
if ( !$mergeStrategies ) {
|
|
$this->config = array_merge( $this->config, $values );
|
|
return $this;
|
|
}
|
|
|
|
foreach ( $values as $key => $newValue ) {
|
|
// Optimization: Inlined logic from set() for performance
|
|
if ( array_key_exists( $key, $this->config ) ) {
|
|
$mergeStrategy = $mergeStrategies[$key] ?? null;
|
|
if ( $mergeStrategy && is_array( $newValue ) ) {
|
|
$oldValue = $this->config[$key];
|
|
if ( $oldValue && is_array( $oldValue ) ) {
|
|
$newValue = $mergeStrategy->merge( $oldValue, $newValue );
|
|
}
|
|
}
|
|
}
|
|
$this->config[$key] = $newValue;
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Build the configuration.
|
|
*
|
|
* @todo Once we can use PHP 7.4, change the return type declaration to IterableConfig.
|
|
* @return IterableConfig
|
|
*/
|
|
public function build(): Config {
|
|
return new HashConfig( $this->config );
|
|
}
|
|
|
|
public function setMultiDefault( $defaults, $mergeStrategies ): ConfigBuilder {
|
|
foreach ( $defaults as $key => $defaultValue ) {
|
|
// Optimization: Inlined logic from setDefault() for performance
|
|
if ( array_key_exists( $key, $this->config ) ) {
|
|
$mergeStrategy = $mergeStrategies[$key] ?? null;
|
|
if ( $mergeStrategy && $defaultValue && is_array( $defaultValue ) ) {
|
|
$customValue = $this->config[$key];
|
|
if ( is_array( $customValue ) ) {
|
|
$newValue = $mergeStrategy->merge( $defaultValue, $customValue );
|
|
$this->config[$key] = $newValue;
|
|
}
|
|
}
|
|
} else {
|
|
$this->config[$key] = $defaultValue;
|
|
}
|
|
}
|
|
return $this;
|
|
}
|
|
|
|
}
|