2021-11-09 17:29:14 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Settings\Config;
|
|
|
|
|
|
|
|
|
|
use Config;
|
|
|
|
|
use HashConfig;
|
2022-02-11 10:06:46 +00:00
|
|
|
use MediaWiki\Config\IterableConfig;
|
2022-05-05 02:48:49 +00:00
|
|
|
use function array_key_exists;
|
2021-11-09 17:29:14 +00:00
|
|
|
|
2022-02-10 21:12:10 +00:00
|
|
|
class ArrayConfigBuilder extends ConfigBuilderBase {
|
2022-01-21 20:07:42 +00:00
|
|
|
|
2021-11-09 17:29:14 +00:00
|
|
|
/** @var array */
|
2021-11-11 17:50:14 +00:00
|
|
|
protected $config = [];
|
|
|
|
|
|
2022-02-10 21:12:10 +00:00
|
|
|
protected function has( string $key ): bool {
|
|
|
|
|
return array_key_exists( $key, $this->config );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function get( string $key ) {
|
|
|
|
|
return $this->config[$key] ?? null;
|
2021-11-09 17:29:14 +00:00
|
|
|
}
|
|
|
|
|
|
2022-02-10 21:12:10 +00:00
|
|
|
protected function update( string $key, $value ) {
|
|
|
|
|
$this->config[$key] = $value;
|
2021-11-09 17:29:14 +00:00
|
|
|
}
|
|
|
|
|
|
2022-05-05 02:48:49 +00:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-04 14:52:01 +00:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-09 17:29:14 +00:00
|
|
|
/**
|
|
|
|
|
* Build the configuration.
|
|
|
|
|
*
|
2022-02-11 10:06:46 +00:00
|
|
|
* @todo Once we can use PHP 7.4, change the return type declaration to IterableConfig.
|
|
|
|
|
* @return IterableConfig
|
2021-11-09 17:29:14 +00:00
|
|
|
*/
|
|
|
|
|
public function build(): Config {
|
|
|
|
|
return new HashConfig( $this->config );
|
|
|
|
|
}
|
2022-05-05 02:48:49 +00:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-09 17:29:14 +00:00
|
|
|
}
|