2021-11-09 17:29:14 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Settings\Config;
|
|
|
|
|
|
|
|
|
|
use Config;
|
|
|
|
|
use HashConfig;
|
2021-11-11 17:50:14 +00:00
|
|
|
use MediaWiki\Settings\SettingsBuilderException;
|
2021-11-09 17:29:14 +00:00
|
|
|
|
2021-11-16 20:41:38 +00:00
|
|
|
class ArrayConfigBuilder implements ConfigBuilder {
|
2021-11-09 17:29:14 +00:00
|
|
|
|
|
|
|
|
/** @var array */
|
2021-11-11 17:50:14 +00:00
|
|
|
protected $config = [];
|
|
|
|
|
|
2021-11-16 20:41:38 +00:00
|
|
|
public function set( string $key, $value, MergeStrategy $mergeStrategy = null ): ConfigBuilder {
|
2021-11-11 17:50:14 +00:00
|
|
|
if ( $mergeStrategy ) {
|
|
|
|
|
if ( !is_array( $value ) ) {
|
|
|
|
|
throw new SettingsBuilderException(
|
|
|
|
|
'Cannot merge non-array value of type {value_type} under {key}',
|
|
|
|
|
[
|
|
|
|
|
'value_type' => get_debug_type( $value ),
|
|
|
|
|
'key' => $key,
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
} elseif ( !array_key_exists( $key, $this->config ) ) {
|
|
|
|
|
// Optimization: If the $key is not set in the config at all, no need to merge.
|
|
|
|
|
$this->config[$key] = $value;
|
|
|
|
|
} elseif ( is_array( $this->config[$key] ) && !$this->config[$key] ) {
|
|
|
|
|
// Optimization: If the existing value is an empty array, no need to merge.
|
|
|
|
|
$this->config[$key] = $value;
|
|
|
|
|
} else {
|
|
|
|
|
$this->config[$key] = $mergeStrategy->merge( $this->config[$key] ?? [], $value );
|
|
|
|
|
}
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
2021-11-09 17:29:14 +00:00
|
|
|
$this->config[$key] = $value;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-16 20:41:38 +00:00
|
|
|
public function setDefault( string $key, $value, MergeStrategy $mergeStrategy = null ): ConfigBuilder {
|
2021-11-11 17:50:14 +00:00
|
|
|
if ( $mergeStrategy ) {
|
2021-11-19 19:20:02 +00:00
|
|
|
self::set( $key, $value, $mergeStrategy->reverse() );
|
2021-11-11 17:50:14 +00:00
|
|
|
} elseif ( !array_key_exists( $key, $this->config ) ) {
|
|
|
|
|
$this->config[$key] = $value;
|
2021-11-09 17:29:14 +00:00
|
|
|
}
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Build the configuration.
|
|
|
|
|
*
|
|
|
|
|
* @return Config
|
|
|
|
|
*/
|
|
|
|
|
public function build(): Config {
|
|
|
|
|
return new HashConfig( $this->config );
|
|
|
|
|
}
|
|
|
|
|
}
|